Skip to content

Rev A: optional USB bulk transport to fix corrupted images on macOS - #1138

Open
eaglstun wants to merge 2 commits into
mathoudebine:mainfrom
eaglstun:fix/macos-rev-a-usb-bulk
Open

eaglstun wants to merge 2 commits into
mathoudebine:mainfrom
eaglstun:fix/macos-rev-a-usb-bulk

Conversation

@eaglstun

Copy link
Copy Markdown

Addresses #7 (corrupted images on macOS with rev. A displays).

Problem

On macOS, rev. A displays (1a86:5722, serial USB35INCHIPSV2, product "UsbMonitor") draw corrupted images through the CDC serial device /dev/cu.usbmodem*: staircases, slanted rows, repeated passes, and a desync that persists until the display is unplugged.

On my unit, test patterns showed that the firmware handles pixel data per 64-byte USB packet:

  • a short packet is drawn padded with stale bytes, so the image shears;
  • after a write whose length is a multiple of 64 bytes, macOS sends a zero-length packet, which is drawn as a duplicate packet. Large writes come out about 20% too long, and the leftover pixels wrap onto the screen as a second pass.

Flushes and delays change where the packet boundaries fall, not what the firmware does with them, which is why they never fully fixed this. The vendor Windows app writes straight to the USB bulk endpoint.

Change

  • New library/lcd/lcd_comm_rev_a_usb.py (LcdCommRevAUsb), a subclass of LcdCommRevA that writes to bulk OUT endpoint 0x03 with pyusb/libusb instead of pyserial:
    • every command is its own USB transfer;
    • DisplayPILImage keeps a framebuffer copy of the screen and widens each update horizontally to the 32-px grid, so pixel data is always whole 64-byte packets and exactly w*h*2 bytes. Partial updates (text, bars, graphs) are packet-safe too, not just full frames;
    • SET_ORIENTATION is sent as 11 bytes;
    • Reset() is a logged no-op on this transport. A desynced panel swallows RESET as pixel data, and I didn't validate reset behavior over USB.
  • config.yaml: new display.USB_BULK option, default false, so nothing changes for existing users.
  • library/display.py: uses LcdCommRevAUsb when USB_BULK: true, and falls back to the serial port if the device can't be opened over USB (for example, libusb missing).

pyusb is already in requirements.txt. macOS needs libusb (brew install libusb). No root is needed: detach_kernel_driver() fails with "Access denied", but claim_interface(1) succeeds with Apple's CDC driver still attached, and writes to 0x03 work.

Testing

  • macOS 27, Apple M4 Max, display connected through a USB-C dock: full-screen test patterns, simple-program-style text and clock updates, and main.py with 3.5inchTheme2, SimpleBlueGauge and bash-dark-green. Images are clean with no artifacts. A full 320×480 frame takes about 1.9 s, and steady state is about 1.3% of one core.
  • The same unit on Raspberry Pi OS (Bookworm) draws correctly through the stock serial path. Linux isn't affected, so the default stays serial.
  • I checked the exact USB transfers this branch writes against my tested local build using a fake USB device: identical byte for byte, and every pixel transfer is a multiple of 64 bytes.

Possible follow-up, if wanted: enable USB_BULK automatically on macOS.

Credit to marcelors27 and amarok30 in #7 for the bulk-endpoint approach.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AJosYpRiBzMfZgig6BxiN3

On macOS, rev A displays (1a86:5722, "UsbMonitor") draw corrupted
images through the CDC serial device (issue mathoudebine#7). The firmware handles
pixel data per 64-byte USB packet: short packets are drawn padded with
stale bytes, and the zero-length packet macOS sends after a write that
is a multiple of 64 bytes is drawn as a duplicate packet, so the pixel
stream falls out of step.

Add LcdCommRevAUsb, which writes to the bulk OUT endpoint 0x03 with
pyusb/libusb (as the vendor Windows app does). Each command is its own
transfer, and bitmaps are widened to the 32-px grid from a framebuffer
copy of the screen so pixel data is always whole 64-byte packets of
exactly w*h*2 bytes. On macOS, claiming the interface works without
root even though detaching the kernel driver does not.

Opt-in with display.USB_BULK in config.yaml (default false); falls back
to the serial port if the device cannot be opened over USB. Reset is a
logged no-op on this transport.

Approach from issue mathoudebine#7 (findings by marcelors27 and amarok30).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AJosYpRiBzMfZgig6BxiN3
eaglstun added a commit to eaglstun/turing-smart-screen-python that referenced this pull request Nov 19, 2026
Takes the PR's standalone lcd_comm_rev_a_usb.py (Reset is a no-op over
USB, clearer header). Keeps main's display.py (serial fallback uses
PACKET_FIX) and config.yaml (USB_BULK and PACKET_FIX enabled for this
Mac). USB transfers verified byte-identical to the tested build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AJosYpRiBzMfZgig6BxiN3

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant