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}")
|
||||
|
||||
|
||||
@@ -530,17 +530,20 @@ def optimize_prompt(asr_text, progress_callback=None):
|
||||
if progress_callback:
|
||||
progress_callback(0, "正在准备优化提示词...")
|
||||
|
||||
system_prompt = """你是一个AI图像提示词优化专家。将用户简短的语音识别结果转化为详细的、适合AI图像生成的中文提示词。
|
||||
要求:
|
||||
1. 用于热敏打印机的中文提示词图片
|
||||
2. 添加适合AI绘画的描述词尺寸宽48mm, 高30mm 的线稿图片,线稿要存粗方便热敏打印
|
||||
3. 适合热敏打印机打印图片,还可以是icon方便这个标签打印机打印效果
|
||||
4. 简洁但描述详细
|
||||
5. 不要添加多余解释,直接输出优化后的提示词"""
|
||||
system_prompt = """你是一个AI图像提示词优化专家。你的任务是将用户的语音识别结果转化为适合生成"黑白线稿"的提示词。
|
||||
关键要求:
|
||||
1. 风格必须是:简单的黑白线稿、简笔画、图标风格 (Line art, Sketch, Icon style)。
|
||||
2. 画面必须清晰、线条粗壮,适合低分辨率热敏打印机打印。
|
||||
3. 绝对不要有复杂的阴影、渐变、彩色描述。
|
||||
4. 背景必须是纯白 (White background)。
|
||||
5. 提示词内容请使用英文描述,因为绘图模型对英文理解更好,但在描述中强调 "black and white line art", "simple lines", "vector style"。
|
||||
6. 尺寸比例遵循宽48mm:高30mm (约 1.6:1)。
|
||||
7. 直接输出优化后的提示词,不要包含任何解释。"""
|
||||
|
||||
try:
|
||||
if progress_callback:
|
||||
progress_callback(10, "正在调用AI优化提示词...")
|
||||
print(f"Calling AI with prompt: {system_prompt}\n\n用户语音识别结果:{asr_text}\n\n优化后的提示词:")
|
||||
|
||||
response = Generation.call(
|
||||
model='qwen-turbo',
|
||||
|
||||
Reference in New Issue
Block a user