Created
January 29, 2026 22:33
-
-
Save Doridian/7c70cf2ce6126ea702574ede88eb59eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S uv run --script | |
| # /// script | |
| # requires-python = ">=3.12" | |
| # dependencies = [ | |
| # "pyserial>=3.5", | |
| # ] | |
| # /// | |
| from sys import argv | |
| from serial import Serial | |
| from math import ceil | |
| import json | |
| from time import sleep | |
| class InsightHub: | |
| IMAGE_W = 226 | |
| IMAGE_H = 90 | |
| def __init__(self, port: Serial): | |
| self.ser = port | |
| def _read_response(self): | |
| line = self.ser.readline().decode("utf-8").rstrip() | |
| if not line: | |
| raise TimeoutError("No response received from USB Insight Hub") | |
| resp = json.loads(line) | |
| print("<<<", resp) | |
| print("") | |
| if resp["status"] != "ok": | |
| raise ValueError(line) | |
| return resp | |
| def _rpc(self, data: bytes): | |
| _ = self.ser.write(data) | |
| return self._read_response() | |
| def send_bitmap(self, index: int, bpp: int, bitmap: bytes, dbgstr: str): | |
| if bpp not in (8, 16): | |
| raise ValueError("BPP must be one of 8 or 16") | |
| image_size = ceil((self.IMAGE_W * self.IMAGE_H * bpp) / 8) | |
| if len(bitmap) != image_size: | |
| raise ValueError( | |
| f"Image data must be exactly {image_size} bytes" | |
| ) | |
| print(">>>", "[0x%02x (Index %d), 0x%02x (%d BPP)] + image data (%s)" % (index, index, bpp, bpp, dbgstr)) | |
| return self._rpc(bytes([index, bpp]) + bitmap) | |
| def send_json(self, data: dict): | |
| payload = json.dumps(data).encode("utf-8") + b"\n" | |
| print(">>>", payload.decode("utf-8").strip()) | |
| return self._rpc(payload) | |
| def set_image(self, index: int, bpp: int, bitmap: bytes, dbgstr: str): | |
| _ = self.send_bitmap(index, bpp, bitmap, dbgstr) | |
| _ = self.send_json({ | |
| "action": "set", | |
| "params": { | |
| f"CH{index}": { | |
| "numDev": "11", | |
| "usbType": "3", | |
| }, | |
| }, | |
| }) | |
| def run(self): | |
| self.send_json({ | |
| "action": "set", | |
| "params": { | |
| "wifi_enabled": "false", # Needed for RAM | |
| }, | |
| }) | |
| while True: | |
| print("=========================") | |
| self.set_image(1, 16, b"\x00\xFF" * (self.IMAGE_W * self.IMAGE_H), "[0x00, 0xFF] * W * H") | |
| self.set_image(2, 8, b"\xF0" * (self.IMAGE_W * self.IMAGE_H), "[0xF0] * W * H") | |
| self.set_image(3, 16, b"\xFF\x00" * (self.IMAGE_W * self.IMAGE_H), "[0xFF, 0x00] * W * H") | |
| print("=========================") | |
| sleep(1) | |
| with Serial(argv[1], baudrate=115200, timeout=1, dsrdtr=True) as port: | |
| hub = InsightHub(port) | |
| hub.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment