Skip to content

Avoid AttributeError when calling start_wifi() after stop_bluetooth() #8

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
Feb 27, 2023
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
51 changes: 28 additions & 23 deletions adafruit_airlift/esp32.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,22 @@ def reset(self, mode: int, debug: bool = False) -> None:
return

startup_message = b""
while self._uart.in_waiting: # pylint: disable=no-member
more = self._uart.read()
if more:
startup_message += more

if not startup_message:
if self._uart is not None:
while self._uart.in_waiting: # pylint: disable=no-member
more = self._uart.read()
if more:
startup_message += more

if startup_message:
if debug:
try:
print(startup_message.decode("utf-8"))
except UnicodeError:
raise RuntimeError(
"Garbled ESP32 startup message"
) from UnicodeError
else:
raise RuntimeError("ESP32 did not respond with a startup message")
if debug:
try:
print(startup_message.decode("utf-8"))
except UnicodeError:
raise RuntimeError("Garbled ESP32 startup message") from UnicodeError

# Everything's fine. Remember mode.
self._mode = mode
Expand All @@ -174,13 +178,14 @@ def start_bluetooth(self, debug: bool = False) -> Adapter:
# Choose Bluetooth mode.
self._chip_select.switch_to_output(False)

self._uart = busio.UART(
self._tx or board.ESP_TX,
self._rx or board.ESP_RX,
baudrate=115200,
timeout=0,
receiver_buffer_size=512,
)
if self._uart is None:
self._uart = busio.UART(
self._tx or board.ESP_TX,
self._rx or board.ESP_RX,
baudrate=115200,
timeout=0,
receiver_buffer_size=512,
)

# Reset into Bluetooth mode.
self.reset(ESP32.BLUETOOTH, debug=debug)
Expand All @@ -189,9 +194,11 @@ def start_bluetooth(self, debug: bool = False) -> Adapter:
self._gpio0_rts.switch_to_output()
# pylint: disable=no-member
# pylint: disable=unexpected-keyword-arg
self._bleio_adapter = _bleio.Adapter(
uart=self._uart, rts=self._gpio0_rts, cts=self._busy_cts
)
if self._bleio_adapter is None:
self._bleio_adapter = _bleio.Adapter(
uart=self._uart, rts=self._gpio0_rts, cts=self._busy_cts
)

self._bleio_adapter.enabled = True
return self._bleio_adapter

Expand All @@ -201,8 +208,6 @@ def stop_bluetooth(self):
return
self._bleio_adapter.enabled = False
self.reset(ESP32.NOT_IN_USE)
self._uart.deinit()
self._uart = None

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