Skip to content
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

Don't check for startup message if no uart #11

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/pylint
rev: v2.17.4
rev: v3.1.0
hooks:
- id: pylint
name: pylint (library code)
Expand Down
50 changes: 26 additions & 24 deletions adafruit_airlift/esp32.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ESP32:
_MODES = (NOT_IN_USE, BOOTLOADER, BLUETOOTH, WIFI)

# pylint: disable=invalid-name
# pylint: disable=too-many-arguments
def __init__(
self,
*,
Expand All @@ -54,24 +55,24 @@ def __init__(
"""Create an ESP32 instance, passing the objects needed to reset and communicate
with the adapter.

:param reset ~microcontroller.Pin: ESP32 RESET pin.
:param ~microcontroller.Pin reset: ESP32 RESET pin.
If `None`, use ``board.ESP_RESET``.
:param reset_high bool: True if `reset` is brought high to reset;
:param bool reset_high: True if `reset` is brought high to reset;
`False` if brought low.
:param gpio0 ~microcontroller.Pin: ESP32 GPIO0 pin.
:param ~microcontroller.Pin gpio0: ESP32 GPIO0 pin.
Used for ESP32 boot selection when reset, and as RTS for UART communication.
If `None`, use ``board.ESP_GPIO0``.
:param busy ~microcontroller.Pin: ESP32 BUSY pin (sometimes called READY).
:param ~microcontroller.Pin busy: ESP32 BUSY pin (sometimes called READY).
Used as CTS indicator for UART communication.
If `None`, use ``board.ESP_BUSY``.
:param chip_select ~microcontroller.Pin: ESP32 CS (chip select) pin.
:param ~microcontroller.Pin chip_select: ESP32 CS (chip select) pin.
Also used for ESP32 mode selection when reset.
If `None`, use ``board.ESP_CS``.
:param tx ~microcontroller.Pin: ESP32 TX pin for Bluetooth UART communication.
:param ~microcontroller.Pin tx: ESP32 TX pin for Bluetooth UART communication.
If `None`, use ``board.ESP_TX`` when in Bluetooth mode.
:param rx ~microcontroller.Pin: ESP32 RX pin for Bluetooth UART communication.
:param ~microcontroller.Pin rx: ESP32 RX pin for Bluetooth UART communication.
If `None`, use ``board.ESP_RX`` when in Bluetooth mode.
:param spi busio.SPI: Used for communication with the ESP32.
:param busio.SPI spi: Used for communication with the ESP32.
If not supplied, ``board.SPI()`` is used when in WiFi mode.
"""
self._mode = ESP32.NOT_IN_USE
Expand Down Expand Up @@ -134,28 +135,28 @@ def reset(self, mode: int, debug: bool = False) -> None:
# No startup message expected.
return

startup_message = b""
if self._uart is not None:
# Don't look for a startup message if there is no UART
if self._uart:
startup_message = b""
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 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")

# Everything's fine. Remember mode.
self._mode = mode

# pylint: disable=invalid-name
def start_bluetooth(self, debug: bool = False) -> Adapter:
"""Set up the ESP32 in HCI Bluetooth mode, if it is not already doing something else.

Expand All @@ -174,9 +175,7 @@ def start_bluetooth(self, debug: bool = False) -> Adapter:
if self._mode == ESP32.WIFI:
raise RuntimeError("ESP32 is in WiFi mode; use stop_wifi() first")

# Choose Bluetooth mode.
self._chip_select.switch_to_output(False)

# For bluetooth, we must be able to talk over UART to the ESP32.
if self._uart is None:
self._uart = busio.UART(
self._tx or board.ESP_TX,
Expand All @@ -186,6 +185,9 @@ def start_bluetooth(self, debug: bool = False) -> Adapter:
receiver_buffer_size=512,
)

# Choose Bluetooth mode.
self._chip_select.switch_to_output(False)

# Reset into Bluetooth mode.
self.reset(ESP32.BLUETOOTH, debug=debug)

Expand Down
Loading