2026-05-26 21:26:07 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
2026-05-28 01:03:47 +02:00
|
|
|
Screenshot tool for Wio L1 Tracker Pro (OLED SH1106 and e-ink GxEPD2).
|
2026-05-26 21:26:07 +02:00
|
|
|
Connects via USB serial using mesh companion protocol, captures framebuffer, saves as PNG.
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
python screenshot.py [--port PORT] [--scale SCALE]
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
--port PORT Serial port to use (default: auto-detect)
|
|
|
|
|
--scale SCALE Upscale factor: 1=no upscale, 2=2x, 3=3x, 4=4x, etc. (default: 1)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import serial
|
|
|
|
|
import serial.tools.list_ports
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# Mesh protocol constants
|
|
|
|
|
FRAME_START_OUT = 0x3C # '<' - used when sending TO device
|
|
|
|
|
FRAME_START_IN = 0x3E # '>' - used when receiving FROM device
|
|
|
|
|
CMD_GET_SCREENSHOT = 66
|
|
|
|
|
RESP_CODE_SCREENSHOT = 29
|
|
|
|
|
RESP_CODE_ERR = 1
|
2026-05-28 01:03:47 +02:00
|
|
|
|
|
|
|
|
# display_type values (byte [5] in response frame)
|
|
|
|
|
DISPLAY_TYPE_OLED = 0 # page-based, column-major (SH1106/SSD1306)
|
|
|
|
|
DISPLAY_TYPE_EINK = 1 # row-major, MSB-first, 1=white/0=black (GxEPD2)
|
|
|
|
|
|
|
|
|
|
# E-ink panel constants for GxEPD2_213_B74 / GxEPD2_213_BN
|
|
|
|
|
EINK_PHYS_WIDTH = 128 # full hardware width including invisible columns
|
|
|
|
|
EINK_VISIBLE_W = 122 # WIDTH_VISIBLE — columns actually shown on panel
|
2026-05-26 21:26:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2026-05-28 01:03:47 +02:00
|
|
|
description="Capture screenshots from Wio L1 Tracker Pro (OLED or e-ink)"
|
2026-05-26 21:26:07 +02:00
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--port", "-p", help="Serial port to use (default: auto-detect)"
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--scale",
|
|
|
|
|
"-s",
|
|
|
|
|
type=int,
|
|
|
|
|
default=1,
|
|
|
|
|
help="Upscale factor: 1=no upscale, 2=2x, 3=3x, 4=4x, etc. (default: 1)",
|
|
|
|
|
)
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_wio_port():
|
|
|
|
|
"""Find Wio Tracker L1 serial port"""
|
|
|
|
|
ports = serial.tools.list_ports.comports()
|
|
|
|
|
for port in ports:
|
|
|
|
|
desc = str(port.description).lower()
|
|
|
|
|
if "wio" in desc or "seeed" in desc:
|
|
|
|
|
return port.device
|
|
|
|
|
|
|
|
|
|
# Fallback: check common port names
|
|
|
|
|
for port in ports:
|
|
|
|
|
if "ACM" in port.device or port.device.startswith("COM"):
|
|
|
|
|
return port.device
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_frame(ser):
|
|
|
|
|
"""Read a complete frame from the mesh protocol (from device)"""
|
|
|
|
|
header = ser.read(3)
|
|
|
|
|
if len(header) != 3:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
if header[0] != FRAME_START_IN:
|
|
|
|
|
print(
|
|
|
|
|
f"Error: Invalid frame start byte: {header[0]:02x} (expected {FRAME_START_IN:02x})"
|
|
|
|
|
)
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
frame_len = header[1] | (header[2] << 8)
|
|
|
|
|
|
|
|
|
|
if frame_len > 0:
|
|
|
|
|
data = bytearray()
|
|
|
|
|
while len(data) < frame_len:
|
|
|
|
|
chunk = ser.read(frame_len - len(data))
|
|
|
|
|
if not chunk:
|
|
|
|
|
print(f"Error: Timeout waiting for frame data")
|
|
|
|
|
return None
|
|
|
|
|
data.extend(chunk)
|
|
|
|
|
return bytes(data)
|
|
|
|
|
else:
|
|
|
|
|
return b""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_command(ser, command, data=b""):
|
|
|
|
|
"""Send a command frame using mesh protocol (to device)"""
|
|
|
|
|
frame_len = len(data) + 1
|
|
|
|
|
header = bytes([FRAME_START_OUT, frame_len & 0xFF, (frame_len >> 8) & 0xFF])
|
|
|
|
|
frame = header + bytes([command]) + data
|
|
|
|
|
ser.write(frame)
|
|
|
|
|
ser.flush()
|
|
|
|
|
return frame_len + 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def receive_screenshot(ser, timeout=5):
|
2026-05-28 01:03:47 +02:00
|
|
|
"""Receive screenshot data (may be split across multiple frames).
|
|
|
|
|
|
|
|
|
|
Frame format: RESP_CODE_SCREENSHOT, width, height, chunk_idx,
|
|
|
|
|
total_chunks, display_type, [chunk_data...]
|
|
|
|
|
"""
|
2026-05-26 21:26:07 +02:00
|
|
|
start_time = time.time()
|
|
|
|
|
buffer_data = bytearray()
|
|
|
|
|
width = None
|
|
|
|
|
height = None
|
2026-05-28 01:03:47 +02:00
|
|
|
display_type = None
|
2026-05-26 21:26:07 +02:00
|
|
|
total_chunks = None
|
|
|
|
|
received_chunks = 0
|
|
|
|
|
|
|
|
|
|
while time.time() - start_time < timeout:
|
|
|
|
|
frame = read_frame(ser)
|
|
|
|
|
if frame is None:
|
|
|
|
|
continue
|
|
|
|
|
|
2026-05-28 01:03:47 +02:00
|
|
|
if len(frame) < 6:
|
2026-05-26 21:26:07 +02:00
|
|
|
print(f"Error: Frame too short: {len(frame)} bytes")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
resp_code = frame[0]
|
|
|
|
|
|
|
|
|
|
if resp_code == RESP_CODE_SCREENSHOT:
|
2026-05-28 01:03:47 +02:00
|
|
|
w = frame[1]
|
|
|
|
|
h = frame[2]
|
|
|
|
|
chunk_idx = frame[3]
|
|
|
|
|
num_chunks = frame[4]
|
|
|
|
|
disp_type = frame[5]
|
|
|
|
|
chunk_data = frame[6:]
|
2026-05-26 21:26:07 +02:00
|
|
|
|
|
|
|
|
if chunk_idx == 0:
|
2026-05-28 01:03:47 +02:00
|
|
|
width = w
|
|
|
|
|
height = h
|
|
|
|
|
display_type = disp_type
|
|
|
|
|
total_chunks = num_chunks
|
|
|
|
|
buffer_data = bytearray()
|
2026-05-26 21:26:07 +02:00
|
|
|
received_chunks = 0
|
|
|
|
|
|
|
|
|
|
if width is None or width != w or height != h:
|
|
|
|
|
print(f"Error: Inconsistent dimensions in chunk {chunk_idx}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
if total_chunks is None or total_chunks != num_chunks:
|
|
|
|
|
print(f"Error: Inconsistent chunk count in chunk {chunk_idx}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
buffer_data.extend(chunk_data)
|
|
|
|
|
received_chunks += 1
|
|
|
|
|
|
|
|
|
|
if received_chunks >= total_chunks:
|
2026-05-28 01:03:47 +02:00
|
|
|
return bytes(buffer_data), width, height, display_type
|
2026-05-26 21:26:07 +02:00
|
|
|
|
|
|
|
|
elif resp_code == RESP_CODE_ERR:
|
|
|
|
|
print("Error: Device returned error")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
print(
|
|
|
|
|
f"Error: Timeout waiting for screenshot (received {received_chunks}/{total_chunks or '?'} chunks)"
|
|
|
|
|
)
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2026-05-28 01:03:47 +02:00
|
|
|
def oled_buffer_to_image(buffer, width, height):
|
|
|
|
|
"""Decode SH1106/SSD1306 framebuffer (page-based, column-major).
|
|
|
|
|
Each byte covers 8 vertical pixels; bit 0 = top pixel of the byte.
|
|
|
|
|
"""
|
2026-05-26 21:26:07 +02:00
|
|
|
image = Image.new("1", (width, height), 0)
|
|
|
|
|
pixels = image.load()
|
|
|
|
|
bytes_per_page = width
|
|
|
|
|
for page in range(0, height, 8):
|
|
|
|
|
page_start = (page // 8) * bytes_per_page
|
|
|
|
|
for col in range(width):
|
|
|
|
|
byte_idx = page_start + col
|
|
|
|
|
if byte_idx < len(buffer):
|
|
|
|
|
byte_val = buffer[byte_idx]
|
|
|
|
|
for bit in range(8):
|
|
|
|
|
if page + bit < height:
|
2026-05-28 01:03:47 +02:00
|
|
|
pixels[col, page + bit] = 1 if (byte_val & (1 << bit)) else 0
|
|
|
|
|
return image.convert("RGB")
|
2026-05-26 21:26:07 +02:00
|
|
|
|
|
|
|
|
|
2026-05-28 01:03:47 +02:00
|
|
|
def eink_buffer_to_image(buffer, log_width, log_height):
|
|
|
|
|
"""Decode GxEPD2 framebuffer for GxEPD2_213_B74 with DISPLAY_ROTATION=1 (landscape).
|
|
|
|
|
|
|
|
|
|
Physical panel: PHYS_WIDTH=128, HEIGHT=250.
|
|
|
|
|
Buffer layout: row-major, stride=PHYS_WIDTH/8=16 bytes, MSB-first.
|
|
|
|
|
byte index = phys_y * 16 + phys_x // 8
|
|
|
|
|
bit = 7 - phys_x % 8
|
|
|
|
|
value 1 = white (paper), 0 = black (ink)
|
|
|
|
|
|
|
|
|
|
With setRotation(1), GxEPD2 maps logical (lx, ly) to physical:
|
|
|
|
|
phys_x = PHYS_WIDTH - 1 - ly (= 127 - ly)
|
|
|
|
|
phys_y = lx
|
2026-05-26 21:26:07 +02:00
|
|
|
|
2026-05-28 01:03:47 +02:00
|
|
|
Visible logical height = EINK_VISIBLE_W = 122 (PHYS_WIDTH_VISIBLE).
|
|
|
|
|
We trim to visible_h rows so the 6 invisible physical columns don't appear.
|
|
|
|
|
"""
|
|
|
|
|
phys_stride = EINK_PHYS_WIDTH // 8 # 16 bytes per physical row
|
|
|
|
|
visible_h = min(log_height, EINK_VISIBLE_W) # trim to 122
|
|
|
|
|
|
|
|
|
|
image = Image.new("RGB", (log_width, visible_h), (255, 255, 255))
|
|
|
|
|
pixels = image.load()
|
|
|
|
|
|
|
|
|
|
for ly in range(visible_h):
|
|
|
|
|
phys_x = EINK_PHYS_WIDTH - 1 - ly # 127 - ly
|
|
|
|
|
for lx in range(log_width):
|
|
|
|
|
phys_y = lx
|
|
|
|
|
byte_idx = phys_y * phys_stride + phys_x // 8
|
|
|
|
|
bit = 7 - phys_x % 8
|
|
|
|
|
if byte_idx < len(buffer):
|
|
|
|
|
raw = (buffer[byte_idx] >> bit) & 1
|
|
|
|
|
# 1=white (paper), 0=black (ink)
|
|
|
|
|
color = (255, 255, 255) if raw else (0, 0, 0)
|
|
|
|
|
pixels[lx, ly] = color
|
|
|
|
|
|
|
|
|
|
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def buffer_to_image(buffer, width, height, display_type, scale=1):
|
|
|
|
|
"""Convert framebuffer to PIL Image with a 3-pixel black frame."""
|
|
|
|
|
if display_type == DISPLAY_TYPE_EINK:
|
|
|
|
|
image_rgb = eink_buffer_to_image(buffer, width, height)
|
|
|
|
|
else:
|
|
|
|
|
image_rgb = oled_buffer_to_image(buffer, width, height)
|
|
|
|
|
|
|
|
|
|
# Add 3-pixel black frame
|
|
|
|
|
FRAME_WIDTH = 3
|
|
|
|
|
fw = image_rgb.width + FRAME_WIDTH * 2
|
|
|
|
|
fh = image_rgb.height + FRAME_WIDTH * 2
|
|
|
|
|
final_image = Image.new("RGB", (fw, fh), (0, 0, 0))
|
2026-05-26 21:26:07 +02:00
|
|
|
final_image.paste(image_rgb, (FRAME_WIDTH, FRAME_WIDTH))
|
|
|
|
|
|
|
|
|
|
if scale > 1:
|
|
|
|
|
final_image = final_image.resize(
|
|
|
|
|
(final_image.width * scale, final_image.height * scale),
|
|
|
|
|
Image.Resampling.NEAREST,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return final_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_filename():
|
|
|
|
|
"""Generate timestamp filename in local time with seconds"""
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
os.makedirs("tools/pngs", exist_ok=True)
|
|
|
|
|
return os.path.join("tools/pngs", now.strftime("screenshot_%Y-%m-%d_%H-%M-%S.png"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
|
|
port = args.port
|
|
|
|
|
if not port:
|
|
|
|
|
port = find_wio_port()
|
|
|
|
|
|
|
|
|
|
if not port:
|
|
|
|
|
print("Error: Could not find Wio Tracker L1 serial port")
|
|
|
|
|
print("\nAvailable ports:")
|
|
|
|
|
for p in serial.tools.list_ports.comports():
|
|
|
|
|
print(f" {p.device}: {p.description}")
|
|
|
|
|
|
|
|
|
|
port = input("\nEnter serial port manually: ")
|
|
|
|
|
|
|
|
|
|
print(f"\nConnecting to {port} at 115200 baud (scale: {args.scale}x)...")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
ser = serial.Serial(port, 115200, timeout=3)
|
|
|
|
|
print("Connected!\n")
|
|
|
|
|
except serial.SerialException as e:
|
|
|
|
|
print(f"Error opening serial port: {e}")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
try:
|
2026-05-28 01:03:47 +02:00
|
|
|
print("Screenshot Tool for Wio L1 Tracker Pro (OLED + e-ink)")
|
2026-05-26 21:26:07 +02:00
|
|
|
print("Press 'S' to capture screenshot, 'Q' to quit\n")
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
key = input("> ").strip().upper()
|
|
|
|
|
except (KeyboardInterrupt, EOFError):
|
|
|
|
|
print("\nExiting...")
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if key == "Q":
|
|
|
|
|
print("Exiting...")
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if key == "S":
|
|
|
|
|
print("Requesting screenshot...")
|
|
|
|
|
|
|
|
|
|
ser.reset_input_buffer()
|
|
|
|
|
send_command(ser, CMD_GET_SCREENSHOT)
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
result = receive_screenshot(ser)
|
|
|
|
|
if result:
|
2026-05-28 01:03:47 +02:00
|
|
|
buffer_data, width, height, display_type = result
|
|
|
|
|
type_str = "e-ink" if display_type == DISPLAY_TYPE_EINK else "OLED"
|
2026-05-26 21:26:07 +02:00
|
|
|
print(
|
2026-05-28 01:03:47 +02:00
|
|
|
f"Received framebuffer: {width}x{height} {type_str}, {len(buffer_data)} bytes"
|
2026-05-26 21:26:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
image = buffer_to_image(
|
2026-05-28 01:03:47 +02:00
|
|
|
buffer_data, width, height, display_type, scale=args.scale
|
2026-05-26 21:26:07 +02:00
|
|
|
)
|
|
|
|
|
filename = generate_filename()
|
|
|
|
|
image.save(filename)
|
|
|
|
|
print(f"Saved as {filename} ({image.width}x{image.height})")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error saving image: {e}")
|
|
|
|
|
else:
|
|
|
|
|
print("Error: Failed to receive screenshot")
|
|
|
|
|
else:
|
|
|
|
|
print("Unknown command. Press 'S' for screenshot, 'Q' to quit")
|
|
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print("\nExiting...")
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
ser.close()
|
|
|
|
|
print("Connection closed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|