diff --git a/api/__init__.py b/Arduino/__init__.py similarity index 100% rename from api/__init__.py rename to Arduino/__init__.py diff --git a/api/arduino.py b/Arduino/arduino.py similarity index 96% rename from api/arduino.py rename to Arduino/arduino.py index d177b05..973444e 100644 --- a/api/arduino.py +++ b/Arduino/arduino.py @@ -340,6 +340,7 @@ class SoftwareSerial(object): return False if __name__=="__main__": + # quick test board=Arduino(9600) board.Servos.attach(9) board.Servos.write(9,90) diff --git a/README.md b/README.md index 748c4f5..829be19 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,18 @@ > © 2012-2013 Tristan A. Hearn > under the MIT License -Based in part on the Python Arduino Prototyping API by Akash Manohar (https://github.com/HashNuke/Python-Arduino-Prototyping-API/). +Based in part on the Python Arduino Prototyping API by Akash Manohar +(https://github.com/HashNuke/Python-Arduino-Prototyping-API/). -The Python Arduino Command API is a light-weight Python package for communicating with Arduino microcontroller boards. It is written -using a custom protocol, similar to Firmata (http://firmata.org/wiki/Main_Page). This allows a user to quickly protoype programs -for Arduino or to simply read and control harware connected to an Arduino from a host computer, without having to reload sketches onto an Arduino board. +The Python Arduino Command API is a light-weight Python package for +communicating with Arduino microcontroller boards. It is written +using a custom protocol, similar to Firmata (http://firmata.org/wiki/Main_Page). +This allows a user to quickly protoype programs +for Arduino or to simply read and control harware connected to an Arduino from +a host computer, without having to reload sketches onto an Arduino board. -Method names within the Python Arduino Command API are designed to be as close as possible to their Arduino programming language counterparts. +Method names within the Python Arduino Command API are designed to be as close +as possible to their Arduino programming language counterparts. ## Simple usage example (LED blink) ```python @@ -40,17 +45,20 @@ the functionality of many Arduino demo sketches. * Arduino compatible microcontroller with at least 14KB of memory #### Setup: -1. Verify that your Arduino board communicates at the baud rate specified in the setup() function in prototype.ino. Change it if necessary. +1. Verify that your Arduino board communicates at the baud rate specified in the +setup() function in prototype.ino. Change it if necessary. 1. Load the sketch prototype.ino onto your Arduino board. 2. Import the included arduino library into your python script. ## Classes -*Arduino(baud)* - Set up communication with currently connected and powered Arduino. +*Arduino(baud)* - Set up communication with currently connected and powered +Arduino. ```python board = Arduino("9600") #Example ``` -The device name / COM port of the connected Arduino will be auto-detected. If there are more than one Arduino boards connected, +The device name / COM port of the connected Arduino will be auto-detected. +If there are more than one Arduino boards connected, the desired COM port can be also be passed as an optional argument: ```python board = Arduino("9600", port = "COM3") #Windows example @@ -58,12 +66,15 @@ board = Arduino("9600", port = "COM3") #Windows example ```python board = Arduino("9600", port = "/dev/tty.usbmodemfa141") #OSX example ``` -A time-out for reading from the Arduino can also be specified as an optional argument: +A time-out for reading from the Arduino can also be specified as an optional +argument: ```python -board = Arduino("9600", timeout = 2) #Serial reading functions will wait for no more than 2 seconds +board = Arduino("9600", timeout = 2) #Serial reading functions will +#wait for no more than 2 seconds ``` -*SoftwareSerial()* - A class for handling software serial functionality. Is used internally by the Arduino class, and should not be called directly. +*SoftwareSerial()* - A class for handling software serial functionality. +Is used internally by the Arduino class, and should not be called directly. ## Methods diff --git a/api/examples.py b/api/examples.py deleted file mode 100644 index 1174e9b..0000000 --- a/api/examples.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python -from arduino import Arduino -import time - -def Blink(led_pin,baud): - """ - Blinks an LED in 1 sec intervals - """ - board = Arduino(baud) - while True: - board.digitalWrite(led_pin,"LOW") - print board.digitalRead(led_pin) #confirm LOW (0) - time.sleep(1) - board.digitalWrite(led_pin,"HIGH") - print board.digitalRead(led_pin) #confirm HIGH (1) - time.sleep(1) - -def softBlink(led_pin,baud): - """ - Fades an LED off and on, using - Arduino's analogWrite (PWM) function - """ - board=Arduino(baud) - i=0 - while True: - i+=1 - k=i%510 - if k%5==0: - if k>255: - k=510-k - board.analogWrite(led_pin,k) - -def adjustBrightness(pot_pin,led_pin,baud): - """ - Adjusts brightness of an LED using a - potentiometer - """ - board=Arduino(baud) - while True: - time.sleep(0.01) - val=board.analogRead(pot_pin)/4 - print val - board.analogWrite(led_pin,val) - - -def PingSonar(pw_pin,baud): - """ - Gets distance measurement from Ping))) - ultrasonic rangefinder connected to pw_pin - """ - board = Arduino(baud) - pingPin=pw_pin - while True: - duration = board.pulseIn(pingPin, "HIGH") - inches = duration/72./2. - cent = duration/29./2. - print inches,"inches" - time.sleep(0.1) - -def LCD(tx,baud,ssbaud,message): - """ - Prints to two-line LCD connected to - pin tx - """ - board = Arduino(baud) - board.SoftwareSerial.begin(0,tx,ssbaud) - while True: - board.SoftwareSerial.write(" test ") - - - - -if __name__=="__main__": - #LCD(5,9600,9600," test ") - adjustBrightness(5,11,9600) - #softBlink(11,9600) \ No newline at end of file diff --git a/examples.py b/examples.py new file mode 100644 index 0000000..46c8258 --- /dev/null +++ b/examples.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python +from Arduino import Arduino +import time + +class ArduinoExample(object): + + def __init__(self, baud,port="",timeout=2): + self.board = Arduino(baud = baud, port = port, timeout = timeout) + + def execute(self): + pass + +class Blink(ArduinoExample): + """ + Blinks an LED off and on, using + Arduino's digitalWrite function + """ + def __init__(self, led_pin, baud,port="",timeout=2): + super(Blink, self).__init__(baud, port = port, timeout = timeout) + self.led_pin = led_pin + + def execute(self): + self.board.digitalWrite(self.led_pin,"LOW") + print self.board.digitalRead(self.led_pin) #confirm LOW (0) + time.sleep(1) + self.board.digitalWrite(self.led_pin,"HIGH") + print self.board.digitalRead(self.led_pin) #confirm HIGH (1) + time.sleep(1) + +class SoftBlink(ArduinoExample): + """ + Fades an LED off and on, using + Arduino's analogWrite (PWM) function + """ + def __init__(self, led_pin, baud,port="",timeout=2): + super(SoftBlink, self).__init__(baud, port = port, timeout = timeout) + self.led_pin = led_pin + self.i = 0 + + def execute(self): + self.i+=1 + k=self.i%510 + if k%5==0: + if k>255: + k=510-k + self.board.analogWrite(self.led_pin,k) + +class AdjustBrightness(ArduinoExample): + """ + Adjusts brightness of an LED using a + potentiometer + """ + def __init__(self, led_pin, pot_pin,baud,port="",timeout=2): + super(AdjustBrightness, self).__init__(baud, port = port, + timeout = timeout) + self.led_pin = led_pin + self.pot_pin = pot_pin + + def execute(self): + time.sleep(0.01) + val=self.board.analogRead(self.pot_pin)/4 + print val + self.board.analogWrite(self.led_pin,self.val) + +class PingSonar(ArduinoExample): + """ + Gets distance measurement from Ping))) + ultrasonic rangefinder connected to pw_pin + """ + def __init__(self, pw_pin ,baud,port="",timeout=2): + super(PingSonar, self).__init__(baud, port = port, timeout = timeout) + self.pw_pin = pw_pin + + def execute(self): + duration = self.board.pulseIn(self.pw_pin, "HIGH") + inches = duration/72./2. + cent = duration/29./2. + print inches,"inches" + time.sleep(0.1) + +class LCD(ArduinoExample): + """ + Prints to two-line LCD connected to + pin tx + """ + def __init__(self, tx,ssbaud ,baud,port="",timeout=2): + super(LCD, self).__init__(baud, port = port, timeout = timeout) + self.tx = tx + self.ssbaud = ssbaud + + def execute(self, message): + self.board.SoftwareSerial.write(message) + + +if __name__=="__main__": + app = Blink(9600) + while True: + app.execute() \ No newline at end of file diff --git a/prototypes/prototype.ino b/sketches/prototype.ino similarity index 100% rename from prototypes/prototype.ino rename to sketches/prototype.ino