diff --git a/Arduino/arduino.py b/Arduino/arduino.py index 13e9ce4..2b181fb 100644 --- a/Arduino/arduino.py +++ b/Arduino/arduino.py @@ -345,7 +345,51 @@ class Arduino(object): rd = self.sr.readline().replace("\r\n","") if rd.isdigit() == True: return int(rd) - + + def shiftOut(self, dataPin, clockPin, pinOrder, value): + """ + Shift a byte out on the datapin using Arduino's shiftOut(). + + Input: + dataPin (int): pin for data + clockPin (int): pin for clock + pinOrder (String): either 'MSBFIRST' or 'LSBFIRST' + value (int): an integer from 0 and 255 + """ + cmd_str = self._buildCmdStr("so", + (dataPin, clockPin, pinOrder, value)) + self.sr.write(cmd_str) + self.sr.flush() + + def shiftIn(self, dataPin, clockPin, pinOrder): + """ + Shift a byte in from the datapin using Arduino's shiftIn(). + + Input: + dataPin (int): pin for data + clockPin (int): pin for clock + pinOrder (String): either 'MSBFIRST' or 'LSBFIRST' + Output: + (int) an integer from 0 to 255 + """ + cmd_str = self._buildCmdStr("si", (dataPin, clockPin, pinOrder)) + self.sr.write(cmd_str) + self.sr.flush() + rd = self.sr.readline().replace("\r\n","") + if rd.isdigit() == True: + 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): def __init__(self): diff --git a/sketches/prototype/prototype.ino b/sketches/prototype/prototype.ino index a245952..867c360 100644 --- a/sketches/prototype/prototype.ino +++ b/sketches/prototype/prototype.ino @@ -154,6 +154,35 @@ void ConfigurePinHandler(String data){ } } +void shiftOutHandler(String data) { + String sdata[4]; + split(sdata, 4, data, '%'); + int dataPin = sdata[0].toInt(); + int clockPin = sdata[1].toInt(); + String bitOrderName = sdata[2]; + byte value = (byte)(sdata[3].toInt()); + if (bitOrderName == "MSBFIRST") { + shiftOut(dataPin, clockPin, MSBFIRST, value); + } else { + shiftOut(dataPin, clockPin, LSBFIRST, value); + } +} + +void shiftInHandler(String data) { + String sdata[3]; + split(sdata, 3, data, '%'); + int dataPin = sdata[0].toInt(); + int clockPin = sdata[1].toInt(); + String bitOrderName = sdata[2]; + int incoming; + if (bitOrderName == "MSBFIRST") { + incoming = (int)shiftIn(dataPin, clockPin, MSBFIRST); + } else { + incoming = (int)shiftIn(dataPin, clockPin, LSBFIRST); + } + Serial.println(incoming); +} + void SS_set(String data){ delete sserial; String sdata[3]; @@ -341,7 +370,14 @@ void SerialParser(void) { } else if (cmd == "cap") { readCapacitivePin(data); - } + } + else if (cmd == "so") { + shiftOutHandler(data); + } + else if (cmd == "si") { + shiftInHandler(data); + } + } void setup() {