11#!/usr/bin/env python
22from serial .tools import list_ports
33import serial , time
4- ##SDM
54import platform
65if platform .system () == 'Windows' :
76 import _winreg as winreg
87if platform .system () == 'Linux' :
98 import glob
109import itertools
1110
11+
1212def enumerate_serial_ports ():
1313 """ Uses the Win32 registry to return a iterator of serial
1414 (COM) ports existing on this computer.
@@ -25,7 +25,7 @@ def enumerate_serial_ports():
2525 yield (str (val [1 ]))#, str(val[0]))
2626 except EnvironmentError :
2727 break
28- ##SDM
28+
2929
3030class Arduino (object ):
3131 def __init__ (self ,baud = 9600 ,port = "" ,timeout = 2 ):
@@ -36,7 +36,6 @@ def __init__(self,baud=9600,port="",timeout=2):
3636 self .baud = baud
3737 self .timeout = timeout
3838 self .ss_connected = False
39- ##SDM
4039 self .port = port
4140 if self .port == "" :
4241 if platform .system () == 'Windows' :
@@ -59,13 +58,11 @@ def __init__(self,baud=9600,port="",timeout=2):
5958 except Exception , e :
6059 print "Exception: " , e
6160 pass
62- ##SDM
63- #--self.sr = serial.Serial(self.port, self.baud,timeout =self.timeout)
64- #--time.sleep(2)
6561 self .SoftwareSerial = SoftwareSerial (self )
6662 self .Servos = Servos (self )
6763 self .sr .flush ()
6864
65+
6966 def version (self ):
7067 cmd_str = '' .join (["@version%$!" ])
7168 try :
@@ -86,6 +83,7 @@ def findPort(self):
8683 if ("FTDIBUS" in pt [- 1 ]) or ("usbserial" in pt [- 1 ]):
8784 self .port = pt [0 ]
8885 return
86+
8987
9088 def digitalWrite (self ,pin ,val ):
9189 """
@@ -107,6 +105,7 @@ def digitalWrite(self,pin,val):
107105 except :
108106 pass
109107
108+
110109 def analogWrite (self ,pin ,val ):
111110 """
112111 Sends analogWrite pwm command
@@ -127,6 +126,7 @@ def analogWrite(self,pin,val):
127126 except :
128127 pass
129128
129+
130130 def analogRead (self ,pin ):
131131 """
132132 Returns the value of a specified
@@ -166,6 +166,7 @@ def pinMode(self,pin,val):
166166 self .sr .flush ()
167167 except :
168168 pass
169+
169170
170171 def pulseIn (self ,pin ,val ):
171172 """
@@ -175,7 +176,6 @@ def pulseIn(self,pin,val):
175176 pin: pin number for pulse measurement
176177 returns:
177178 duration : pulse length measurement
178-
179179 """
180180 if val == "LOW" :
181181 pin_ = - pin
@@ -192,6 +192,7 @@ def pulseIn(self,pin,val):
192192 return float (rd )
193193 except :
194194 return - 1
195+
195196
196197 def pulseIn_set (self ,pin ,val ,numTrials = 5 ):
197198 """
@@ -238,7 +239,6 @@ def pulseIn_set(self,pin,val,numTrials=5):
238239 if rd .isdigit () == True :
239240 if (int (rd ) > 1 ) == True :
240241 durations .append (int (rd ))
241- #print durations
242242 if len (durations ) > 0 :
243243 duration = int (sum (durations )) / int (len (durations ))
244244 else :
@@ -248,10 +248,12 @@ def pulseIn_set(self,pin,val,numTrials=5):
248248 return float (duration )
249249 except :
250250 return - 1
251+
251252
252253 def close (self ):
253254 self .sr .flush ()
254255 self .sr .close ()
256+
255257
256258 def digitalRead (self ,pin ):
257259 """
@@ -274,6 +276,7 @@ def digitalRead(self,pin):
274276 except :
275277 return 0
276278
279+
277280 def Melody (self , pin , melody , durations ):
278281 """
279282 Plays a melody.
@@ -312,14 +315,12 @@ def Melody(self, pin, melody, durations):
312315 d = str (durations [duration ])
313316 cmd_str = cmd_str + d + "%"
314317 cmd_str = cmd_str [:- 1 ]+ "$!"
315- #print cmd_str
316318 try :
317319 self .sr .write (cmd_str )
318320 self .sr .flush ()
319321 except :
320322 pass
321323 cmd_str = '' .join (["@nto%" ,str (pin ),"$!" ])
322- #print cmd_str
323324 try :
324325 self .sr .write (cmd_str )
325326 self .sr .flush ()
@@ -330,6 +331,7 @@ def Melody(self, pin, melody, durations):
330331 else :
331332 return - 1
332333
334+
333335 def capacitivePin (self , pin ):
334336 '''
335337 Input:
@@ -347,15 +349,13 @@ def capacitivePin(self, pin):
347349 self .sr .write (cmd_str )
348350 rd = self .sr .readline ().replace ("\r \n " ,"" )
349351 if rd .isdigit () == True :
350- #print "rd: " + str(rd)
351352 return int (rd )
352353
353354
354- ##SDM
355355class Shrimp (Arduino ):
356356 def __init__ (self ):
357357 Arduino .__init__ (self )
358- ##SDM
358+
359359
360360class Wires (object ):
361361 """
@@ -364,6 +364,7 @@ class Wires(object):
364364 def __init__ (self , board ):
365365 self .board = board
366366 self .sr = board .sr
367+
367368
368369class Servos (object ):
369370 """
@@ -374,6 +375,7 @@ def __init__(self, board):
374375 self .board = board
375376 self .sr = board .sr
376377 self .servo_pos = {}
378+
377379
378380 def attach (self ,pin ,min = 544 , max = 2400 ):
379381 cmd_str = '' .join (["@sva%" ,str (pin ),"%" ,str (min ),"%" ,str (max ),"$!" ])
@@ -389,6 +391,7 @@ def attach(self,pin,min = 544, max = 2400):
389391 return 1
390392 except :
391393 return 0
394+
392395
393396 def detach (self ,pin ):
394397 cmd_str = '' .join (["@svd%" ,str (position ),"$!" ])
@@ -399,6 +402,7 @@ def detach(self,pin):
399402 pass
400403 del self .servo_pos [pin ]
401404
405+
402406 def write (self ,pin ,angle ):
403407 position = self .servo_pos [pin ]
404408 cmd_str = '' .join (["@svw%" ,str (position ),"%" ,str (angle ),"$!" ])
@@ -407,6 +411,7 @@ def write(self,pin,angle):
407411 self .sr .flush ()
408412 except :
409413 pass
414+
410415
411416 def writeMicroseconds (self ,pin ,uS ):
412417 cmd_str = '' .join (["@svw%" ,str (position ),"%" ,str (uS ),"$!" ])
@@ -415,6 +420,7 @@ def writeMicroseconds(self,pin,uS):
415420 self .sr .flush ()
416421 except :
417422 pass
423+
418424
419425 def read (self ,pin ):
420426 if pin not in self .servo_pos .keys ():
@@ -433,6 +439,7 @@ def read(self,pin):
433439 except :
434440 return None
435441
442+
436443class SoftwareSerial (object ):
437444 """
438445 Class for Arduino software serial functionality
@@ -442,6 +449,7 @@ def __init__(self,board):
442449 self .sr = board .sr
443450 self .connected = False
444451
452+
445453 def begin (self ,p1 ,p2 ,baud ):
446454 """
447455 Create software serial instance on
@@ -460,6 +468,7 @@ def begin(self,p1,p2,baud):
460468 else :
461469 self .connected = False
462470 return False
471+
463472
464473 def write (self ,data ):
465474 """
@@ -479,6 +488,7 @@ def write(self,data):
479488 else :
480489 return False
481490
491+
482492 def read (self ):
483493 """
484494 returns first character read from
@@ -493,18 +503,3 @@ def read(self):
493503 return response
494504 else :
495505 return False
496-
497- ##if __name__=="__main__":
498- # quick test
499- ## board=Arduino(9600, 'COM9')
500- ## board.Servos.attach(9)
501- ## board.Servos.write(9,90)
502- ## time.sleep(1)
503- ## print board.Servos.read(9)
504- ## t=time.time()
505- ## board.Servos.write(9,1)
506- ## while True:
507- ## a=board.Servos.read(9)
508- ## if a == 1:
509- ## print "time",time.time() - t
510- ## break
0 commit comments