40 'D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 D10 D11 D12 D13 A1 A2 A3 A4 A5')
42 AnalogReferences = Enum(
'AnalogReferences',
'Default Internal External')
47 def __exit__(self, exc_type, exc_value, traceback):
57 self.
_serial = serial.Serial(port, 115200, timeout=2)
61 raise RuntimeError(
'Could not connect to Arduino')
66 raise RuntimeError(
'Could not connect to Arduino')
72 def __clamp(self, value, min, max):
73 return sorted((min, value, max))[1]
75 def __create_pin_info(self, pid, pwm=False):
76 is_analog = pid.name.startswith(
'A')
83 'read_range': (0, 1023)
if is_analog
else None,
87 'input':
not is_analog,
88 'output':
not is_analog,
89 'pwm':
not is_analog
and pwm
93 obj[
'name'] =
'Analog %s' % (pid.value - 14)
95 obj[
'name'] =
'Digital %s' % (pid.value - 1)
99 pins = [p
for p
in Driver.Pins]
102 if (pin.value - 1)
in [3, 5, 6, 9, 10, 11]]
105 if (pin.value - 1)
not in [3, 5, 6, 9, 10, 11]]
106 return sorted(pwms + pins, key=
lambda pin: pin[
'id'].value)
108 def _set_pin_direction(self, pin, direction):
109 if pin.name.startswith(
'A')
and direction == ahio.Direction.Output:
110 raise RuntimeError(
'Analog pins can only be used as Input')
111 if direction == ahio.Direction.Input:
113 b
'\x02\xC3' + bytes({pin.value - 1}) + bytes({1}))
116 b
'\x02\xC3' + bytes({pin.value - 1}) + bytes({0}))
118 b
'\x02\xC7' + bytes({pin.value - 1}) + bytes({0}))
120 def _pin_direction(self, pin):
121 self.
_serial.
write(b
'\x02\xC4' + bytes({pin.value - 1}))
123 if direction == b
'\x01':
124 return ahio.Direction.Input
125 elif direction == b
'\x00':
126 return ahio.Direction.Output
130 def _set_pin_type(self, pin, ptype):
131 is_analog = pin.name.startswith(
'A')
132 if is_analog
and ptype == ahio.PortType.Digital:
133 raise RuntimeError(
'Analog pin can not be set as digital')
134 if not is_analog
and ptype == ahio.PortType.Analog:
135 raise RuntimeError(
'Digtal pin can not be set as analog')
137 def _pin_type(self, pin):
139 return pt.Analog
if pin.name.startswith(
'A')
else pt.Digital
141 def _write(self, pin, value, pwm):
144 if pin.name.startswith(
'D'):
146 if (pin.value - 1)
not in [3, 5, 6, 9, 10, 11]:
147 raise RuntimeError(
'Pin does not support PWM output')
148 if type(value)
is int
or type(value)
is float:
149 value = int(255 * self.
__clamp(float(value), 0.0, 1.0))
150 command = b
'\x02\xC8' + bytes({pin.value - 1})
154 raise TypeError(
'value not a float or int between 0 and 1')
156 if type(value)
is ahio.LogicValue:
157 value = 1
if value == ahio.LogicValue.High
else 0
158 command = b
'\x02\xC7' + bytes({pin.value - 1})
162 raise TypeError(
'Value should be of type ahio.LogicValue')
164 raise RuntimeError(
'Can not write to analog pin')
166 def _read(self, pin):
167 if pin.name.startswith(
'D'):
168 self.
_serial.
write(b
'\x02\xC5' + bytes({pin.value - 1}))
171 return lv.High
if value == b
'\x01' else lv.Low
173 self.
_serial.
write(b
'\x02\xC6' + bytes({pin.value - 14}))
176 return (value_high[0] << 8) | value_low[0]
179 return [r
for r
in Driver.AnalogReferences]
181 def _set_analog_reference(self, reference, pin):
183 raise RuntimeError(
'Per pin analog reference is not supported')
184 self.
_serial.
write(b
'\x02\xC2' + bytes({reference.value - 1}))
186 def _analog_reference(self, pin):
189 return [Driver.AnalogReferences.Default,
190 Driver.AnalogReferences.Internal,
191 Driver.AnalogReferences.External][reference]
193 def _set_pwm_frequency(self, frequency, pin):
195 'Setting PWM frequency is not supported by hardware')
def __exit__(self, exc_type, exc_value, traceback)
def _pin_direction(self, pin)
def available_pins(self)
Returns available pins.
def setup(self, port)
Connects to an Arduino UNO on serial port port.
def __create_pin_info(self, pid, pwm=False)
Abstract class containing information about the driver.
def write(self, pin, value, pwm=False)
Sets the output to the given value.
def __clamp(self, value, min, max)
Contains abstract classes that should be implemented by drivers.
def analog_references(self)
def read(self, pin)
Reads value from pin pin.
def _set_pin_direction(self, pin, direction)