├── arduino ├── __init__.py ├── arduino.pyc ├── __init__.pyc └── arduino.py ├── sample_blink.py ├── MIT-license.txt ├── README.md └── prototype.pde /arduino/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | VERSION = '0.5' 4 | 5 | from arduino import * 6 | 7 | -------------------------------------------------------------------------------- /arduino/arduino.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HashNuke/Python-Arduino-Prototyping-API/HEAD/arduino/arduino.pyc -------------------------------------------------------------------------------- /arduino/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HashNuke/Python-Arduino-Prototyping-API/HEAD/arduino/__init__.pyc -------------------------------------------------------------------------------- /sample_blink.py: -------------------------------------------------------------------------------- 1 | #import the lib 2 | from arduino import Arduino 3 | 4 | import time 5 | 6 | #specify the port as an argument 7 | my_board = Arduino('/dev/ttyUSB0') 8 | 9 | #declare output pins as a list/tuple 10 | my_board.output([11,12,13]) 11 | 12 | #perform operations 13 | i=0 14 | while(i<10): 15 | my_board.setHigh(13) 16 | time.sleep(1) 17 | my_board.setLow(13) 18 | time.sleep(1) 19 | i+=1 20 | -------------------------------------------------------------------------------- /MIT-license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2010 Akash Manohar J 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Arduino Prototyping API (version: 0.5) 2 | 3 | > © 2009-2010 Akash Manohar J 4 | > under the MIT License 5 | 6 | The Python Arduino Prototyping API helps you to quickly prototype Arduino programs, 7 | without having to repeatedly load the program to the Arduino board. 8 | 9 | #### Setup: 10 | 11 | 1. Load prototype.pde onto your Arduino dev board. 12 | 2. Import the arduino lib in your python script. 13 | 14 | 15 | ## Methods 16 | 17 | *Arduino.output(list_of_output_pins)* - set the output pins 18 | 19 | **Digital I/O** 20 | 21 | 1. *Arduino.setHigh(pin_number)* 22 | 2. *Arduino.setLow(pin_number)* 23 | 3. *Arduino.getState(pin_number)* 24 | 4. *Arduino.getState()* - returns true if pin state is high, else it returns false. 25 | 26 | **Analog I/O** 27 | 28 | 1. *Arduino.analogRead(pin_number)* - returns the analog value 29 | 2. *Arduino.analogWrite(pin_number, value)* - sets the analog value 30 | 31 | **Misc** 32 | 33 | 1.) *Arduino.turnOff()* - sets all the pins to low state 34 | 35 | 2.) *Arduino.close()* - closes serial connection. Using this makes sure that you won't have to disconnect & reconnect the Arduino again to recover the serial port. 36 | 37 | ## Usage example 38 | 39 | #the blink program 40 | 41 | #import the lib 42 | from arduino import Arduino 43 | 44 | import time 45 | 46 | #specify the port as an argument 47 | my_board = Arduino('/dev/ttyUSB1') 48 | 49 | #declare output pins as a list/tuple 50 | my_board.output([11,12,13]) 51 | 52 | #perform operations 53 | i=0 54 | while(i<10): 55 | my_board.setHigh(13) 56 | time.sleep(1) 57 | my_board.setLow(13) 58 | time.sleep(1) 59 | i+=1 60 | -------------------------------------------------------------------------------- /arduino/arduino.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import serial, time 4 | 5 | class Arduino(object): 6 | 7 | __OUTPUT_PINS = -1 8 | 9 | def __init__(self, port, baudrate=115200): 10 | self.serial = serial.Serial(port, baudrate) 11 | 12 | def __str__(self): 13 | return "Arduino is on port %s at %d baudrate" %(self.serial.port, self.serial.baudrate) 14 | 15 | def output(self, pinArray): 16 | self.__sendData(len(pinArray)) 17 | 18 | if(isinstance(pinArray, list) or isinstance(pinArray, tuple)): 19 | self.__OUTPUT_PINS = pinArray 20 | for each_pin in pinArray: 21 | self.__sendPin(each_pin) 22 | return True 23 | 24 | def setLow(self, pin): 25 | self.__sendData('0') 26 | self.__sendPin(pin) 27 | return True 28 | 29 | def setHigh(self, pin): 30 | self.__sendData('1') 31 | self.__sendPin(pin) 32 | return True 33 | 34 | def getState(self, pin): 35 | self.__sendData('2') 36 | self.__sendPin(pin) 37 | return self.__formatPinState(self.__getData()) 38 | 39 | def analogWrite(self, pin, value): 40 | self.__sendData('3') 41 | self.__sendPin(pin) 42 | hex_value = hex(value)[2:] 43 | if(len(hex_value)==1): 44 | hex1 = '0' 45 | hex2 = hex_value[0] 46 | else: 47 | hex1 = hex_value[0] 48 | hex2 = hex_value[1] 49 | 50 | self.__sendData(hex1) 51 | self.__sendData(hex2) 52 | return True 53 | 54 | def analogRead(self, pin): 55 | self.__sendData('4') 56 | self.__sendPin(pin) 57 | return self.__getData() 58 | 59 | def turnOff(self): 60 | for each_pin in self.__OUTPUT_PINS: 61 | self.setLow(each_pin) 62 | return True 63 | 64 | def __sendPin(self, pin): 65 | pin_in_char = chr(pin+48) 66 | self.__sendData(pin_in_char) 67 | 68 | def __sendData(self, serial_data): 69 | while(self.__getData()!="what"): 70 | pass 71 | self.serial.write(str(serial_data)) 72 | 73 | def __getData(self): 74 | return self.serial.readline().replace("\r\n","") 75 | 76 | def __formatPinState(self, pinValue): 77 | if pinValue=='1': 78 | return True 79 | else: 80 | return False 81 | 82 | def close(self): 83 | self.serial.close() 84 | return True 85 | 86 | """ 87 | def __del__(self): 88 | #close serial connection once program ends 89 | #this fixes the problem of port getting locked or unrecoverable in some linux systems 90 | self.serial.close() 91 | """ 92 | -------------------------------------------------------------------------------- /prototype.pde: -------------------------------------------------------------------------------- 1 | // variable to store the data from the serial port 2 | int cmd = 0; 3 | 4 | // command arguments 5 | int cmd_arg[2]; 6 | 7 | 8 | void setup() { 9 | // connect to the serial port 10 | Serial.begin(115200); 11 | // confirm ready state 12 | 13 | while(Serial.available()<1) 14 | { 15 | // get number of output pins and convert to int 16 | cmd = int(readData()) - 48; 17 | for(int i=0; i0) 32 | { 33 | cmd = int(Serial.read()) - 48; 34 | 35 | if(cmd==0) //set digital low 36 | { 37 | cmd_arg[0] = int(readData()) - 48; 38 | digitalWrite(cmd_arg[0],LOW); 39 | } 40 | 41 | if(cmd==1) //set digital high 42 | { 43 | cmd_arg[0] = int(readData()) - 48; 44 | digitalWrite(cmd_arg[0],HIGH); 45 | } 46 | 47 | if(cmd==2) //get digital value 48 | { 49 | cmd_arg[0] = int(readData()) - 48; 50 | cmd_arg[0] = digitalRead(cmd_arg[0]); 51 | Serial.println(cmd_arg[0]); 52 | } 53 | 54 | if(cmd==3) // set analog value 55 | { 56 | Serial.println("I'm in the right place"); 57 | cmd_arg[0] = int(readData()) - 48; 58 | cmd_arg[1] = readHexValue(); 59 | analogWrite(cmd_arg[0],cmd_arg[1]); 60 | } 61 | 62 | if(cmd==4) //read analog value 63 | { 64 | cmd_arg[0] = int(readData()) - 48; 65 | cmd_arg[0] = analogRead(cmd_arg[0]); 66 | Serial.println(cmd_arg[0]); 67 | } 68 | } 69 | } 70 | 71 | char readData() 72 | { 73 | askData(); 74 | 75 | while(1) 76 | { 77 | if(Serial.available()>0) 78 | { 79 | return Serial.read(); 80 | } 81 | } 82 | } 83 | 84 | 85 | //read hex value from serial and convert to integer 86 | int readHexValue() 87 | { 88 | int strval[2]; 89 | int converted_str; 90 | 91 | while(1) 92 | { 93 | if(Serial.available()>0) 94 | { 95 | strval[0] = convert_hex_to_int(Serial.read()); 96 | break; 97 | } 98 | } 99 | 100 | askData(); 101 | 102 | while(1) 103 | { 104 | if(Serial.available()>0) 105 | { 106 | strval[1] = convert_hex_to_int(Serial.read()); 107 | break; 108 | } 109 | } 110 | 111 | converted_str = (strval[0]*16) + strval[1]; 112 | return converted_str; 113 | } 114 | 115 | int convert_hex_to_int(char c) 116 | { 117 | return (c <= '9') ? c-'0' : c-'a'+10; 118 | } 119 | 120 | void askData() 121 | { 122 | Serial.println("what"); 123 | } 124 | 125 | void askCmd() 126 | { 127 | askData(); 128 | while(Serial.available()<=0) 129 | {} 130 | } 131 | 132 | --------------------------------------------------------------------------------