Skip to content

Commit 912f82f

Browse files
committed
Allow reuse of _bleio.Adapter
1 parent 663152f commit 912f82f

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

adafruit_airlift/esp32.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,15 @@ def reset(self, mode: int, debug: bool = False) -> None:
142142
if more:
143143
startup_message += more
144144

145-
if not startup_message:
145+
if startup_message:
146+
if debug:
147+
try:
148+
print(startup_message.decode("utf-8"))
149+
except UnicodeError:
150+
raise RuntimeError("Garbled ESP32 startup message") from UnicodeError
151+
else:
146152
raise RuntimeError("ESP32 did not respond with a startup message")
147-
if debug:
148-
try:
149-
print(startup_message.decode("utf-8"))
150-
except UnicodeError:
151-
raise RuntimeError("Garbled ESP32 startup message") from UnicodeError
152-
153+
153154
# Everything's fine. Remember mode.
154155
self._mode = mode
155156

@@ -175,13 +176,14 @@ def start_bluetooth(self, debug: bool = False) -> Adapter:
175176
# Choose Bluetooth mode.
176177
self._chip_select.switch_to_output(False)
177178

178-
self._uart = busio.UART(
179-
self._tx or board.ESP_TX,
180-
self._rx or board.ESP_RX,
181-
baudrate=115200,
182-
timeout=0,
183-
receiver_buffer_size=512,
184-
)
179+
if self._uart is None:
180+
self._uart = busio.UART(
181+
self._tx or board.ESP_TX,
182+
self._rx or board.ESP_RX,
183+
baudrate=115200,
184+
timeout=0,
185+
receiver_buffer_size=512,
186+
)
185187

186188
# Reset into Bluetooth mode.
187189
self.reset(ESP32.BLUETOOTH, debug=debug)
@@ -190,9 +192,11 @@ def start_bluetooth(self, debug: bool = False) -> Adapter:
190192
self._gpio0_rts.switch_to_output()
191193
# pylint: disable=no-member
192194
# pylint: disable=unexpected-keyword-arg
193-
self._bleio_adapter = _bleio.Adapter(
194-
uart=self._uart, rts=self._gpio0_rts, cts=self._busy_cts
195-
)
195+
if self._bleio_adapter is None:
196+
self._bleio_adapter = _bleio.Adapter(
197+
uart=self._uart, rts=self._gpio0_rts, cts=self._busy_cts
198+
)
199+
196200
self._bleio_adapter.enabled = True
197201
return self._bleio_adapter
198202

@@ -202,8 +206,6 @@ def stop_bluetooth(self):
202206
return
203207
self._bleio_adapter.enabled = False
204208
self.reset(ESP32.NOT_IN_USE)
205-
self._uart.deinit()
206-
self._uart = None
207209

208210
def start_wifi(self, debug: bool = False) -> SPI:
209211
"""Start WiFi on the ESP32.

0 commit comments

Comments
 (0)