Skip to content

Commit 433340d

Browse files
committed
Added API for accessing controller I/O pins.
1 parent bc7508d commit 433340d

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

modulo/__init__.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,44 @@ class Controller(Module) :
510510
511511
TODO: Needs additional API for accessing I/O pins.
512512
"""
513+
_FunctionSetPinDirection = 0
514+
_FunctionGetDigitalInput = 1
515+
_FunctionSetDigitalOutput = 2
516+
_FunctionSetPWMOutput = 3
517+
_FunctionGetAnalogInput = 4
518+
513519

514520
def __init__(self, port) :
515-
super(Controller, self).__init__(port, "co.modulo.controller", deviceID)
521+
super(Controller, self).__init__(port, "co.modulo.controller", None)
522+
523+
def setPinDirection(self, pin, output, pullup) :
524+
val = (pin << 2) | (pullup << 1) | output;
525+
self.transfer(self._FunctionSetPinDirection, [val], 0)
526+
527+
def getDigitalInput(self, pin) :
528+
receivedData = self.transfer(self._FunctionGetDigitalInput, [pin], 1)
529+
return (receivedData[0] != 0)
530+
531+
def setDigitalOutput(self, pin, value) :
532+
data = (pin << 1) | value
533+
self.transfer(self._FunctionSetDigitalOutput, [data], 0)
534+
535+
def setPWMOutput(self, pin, value) :
536+
intValue = int(255*max(0, min(1, value)))
537+
self.transfer(self._FunctionSetPWMOutput, [pin, intValue], 0)
516538

539+
def getAnalogInput(self, pin) :
540+
receivedData = self.transfer(self._FunctionGetAnalogInput, [pin], 2)
541+
return ctypes.c_short(receivedData[0] | (receivedData[1] << 8)).value
542+
543+
# DEPRECATED
517544
def readTemperatureProbe(self, pin) :
518545
receivedData = self.transfer(self._FunctionReadTemperatureProbe, [pin], 2)
519546
return ctypes.c_short(receivedData[0] | (receivedData[1] << 8)).value
520547

548+
549+
550+
521551
class Display(Module) :
522552
"""
523553
Connect to the module with the specified *deviceID* on the given *port*.

0 commit comments

Comments
 (0)