|
| 1 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +# |
| 3 | +# turing-smart-screen-python - a Python system monitor and library for USB-C displays like Turing Smart Screen or XuanFang |
| 4 | +# https://github.com/mathoudebine/turing-smart-screen-python/ |
| 5 | +# |
| 6 | +# Driver for HL-VMAX / Solarmax USB LCD Displays (Artinchip VID 0x33C3, PID 0xF101) |
| 7 | +# Supports 462x1920, 320x1480, 480x1920 resolutions. |
| 8 | +# |
| 9 | + |
| 10 | +import io |
| 11 | +import re |
| 12 | +import time |
| 13 | +import threading |
| 14 | +from typing import Optional, Tuple |
| 15 | +from enum import IntEnum |
| 16 | +import serial |
| 17 | +from serial.tools.list_ports import comports |
| 18 | +from PIL import Image |
| 19 | + |
| 20 | +from library.lcd.lcd_comm import LcdComm, Orientation, queue |
| 21 | +from library.log import logger |
| 22 | + |
| 23 | +VID = 0x33C3 |
| 24 | +PID = 0xF101 |
| 25 | +HANDSHAKE = bytes([0xF0, 0xA5, 0x5A, 0x0F]) |
| 26 | +SN_LEN = 26 |
| 27 | +DEFAULT_WIDTH = 462 |
| 28 | +DEFAULT_HEIGHT = 1920 |
| 29 | + |
| 30 | + |
| 31 | +class LcdCommVmax(LcdComm): |
| 32 | + def __init__(self, com_port: str = "AUTO", display_width: int = DEFAULT_WIDTH, |
| 33 | + display_height: int = DEFAULT_HEIGHT, update_queue: Optional[queue.Queue] = None): |
| 34 | + logger.debug("HW revision: VMAX / Solarmax (Artinchip)") |
| 35 | + super().__init__(com_port, display_width, display_height, update_queue) |
| 36 | + self.serial_number = None |
| 37 | + self.brightness = 100 |
| 38 | + |
| 39 | + # Canvas for full frame composition |
| 40 | + self._canvas_lock = threading.Lock() |
| 41 | + self._canvas = Image.new("RGB", (self.display_width, self.display_height), (0, 0, 0)) |
| 42 | + self._frame_pending = False |
| 43 | + self._serial_lock = threading.Lock() |
| 44 | + |
| 45 | + self.openSerial() |
| 46 | + |
| 47 | + def __del__(self): |
| 48 | + self.closeSerial() |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def auto_detect_com_port() -> Optional[str]: |
| 52 | + ports = comports() |
| 53 | + for p in ports: |
| 54 | + if p.vid == VID and p.pid == PID: |
| 55 | + return p.device |
| 56 | + if p.serial_number and "VMAX" in p.serial_number.upper(): |
| 57 | + return p.device |
| 58 | + return None |
| 59 | + |
| 60 | + def openSerial(self): |
| 61 | + with self._serial_lock: |
| 62 | + for attempt in range(1, 11): |
| 63 | + com_port = self.com_port |
| 64 | + if com_port == 'AUTO': |
| 65 | + com_port = self.auto_detect_com_port() |
| 66 | + if not com_port: |
| 67 | + logger.warning(f"Cannot find HL-VMAX COM port automatically, retrying ({attempt}/10)") |
| 68 | + time.sleep(1) |
| 69 | + continue |
| 70 | + logger.debug(f"Auto detected COM port: {com_port}") |
| 71 | + else: |
| 72 | + logger.debug(f"Static COM port: {com_port}") |
| 73 | + |
| 74 | + try: |
| 75 | + self.lcd_serial = serial.Serial( |
| 76 | + com_port, |
| 77 | + 115200, |
| 78 | + timeout=2.0, |
| 79 | + write_timeout=2.0, |
| 80 | + rtscts=False, |
| 81 | + dsrdtr=False |
| 82 | + ) |
| 83 | + self.lcd_serial.dtr = True |
| 84 | + self.lcd_serial.rts = True |
| 85 | + self.lcd_serial.reset_input_buffer() |
| 86 | + self.lcd_serial.reset_output_buffer() |
| 87 | + return |
| 88 | + except Exception as e: |
| 89 | + logger.warning(f"Cannot open COM port {com_port}: {e} - retrying ({attempt}/10)") |
| 90 | + time.sleep(1) |
| 91 | + |
| 92 | + def InitializeComm(self): |
| 93 | + with self._serial_lock: |
| 94 | + logger.debug("Initializing HL-VMAX communication...") |
| 95 | + self.serial_flush_input() |
| 96 | + try: |
| 97 | + self.serial_write(HANDSHAKE) |
| 98 | + raw = self.serial_read(SN_LEN) |
| 99 | + if len(raw) == SN_LEN: |
| 100 | + self.serial_number = raw.decode("ascii", errors="replace").strip() |
| 101 | + logger.info(f"Connected to HL-VMAX screen, Serial Number: {self.serial_number}") |
| 102 | + self._parse_geometry(self.serial_number) |
| 103 | + else: |
| 104 | + logger.warning(f"Unexpected handshake response length: {len(raw)} bytes") |
| 105 | + except Exception as e: |
| 106 | + logger.error(f"Failed during HL-VMAX handshake: {e}") |
| 107 | + |
| 108 | + def _parse_geometry(self, sn: str): |
| 109 | + # Format example: VMAXD160462*1920S262001750 or VMAXA170320*1480S261001155 |
| 110 | + m = re.search(r"(\d{3,4})\s*\*\s*(\d{2,5})", sn) |
| 111 | + if m: |
| 112 | + w, h = int(m.group(1)), int(m.group(2)) |
| 113 | + if w >= 100 and h >= 100: |
| 114 | + logger.info(f"Detected hardware panel geometry: {w}x{h}") |
| 115 | + self.display_width = w |
| 116 | + self.display_height = h |
| 117 | + with self._canvas_lock: |
| 118 | + self._canvas = Image.new("RGB", (self.display_width, self.display_height), (0, 0, 0)) |
| 119 | + |
| 120 | + def Reset(self): |
| 121 | + with self._serial_lock: |
| 122 | + self.serial_flush_input() |
| 123 | + self.Clear() |
| 124 | + |
| 125 | + def Clear(self): |
| 126 | + with self._canvas_lock: |
| 127 | + self._canvas = Image.new("RGB", (self.display_width, self.display_height), (0, 0, 0)) |
| 128 | + self._enqueue_render() |
| 129 | + |
| 130 | + def ScreenOff(self): |
| 131 | + self.SetBrightness(0) |
| 132 | + |
| 133 | + def ScreenOn(self): |
| 134 | + self.SetBrightness(self.brightness if self.brightness > 0 else 100) |
| 135 | + |
| 136 | + def SetBrightness(self, level: int): |
| 137 | + self.brightness = max(0, min(100, int(level))) |
| 138 | + cmd = bytes([0xAA, 0xBB, self.brightness, 0xCC, 0xDD]) |
| 139 | + if self.update_queue: |
| 140 | + with self.update_queue_mutex: |
| 141 | + self.update_queue.put((self._send_brightness_direct, [cmd])) |
| 142 | + else: |
| 143 | + self._send_brightness_direct(cmd) |
| 144 | + |
| 145 | + def _send_brightness_direct(self, cmd: bytes): |
| 146 | + with self._serial_lock: |
| 147 | + try: |
| 148 | + if self.lcd_serial and self.lcd_serial.is_open: |
| 149 | + self.serial_write(cmd) |
| 150 | + except Exception as e: |
| 151 | + logger.warning(f"Failed to send brightness command: {e}") |
| 152 | + |
| 153 | + def SetOrientation(self, orientation: Orientation): |
| 154 | + self.orientation = orientation |
| 155 | + with self._canvas_lock: |
| 156 | + target_w = self.get_width() |
| 157 | + target_h = self.get_height() |
| 158 | + self._canvas = Image.new("RGB", (target_w, target_h), (0, 0, 0)) |
| 159 | + |
| 160 | + def DisplayPILImage( |
| 161 | + self, |
| 162 | + image: Image.Image, |
| 163 | + x: int = 0, y: int = 0, |
| 164 | + image_width: int = 0, |
| 165 | + image_height: int = 0 |
| 166 | + ): |
| 167 | + if image_width != 0 and image_height != 0 and (image.size[0] != image_width or image.size[1] != image_height): |
| 168 | + image = image.resize((image_width, image_height)) |
| 169 | + |
| 170 | + target_w = self.get_width() |
| 171 | + target_h = self.get_height() |
| 172 | + |
| 173 | + with self._canvas_lock: |
| 174 | + if self._canvas.size != (target_w, target_h): |
| 175 | + self._canvas = Image.new("RGB", (target_w, target_h), (0, 0, 0)) |
| 176 | + |
| 177 | + if x == 0 and y == 0 and image.size == (target_w, target_h): |
| 178 | + self._canvas.paste(image, (0, 0)) |
| 179 | + else: |
| 180 | + self._canvas.paste(image, (x, y)) |
| 181 | + |
| 182 | + self._enqueue_render() |
| 183 | + |
| 184 | + def _enqueue_render(self): |
| 185 | + if self.update_queue: |
| 186 | + with self.update_queue_mutex: |
| 187 | + if not self._frame_pending: |
| 188 | + self._frame_pending = True |
| 189 | + self.update_queue.put((self._render_and_send_frame, [])) |
| 190 | + else: |
| 191 | + self._render_and_send_frame() |
| 192 | + |
| 193 | + def _render_and_send_frame(self): |
| 194 | + with self.update_queue_mutex: |
| 195 | + self._frame_pending = False |
| 196 | + |
| 197 | + with self._canvas_lock: |
| 198 | + frame_to_send = self._orient_image(self._canvas.copy()) |
| 199 | + |
| 200 | + if frame_to_send.mode != "RGB": |
| 201 | + frame_to_send = frame_to_send.convert("RGB") |
| 202 | + |
| 203 | + buf = io.BytesIO() |
| 204 | + frame_to_send.save(buf, format="JPEG", quality=88) |
| 205 | + jpeg_bytes = buf.getvalue() |
| 206 | + |
| 207 | + with self._serial_lock: |
| 208 | + try: |
| 209 | + if self.lcd_serial and self.lcd_serial.is_open: |
| 210 | + self.serial_write(jpeg_bytes) |
| 211 | + self.lcd_serial.flush() |
| 212 | + except Exception as e: |
| 213 | + logger.warning(f"Failed to send frame to HL-VMAX: {e}") |
| 214 | + |
| 215 | + def _orient_image(self, img: Image.Image) -> Image.Image: |
| 216 | + """Transforms logical image to match physical portrait panel (width x height).""" |
| 217 | + if self.orientation == Orientation.PORTRAIT: |
| 218 | + if img.size != (self.display_width, self.display_height): |
| 219 | + img = img.resize((self.display_width, self.display_height)) |
| 220 | + return img |
| 221 | + elif self.orientation == Orientation.REVERSE_PORTRAIT: |
| 222 | + if img.size != (self.display_width, self.display_height): |
| 223 | + img = img.resize((self.display_width, self.display_height)) |
| 224 | + return img.rotate(180) |
| 225 | + elif self.orientation == Orientation.LANDSCAPE: |
| 226 | + if img.size != (self.display_height, self.display_width): |
| 227 | + img = img.resize((self.display_height, self.display_width)) |
| 228 | + return img.rotate(90, expand=True) |
| 229 | + elif self.orientation == Orientation.REVERSE_LANDSCAPE: |
| 230 | + if img.size != (self.display_height, self.display_width): |
| 231 | + img = img.resize((self.display_height, self.display_width)) |
| 232 | + return img.rotate(270, expand=True) |
| 233 | + return img |
0 commit comments