Skip to content

Commit 011a9d6

Browse files
authored
Merge pull request #38 from cpforbes/add_gpio
Expose GPIO commands from nina-fw in ESP_SPIcontrol class
2 parents 607697c + fcf5feb commit 011a9d6

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
# pylint: disable=bad-whitespace
5555
_SET_NET_CMD = const(0x10)
5656
_SET_PASSPHRASE_CMD = const(0x11)
57+
_SET_DEBUG_CMD = const(0x1A)
5758

5859
_GET_CONN_STATUS_CMD = const(0x20)
5960
_GET_IPADDR_CMD = const(0x21)
@@ -87,6 +88,10 @@
8788
_SET_ENT_PASSWD_CMD = const(0x4C)
8889
_SET_ENT_ENABLE_CMD = const(0x4F)
8990

91+
_SET_PIN_MODE_CMD = const(0x50)
92+
_SET_DIGITAL_WRITE_CMD = const(0x51)
93+
_SET_ANALOG_WRITE_CMD = const(0x52)
94+
9095
_START_CMD = const(0xE0)
9196
_END_CMD = const(0xEE)
9297
_ERR_CMD = const(0xEF)
@@ -611,3 +616,53 @@ def socket_close(self, socket_num):
611616
resp = self._send_command_get_response(_STOP_CLIENT_TCP_CMD, self._socknum_ll)
612617
if resp[0][0] != 1:
613618
raise RuntimeError("Failed to close socket")
619+
620+
def set_esp_debug(self, enabled):
621+
"""Enable/disable debug mode on the ESP32. Debug messages will be
622+
written to the ESP32's UART."""
623+
resp = self._send_command_get_response(_SET_DEBUG_CMD, ((bool(enabled),),))
624+
if resp[0][0] != 1:
625+
raise RuntimeError("Failed to set debug mode")
626+
627+
def set_pin_mode(self, pin, mode):
628+
"""
629+
Set the io mode for a GPIO pin.
630+
631+
:param int pin: ESP32 GPIO pin to set.
632+
:param value: direction for pin, digitalio.Direction or integer (0=input, 1=output).
633+
"""
634+
if mode == Direction.OUTPUT:
635+
pin_mode = 1
636+
elif mode == Direction.INPUT:
637+
pin_mode = 0
638+
else:
639+
pin_mode = mode
640+
resp = self._send_command_get_response(_SET_PIN_MODE_CMD,
641+
((pin,), (pin_mode,)))
642+
if resp[0][0] != 1:
643+
raise RuntimeError("Failed to set pin mode")
644+
645+
def set_digital_write(self, pin, value):
646+
"""
647+
Set the digital output value of pin.
648+
649+
:param int pin: ESP32 GPIO pin to write to.
650+
:param bool value: Value for the pin.
651+
"""
652+
resp = self._send_command_get_response(_SET_DIGITAL_WRITE_CMD,
653+
((pin,), (value,)))
654+
if resp[0][0] != 1:
655+
raise RuntimeError("Failed to write to pin")
656+
657+
def set_analog_write(self, pin, analog_value):
658+
"""
659+
Set the analog output value of pin, using PWM.
660+
661+
:param int pin: ESP32 GPIO pin to write to.
662+
:param float value: 0=off 1.0=full on
663+
"""
664+
value = int(255 * analog_value)
665+
resp = self._send_command_get_response(_SET_ANALOG_WRITE_CMD,
666+
((pin,), (value,)))
667+
if resp[0][0] != 1:
668+
raise RuntimeError("Failed to write to pin")

0 commit comments

Comments
 (0)