-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyduino.py
More file actions
executable file
·290 lines (236 loc) · 9.44 KB
/
pyduino.py
File metadata and controls
executable file
·290 lines (236 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#! /usr/bin/env python
"""pyduino - A python library to interface with the firmata arduino firmware.
Copyright (C) 2007 Joe Turner <orphansandoligarchs@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
__version__ = "0.19_dev"
import time
import serial
# Message command bytes - straight outta Pd_firmware.pde
DIGITAL_MESSAGE = 0x90 # send data for a digital pin
ANALOG_MESSAGE = 0xE0 # send data for an analog pin (or PWM)
# PULSE_MESSAGE = 0xA0 # proposed pulseIn/Out message (SysEx)
# SHIFTOUT_MESSAGE = 0xB0 # proposed shiftOut message (SysEx)
REPORT_ANALOG_PIN = 0xC0 # enable analog input by pin #
REPORT_DIGITAL_PORTS = 0xD0 # enable digital input by port pair
START_SYSEX = 0xF0 # start a MIDI SysEx message
SET_DIGITAL_PIN_MODE = 0xF4 # set a digital pin to INPUT or OUTPUT
END_SYSEX = 0xF7 # end a MIDI SysEx message
REPORT_VERSION = 0xF9 # report firmware version
SYSTEM_RESET = 0xFF # reset from MIDI
# Pin modes
UNAVAILABLE = -1
DIGITAL_INPUT = 0
DIGITAL_OUTPUT = 1
DIGITAL_PWM = 2
PWM_PINS = (9, 10, 11)
class Arduino:
"""Base class for the arduino board"""
def __init__(self, port):
self.sp = serial.Serial(port, 115200, timeout=0.02)
# Allow 2 secs for Diecimila auto-reset to happen
time.sleep(2)
self.analog = []
for i in range(6):
self.analog.append(AnalogPin(self.sp, i))
self.digital_ports = []
for i in range(2):
self.digital_ports.append(DigitalPort(self.sp, i))
# Don't mess with Rx/Tx
self.digital_ports[0].pins[0].mode = UNAVAILABLE
self.digital_ports[0].pins[1].mode = UNAVAILABLE
# Pins 14 and 15 aren't used
self.digital_ports[1].pins[6].mode = UNAVAILABLE
self.digital_ports[1].pins[7].mode = UNAVAILABLE
# Syntactic sugar - don't make the end user mess with ports too much
# and keep the api stable-ish
self.digital = []
self.digital += self.digital_ports[0].pins
# Leave out pins 14 and 15 so the length matches the board
self.digital += self.digital_ports[1].pins[:6]
# Obtain firmata version
self.firmata_version = None
self.sp.write(chr(REPORT_VERSION))
self.iterate()
def __str__(self):
return "Arduino: %s"% self.sp.port
def iterate(self):
"""Read and handle a command byte from Arduino's serial port"""
data = self.sp.read()
if data != "":
self._process_input(ord(data))
def _process_input(self, data):
"""Process a command byte and any additional information bytes"""
if data < 0xF0:
#Multibyte
message = data & 0xF0
if message == DIGITAL_MESSAGE:
port_number = data & 0x0F
#Digital in
lsb = ""
msb = ""
while lsb == "":
lsb = self.sp.read()
while msb == "":
msb = self.sp.read()
lsb = ord(lsb)
msb = ord(msb)
self.digital_ports[port_number].set_value(msb << 7 | lsb)
elif message == ANALOG_MESSAGE:
pin_number = data & 0x0F
#Analog in
lsb = ""
msb = ""
while lsb == "":
lsb = self.sp.read()
while msb == "":
msb = self.sp.read()
lsb = ord(lsb)
msb = ord(msb)
value = float(msb << 7 | lsb) / 1023
self.analog[pin_number].set_value(value)
elif data == REPORT_VERSION:
minor, major = self.sp.read(2)
self.firmata_version = (ord(major), ord(minor))
def get_firmata_version(self):
"""Return a (major, minor) version tuple for the firmata firmware"""
return self.firmata_version
def exit(self):
"""Exit the application cleanly"""
self.sp.close()
class DigitalPort:
"""Digital pin on the arduino board"""
def __init__(self, sp, port_number):
self.sp = sp
self.port_number = port_number
self.is_active = 0
self.pins = []
for i in range(8):
self.pins.append(DigitalPin(sp, self, i))
def __str__(self):
return "Digital Port %i"% self.port_number
def set_active(self, active):
""" Set the port to report values """
self.is_active = active
message = chr(REPORT_DIGITAL_PORTS + self.port_number)
message += chr(active)
self.sp.write(message)
def get_active(self):
"""Return whether the port is reporting values"""
return self.is_active
def set_value(self, mask):
"""Record the value of each of the input pins belonging to the port"""
for pin in self.pins:
if pin.mode == DIGITAL_INPUT:
pin.set_value((mask & (1 << pin.pin_number)) > 1)
def write(self):
"""Set the output pins of the port to the correct state"""
mask = 0
for pin in self.pins:
if pin.mode == DIGITAL_OUTPUT:
if pin.value == 1:
mask |= 1 << pin.pin_number
message = chr(DIGITAL_MESSAGE + self.port_number)
message += chr(mask % 128)
message += chr(mask >> 7)
self.sp.write(message)
class DigitalPin:
"""Digital pin on the arduino board"""
def __init__(self, sp, port, pin_number):
self.sp = sp
self.port = port
self.pin_number = pin_number
self.value = 0
self.mode = DIGITAL_INPUT
def __str__(self):
return "Digital Pin %i"% self.pin_number
def set_mode(self, mode):
"""Set the mode of operation for the pin
Argument:
mode, takes a value of: - DIGITAL_INPUT
- DIGITAL_OUTPUT
- DIGITAL_PWM
"""
if (mode == DIGITAL_PWM and
self._get_board_pin_number() not in PWM_PINS):
error_message = "Digital pin %i does not have PWM capabilities" \
% (self._get_board_pin_number())
raise IOError, error_message
if self.mode == UNAVAILABLE:
raise IOError, "Cannot set mode for pin %i" \
% (self._get_board_pin_number())
self.mode = mode
command = chr(SET_DIGITAL_PIN_MODE)
command += chr(self._get_board_pin_number())
command += chr(mode)
self.sp.write(command)
def get_mode(self):
"""Return the pin mode, values explained in set_mode()"""
return self.mode
def _get_board_pin_number(self):
"""Return the number of the pin as written on the board"""
return (self.port.port_number * 8) + self.pin_number
def set_value(self, value):
"""Record the value of the pin"""
self.value = value
def read(self):
"""Return the output value of the pin, values explained in write()"""
if self.mode == UNAVAILABLE:
raise IOError, "Cannot read pin %i"% self._get_board_pin_number()
return self.value
def write(self, value):
"""Output a voltage from the pin
Argument:
value, takes a boolean if the pin is in output mode, or a float from 0
to 1 if the pin is in PWM mode
"""
if self.mode == UNAVAILABLE:
raise IOError, "Cannot read from pin %i" \
% (self._get_board_pin_number())
if self.mode == DIGITAL_INPUT:
raise IOError, "Digital pin %i is not an output" \
% (self._get_board_pin_number())
elif value != self.read():
self.value = value
if self.mode == DIGITAL_OUTPUT:
self.port.write()
elif self.mode == DIGITAL_PWM:
value = int(round(value * 255))
message = chr(ANALOG_MESSAGE + self._get_board_pin_number())
message += chr(value % 128)
message += chr(value >> 7)
self.sp.write(message)
class AnalogPin:
"""Analog pin on the arduino board"""
def __init__(self, sp, pin_number):
self.sp = sp
self.pin_number = pin_number
self.active = 0
self.value = -1
def __str__(self):
return "Analog Input %i"% self.pin_number
def set_active(self, active):
"""Set the pin to report values"""
self.active = active
message = chr(REPORT_ANALOG_PIN + self.pin_number)
message += chr(active)
self.sp.write(message)
def get_active(self):
"""Return whether the pin is reporting values"""
return self.active
def set_value(self, value):
"""Record the value of the pin"""
self.value = value
def read(self):
"""Return the input in the range 0-1"""
return self.value