Skip to content

Commit de8a3b0

Browse files
committed
Use a single pipelined request to make analogue reads
1 parent fad94f6 commit de8a3b0

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

sbot/arduino.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,19 @@ def analog_value(self) -> float:
358358
ADC_MIN = 0
359359

360360
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}')
363361
if not self._supports_analog:
364-
raise IOError('Pin does not support analog read')
365-
response = self._serial.query(f'PIN:{self._index}:ANALOG:GET?')
362+
raise IOError(f'Analog read is not supported on pin {self._index}')
363+
364+
# Combine the mode and response queries into a single pipeline
365+
mode, response = self._serial.query_multi([
366+
f'PIN:{self._index}:MODE:GET?',
367+
f'PIN:{self._index}:ANALOG:GET?',
368+
])
369+
mode = GPIOPinMode(mode)
370+
371+
if mode not in ANALOG_READ_MODES:
372+
raise IOError(f'Analog read is not supported in {self.mode}')
373+
366374
# map the response from the ADC range to the voltage range
367375
return map_to_float(int(response), ADC_MIN, ADC_MAX, 0.0, 5.0)
368376

tests/test_arduino.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ def test_arduino_get_analog_value(arduino_serial: MockArduino) -> None:
209209

210210
def test_arduino_get_invalid_analog_value_from_digital_only_pin(arduino_serial: MockArduino) -> None:
211211
arduino = arduino_serial.arduino_board
212-
arduino_serial.serial_wrapper._add_responses([
213-
("PIN:2:MODE:GET?", "OUTPUT"),
214-
("PIN:2:MODE:GET?", "OUTPUT"),
215-
])
216212

217213
with pytest.raises(IOError, match=r".*not support.*"):
218214
arduino.pins[2].analog_value

0 commit comments

Comments
 (0)