This commit is contained in:
54
main.py
54
main.py
@@ -112,7 +112,7 @@ def connect_wifi(display=None, max_retries=5):
|
||||
|
||||
|
||||
|
||||
def process_message(msg, display, image_state, image_data_list):
|
||||
def process_message(msg, display, image_state, image_data_list, printer_uart=None):
|
||||
"""处理WebSocket消息"""
|
||||
# Handle binary image data
|
||||
if isinstance(msg, (bytes, bytearray)):
|
||||
@@ -138,6 +138,12 @@ def process_message(msg, display, image_state, image_data_list):
|
||||
print(f"Stream image error: {e}")
|
||||
|
||||
return image_state, None
|
||||
|
||||
elif image_state == PRINTER_STATE_RECEIVING:
|
||||
if printer_uart:
|
||||
printer_uart.write(msg)
|
||||
return image_state, None
|
||||
|
||||
return image_state, None
|
||||
|
||||
if not isinstance(msg, str):
|
||||
@@ -153,6 +159,14 @@ def process_message(msg, display, image_state, image_data_list):
|
||||
print_asr(msg[4:], display)
|
||||
return image_state, ("asr", msg[4:])
|
||||
|
||||
elif msg.startswith("PRINTER_DATA_START:"):
|
||||
print(f"Start receiving printer data...")
|
||||
return PRINTER_STATE_RECEIVING, ("printer_start",)
|
||||
|
||||
elif msg == "PRINTER_DATA_END":
|
||||
print("Printer data received completely")
|
||||
return IMAGE_STATE_IDLE, ("printer_done",)
|
||||
|
||||
elif msg.startswith("STATUS:"):
|
||||
parts = msg[7:].split(":", 1)
|
||||
status_type = parts[0]
|
||||
@@ -284,6 +298,9 @@ def main():
|
||||
mic = Microphone()
|
||||
display = Display()
|
||||
|
||||
# 初始化打印机 UART
|
||||
printer_uart = machine.UART(1, baudrate=115200, tx=ttl_tx, rx=ttl_rx)
|
||||
|
||||
if display.tft:
|
||||
display.init_ui()
|
||||
display.render_home_screen()
|
||||
@@ -446,9 +463,20 @@ def main():
|
||||
if display.tft:
|
||||
display.render_result_screen("OPTIMIZING", current_asr_text, False)
|
||||
time.sleep(0.5)
|
||||
elif ui_screen == UI_SCREEN_RESULT:
|
||||
# Re-record
|
||||
print(">>> Re-record (Short Press)")
|
||||
current_asr_text = ""
|
||||
confirm_waiting = False
|
||||
ui_screen = UI_SCREEN_RECORDING
|
||||
is_recording = False
|
||||
image_generation_done = False
|
||||
if display.tft:
|
||||
display.render_recording_screen("", 0, False)
|
||||
time.sleep(0.5)
|
||||
|
||||
elif btn_action == 2:
|
||||
if ui_screen == UI_SCREEN_CONFIRM or ui_screen == UI_SCREEN_RESULT:
|
||||
if ui_screen == UI_SCREEN_CONFIRM:
|
||||
print(">>> Re-record")
|
||||
current_asr_text = ""
|
||||
confirm_waiting = False
|
||||
@@ -458,6 +486,17 @@ def main():
|
||||
if display.tft:
|
||||
display.render_recording_screen("", 0, False)
|
||||
time.sleep(0.5)
|
||||
elif ui_screen == UI_SCREEN_RESULT:
|
||||
# Print Image
|
||||
print(">>> Print Image (Long Press)")
|
||||
if ws and ws.is_connected():
|
||||
try:
|
||||
ws.send("PRINT_IMAGE")
|
||||
if display.tft:
|
||||
display.render_result_screen("PRINTING", "正在请求打印...", True)
|
||||
except:
|
||||
ws = None
|
||||
time.sleep(0.5)
|
||||
|
||||
elif btn_action == 3:
|
||||
print(">>> Config mode")
|
||||
@@ -484,7 +523,7 @@ def main():
|
||||
if events:
|
||||
msg = ws.recv()
|
||||
if msg:
|
||||
image_state, event_data = process_message(msg, display, image_state, image_data_list)
|
||||
image_state, event_data = process_message(msg, display, image_state, image_data_list, printer_uart)
|
||||
|
||||
if event_data:
|
||||
if event_data[0] == "asr":
|
||||
@@ -521,6 +560,15 @@ def main():
|
||||
elif event_data[0] == "error":
|
||||
if display.tft and ui_screen == UI_SCREEN_RESULT:
|
||||
display.render_result_screen("ERROR", current_prompt, False)
|
||||
|
||||
elif event_data[0] == "printer_start":
|
||||
if display.tft and ui_screen == UI_SCREEN_RESULT:
|
||||
display.render_result_screen("PRINTING", "正在打印...", True)
|
||||
|
||||
elif event_data[0] == "printer_done":
|
||||
if display.tft and ui_screen == UI_SCREEN_RESULT:
|
||||
display.render_result_screen("COMPLETE", "打印完成", True)
|
||||
time.sleep(1.0)
|
||||
except Exception as e:
|
||||
print(f"WS Recv Error: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user