Skip to content

Commit a7c23d9

Browse files
committed
init
0 parents  commit a7c23d9

8 files changed

+254
-0
lines changed

MIT-license.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2009-2010 Akash Manohar J <[email protected]>
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.

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Python Arduino Prototyping API v2
2+
3+
This is a project based on the original [Python Arduino Prototyping API](https://github.com/HashNuke/Python-Arduino-Prototyping-API).
4+
I started a fork and after a while the whole thing was getting too different to make a pull request so I just put it here.
5+
The old project had wierd things going on to handle the communication, where I rely on parseInt() to do the work for me.
6+
There were also problems with analogWrite(), which is working in this version.
7+
8+
Major changes:
9+
- .pde is completely redone
10+
- Small changes in the arduino.py file
11+
- Added examples
12+
13+
Here follows the original README:
14+
15+
> &copy; 2009-2010 Akash Manohar J <[email protected]>
16+
> under the MIT License
17+
18+
The Python Arduino Prototyping API helps you to quickly prototype Arduino programs,
19+
without having to repeatedly load the program to the Arduino board.
20+
21+
#### Setup:
22+
23+
1. Load prototype.pde onto your Arduino dev board.
24+
2. Import the arduino lib in your python script.
25+
26+
27+
## Methods
28+
29+
*Arduino.output(list_of_output_pins)* - set the output pins
30+
31+
**Digital I/O**
32+
33+
1. *Arduino.setHigh(pin_number)*
34+
2. *Arduino.setLow(pin_number)*
35+
3. *Arduino.getState(pin_number)*
36+
4. *Arduino.getState()* - returns true if pin state is high, else it returns false.
37+
38+
**Analog I/O**
39+
40+
1. *Arduino.analogRead(pin_number)* - returns the analog value
41+
2. *Arduino.analogWrite(pin_number, value)* - sets the analog value
42+
43+
**Misc**
44+
45+
1.) *Arduino.turnOff()* - sets all the pins to low state
46+
47+
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.
48+
49+
## Usage example
50+
51+
from arduino import Arduino
52+
import time
53+
54+
b = Arduino('/dev/ttyUSB0')
55+
pin = 9
56+
57+
#declare output pins as a list/tuple
58+
b.output([pin])
59+
60+
for xrange(10):
61+
b.setHigh(pin)
62+
time.sleep(1)
63+
print b.getState(pin)
64+
b.setLow(pin)
65+
print b.getState(pin)
66+
time.sleep(1)
67+
68+
b.close()

arduino/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python
2+
3+
from arduino import *
4+

arduino/arduino.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
3+
import serial
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.__sendData(each_pin)
22+
return True
23+
24+
def setLow(self, pin):
25+
self.__sendData('0')
26+
self.__sendData(pin)
27+
return True
28+
29+
def setHigh(self, pin):
30+
self.__sendData('1')
31+
self.__sendData(pin)
32+
return True
33+
34+
def getState(self, pin):
35+
self.__sendData('2')
36+
self.__sendData(pin)
37+
return self.__formatPinState(self.__getData()[0])
38+
39+
def analogWrite(self, pin, value):
40+
self.__sendData('3')
41+
self.__sendData(pin)
42+
self.__sendData(value)
43+
return True
44+
45+
def analogRead(self, pin):
46+
self.__sendData('4')
47+
self.__sendData(pin)
48+
return self.__getData()
49+
50+
def turnOff(self):
51+
for each_pin in self.__OUTPUT_PINS:
52+
self.setLow(each_pin)
53+
return True
54+
55+
def __sendData(self, serial_data):
56+
while(self.__getData()[0] != "w"):
57+
pass
58+
self.serial.write(str(serial_data))
59+
60+
def __getData(self):
61+
return self.serial.readline().rstrip('\n')
62+
63+
def __formatPinState(self, pinValue):
64+
if pinValue == '1':
65+
return True
66+
else:
67+
return False
68+
69+
def close(self):
70+
self.serial.close()
71+
return True

prototype.pde

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
void setup() {
2+
Serial.begin(115200);
3+
Serial.setTimeout(5);
4+
5+
int cmd = readData();
6+
for (int i = 0; i < cmd; i++) {
7+
pinMode(readData(), OUTPUT);
8+
}
9+
}
10+
11+
void loop() {
12+
switch (readData()) {
13+
case 0 :
14+
//set digital low
15+
digitalWrite(readData(), LOW); break;
16+
case 1 :
17+
//set digital high
18+
digitalWrite(readData(), HIGH); break;
19+
case 2 :
20+
//get digital value
21+
Serial.println(digitalRead(readData())); break;
22+
case 3 :
23+
// set analog value
24+
analogWrite(readData(), readData()); break;
25+
case 4 :
26+
//read analog value
27+
Serial.println(analogRead(readData()));
28+
}
29+
}
30+
31+
char readData() {
32+
Serial.println("w");
33+
while(1) {
34+
if(Serial.available() > 0) {
35+
return Serial.parseInt();
36+
}
37+
}
38+
}

sample_analogread.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from arduino import Arduino
2+
import time
3+
4+
b = Arduino('/dev/ttyUSB0')
5+
pin = 1
6+
7+
b.output([])
8+
9+
while (True):
10+
val = b.analogRead(pin)
11+
print val
12+
time.sleep(0.5)

sample_analogwrite.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from arduino import Arduino
2+
import time
3+
4+
b = Arduino('/dev/ttyUSB0')
5+
pin = 9
6+
7+
#declare output pins as a list/tuple
8+
b.output([pin])
9+
10+
brightness = 0
11+
fadeAmount = 3
12+
13+
b.output([pin])
14+
15+
while (True):
16+
b.analogWrite(pin, brightness)
17+
time.sleep(0.05)
18+
print b.analogRead(pin)
19+
brightness = brightness + fadeAmount
20+
21+
if (brightness == 0 or brightness == 255):
22+
fadeAmount = -fadeAmount

sample_blink.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from arduino import Arduino
2+
import time
3+
4+
b = Arduino('/dev/ttyUSB0')
5+
pin = 9
6+
7+
#declare output pins as a list/tuple
8+
b.output([pin])
9+
10+
11+
for xrange(10):
12+
b.setHigh(pin)
13+
time.sleep(1)
14+
print b.getState(pin)
15+
b.setLow(pin)
16+
print b.getState(pin)
17+
time.sleep(1)
18+
19+
b.close()
20+

0 commit comments

Comments
 (0)