@@ -30,15 +30,27 @@ class ESP32:
30
30
"""WiFi mode."""
31
31
_MODES = (NOT_IN_USE , BOOTLOADER , BLUETOOTH , WIFI )
32
32
33
+ # pylint: disable=invalid-name
33
34
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
35
45
):
36
46
37
47
"""Create an ESP32 instance, passing the objects needed to reset and communicate
38
48
with the adapter.
39
49
40
50
:param reset ~microcontroller.Pin: ESP32 RESET pin.
41
51
If `None`, use ``board.ESP_RESET``.
52
+ :param reset_high bool: True if `reset` is brought high to reset;
53
+ `False` if brought low.
42
54
:param gpio0 ~microcontroller.Pin: ESP32 GPIO0 pin.
43
55
Used for ESP32 boot selection when reset, and as RTS for UART communication.
44
56
If `None`, use ``board.ESP_GPIO0``.
@@ -48,8 +60,12 @@ def __init__(
48
60
:param chip_select ~microcontroller.Pin: ESP32 CS (chip select) pin.
49
61
Also used for ESP32 mode selection when reset.
50
62
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.
53
69
"""
54
70
self ._mode = ESP32 .NOT_IN_USE
55
71
@@ -65,10 +81,13 @@ def __init__(
65
81
self ._chip_select = DigitalInOut (chip_select or board .ESP_CS )
66
82
67
83
# Used for Bluetooth mode.
84
+ self ._tx = tx
85
+ self ._rx = rx
68
86
self ._uart = None
69
87
self ._bleio_adapter = None
88
+
70
89
# Used for WiFi mode.
71
- self ._spi = None
90
+ self ._spi = spi
72
91
73
92
def reset (self , mode , debug = False ):
74
93
"""Do hard reset of the ESP32.
@@ -126,13 +145,9 @@ def reset(self, mode, debug=False):
126
145
self ._mode = mode
127
146
128
147
# pylint: disable=invalid-name
129
- def start_bluetooth (self , tx = None , rx = None , debug = False ):
148
+ def start_bluetooth (self , debug = False ):
130
149
"""Set up the ESP32 in HCI Bluetooth mode, if it is not already doing something else.
131
150
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``.
136
151
:param debug bool: Print out some debugging information.
137
152
:return: A `_bleio.Adapter`, to be passed to ``_bleio.set_adapter()``.
138
153
"""
@@ -152,8 +167,8 @@ def start_bluetooth(self, tx=None, rx=None, debug=False):
152
167
self ._chip_select .switch_to_output (False )
153
168
154
169
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 ,
157
172
baudrate = 115200 ,
158
173
timeout = 0 ,
159
174
receiver_buffer_size = 512 ,
@@ -181,12 +196,10 @@ def stop_bluetooth(self):
181
196
self ._uart .deinit ()
182
197
self ._uart = None
183
198
184
- def start_wifi (self , spi = None ):
199
+ def start_wifi (self , debug = False ):
185
200
"""Start WiFi on the ESP32.
186
201
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.
190
203
:rtype: busio.SPI
191
204
"""
192
205
if self ._mode == ESP32 .WIFI :
@@ -196,13 +209,14 @@ def start_wifi(self, spi=None):
196
209
if self ._mode == ESP32 .BLUETOOTH :
197
210
raise RuntimeError ("ESP32 is in Bluetooth mode; use stop_bluetooth() first" )
198
211
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 ()
201
215
return self ._spi
202
216
203
217
def stop_wifi (self ):
204
218
"""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.
206
220
"""
207
221
if self ._mode != ESP32 .WIFI :
208
222
return
0 commit comments