Skip to content

Expose GPIO commands from nina-fw in ESP_SPIcontrol class #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions adafruit_esp32spi/adafruit_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
# pylint: disable=bad-whitespace
_SET_NET_CMD = const(0x10)
_SET_PASSPHRASE_CMD = const(0x11)
_SET_DEBUG_CMD = const(0x1A)

_GET_CONN_STATUS_CMD = const(0x20)
_GET_IPADDR_CMD = const(0x21)
Expand Down Expand Up @@ -87,6 +88,10 @@
_SET_ENT_PASSWD_CMD = const(0x4C)
_SET_ENT_ENABLE_CMD = const(0x4F)

_SET_PIN_MODE_CMD = const(0x50)
_SET_DIGITAL_WRITE_CMD = const(0x51)
_SET_ANALOG_WRITE_CMD = const(0x52)

_START_CMD = const(0xE0)
_END_CMD = const(0xEE)
_ERR_CMD = const(0xEF)
Expand Down Expand Up @@ -611,3 +616,53 @@ def socket_close(self, socket_num):
resp = self._send_command_get_response(_STOP_CLIENT_TCP_CMD, self._socknum_ll)
if resp[0][0] != 1:
raise RuntimeError("Failed to close socket")

def set_esp_debug(self, enabled):
"""Enable/disable debug mode on the ESP32. Debug messages will be
written to the ESP32's UART."""
resp = self._send_command_get_response(_SET_DEBUG_CMD, ((bool(enabled),),))
if resp[0][0] != 1:
raise RuntimeError("Failed to set debug mode")

def set_pin_mode(self, pin, mode):
"""
Set the io mode for a GPIO pin.

:param int pin: ESP32 GPIO pin to set.
:param value: direction for pin, digitalio.Direction or integer (0=input, 1=output).
"""
if mode == Direction.OUTPUT:
pin_mode = 1
elif mode == Direction.INPUT:
pin_mode = 0
else:
pin_mode = mode
resp = self._send_command_get_response(_SET_PIN_MODE_CMD,
((pin,), (pin_mode,)))
if resp[0][0] != 1:
raise RuntimeError("Failed to set pin mode")

def set_digital_write(self, pin, value):
"""
Set the digital output value of pin.

:param int pin: ESP32 GPIO pin to write to.
:param bool value: Value for the pin.
"""
resp = self._send_command_get_response(_SET_DIGITAL_WRITE_CMD,
((pin,), (value,)))
if resp[0][0] != 1:
raise RuntimeError("Failed to write to pin")

def set_analog_write(self, pin, analog_value):
"""
Set the analog output value of pin, using PWM.

:param int pin: ESP32 GPIO pin to write to.
:param float value: 0=off 1.0=full on
"""
value = int(255 * analog_value)
resp = self._send_command_get_response(_SET_ANALOG_WRITE_CMD,
((pin,), (value,)))
if resp[0][0] != 1:
raise RuntimeError("Failed to write to pin")