Skip to content

Commit c68c642

Browse files
authored
Merge pull request #80 from anecdata/Kraken_II
Expose NINA command handlers for Digital Read and Analog Read
2 parents 392c426 + ecb6eaa commit c68c642

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

+46
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
_SET_PIN_MODE_CMD = const(0x50)
101101
_SET_DIGITAL_WRITE_CMD = const(0x51)
102102
_SET_ANALOG_WRITE_CMD = const(0x52)
103+
_SET_DIGITAL_READ_CMD = const(0x53)
104+
_SET_ANALOG_READ_CMD = const(0x54)
103105

104106
_START_CMD = const(0xE0)
105107
_END_CMD = const(0xEE)
@@ -131,6 +133,11 @@
131133
WL_AP_LISTENING = const(7)
132134
WL_AP_CONNECTED = const(8)
133135
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)
134141
# pylint: enable=bad-whitespace
135142

136143
class ESP_SPIcontrol: # pylint: disable=too-many-public-methods, too-many-instance-attributes
@@ -779,6 +786,45 @@ def set_analog_write(self, pin, analog_value):
779786
if resp[0][0] != 1:
780787
raise RuntimeError("Failed to write to pin")
781788

789+
def set_digital_read(self, pin):
790+
"""
791+
Get the digital input value of pin. Returns the boolean value of the pin.
792+
793+
:param int pin: ESP32 GPIO pin to read from.
794+
"""
795+
# Verify nina-fw => 1.5.0
796+
fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2]
797+
assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above."
798+
799+
resp = self._send_command_get_response(_SET_DIGITAL_READ_CMD,
800+
((pin,),))[0]
801+
if resp[0] == 0:
802+
return False
803+
elif resp[0] == 1:
804+
return True
805+
else:
806+
raise ValueError("_SET_DIGITAL_READ response error: response is not boolean", resp[0])
807+
808+
def set_analog_read(self, pin, atten=ADC_ATTEN_DB_11):
809+
"""
810+
Get the analog input value of pin. Returns an int between 0 and 65536.
811+
812+
:param int pin: ESP32 GPIO pin to read from.
813+
:param int atten: attenuation constant
814+
"""
815+
# Verify nina-fw => 1.5.0
816+
fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2]
817+
assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above."
818+
819+
resp = self._send_command_get_response(_SET_ANALOG_READ_CMD,
820+
((pin,), (atten,)))
821+
resp_analog = struct.unpack('<i', resp[0])
822+
if resp_analog[0] < 0:
823+
raise ValueError("_SET_ANALOG_READ parameter error: invalid pin", resp_analog[0])
824+
if self._debug:
825+
print(resp, resp_analog, resp_analog[0], 16 * resp_analog[0])
826+
return 16 * resp_analog[0]
827+
782828
def get_time(self):
783829
"""The current unix timestamp"""
784830
if self.status == WL_CONNECTED:

0 commit comments

Comments
 (0)