diff --git a/sbot/arduino.py b/sbot/arduino.py index faac772..c6fd9a8 100644 --- a/sbot/arduino.py +++ b/sbot/arduino.py @@ -358,11 +358,19 @@ def analog_value(self) -> float: ADC_MIN = 0 self._check_if_disabled() - if self.mode not in ANALOG_READ_MODES: - raise IOError(f'Analog read is not supported in {self.mode}') if not self._supports_analog: - raise IOError('Pin does not support analog read') - response = self._serial.query(f'PIN:{self._index}:ANALOG:GET?') + raise IOError(f'Analog read is not supported on pin {self._index}') + + # Combine the mode and response queries into a single pipeline + mode, response = self._serial.query_multi([ + f'PIN:{self._index}:MODE:GET?', + f'PIN:{self._index}:ANALOG:GET?', + ]) + mode = GPIOPinMode(mode) + + if mode not in ANALOG_READ_MODES: + raise IOError(f'Analog read is not supported in {self.mode}') + # map the response from the ADC range to the voltage range return map_to_float(int(response), ADC_MIN, ADC_MAX, 0.0, 5.0) diff --git a/tests/test_arduino.py b/tests/test_arduino.py index 786db0e..c238e59 100644 --- a/tests/test_arduino.py +++ b/tests/test_arduino.py @@ -209,10 +209,6 @@ def test_arduino_get_analog_value(arduino_serial: MockArduino) -> None: def test_arduino_get_invalid_analog_value_from_digital_only_pin(arduino_serial: MockArduino) -> None: arduino = arduino_serial.arduino_board - arduino_serial.serial_wrapper._add_responses([ - ("PIN:2:MODE:GET?", "OUTPUT"), - ("PIN:2:MODE:GET?", "OUTPUT"), - ]) with pytest.raises(IOError, match=r".*not support.*"): arduino.pins[2].analog_value