Skip to content

Commit c3cda2a

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

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
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

0 commit comments

Comments
 (0)