Skip to content

Commit f289766

Browse files
committed
pass UART pins and SPI to constructor
1 parent 16b04e6 commit f289766

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

adafruit_airlift/esp32.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,27 @@ class ESP32:
3030
"""WiFi mode."""
3131
_MODES = (NOT_IN_USE, BOOTLOADER, BLUETOOTH, WIFI)
3232

33+
# pylint: disable=invalid-name
3334
def __init__(
34-
self, *, reset=None, gpio0=None, busy=None, chip_select=None, reset_high=False,
35+
self,
36+
*,
37+
reset=None,
38+
reset_high=False,
39+
gpio0=None,
40+
busy=None,
41+
chip_select=None,
42+
tx=None,
43+
rx=None,
44+
spi=None
3545
):
3646

3747
"""Create an ESP32 instance, passing the objects needed to reset and communicate
3848
with the adapter.
3949
4050
:param reset ~microcontroller.Pin: ESP32 RESET pin.
4151
If `None`, use ``board.ESP_RESET``.
52+
:param reset_high bool: True if `reset` is brought high to reset;
53+
`False` if brought low.
4254
:param gpio0 ~microcontroller.Pin: ESP32 GPIO0 pin.
4355
Used for ESP32 boot selection when reset, and as RTS for UART communication.
4456
If `None`, use ``board.ESP_GPIO0``.
@@ -48,8 +60,12 @@ def __init__(
4860
:param chip_select ~microcontroller.Pin: ESP32 CS (chip select) pin.
4961
Also used for ESP32 mode selection when reset.
5062
If `None`, use ``board.ESP_CS``.
51-
:param reset_high bool: True if `reset` is brought high to reset;
52-
`False` if brought low.
63+
:param tx ~microcontroller.Pin: ESP32 TX pin for Bluetooth UART communication.
64+
If `None`, use ``board.ESP_TX`` when in Bluetooth mode.
65+
:param rx ~microcontroller.Pin: ESP32 RX pin for Bluetooth UART communication.
66+
If `None`, use ``board.ESP_RX`` when in Bluetooth mode.
67+
:param spi busio.SPI: Used for communication with the ESP32.
68+
If not supplied, ``board.SPI()`` is used when in WiFi mode.
5369
"""
5470
self._mode = ESP32.NOT_IN_USE
5571

@@ -65,10 +81,13 @@ def __init__(
6581
self._chip_select = DigitalInOut(chip_select or board.ESP_CS)
6682

6783
# Used for Bluetooth mode.
84+
self._tx = tx
85+
self._rx = rx
6886
self._uart = None
6987
self._bleio_adapter = None
88+
7089
# Used for WiFi mode.
71-
self._spi = None
90+
self._spi = spi
7291

7392
def reset(self, mode, debug=False):
7493
"""Do hard reset of the ESP32.
@@ -126,13 +145,9 @@ def reset(self, mode, debug=False):
126145
self._mode = mode
127146

128147
# pylint: disable=invalid-name
129-
def start_bluetooth(self, tx=None, rx=None, debug=False):
148+
def start_bluetooth(self, debug=False):
130149
"""Set up the ESP32 in HCI Bluetooth mode, if it is not already doing something else.
131150
132-
:param reset ~microcontroller.Pin: ESP32 TX pin for Bluetooth UART communication.
133-
If `None`, use ``board.ESP_TX``.
134-
:param gpio0 ~microcontroller.Pin: ESP32 RX pin for Bluetooth UART communication.
135-
If `None`, use ``board.ESP_RX``.
136151
:param debug bool: Print out some debugging information.
137152
:return: A `_bleio.Adapter`, to be passed to ``_bleio.set_adapter()``.
138153
"""
@@ -152,8 +167,8 @@ def start_bluetooth(self, tx=None, rx=None, debug=False):
152167
self._chip_select.switch_to_output(False)
153168

154169
self._uart = busio.UART(
155-
tx or board.ESP_TX,
156-
rx or board.ESP_RX,
170+
self._tx or board.ESP_TX,
171+
self._rx or board.ESP_RX,
157172
baudrate=115200,
158173
timeout=0,
159174
receiver_buffer_size=512,
@@ -181,12 +196,10 @@ def stop_bluetooth(self):
181196
self._uart.deinit()
182197
self._uart = None
183198

184-
def start_wifi(self, spi=None):
199+
def start_wifi(self, debug=False):
185200
"""Start WiFi on the ESP32.
186201
187-
:param spi busio.SPI: Used for communication with the eSP32.
188-
If not supplied, ``board.SPI()`` is used.
189-
:return: the ``busio.SPI`` object that will be used.
202+
:return: the ``busio.SPI`` object that will be used to communicate with the ESP32.
190203
:rtype: busio.SPI
191204
"""
192205
if self._mode == ESP32.WIFI:
@@ -196,13 +209,14 @@ def start_wifi(self, spi=None):
196209
if self._mode == ESP32.BLUETOOTH:
197210
raise RuntimeError("ESP32 is in Bluetooth mode; use stop_bluetooth() first")
198211

199-
self.reset(ESP32.WIFI)
200-
self._spi = spi or board.SPI()
212+
self.reset(ESP32.WIFI, debug=debug)
213+
if self._spi is None:
214+
self._spi = board.SPI()
201215
return self._spi
202216

203217
def stop_wifi(self):
204218
"""Stop WiFi on the ESP32.
205-
The `busio.SPI` used is not deinitialized, since it may be in use for other devices.
219+
The `busio.SPI` object used is not deinitialized, since it may be in use for other devices.
206220
"""
207221
if self._mode != ESP32.WIFI:
208222
return

0 commit comments

Comments
 (0)