From 30aff5eb8b74cfb000525573680b8e912a405d36 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Fri, 19 Jun 2026 13:38:25 -0400 Subject: [PATCH] Optimize draw_rgb565 display write speed by skipping slow synchronous canvas updates during raw video frames --- lib/ili9341.py | 5 +++-- lib/st7796.py | 5 +++-- mcp_server.py | 18 +++++++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/ili9341.py b/lib/ili9341.py index b1a95e3..89be7f3 100644 --- a/lib/ili9341.py +++ b/lib/ili9341.py @@ -233,7 +233,7 @@ class ILI9341: mono = 1 if lum >= 128 else 0 self.canvas.pixel(screen_x, screen_y, mono) - def draw_rgb565(self, x, y, w, h, data): + def draw_rgb565(self, x, y, w, h, data, sync_canvas=True): """Draw raw RGB565 pixel data on the screen at specified (x,y) with width and height.""" # Clip coordinates x_start = max(0, x) @@ -265,7 +265,8 @@ class ILI9341: self.cs(1) # Sync the internal 1-bit canvas buffer - self._update_mono_canvas_rgb565(x, y, w, h, data) + if sync_canvas: + self._update_mono_canvas_rgb565(x, y, w, h, data) return True # --- SCREENSHOT --- diff --git a/lib/st7796.py b/lib/st7796.py index 4e7c945..d6558ec 100644 --- a/lib/st7796.py +++ b/lib/st7796.py @@ -233,7 +233,7 @@ class ST7796: mono = 1 if lum >= 128 else 0 self.canvas.pixel(screen_x, screen_y, mono) - def draw_rgb565(self, x, y, w, h, data): + def draw_rgb565(self, x, y, w, h, data, sync_canvas=True): """Draw raw RGB565 pixel data on the screen at specified (x,y) with width and height.""" # Clip coordinates x_start = max(0, x) @@ -265,7 +265,8 @@ class ST7796: self.cs(1) # Sync the internal 1-bit canvas buffer - self._update_mono_canvas_rgb565(x, y, w, h, data) + if sync_canvas: + self._update_mono_canvas_rgb565(x, y, w, h, data) return True # --- SCREENSHOT --- diff --git a/mcp_server.py b/mcp_server.py index db19d9e..6c22f3c 100644 --- a/mcp_server.py +++ b/mcp_server.py @@ -77,8 +77,11 @@ class MCPServer: if self.sock is None: return + t0 = 0 + t1 = 0 try: client, addr = self.sock.accept() + t0 = time.ticks_ms() except OSError as e: import errno err = getattr(e, 'errno', None) @@ -110,6 +113,7 @@ class MCPServer: header_text = req_bytes[:header_end].decode('utf-8', 'ignore') lines = header_text.split('\r\n') + t1 = time.ticks_ms() if len(lines) == 0 or not lines[0]: client.close() return @@ -171,6 +175,7 @@ class MCPServer: if not chunk: break body_bytes.extend(chunk) + t2 = time.ticks_ms() # Parse query parameters from path (e.g. /api/screen/raw?x=0&y=0&w=320&h=240) width = getattr(self.display, 'width', 320) @@ -186,10 +191,12 @@ class MCPServer: elif k == 'y': y = int(v) elif k == 'w': w = int(v) elif k == 'h': h = int(v) + t3 = time.ticks_ms() # Draw directly using display raw RGB565 method self.override_active = True - self.display.draw_rgb565(x, y, w, h, body_bytes) + self.display.draw_rgb565(x, y, w, h, body_bytes, sync_canvas=False) + t4 = time.ticks_ms() resp_body = "OK" resp = "HTTP/1.1 200 OK\r\n" @@ -206,6 +213,15 @@ class MCPServer: time.sleep_ms(10) continue total_sent += sent + t5 = time.ticks_ms() + print("[RAW API Profile] total={}ms: accept_to_header={}ms, recv_body={}ms, route_n_prep={}ms, draw={}ms, send_resp={}ms".format( + time.ticks_diff(t5, t0), + time.ticks_diff(t1, t0), + time.ticks_diff(t2, t1), + time.ticks_diff(t3, t2), + time.ticks_diff(t4, t3), + time.ticks_diff(t5, t4) + )) else: # Return 404 resp = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"