""" Pre-upload script that sends FLASH command to device before firmware upload. This allows the firmware to display "Flashing firmware..." on the e-ink display before the actual flash begins. The e-ink retains this message throughout the flash process since it doesn't require power to maintain the display. Protocol: Sends "FLASH:version\n" where version is read from platformio.ini """ Import("env") import serial import time from version_utils import get_version def before_upload(source, target, env): """Send FLASH command with version to device before upload begins.""" port = env.GetProjectOption("upload_port", None) if not port: import serial.tools.list_ports # Look for common ESP32-C3 USB serial port patterns ports = [ p.device for p in serial.tools.list_ports.comports() if "usbmodem" in p.device.lower() or "ttyacm" in p.device.lower() ] port = ports[0] if ports else None if port: try: version = get_version(env) ser = serial.Serial(port, 115200, timeout=1) ser.write(f"FLASH:{version}\n".encode()) ser.flush() ser.close() time.sleep(0.8) # Wait for e-ink fast refresh (~500ms) plus margin print(f"[pre_flash] Flash notification sent to {port} (version {version})") except Exception as e: print(f"[pre_flash] Notification skipped: {e}") else: print("[pre_flash] No serial port found, skipping notification") env.AddPreAction("upload", before_upload)