Skip to content

Commit c713122

Browse files
committed
Add commands for setting GPIO pins.
1 parent 98cfc0e commit c713122

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@
8888
_SET_ENT_PASSWD_CMD = const(0x4C)
8989
_SET_ENT_ENABLE_CMD = const(0x4F)
9090

91+
_SET_PIN_MODE_CMD = const(0x50)
92+
_SET_DIGITAL_WRITE_CMD = const(0x51)
93+
_SET_ANALOG_WRITE_CMD = const(0x52)
94+
9195
_START_CMD = const(0xE0)
9296
_END_CMD = const(0xEE)
9397
_ERR_CMD = const(0xEF)
@@ -619,3 +623,46 @@ def set_esp_debug(self, enabled):
619623
resp = self._send_command_get_response(_SET_DEBUG_CMD, ((bool(enabled),),))
620624
if resp[0][0] != 1:
621625
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)