Skip to content

Commit

Permalink
Use a single pipelined request to make analogue reads
Browse files Browse the repository at this point in the history
  • Loading branch information
prophile committed Aug 5, 2024
1 parent fad94f6 commit de8a3b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 12 additions & 4 deletions sbot/arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 0 additions & 4 deletions tests/test_arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de8a3b0

Please sign in to comment.