Add maintenance mode and board status indicators
Provide BOOT-triggered USB CDC maintenance mode for file updates, plus onboard LED activity flashes and enhanced display status information.
This commit is contained in:
@@ -4,8 +4,10 @@ import network
|
||||
import time
|
||||
import urandom
|
||||
|
||||
import maintenance
|
||||
from display import StatusDisplay
|
||||
from hid import get_hid
|
||||
from led import ActivityLed
|
||||
from settings import load, save
|
||||
from web import WebServer
|
||||
|
||||
@@ -51,13 +53,39 @@ def ip_address(wlan):
|
||||
return wlan.ifconfig()[0]
|
||||
|
||||
|
||||
def mac_address(wlan):
|
||||
"""Return the station interface MAC address in conventional display form."""
|
||||
try:
|
||||
mac = wlan.config("mac")
|
||||
except (AttributeError, OSError, ValueError):
|
||||
return None
|
||||
if not isinstance(mac, (bytes, bytearray)) or len(mac) != 6:
|
||||
return None
|
||||
return "%02X:%02X:%02X:%02X:%02X:%02X" % tuple(mac)
|
||||
|
||||
|
||||
def wifi_rssi(wlan):
|
||||
"""Return RSSI in dBm when the current ESP32 port exposes it."""
|
||||
if not wlan.isconnected():
|
||||
return None
|
||||
try:
|
||||
return wlan.status("rssi")
|
||||
except (AttributeError, OSError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
# boot.py leaves USB CDC enabled in maintenance mode; do not activate Wi-Fi,
|
||||
# the display, web server, or injection scheduler in that local service mode.
|
||||
if maintenance.active:
|
||||
return
|
||||
|
||||
settings = load()
|
||||
display = None
|
||||
if settings["display_enabled"]:
|
||||
display = StatusDisplay()
|
||||
hid = get_hid()
|
||||
activity_led = ActivityLed()
|
||||
wlan = connect_wifi(settings)
|
||||
volume_requests = []
|
||||
|
||||
@@ -76,9 +104,14 @@ def main():
|
||||
# connect_wifi() already started the first attempt; do not call connect()
|
||||
# again while the ESP32 station is still in STAT_CONNECTING state.
|
||||
next_wifi_retry = time.ticks_add(now, 10000)
|
||||
next_display_network_refresh = now
|
||||
display_address = None
|
||||
display_mac = None
|
||||
display_rssi = None
|
||||
|
||||
while True:
|
||||
now = time.ticks_ms()
|
||||
activity_led.tick(now)
|
||||
server.poll()
|
||||
|
||||
# Reconnect without blocking injections or the web server indefinitely.
|
||||
@@ -89,8 +122,17 @@ def main():
|
||||
next_wifi_retry = time.ticks_add(now, 10000)
|
||||
|
||||
if display is not None:
|
||||
address = ip_address(wlan) if wlan.isconnected() else None
|
||||
display.tick(now, settings["wifi_ssid"], address)
|
||||
# The display itself is frame-driven; query WLAN state only once per
|
||||
# second rather than for every 25 ms application-loop iteration.
|
||||
if time.ticks_diff(now, next_display_network_refresh) >= 0:
|
||||
display_address = ip_address(wlan) if wlan.isconnected() else None
|
||||
display_mac = mac_address(wlan)
|
||||
display_rssi = wifi_rssi(wlan)
|
||||
next_display_network_refresh = time.ticks_add(now, 1000)
|
||||
display.tick(
|
||||
now, settings["wifi_ssid"], display_address, display_mac,
|
||||
display_rssi, maintenance.button_pressed()
|
||||
)
|
||||
|
||||
|
||||
if release_at is not None and time.ticks_diff(now, release_at) >= 0:
|
||||
@@ -105,11 +147,13 @@ def main():
|
||||
hid.press_volume_up()
|
||||
else:
|
||||
hid.press_volume_down()
|
||||
activity_led.pulse(0, 0, 255, now)
|
||||
consumer_release_at = time.ticks_add(now, KEY_HOLD_MS)
|
||||
|
||||
if (settings["enabled"] and settings["return_enabled"] and
|
||||
release_at is None and time.ticks_diff(now, next_return) >= 0):
|
||||
hid.press_return()
|
||||
activity_led.pulse(255, 0, 0, now)
|
||||
release_at = time.ticks_add(now, KEY_HOLD_MS)
|
||||
next_return = choose_due(
|
||||
now, settings["return_min_seconds"], settings["return_max_seconds"]
|
||||
@@ -124,6 +168,7 @@ def main():
|
||||
if x == 0 and y == 0:
|
||||
x = distance
|
||||
hid.move(x, y)
|
||||
activity_led.pulse(0, 255, 0, now)
|
||||
next_mouse = choose_due(
|
||||
now, settings["mouse_min_seconds"], settings["mouse_max_seconds"]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user