t
All checks were successful
Deploy WebSocket Server / deploy (push) Successful in 20s

This commit is contained in:
jeremygan2021
2026-03-05 21:56:44 +08:00
parent b430051d29
commit 9558ea4b35
4 changed files with 63 additions and 31 deletions

42
main.py
View File

@@ -6,6 +6,7 @@ import network
import st7789py as st7789
from config import CURRENT_CONFIG, SERVER_URL, ttl_tx, ttl_rx
from audio import AudioPlayer, Microphone
import convert_img
# Define colors that might be missing in st7789py
DARKGREY = 0x4208
@@ -118,21 +119,22 @@ def process_message(msg, display, image_state, image_data_list, printer_uart=Non
if isinstance(msg, (bytes, bytearray)):
if image_state == IMAGE_STATE_RECEIVING:
try:
if len(image_data_list) < 2:
if len(image_data_list) < 3:
# 异常情况,重置
return IMAGE_STATE_IDLE, None
img_size = image_data_list[0]
current_offset = image_data_list[1]
width = image_data_list[0]
height = image_data_list[1]
current_offset = image_data_list[2]
# Stream directly to display
if display and display.tft:
x = (240 - img_size) // 2
y = (240 - img_size) // 2
display.show_image_chunk(x, y, img_size, img_size, msg, current_offset)
x = (240 - width) // 2
y = (240 - height) // 2
display.show_image_chunk(x, y, width, height, msg, current_offset)
# Update offset
image_data_list[1] += len(msg)
image_data_list[2] += len(msg)
except Exception as e:
print(f"Stream image error: {e}")
@@ -194,18 +196,28 @@ def process_message(msg, display, image_state, image_data_list, printer_uart=Non
try:
parts = msg.split(":")
size = int(parts[1])
img_size = int(parts[2]) if len(parts) > 2 else 64
print(f"Image start, size: {size}, img_size: {img_size}")
width = 64
height = 64
if len(parts) >= 4:
width = int(parts[2])
height = int(parts[3])
elif len(parts) == 3:
width = int(parts[2])
height = int(parts[2]) # assume square
print(f"Image start, size: {size}, dim: {width}x{height}")
image_data_list.clear()
image_data_list.append(img_size) # Store metadata at index 0
image_data_list.append(0) # Store current received bytes offset at index 1
image_data_list.append(width) # index 0
image_data_list.append(height) # index 1
image_data_list.append(0) # index 2: offset
# Prepare display for streaming
if display and display.tft:
# Calculate position
x = (240 - img_size) // 2
y = (240 - img_size) // 2
# Pre-set window (this will be done in first chunk call)
# Clear screen area where image will be
# optional, but good practice if new image is smaller
pass
return IMAGE_STATE_RECEIVING, None
except Exception as e: