Skip to content

Commit 876e77d

Browse files
committed
Digital: return boolean instead of 0 or 1. Analog: scaled return from 12-bit to 0-65535.
1 parent 2eb122f commit 876e77d

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@
133133
WL_AP_LISTENING = const(7)
134134
WL_AP_CONNECTED = const(8)
135135
WL_AP_FAILED = const(9)
136+
137+
ADC_ATTEN_DB_0 = const(0)
138+
ADC_ATTEN_DB_2_5 = const(1)
139+
ADC_ATTEN_DB_6 = const(2)
140+
ADC_ATTEN_DB_11 = const(3)
136141
# pylint: enable=bad-whitespace
137142

138143
class ESP_SPIcontrol: # pylint: disable=too-many-public-methods, too-many-instance-attributes
@@ -783,26 +788,34 @@ def set_analog_write(self, pin, analog_value):
783788

784789
def set_digital_read(self, pin):
785790
"""
786-
Get the digital input value of pin.
791+
Get the digital input value of pin. Returns the boolean value of the pin.
787792
788793
:param int pin: ESP32 GPIO pin to read from.
789794
"""
790795
resp = self._send_command_get_response(_SET_DIGITAL_READ_CMD,
791796
((pin,),))[0]
792-
return resp[0]
797+
if resp[0] == 0:
798+
return False
799+
elif resp[0] == 1:
800+
return True
801+
else:
802+
raise ValueError("_SET_DIGITAL_READ response error", resp_[0])
793803

794-
def set_analog_read(self, pin):
804+
def set_analog_read(self, pin, atten=ADC_ATTEN_DB_11):
795805
"""
796-
Get the analog input value of pin.
806+
Get the analog input value of pin. Returns an int between 0 and 65536.
797807
798808
:param int pin: ESP32 GPIO pin to read from.
809+
:param int atten: attenuation constant
799810
"""
800811
resp = self._send_command_get_response(_SET_ANALOG_READ_CMD,
801-
((pin,),))[0]
812+
((pin,), (atten,)))
802813
resp_a = struct.unpack('<i', resp[0])
803-
if resp_a == (-1,):
804-
raise ValueError("_SET_ANALOG_READ parameter error -1")
805-
return resp_time
814+
if resp_a[0] < 0:
815+
raise ValueError("_SET_ANALOG_READ parameter error", resp_a[0])
816+
if self._debug:
817+
print(resp, resp_a, resp_a[0], 16 * resp_a[0])
818+
return 16 * resp_a[0]
806819

807820
def get_time(self):
808821
"""The current unix timestamp"""

0 commit comments

Comments
 (0)