mirror of
https://github.com/thearn/Python-Arduino-Command-API.git
synced 2026-04-24 03:00:20 -04:00
Add support for shiftIn and shiftOut commands.
This commit is contained in:
@@ -345,7 +345,51 @@ class Arduino(object):
|
|||||||
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 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):
|
class Shrimp(Arduino):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|||||||
@@ -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){
|
void SS_set(String data){
|
||||||
delete sserial;
|
delete sserial;
|
||||||
String sdata[3];
|
String sdata[3];
|
||||||
@@ -341,7 +370,14 @@ void SerialParser(void) {
|
|||||||
}
|
}
|
||||||
else if (cmd == "cap") {
|
else if (cmd == "cap") {
|
||||||
readCapacitivePin(data);
|
readCapacitivePin(data);
|
||||||
}
|
}
|
||||||
|
else if (cmd == "so") {
|
||||||
|
shiftOutHandler(data);
|
||||||
|
}
|
||||||
|
else if (cmd == "si") {
|
||||||
|
shiftInHandler(data);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|||||||
Reference in New Issue
Block a user