From 95f2bf2e4a0744f7aa7b9049988a2ca4fe58315f Mon Sep 17 00:00:00 2001 From: kicer Date: Thu, 29 Jan 2026 18:32:20 +0800 Subject: [PATCH] backup codes --- src/rom/app.py | 16 ++++++++++++++-- src/rom/captive_http.py | 9 +++++---- src/rom/captive_portal.py | 16 ++++++++-------- src/rom/display.py | 13 ++++++++++++- src/rom/wifi_manager.py | 5 +++-- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/rom/app.py b/src/rom/app.py index 85551ef..ef2f86f 100644 --- a/src/rom/app.py +++ b/src/rom/app.py @@ -198,6 +198,16 @@ async def animation_task(): except Exception as e: print(f"动画任务初始化失败: {e}") +def cb_progress(data): + if isinstance(data, bytes): + if data == b'/': + display.portal_info('load iwconfig page ') + elif data == b'/login': + display.portal_info('WiFi connecting ... ') + elif isinstance(data, str): + display.message(data, 19, 204) + + if data: print(f'progress: {str(data)}') def start(): # 初始化液晶屏 @@ -207,12 +217,14 @@ def start(): gc.collect() display.message("WiFi connect ...") - if not wifi_manager.connect(): + if not wifi_manager.connect(cb_progress): gc.collect() from captive_portal import CaptivePortal portal = CaptivePortal() display.portal_win(portal.essid.decode('ascii')) - return portal.start() + portal.start(cb_progress) + # just reboot + machine.reset() gc.collect() diff --git a/src/rom/captive_http.py b/src/rom/captive_http.py index 637edec..68b9756 100644 --- a/src/rom/captive_http.py +++ b/src/rom/captive_http.py @@ -70,15 +70,15 @@ class HTTPServer(BaseServer): if sock is self.sock: # client connecting on port 80, so spawn off a new # socket to handle this connection - print("- Accepting new HTTP connection") + # print("- Accepting new HTTP connection") self.accept(sock) elif event & select.POLLIN: # socket has data to read in - print("- Reading incoming HTTP data") - self.read(sock) + # print("- Reading incoming HTTP data") + return self.read(sock) elif event & select.POLLOUT: # existing connection has space to send more data - print("- Sending outgoing HTTP data") + # print("- Sending outgoing HTTP data") self.write_to(sock) def accept(self, server_sock): @@ -217,6 +217,7 @@ class HTTPServer(BaseServer): # host and a valid route body, headers = self.get_response(req) self.prepare_write(s, body, headers) + return req.path def prepare_write(self, s, body, headers): # add newline to headers to signify transition to body diff --git a/src/rom/captive_portal.py b/src/rom/captive_portal.py index 0015222..149c7c8 100644 --- a/src/rom/captive_portal.py +++ b/src/rom/captive_portal.py @@ -47,18 +47,18 @@ class CaptivePortal: return True return False - def check_valid_wifi(self): + def check_valid_wifi(self, callback): if not wifi_manager.is_connected(): if config.is_valid(): # have credentials to connect, but not yet connected # return value based on whether the connection was successful - return self.connect_to_wifi() + return True # not connected, and no credentials to connect yet return False return True - def captive_portal(self): + def captive_portal(self, callback): print("Starting captive portal") self.start_access_point() @@ -77,9 +77,9 @@ class CaptivePortal: sock, event, *others = response is_handled = self.handle_dns(sock, event, others) if not is_handled: - self.handle_http(sock, event, others) + callback(self.handle_http(sock, event, others)) - if self.check_valid_wifi(): + if self.check_valid_wifi(callback): print("Connected to WiFi!") self.http_server.stop(self.poller) self.dns_server.stop(self.poller) @@ -100,7 +100,7 @@ class CaptivePortal: return False def handle_http(self, sock, event, others): - self.http_server.handle(sock, event, others) + return self.http_server.handle(sock, event, others) def cleanup(self): print("Cleaning up") @@ -126,8 +126,8 @@ class CaptivePortal: print("Connection failed but keeping configuration for retry") return False - def start(self): + def start(self, callback): # turn off station interface to force a reconnect wifi_manager.disconnect() if not self.try_connect_from_file(): - return self.captive_portal() + return self.captive_portal(callback) diff --git a/src/rom/display.py b/src/rom/display.py index 172f8d0..a931fd3 100644 --- a/src/rom/display.py +++ b/src/rom/display.py @@ -29,6 +29,7 @@ class Display: self._backlight = None self._brightness = 80 # 默认亮度80% self._initialized = True + self._pos_y0 = 10 self.en_font = '/rom/fonts/en-8x16.rfont' self.cn_font = '/rom/fonts/cn-22x24.bfont' self.vector_font = '/rom/fonts/en-32x32.hfont' @@ -77,6 +78,7 @@ class Display: def clear(self, color=st7789.BLACK): """清屏""" self.tft.fill(color) + self._pos_y0 = 10 def show_jpg(self, filename, x=0, y=0, mode=st7789.SLOW): """显示JPG图片""" @@ -98,8 +100,14 @@ class Display: self._brightness = _brightness return self._brightness - def message(self, msg, x=10, y=10, fg=st7789.WHITE, bg=st7789.BLACK): + def message(self, msg, x=10, y=None, fg=st7789.WHITE, bg=st7789.BLACK): + if y == None: + y = self._pos_y0 self.tft.text(self.en_font, msg, x, y, fg, bg) + # 每次打印完消息则滚动到下一行 + self._pos_y0 += 20 + if self._pos_y0 >= 240-20: + self._pos_y0 = 10 def window(self, title=None, content=None, info=None): C_FG,C_BG,C_BT = self._COLORS @@ -117,6 +125,9 @@ class Display: if line: self.tft.write(self.cn_font, line, 19, 68+i*28, C_FG, C_BG) + def portal_info(self, msg): + C_FG,C_BG,C_BT = self._COLORS + self.tft.text(self.en_font, msg, 19, 204, C_FG, C_BT) def portal_win(self, ssid): tips = [ "> 连接热点:", diff --git a/src/rom/wifi_manager.py b/src/rom/wifi_manager.py index 8251444..cd24ab8 100644 --- a/src/rom/wifi_manager.py +++ b/src/rom/wifi_manager.py @@ -48,7 +48,7 @@ class WiFiManager: return None return None - def connect(self): + def connect(self, callback=None): """尝试连接到WiFi""" # 加载配置 if not self.config.is_valid(): @@ -86,7 +86,8 @@ class WiFiManager: print(f"Connected successfully! IP: {ip}") return True - print(f"Connection attempt {attempts}/{self.MAX_CONN_ATTEMPTS}...") + # print(f"Connection attempt {attempts}/{self.MAX_CONN_ATTEMPTS}...") + if callback: callback(f"WiFi connect {attempts} ...") time.sleep(2) attempts += 1