diff --git a/library/lcd/lcd_comm_rev_c.py b/library/lcd/lcd_comm_rev_c.py index 980514b0..29950529 100644 --- a/library/lcd/lcd_comm_rev_c.py +++ b/library/lcd/lcd_comm_rev_c.py @@ -119,6 +119,9 @@ class SubRevision(Enum): REV_8INCH = "chs_88inch" +WAKE_RETRIES = 15 + + # This class is for Turing Smart Screen 2.1" / 5" / 8" screens class LcdCommRevC(LcdComm): def __init__(self, com_port: str = "AUTO", display_width: int = 480, display_height: int = 800, @@ -132,22 +135,18 @@ def __del__(self): @staticmethod def auto_detect_com_port() -> Optional[str]: - com_ports = comports() - - # First, try to find sleeping device and wake it up - for com_port in com_ports: + # If sleeping device is detected through serial number or vid/pid, try to wake it up + for com_port in comports(): if com_port.serial_number == 'USB7INCH' or com_port.serial_number == 'CT21INCH': LcdCommRevC._wake_up_device(com_port) - return LcdCommRevC.auto_detect_com_port() - if com_port.vid == 0x1a86 and com_port.pid == 0xca21: + elif com_port.vid == 0x1a86 and com_port.pid == 0xca21: LcdCommRevC._wake_up_device(com_port) - return LcdCommRevC.auto_detect_com_port() - return LcdCommRevC._get_awake_com_port(com_ports) + return LcdCommRevC._get_awake_com_port(comports()) @staticmethod def _get_awake_com_port(com_ports) -> Optional[str]: - # Then try to find awake device through serial number or vid/pid + # Try to find awake device through serial number or vid/pid for com_port in com_ports: if com_port.serial_number == '20080411': return com_port.device @@ -160,10 +159,10 @@ def _get_awake_com_port(com_ports) -> Optional[str]: @staticmethod def _wake_up_device(com_port): - # this device enumerates differently when off, we need to connect once to reset it to correct COM device + # Connect to the device to wake it up logger.debug(f"Waiting for device {com_port} to be turned ON...") - for i in range(15): + for i in range(WAKE_RETRIES): try: # Try to connect every second, since it takes sometimes multiple connect to wake up the device serial.Serial(com_port.device, 115200, timeout=1, rtscts=True) @@ -172,9 +171,13 @@ def _wake_up_device(com_port): if LcdCommRevC._get_awake_com_port(comports()) is not None: time.sleep(1) + logger.debug(f"Detected screen turned ON") return + time.sleep(1) + logger.error(f"Could not turn screen on after {WAKE_RETRIES} seconds, aborting.") + def _send_command(self, cmd: Command, payload: Optional[bytearray] = None, padding: Optional[Padding] = None, bypass_queue: bool = False, readsize: Optional[int] = None): message = bytearray() @@ -431,7 +434,7 @@ def _generate_update_image( img_data, pixel_size = image_to_BGRA(image) else: # BGRA mode on 3 bytes: [6-bit B + 2-bit A, 6-bit G + 2-bit A, 8-bit R] - #img_data, pixel_size = image_to_compressed_BGRA(image) + # img_data, pixel_size = image_to_compressed_BGRA(image) # For now use simple BGR that is more optimized, because this program does not support transparent background img_data, pixel_size = image_to_BGR(image)