Skip to content

Commit f09b6f7

Browse files
committed
Improve Turing Rev.C ROM version detection
1 parent 32a4fdb commit f09b6f7

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

library/lcd/lcd_comm_rev_c.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,11 @@ class Command(Enum):
9292
NO_FLIP = bytearray((0x00,))
9393
SEND_PAYLOAD = bytearray((0xFF,))
9494

95-
def __init__(self, command):
96-
self.command = command
97-
9895

9996
class Padding(Enum):
10097
NULL = bytearray([0x00])
10198
START_DISPLAY_BITMAP = bytearray([0x2c])
10299

103-
def __init__(self, command):
104-
self.command = command
105-
106100

107101
class SleepInterval(Enum):
108102
OFF = bytearray((0x00,))
@@ -117,19 +111,12 @@ class SleepInterval(Enum):
117111
NINE = bytearray((0x09,))
118112
TEN = bytearray((0x0a,))
119113

120-
def __init__(self, command):
121-
self.command = command
122-
123114

124115
class SubRevision(Enum):
125116
UNKNOWN = ""
126117
REV_2INCH = "chs_21inch"
127118
REV_5INCH = "chs_5inch"
128-
REV_8INCH_V88 = "chs_88inch.dev1_rom1.88"
129-
REV_8INCH_V90 = "chs_88inch.dev1_rom1.90"
130-
131-
def __init__(self, command):
132-
self.command = command
119+
REV_8INCH = "chs_88inch"
133120

134121

135122
# This class is for Turing Smart Screen 2.1" / 5" / 8" screens
@@ -213,27 +200,32 @@ def _hello(self):
213200
# This command reads LCD answer on serial link, so it bypasses the queue
214201
self.sub_revision = SubRevision.UNKNOWN
215202
self._send_command(Command.HELLO, bypass_queue=True)
216-
response = str(self.serial_read(23).decode(errors="ignore"))
203+
response = ''.join(filter(lambda x: x in set(string.printable), str(self.serial_read(23).decode(errors="ignore"))))
217204
self.serial_flush_input()
218-
logger.debug("HW sub-revision returned: %s" % ''.join(filter(lambda x: x in set(string.printable), response)))
205+
logger.debug("Display ID returned: %s" % response)
219206

220-
# Note: sub-revisions returned by display are not reliable for some models e.g. 2.1" displays return "chs_5inch"
221-
# Relay mainly on width/height for sub-revision detection, except for 8.8" where ROM version matters
207+
# Note: ID returned by display are not reliable for some models e.g. 2.1" displays return "chs_5inch"
208+
# Rely on width/height for sub-revision detection
222209
if self.display_width == 480 and self.display_height == 480:
223210
self.sub_revision = SubRevision.REV_2INCH
224211
elif self.display_width == 480 and self.display_height == 800:
225212
self.sub_revision = SubRevision.REV_5INCH
226213
elif self.display_width == 480 and self.display_height == 1920:
227-
if response.startswith(SubRevision.REV_8INCH_V88.value):
228-
self.sub_revision = SubRevision.REV_8INCH_V88
229-
elif response.startswith(SubRevision.REV_8INCH_V90.value):
230-
self.sub_revision = SubRevision.REV_8INCH_V90
231-
else:
232-
logger.warning("Display returned unknown sub-revision on Hello answer (%s)" % str(response))
214+
self.sub_revision = SubRevision.REV_8INCH
233215
else:
234216
logger.error(f"Unsupported resolution {self.display_width}x{self.display_height} for revision C")
235217

236-
logger.debug("HW sub-revision detected: %s" % (str(self.sub_revision)))
218+
# Detect ROM version
219+
try:
220+
self.rom_version = int(response.split(".")[2])
221+
if self.rom_version < 80 or self.rom_version > 100:
222+
logger.warning("ROM version %d may be invalid, use default ROM version 87" % self.rom_version)
223+
self.rom_version = 87
224+
except:
225+
logger.warning("Display returned invalid or unsupported ID on Hello answer, use default ROM version 87")
226+
self.rom_version = 87
227+
228+
logger.debug("HW sub-revision detected: %s, ROM version: %d" % ((str(self.sub_revision)), self.rom_version))
237229

238230
def InitializeComm(self):
239231
self._hello()
@@ -328,7 +320,7 @@ def DisplayPILImage(
328320
display_bmp_cmd = Command.DISPLAY_BITMAP_5INCH
329321
elif self.sub_revision == SubRevision.REV_2INCH:
330322
display_bmp_cmd = Command.DISPLAY_BITMAP_2INCH
331-
elif self.sub_revision == SubRevision.REV_8INCH_V88 or self.sub_revision == SubRevision.REV_8INCH_V90:
323+
elif self.sub_revision == SubRevision.REV_8INCH:
332324
display_bmp_cmd = Command.DISPLAY_BITMAP_8INCH
333325

334326
self._send_command(display_bmp_cmd,
@@ -347,7 +339,8 @@ def DisplayPILImage(
347339
Count.Start += 1
348340

349341
def _generate_full_image(self, image: Image.Image) -> bytes:
350-
if self.sub_revision == SubRevision.REV_8INCH_V88 or self.sub_revision == SubRevision.REV_8INCH_V90:
342+
if self.sub_revision == SubRevision.REV_8INCH:
343+
# Switch landscape/portrait mode for 8"
351344
if self.orientation == Orientation.LANDSCAPE:
352345
image = image.rotate(270, expand=True)
353346
elif self.orientation == Orientation.REVERSE_LANDSCAPE:
@@ -372,7 +365,8 @@ def _generate_update_image(
372365
self, image: Image.Image, x: int, y: int, count: int, cmd: Optional[Command] = None
373366
) -> Tuple[bytearray, bytearray]:
374367
x0, y0 = x, y
375-
if self.sub_revision == SubRevision.REV_8INCH_V88 or self.sub_revision == SubRevision.REV_8INCH_V90:
368+
if self.sub_revision == SubRevision.REV_8INCH:
369+
# Switch landscape/portrait mode for 8"
376370
if self.orientation == Orientation.LANDSCAPE:
377371
image = image.rotate(270, expand=True)
378372
y0 = self.get_height() - y - image.width
@@ -404,7 +398,7 @@ def _generate_update_image(
404398
img_raw_data = bytearray()
405399

406400
# Some screens require BGR for update image, some require BGRA
407-
if self.sub_revision == SubRevision.REV_8INCH_V90:
401+
if self.rom_version > 88:
408402
# BGRA mode
409403
img_data = image_to_BGRA(image)
410404
pixel_size = 4
@@ -414,7 +408,8 @@ def _generate_update_image(
414408
pixel_size = 3
415409

416410
for h, line in enumerate(chunked(img_data, image.width * pixel_size)):
417-
if self.sub_revision == SubRevision.REV_8INCH_V88 or self.sub_revision == SubRevision.REV_8INCH_V90:
411+
if self.sub_revision == SubRevision.REV_8INCH:
412+
# Switch landscape/portrait mode for 8"
418413
img_raw_data += int(((x0 + h) * self.display_width) + y0).to_bytes(3, "big")
419414
else:
420415
img_raw_data += int(((x0 + h) * self.display_height) + y0).to_bytes(3, "big")

0 commit comments

Comments
 (0)