Always use build_cmd_str.

This commit is contained in:
Ian Joseph Wilson
2013-05-30 11:24:42 -07:00
parent e8e4359c06
commit baeb5f2f82

View File

@@ -29,6 +29,24 @@ def enumerate_serial_ports():
break break
def build_cmd_str(cmd, args=None):
"""
Build a command string that can be sent to the arduino.
Input:
cmd (str): the command to send to the arduino, must not
contain a % character
args (iterable): the arguments to send to the command
@TODO: a strategy is needed to escape % characters in the args
"""
if args:
args = '%'.join(map(str, args))
else:
args = ''
return "@{cmd}%{args}$!".format(cmd=cmd, args=args)
class Arduino(object): class Arduino(object):
def __init__(self, baud=9600, port=None, timeout=2): def __init__(self, baud=9600, port=None, timeout=2):
""" """
@@ -49,7 +67,7 @@ class Arduino(object):
self.sr.flush() self.sr.flush()
def version(self): def version(self):
cmd_str = ''.join(["@version%$!"]) cmd_str = build_cmd_str("version")
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -99,7 +117,7 @@ class Arduino(object):
pin_ = -pin pin_ = -pin
else: else:
pin_ = pin pin_ = pin
cmd_str = ''.join(["@dw%", str(pin_), "$!"]) cmd_str = build_cmd_str("dw", (pin_,))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -119,7 +137,7 @@ class Arduino(object):
val = 255 val = 255
elif val < 0: elif val < 0:
val = 0 val = 0
cmd_str = ''.join(["@aw%", str(pin), "%", str(val), "$!"]) cmd_str = build_cmd_str("aw", (pin, val))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -135,7 +153,7 @@ class Arduino(object):
returns: returns:
value: integer from 1 to 1023 value: integer from 1 to 1023
""" """
cmd_str = ''.join(["@ar%", str(pin), "$!"]) cmd_str = build_cmd_str("ar", (pin,))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -158,7 +176,7 @@ class Arduino(object):
pin_ = -pin pin_ = -pin
else: else:
pin_ = pin pin_ = pin
cmd_str = ''.join(["@pm%", str(pin_), "$!"]) cmd_str = build_cmd_str("pm", (pin_,))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -178,7 +196,7 @@ class Arduino(object):
pin_ = -pin pin_ = -pin
else: else:
pin_ = pin pin_ = pin
cmd_str = ''.join(["@pi%", str(pin_), "$!"]) cmd_str = build_cmd_str("pi", (pin_,))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -223,7 +241,7 @@ class Arduino(object):
pin_ = -pin pin_ = -pin
else: else:
pin_ = pin pin_ = pin
cmd_str = ''.join(["@ps%", str(pin_), "$!"]) cmd_str = build_cmd_str("ps", (pin_,))
durations = [] durations = []
for s in range(numTrials): for s in range(numTrials):
try: try:
@@ -258,7 +276,7 @@ class Arduino(object):
returns: returns:
value: 0 for "LOW", 1 for "HIGH" value: 0 for "LOW", 1 for "HIGH"
""" """
cmd_str = ''.join(["@dr%", str(pin), "$!"]) cmd_str = build_cmd_str("dr", (pin,))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -300,22 +318,17 @@ class Arduino(object):
,GS7=3322,A7=3520,AS7=3729,B7=3951,C8=4186,CS8=4435,D8=4699,DS8=4978) ,GS7=3322,A7=3520,AS7=3729,B7=3951,C8=4186,CS8=4435,D8=4699,DS8=4978)
if (type(melody) == list) and (type(durations) == list): if (type(melody) == list) and (type(durations) == list):
length = len(melody) length = len(melody)
cmd_str = "@to%"+str(length)+"%"+str(pin)+"%" cmd_args = [length, pin]
d = ""
if length == len(durations): if length == len(durations):
for note in range(length): cmd_args.extend([NOTES.get(melody[note]) for note in range(length)])
n = NOTES.get(melody[note]) cmd_args.extend([durations[duration] for duration in range(len(durations))])
cmd_str = cmd_str+str(n)+"%" cmd_str = build_cmd_str("to", cmd_args)
for duration in range(len(durations)):
d = str(durations[duration])
cmd_str = cmd_str+d+"%"
cmd_str = cmd_str[:-1]+"$!"
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
except: except:
pass pass
cmd_str=''.join(["@nto%",str(pin),"$!"]) cmd_str = build_cmd_str("nto", [pin])
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -340,7 +353,7 @@ class Arduino(object):
will short circuit the pin, potentially damaging will short circuit the pin, potentially damaging
the Arduino/Shrimp and any hardware attached to the pin. the Arduino/Shrimp and any hardware attached to the pin.
''' '''
cmd_str="@cap%"+str(pin)+"$!" cmd_str = build_cmd_str("cap", (pin,))
self.sr.write(cmd_str) self.sr.write(cmd_str)
rd = self.sr.readline().replace("\r\n","") rd = self.sr.readline().replace("\r\n","")
if rd.isdigit() == True: if rd.isdigit() == True:
@@ -356,7 +369,7 @@ class Arduino(object):
pinOrder (String): either 'MSBFIRST' or 'LSBFIRST' pinOrder (String): either 'MSBFIRST' or 'LSBFIRST'
value (int): an integer from 0 and 255 value (int): an integer from 0 and 255
""" """
cmd_str = self._buildCmdStr("so", cmd_str = build_cmd_str("so",
(dataPin, clockPin, pinOrder, value)) (dataPin, clockPin, pinOrder, value))
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -372,24 +385,13 @@ class Arduino(object):
Output: Output:
(int) an integer from 0 to 255 (int) an integer from 0 to 255
""" """
cmd_str = self._buildCmdStr("si", (dataPin, clockPin, pinOrder)) cmd_str = build_cmd_str("si", (dataPin, clockPin, pinOrder))
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
rd = self.sr.readline().replace("\r\n","") rd = self.sr.readline().replace("\r\n","")
if rd.isdigit() == True: if rd.isdigit() == True:
return int(rd) return int(rd)
def _buildCmdStr(self, cmd, args):
"""
Build a command string that can be sent to the arduino.
Input:
cmd (str): the command to send to the arduino
args (iterable): the arguments to send to the command
"""
args = '%'.join(map(str, args))
return "@{cmd}%{args}$!".format(cmd=cmd, args=args)
class Shrimp(Arduino): class Shrimp(Arduino):
def __init__(self): def __init__(self):
@@ -417,7 +419,7 @@ class Servos(object):
def attach(self,pin,min = 544, max = 2400): def attach(self,pin,min = 544, max = 2400):
cmd_str=''.join(["@sva%",str(pin),"%",str(min),"%",str(max),"$!"]) cmd_str = build_cmd_str("sva", (pin, min, max))
while True: while True:
self.sr.write(cmd_str) self.sr.write(cmd_str)
@@ -433,8 +435,8 @@ class Servos(object):
return 1 return 1
def detach(self,pin): def detach(self,pin):
cmd_str=''.join(["@svd%",str(position),"$!"]) cmd_str = build_cmd_str("svd", (position,))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -445,25 +447,23 @@ class Servos(object):
def write(self,pin,angle): def write(self,pin,angle):
position = self.servo_pos[pin] position = self.servo_pos[pin]
cmd_str=''.join(["@svw%",str(position),"%",str(angle),"$!"]) cmd_str = build_cmd_str("svw" (position, angle))
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
def writeMicroseconds(self, pin, uS):
def writeMicroseconds(self,pin,uS):
position = self.servo_pos[pin] position = self.servo_pos[pin]
cmd_str=''.join(["@svw%",str(position),"%",str(uS),"$!"]) cmd_str = build_cmd_str("svw", (position, uS))
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
def read(self,pin): def read(self,pin):
if pin not in self.servo_pos.keys(): if pin not in self.servo_pos.keys():
self.attach(pin) self.attach(pin)
position = self.servo_pos[pin] position = self.servo_pos[pin]
cmd_str=''.join(["@svr%",str(position),"$!"]) cmd_str = build_cmd_str("svr", (position,))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -492,7 +492,7 @@ class SoftwareSerial(object):
Create software serial instance on Create software serial instance on
specified tx,rx pins, at specified baud specified tx,rx pins, at specified baud
""" """
cmd_str=''.join(["@ss%",str(p1),"%",str(p2),"%",str(baud),"$!"]) cmd_str = build_cmd_str("ss", (p1, p2, baud))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -513,7 +513,7 @@ class SoftwareSerial(object):
using Arduino's 'write' function using Arduino's 'write' function
""" """
if self.connected: if self.connected:
cmd_str=''.join(["@sw%",str(data),"$!"]) cmd_str = build_cmd_str("sw", (data,))
try: try:
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
@@ -532,7 +532,7 @@ class SoftwareSerial(object):
existing software serial instance existing software serial instance
""" """
if self.connected: if self.connected:
cmd_str=''.join(["@sr%$!"]) cmd_str = build_cmd_str("sr")
self.sr.write(cmd_str) self.sr.write(cmd_str)
self.sr.flush() self.sr.flush()
response= self.sr.readline().replace("\r\n","") response= self.sr.readline().replace("\r\n","")