Package: serialduino
SerialduinoEasy to use python arduino lib with (optional) 74HC595 microprocessor(Basic 74HC595 mouting)Serialduino makes it easy to control your arduino using pythonInstall .ino programInstall package with pipâœ¨Magic âœ¨InstallationDillinger requirespyserialto run.pipinstallserialduinoUsageInitialize boardfromserialduinoimportArduinoboard=Arduino(port="COM3",baudrate=9600,timeout=.1)Create clock:fromserialduinoimportArduinoboard=Arduino(port="COM3",baudrate=9600,timeout=.1)deftick(arduino:Arduino,arg1,arg2):#Do stuffreturn(arg1,arg2)#IMPORTANT#Example usageboard.tick(tick,(1,2),5)Send message: (not to be used, but-in functions are made for something)fromserialduinoimportArduino,protocolsboard=Arduino(port="COM3",baudrate=9600,timeout=.1)#Example usageboard.setup_microprocessor(4,5,6)board.send_message(protocols["MP_SHIFT_OUT"],5)For more infos see built-in functions and docstringArduino code//ConfigurableunsignedintPIN_DS_DATA=4;unsignedintPIN_STCP_LATCH=5;unsignedintPIN_SHCP_CLOCK=6;boolMICROPROCESSOR_ENABLED=false;#define SERIAL_BAUDRATE 9600#define SERIAL_TIMEOUT 1//Not configurable#define UNKNOWN_PIN 0xFF#define MP_SHIFT_OUT 0#define MP_WRITE 1#define DIGITAL_READ 2#define DIGITAL_WRITE 3#define PIN_MODE 4#define GET_PIN_MODE 5#define PING 6#define MP_READ_GLOBAL 7#define MP_READ_PIN 8#define SEND_PULSE_READ_ECHO 9#define SEND_IMPULSION 10#define MP_SETUP 11//Store pins binaryconstintpinValues[]={1,2,4,8,16,32,64,128};//Max array length for messageconstunsignedintMAX_MESSAGE_LENGTH=12;//Buffer for serial messagecharmessage[MAX_MESSAGE_LENGTH][MAX_MESSAGE_LENGTH];//Array positionsunsignedintmessage_pos=0;unsignedintarray_pos=0;//Current binary 0-255intcurrentMicroprocessorValue=0;constStringsep_string=String(":");//Message idintmsg;//Used often so put in variableintpin;intvalues[MAX_MESSAGE_LENGTH];//Util function to get pin modeuint8_tgetPinMode(uint8_tpin){uint8_tbit=digitalPinToBitMask(pin);uint8_tport=digitalPinToPort(pin);if(NOT_A_PIN==port)returnUNKNOWN_PIN;if(0==bit)returnUNKNOWN_PIN;if(bit&bit-1)returnUNKNOWN_PIN;volatileuint8_t*reg,*out;reg=portModeRegister(port);out=portOutputRegister(port);if(*reg&bit)returnOUTPUT;elseif(*out&bit)returnINPUT_PULLUP;elsereturnINPUT;}//Set microprocessor's binaryvoidsetMicroprocessorState(intval){//Stops if microporcessor not enabledif(!MICROPROCESSOR_ENABLED)return;//Make sure latch is lowdigitalWrite(PIN_STCP_LATCH,0);//Send binary at interval, see 74hc595 docshiftOut(PIN_DS_DATA,PIN_SHCP_CLOCK,LSBFIRST,val);//Put latch on high to apply changesdigitalWrite(PIN_STCP_LATCH,1);}//Set specific microprocessor's pin statusvoidsetMicroprocessorPin(intpin,intval){//Stops if microporcessor not enabledif(!MICROPROCESSOR_ENABLED)return;//Get current pin's valueintcurrentPinValue=getMicroprocessorPin(pin);//Stops if current value is equal to requested value (no change needed)if(currentPinValue!=val){//Add / substract pin value to totalcurrentMicroprocessorValue+=(val==1?1:-1)*pinValues[pin];//Send changessetMicroprocessorState(currentMicroprocessorValue);}}//Send a pulse of x millis in a pinvoidsendPulse(intpin,intduration){//Make sure pin is lowdigitalWrite(pin,LOW);delayMicroseconds(2);//Set high and wait timedigitalWrite(pin,HIGH);delayMicroseconds(duration);//Set back to lowdigitalWrite(pin,LOW);}//Get a microprocessor pin's valueintgetMicroprocessorPin(intpin){//Stops if microporcessor not enabledif(!MICROPROCESSOR_ENABLED)return0;return(currentMicroprocessorValue>>pin)&1;}//Handle avoidhandleMessage(chardata[MAX_MESSAGE_LENGTH][MAX_MESSAGE_LENGTH]){msg=atoi(data[0]);//Handle message differently depending on idswitch(msg){//Set microprocessor binarycaseMP_SHIFT_OUT://Stops if microporcessor not enabledif(!MICROPROCESSOR_ENABLED){Serial.println("mp_not_setup");return;}//Change current valuecurrentMicroprocessorValue=atoi(data[1]);//Apply changessetMicroprocessorState(currentMicroprocessorValue);Serial.println(msg+sep_string+currentMicroprocessorValue);break;//Set microprocessor pincaseMP_WRITE://Stops if microporcessor not enabledif(!MICROPROCESSOR_ENABLED){Serial.println("mp_not_setup");return;}//Apply changespin=atoi(data[1]);values[0]=atoi(data[2]);setMicroprocessorPin(pin,values[0]);Serial.println(msg+sep_string+pin+sep_string+values[0]);break;//Read digital pincaseDIGITAL_READ://Read pin idpin=atoi(data[1]);//Send message back through serialSerial.println(msg+sep_string+pin+sep_string+digitalRead(pin));break;//Write digital pincaseDIGITAL_WRITE://Read pin idpin=atoi(data[1]);//Check if pin is in output mode, else set in output modeif(getPinMode(pin)!=OUTPUT)pinMode(pin,OUTPUT);//Apply changes and send callbackvalues[0]=atoi(data[2]);digitalWrite(pin,values[0]);Serial.println(msg+sep_string+pin+sep_string+values[0]);break;//Set digital pin modecasePIN_MODE://Read pin idpin=atoi(data[1]);//Apply changesvalues[0]=atoi(data[2]);pinMode(pin,values[0]);Serial.println(msg+sep_string+pin+sep_string+values[0]);break;//Get digital pin modecaseGET_PIN_MODE://Read pin idpin=atoi(data[1]);//Send message back through serialSerial.println(msg+sep_string+pin+sep_string+getPinMode(pin));break;//Ping to init communicationcasePING://PongSerial.println("Pong :)");break;//Read microprocessor binarycaseMP_READ_GLOBAL://Stops if microporcessor not enabledif(!MICROPROCESSOR_ENABLED){Serial.println("mp_not_setup");return;}//Send message back through serialSerial.println(msg+sep_string+currentMicroprocessorValue);break;//Read microprocessor valuecaseMP_READ_PIN://Stops if microporcessor not enabledif(!MICROPROCESSOR_ENABLED){Serial.println("mp_not_setup");return;}//Read pin idpin=atoi(data[1]);//Send message back through serialSerial.println(msg+sep_string+pin+sep_string+getMicroprocessorPin(pin));break;//Get pulse duration on pincaseSEND_PULSE_READ_ECHO://Read pin idpin=atoi(data[1]);values[0]=atoi(data[2]);values[1]=atoi(data[3]);sendPulse(pin,values[1]);//Send message back through serialSerial.println(msg+sep_string+pin+sep_string+getPulseIn(values[0]));break;//Send impulsion of X milliseconds on pincaseSEND_IMPULSION://Read pin idpin=atoi(data[1]);values[0]=atoi(data[2]);//Send pulsesendPulse(pin,values[0]);//Send data back through serialSerial.println(msg+sep_string+pin+sep_string+values[0]);break;//Setup microprocessorcaseMP_SETUP://Read dataPIN_DS_DATA=atoi(data[1]);PIN_STCP_LATCH=atoi(data[2]);PIN_SHCP_CLOCK=atoi(data[3]);//Set pins modepinMode(PIN_DS_DATA,OUTPUT);pinMode(PIN_STCP_LATCH,OUTPUT);pinMode(PIN_SHCP_CLOCK,OUTPUT);MICROPROCESSOR_ENABLED=true;//Reset statesetMicroprocessorState(0);//Send callback through serialSerial.println(msg+sep_string+PIN_DS_DATA+sep_string+PIN_STCP_LATCH+sep_string+PIN_SHCP_CLOCK);}}//Get pin's high timeintgetPulseIn(intpin){returnpulseIn(pin,HIGH);}//Handle serial inputvoidhandleSerial(){//If data in Serialif(Serial.available()>0){//Get recieved charcharc=char(Serial.read());//Check if $ (stop char)if(c==36/*$*/){//Handles messagehandleMessage(message);//Clear buffer / array positionsclearMessage();}else{//Add char to messageappendMessage(c);}}}//Clear message arrayvoidclearMessage(){//Clear arraymemset(message,0,sizeofmessage);//Reset array positionsmessage_pos=0;array_pos=0;}//Handle new charvoidappendMessage(charc){//If char is : (split sign)if(c==58){//Add 1 to array posarray_pos++;//Set text pos to 0message_pos=0;}else{//Set char in message arraymessage[array_pos][message_pos]=c;//Set text pos to 0message_pos++;}}//Setups serial communicationvoidinitSerial(){Serial.begin(SERIAL_BAUDRATE);Serial.setTimeout(SERIAL_TIMEOUT);}//Arduino setupvoidsetup(){//Init serialinitSerial();}//Arduino loopvoidloop(){//Handle serial inputshandleSerial();}