Skip to content

Commit af8185d

Browse files
committed
Move chekcing pin mode to firmware
1 parent 2d53cd8 commit af8185d

File tree

2 files changed

+14
-29
lines changed

2 files changed

+14
-29
lines changed

sbot/arduino.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ class AnalogPins(IntEnum):
4444
A5 = 19
4545

4646

47-
DIGITAL_READ_MODES = {GPIOPinMode.INPUT, GPIOPinMode.INPUT_PULLUP, GPIOPinMode.OUTPUT}
48-
DIGITAL_WRITE_MODES = {GPIOPinMode.OUTPUT}
49-
ANALOG_READ_MODES = {GPIOPinMode.INPUT}
50-
51-
5247
class Arduino(Board):
5348
"""
5449
The Arduino board interface.
@@ -319,8 +314,6 @@ def digital_value(self) -> bool:
319314
:return: The digital value of the pin.
320315
"""
321316
self._check_if_disabled()
322-
if self.mode not in DIGITAL_READ_MODES:
323-
raise IOError(f'Digital read is not supported in {self.mode}')
324317
response = self._serial.query(f'PIN:{self._index}:DIGITAL:GET?')
325318
return (response == '1')
326319

@@ -335,12 +328,14 @@ def digital_value(self, value: bool) -> None:
335328
:raises IOError: If this pin cannot be controlled.
336329
"""
337330
self._check_if_disabled()
338-
if self.mode not in DIGITAL_WRITE_MODES:
339-
raise IOError(f'Digital write is not supported in {self.mode}')
340-
if value:
341-
self._serial.write(f'PIN:{self._index}:DIGITAL:SET:1')
342-
else:
343-
self._serial.write(f'PIN:{self._index}:DIGITAL:SET:0')
331+
try:
332+
if value:
333+
self._serial.write(f'PIN:{self._index}:DIGITAL:SET:1')
334+
else:
335+
self._serial.write(f'PIN:{self._index}:DIGITAL:SET:0')
336+
except RuntimeError as e:
337+
if 'is not supported in' in str(e):
338+
raise IOError(str(e))
344339

345340
@property
346341
@log_to_debug
@@ -358,11 +353,14 @@ def analog_value(self) -> float:
358353
ADC_MIN = 0
359354

360355
self._check_if_disabled()
361-
# if self.mode not in ANALOG_READ_MODES:
362-
# raise IOError(f'Analog read is not supported in {self.mode}')
363356
if not self._supports_analog:
364357
raise IOError('Pin does not support analog read')
365-
response = self._serial.query(f'PIN:{self._index}:ANALOG:GET?')
358+
try:
359+
response = self._serial.query(f'PIN:{self._index}:ANALOG:GET?')
360+
except RuntimeError as e:
361+
# The firmware returns a NACK if the pin is not in INPUT mode
362+
if 'is not supported in' in str(e):
363+
raise IOError(str(e))
366364
# map the response from the ADC range to the voltage range
367365
return map_to_float(int(response), ADC_MIN, ADC_MAX, 0.0, 5.0)
368366

tests/test_arduino.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,8 @@ def test_arduino_pins(arduino_serial: MockArduino) -> None:
116116

117117
# Test that we can get the digital value of a pin
118118
arduino_serial.serial_wrapper._add_responses([
119-
("PIN:2:MODE:GET?", "OUTPUT"), # mode is read before digital value
120119
("PIN:2:DIGITAL:GET?", "1"),
121-
("PIN:10:MODE:GET?", "INPUT_PULLUP"),
122120
("PIN:10:DIGITAL:GET?", "0"),
123-
("PIN:14:MODE:GET?", "INPUT"),
124121
("PIN:14:DIGITAL:GET?", "1"),
125122
])
126123
assert arduino.pins[2].digital_value is True
@@ -129,21 +126,11 @@ def test_arduino_pins(arduino_serial: MockArduino) -> None:
129126

130127
# Test that we can set the digital value of a pin
131128
arduino_serial.serial_wrapper._add_responses([
132-
("PIN:2:MODE:GET?", "OUTPUT"), # mode is read before digital value
133129
("PIN:2:DIGITAL:SET:1", "ACK"),
134-
("PIN:2:MODE:GET?", "OUTPUT"),
135130
("PIN:2:DIGITAL:SET:0", "ACK"),
136-
("PIN:10:MODE:GET?", "INPUT_PULLUP"),
137-
("PIN:10:MODE:GET?", "INPUT_PULLUP"),
138-
("PIN:14:MODE:GET?", "INPUT"),
139-
("PIN:14:MODE:GET?", "INPUT"),
140131
])
141132
arduino.pins[2].digital_value = True
142133
arduino.pins[2].digital_value = False
143-
with pytest.raises(IOError, match=r"Digital write is not supported.*"):
144-
arduino.pins[10].digital_value = False
145-
with pytest.raises(IOError, match=r"Digital write is not supported.*"):
146-
arduino.pins[AnalogPins.A0].digital_value = True
147134

148135
# Test that we can get the analog value of a pin
149136
arduino_serial.serial_wrapper._add_responses([

0 commit comments

Comments
 (0)