11#!/usr/bin/env python
2- from serial .tools import list_ports
3- import serial , time
2+ import itertools
43import platform
4+ import serial
5+ import time
6+ from serial .tools import list_ports
57if platform .system () == 'Windows' :
68 import _winreg as winreg
79else :
810 import glob
9- import itertools
1011
1112
1213def enumerate_serial_ports ():
13- """ Uses the Win32 registry to return a iterator of serial
14+ """
15+ Uses the Win32 registry to return a iterator of serial
1416 (COM) ports existing on this computer.
1517 """
1618 path = 'HARDWARE\\ DEVICEMAP\\ SERIALCOMM'
@@ -22,41 +24,40 @@ def enumerate_serial_ports():
2224 for i in itertools .count ():
2325 try :
2426 val = winreg .EnumValue (key , i )
25- yield (str (val [1 ]))# , str(val[0]))
27+ yield (str (val [1 ])) # , str(val[0]))
2628 except EnvironmentError :
2729 break
2830
29-
31+
3032class Arduino (object ):
31- def __init__ (self ,baud = 9600 ,port = None ,timeout = 2 ):
33+ def __init__ (self , baud = 9600 , port = None , timeout = 2 ):
3234 """
3335 Initializes serial communication with Arduino.
3436 Attempts to self-select COM port, if not specified.
3537 """
3638 self .baud = baud
3739 self .timeout = timeout
38- self .ss_connected = False
40+ self .ss_connected = False
3941 self .port = port
4042 if not self .port :
4143 self .findPort ()
4244 else :
43- self .sr = serial .Serial (self .port , self .baud ,timeout = self .timeout )
45+ self .sr = serial .Serial (self .port , self .baud ,
46+ timeout = self .timeout )
4447 self .SoftwareSerial = SoftwareSerial (self )
4548 self .Servos = Servos (self )
4649 self .sr .flush ()
4750
48-
4951 def version (self ):
50- cmd_str = '' .join (["@version%$!" ])
52+ cmd_str = '' .join (["@version%$!" ])
5153 try :
5254 self .sr .write (cmd_str )
5355 self .sr .flush ()
5456 except :
5557 pass
56- version = self .sr .readline ().replace ("\r \n " ,"" )
58+ version = self .sr .readline ().replace ("\r \n " , "" )
5759 return version
5860
59-
6061 def findPort (self ):
6162 """
6263 Sets port to the first Arduino found
@@ -73,7 +74,7 @@ def findPort(self):
7374 version = None
7475 try :
7576 print 'Testing ' , p
76- self .sr = serial .Serial (p , self .baud ,timeout = self .timeout )
77+ self .sr = serial .Serial (p , self .baud , timeout = self .timeout )
7778 time .sleep (2 )
7879 version = self .version ()
7980 if version != 'version' :
@@ -85,8 +86,7 @@ def findPort(self):
8586 print "Exception: " , e
8687 pass
8788
88-
89- def digitalWrite (self ,pin ,val ):
89+ def digitalWrite (self , pin , val ):
9090 """
9191 Sends digitalWrite command
9292 to digital pin on Arduino
@@ -95,19 +95,18 @@ def digitalWrite(self,pin,val):
9595 pin : digital pin number
9696 val : either "HIGH" or "LOW"
9797 """
98- if val == "LOW" :
98+ if val == "LOW" :
9999 pin_ = - pin
100100 else :
101101 pin_ = pin
102- cmd_str = '' .join (["@dw%" ,str (pin_ ),"$!" ])
102+ cmd_str = '' .join (["@dw%" , str (pin_ ), "$!" ])
103103 try :
104104 self .sr .write (cmd_str )
105- self .sr .flush ()
105+ self .sr .flush ()
106106 except :
107107 pass
108108
109-
110- def analogWrite (self ,pin ,val ):
109+ def analogWrite (self , pin , val ):
111110 """
112111 Sends analogWrite pwm command
113112 to pin on Arduino
@@ -116,19 +115,18 @@ def analogWrite(self,pin,val):
116115 pin : pin number
117116 val : integer 0 (off) to 255 (always on)
118117 """
119- if val > 255 :
120- val = 255
121- elif val < 0 :
122- val = 0
123- cmd_str = '' .join (["@aw%" ,str (pin ),"%" ,str (val ),"$!" ])
118+ if val > 255 :
119+ val = 255
120+ elif val < 0 :
121+ val = 0
122+ cmd_str = '' .join (["@aw%" , str (pin ), "%" , str (val ), "$!" ])
124123 try :
125124 self .sr .write (cmd_str )
126125 self .sr .flush ()
127126 except :
128127 pass
129128
130-
131- def analogRead (self ,pin ):
129+ def analogRead (self , pin ):
132130 """
133131 Returns the value of a specified
134132 analog pin.
@@ -137,126 +135,121 @@ def analogRead(self,pin):
137135 returns:
138136 value: integer from 1 to 1023
139137 """
140- cmd_str = '' .join (["@ar%" ,str (pin ),"$!" ])
138+ cmd_str = '' .join (["@ar%" , str (pin ), "$!" ])
141139 try :
142140 self .sr .write (cmd_str )
143141 self .sr .flush ()
144142 except :
145143 pass
146- rd = self .sr .readline ().replace ("\r \n " ,"" )
144+ rd = self .sr .readline ().replace ("\r \n " , "" )
147145 try :
148146 return int (rd )
149147 except :
150148 return 0
151149
152-
153- def pinMode (self ,pin ,val ):
150+ def pinMode (self , pin , val ):
154151 """
155152 Sets I/O mode of pin
156153 inputs:
157154 pin: pin number to toggle
158155 val: "INPUT" or "OUTPUT"
159156 """
160- if val == "INPUT" :
157+ if val == "INPUT" :
161158 pin_ = - pin
162159 else :
163160 pin_ = pin
164- cmd_str = '' .join (["@pm%" ,str (pin_ ),"$!" ])
161+ cmd_str = '' .join (["@pm%" , str (pin_ ), "$!" ])
165162 try :
166163 self .sr .write (cmd_str )
167- self .sr .flush ()
164+ self .sr .flush ()
168165 except :
169166 pass
170167
171-
172- def pulseIn (self ,pin ,val ):
168+ def pulseIn (self , pin , val ):
173169 """
174170 Reads a pulse from a pin
175-
171+
176172 inputs:
177173 pin: pin number for pulse measurement
178174 returns:
179175 duration : pulse length measurement
180176 """
181- if val == "LOW" :
177+ if val == "LOW" :
182178 pin_ = - pin
183179 else :
184180 pin_ = pin
185- cmd_str = '' .join (["@pi%" ,str (pin_ ),"$!" ])
181+ cmd_str = '' .join (["@pi%" , str (pin_ ), "$!" ])
186182 try :
187183 self .sr .write (cmd_str )
188- self .sr .flush ()
184+ self .sr .flush ()
189185 except :
190186 pass
191- rd = self .sr .readline ().replace ("\r \n " ,"" )
187+ rd = self .sr .readline ().replace ("\r \n " , "" )
192188 try :
193189 return float (rd )
194190 except :
195- return - 1
191+ return - 1
196192
197-
198- def pulseIn_set (self ,pin ,val ,numTrials = 5 ):
193+ def pulseIn_set (self , pin , val , numTrials = 5 ):
199194 """
200195 Sets a digital pin value, then reads the response
201196 as a pulse width.
202197 Useful for some ultrasonic rangefinders, etc.
203-
198+
204199 inputs:
205200 pin: pin number for pulse measurement
206201 val: "HIGH" or "LOW". Pulse is measured
207202 when this state is detected
208203 numTrials: number of trials (for an average)
209204 returns:
210205 duration : an average of pulse length measurements
211-
206+
212207 This method will automatically toggle
213- I/O modes on the pin and precondition the
208+ I/O modes on the pin and precondition the
214209 measurment with a clean LOW/HIGH pulse.
215- Arduino.pulseIn_set(pin,"HIGH") is
210+ Arduino.pulseIn_set(pin,"HIGH") is
216211 equivalent to the Arduino sketch code:
217-
212+
218213 pinMode(pin, OUTPUT);
219214 digitalWrite(pin, LOW);
220215 delayMicroseconds(2);
221216 digitalWrite(pin, HIGH);
222217 delayMicroseconds(5);
223218 digitalWrite(pin, LOW);
224219 pinMode(pin, INPUT);
225- long duration = pulseIn(pin, HIGH);
220+ long duration = pulseIn(pin, HIGH);
226221 """
227- if val == "LOW" :
222+ if val == "LOW" :
228223 pin_ = - pin
229224 else :
230225 pin_ = pin
231- cmd_str = '' .join (["@ps%" ,str (pin_ ),"$!" ])
226+ cmd_str = '' .join (["@ps%" , str (pin_ ), "$!" ])
232227 durations = []
233228 for s in range (numTrials ):
234229 try :
235230 self .sr .write (cmd_str )
236- self .sr .flush ()
231+ self .sr .flush ()
237232 except :
238233 pass
239- rd = self .sr .readline ().replace ("\r \n " ,"" )
240- if rd .isdigit () == True :
241- if (int (rd ) > 1 ) == True :
234+ rd = self .sr .readline ().replace ("\r \n " , "" )
235+ if rd .isdigit ():
236+ if (int (rd ) > 1 ):
242237 durations .append (int (rd ))
243238 if len (durations ) > 0 :
244239 duration = int (sum (durations )) / int (len (durations ))
245240 else :
246241 duration = None
247-
242+
248243 try :
249244 return float (duration )
250245 except :
251246 return - 1
252247
253-
254248 def close (self ):
255249 self .sr .flush ()
256- self .sr .close ()
250+ self .sr .close ()
257251
258-
259- def digitalRead (self ,pin ):
252+ def digitalRead (self , pin ):
260253 """
261254 Returns the value of a specified
262255 digital pin.
@@ -265,19 +258,18 @@ def digitalRead(self,pin):
265258 returns:
266259 value: 0 for "LOW", 1 for "HIGH"
267260 """
268- cmd_str = '' .join (["@dr%" ,str (pin ),"$!" ])
261+ cmd_str = '' .join (["@dr%" , str (pin ), "$!" ])
269262 try :
270263 self .sr .write (cmd_str )
271264 self .sr .flush ()
272265 except :
273266 pass
274- rd = self .sr .readline ().replace ("\r \n " ,"" )
267+ rd = self .sr .readline ().replace ("\r \n " , "" )
275268 try :
276269 return 1 - int (rd )
277270 except :
278271 return 0
279272
280-
281273 def Melody (self , pin , melody , durations ):
282274 """
283275 Plays a melody.
@@ -290,8 +282,10 @@ def Melody(self, pin, melody, durations):
290282
291283 Melodies of the following lenght, can cause trouble
292284 when playing it multiple times.
293- board.Melody(9,["C4","G3","G3","A3","G3",0,"B3","C4"],[4,8,8,4,4,4,4,4])
294- Playing short melodies (1 or 2 tones) didn't cause trouble during testing
285+ board.Melody(9,["C4","G3","G3","A3","G3",0,"B3","C4"],
286+ [4,8,8,4,4,4,4,4])
287+ Playing short melodies (1 or 2 tones) didn't cause
288+ trouble during testing
295289 """
296290 NOTES = dict (B0 = 31 ,C1 = 33 ,CS1 = 35 ,D1 = 37 ,DS1 = 39 ,E1 = 41 ,F1 = 44 ,FS1 = 46 ,G1 = 49 \
297291 ,GS1 = 52 ,A1 = 55 ,AS1 = 58 ,B1 = 62 ,C2 = 65 ,CS2 = 69 ,D2 = 73 ,DS2 = 78 ,E2 = 82 \
0 commit comments