Skip to content

Commit 64b0437

Browse files
committed
Improve Turing 2.1/5/8.8 by sending correct colors
1 parent f09b6f7 commit 64b0437

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

library/lcd/lcd_comm_rev_c.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from serial.tools.list_ports import comports
3131

3232
from library.lcd.lcd_comm import Orientation, LcdComm
33-
from library.lcd.serialize import image_to_BGRA, image_to_BGR, chunked
33+
from library.lcd.serialize import image_to_BGRA, image_to_compressed_BGRA, chunked
3434
from library.log import logger
3535

3636

@@ -200,7 +200,8 @@ def _hello(self):
200200
# This command reads LCD answer on serial link, so it bypasses the queue
201201
self.sub_revision = SubRevision.UNKNOWN
202202
self._send_command(Command.HELLO, bypass_queue=True)
203-
response = ''.join(filter(lambda x: x in set(string.printable), str(self.serial_read(23).decode(errors="ignore"))))
203+
response = ''.join(
204+
filter(lambda x: x in set(string.printable), str(self.serial_read(23).decode(errors="ignore"))))
204205
self.serial_flush_input()
205206
logger.debug("Display ID returned: %s" % response)
206207

@@ -252,13 +253,13 @@ def Clear(self):
252253
self.SetOrientation(orientation=backup_orientation)
253254

254255
def ScreenOff(self):
255-
logger.info("Calling ScreenOff")
256+
# logger.info("Calling ScreenOff")
256257
self._send_command(Command.STOP_VIDEO)
257258
self._send_command(Command.STOP_MEDIA, readsize=1024)
258259
self._send_command(Command.TURNOFF)
259260

260261
def ScreenOn(self):
261-
logger.info("Calling ScreenOn")
262+
# logger.info("Calling ScreenOn")
262263
self._send_command(Command.STOP_VIDEO)
263264
self._send_command(Command.STOP_MEDIA, readsize=1024)
264265
# self._send_command(Command.SET_BRIGHTNESS, payload=bytearray([255]))
@@ -357,7 +358,7 @@ def _generate_full_image(self, image: Image.Image) -> bytes:
357358
elif self.orientation == Orientation.REVERSE_LANDSCAPE:
358359
image = image.rotate(180)
359360

360-
bgra_data = image_to_BGRA(image)
361+
bgra_data, pixel_size = image_to_BGRA(image)
361362

362363
return b'\x00'.join(chunked(bgra_data, 249))
363364

@@ -397,15 +398,13 @@ def _generate_update_image(
397398

398399
img_raw_data = bytearray()
399400

400-
# Some screens require BGR for update image, some require BGRA
401+
# Some screens require different RGBA encoding
401402
if self.rom_version > 88:
402-
# BGRA mode
403-
img_data = image_to_BGRA(image)
404-
pixel_size = 4
403+
# BGRA mode on 4 bytes : [B, G, R, A]
404+
img_data, pixel_size = image_to_BGRA(image)
405405
else:
406-
# BGR mode
407-
img_data = image_to_BGR(image)
408-
pixel_size = 3
406+
# BGRA mode on 3 bytes: [6-bit B + 2-bit A, 6-bit G + 2-bit A, R]
407+
img_data, pixel_size = image_to_compressed_BGRA(image)
409408

410409
for h, line in enumerate(chunked(img_data, image.width * pixel_size)):
411410
if self.sub_revision == SubRevision.REV_8INCH:

library/lcd/serialize.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def chunked(data: bytes, chunk_size: int) -> Iterator[bytes]:
88
for i in range(0, len(data), chunk_size):
9-
yield data[i : i + chunk_size]
9+
yield data[i: i + chunk_size]
1010

1111

1212
def image_to_RGB565(image: Image.Image, endianness: Literal["big", "little"]) -> bytes:
@@ -39,20 +39,35 @@ def image_to_RGB565(image: Image.Image, endianness: Literal["big", "little"]) ->
3939
return rgb565.astype(typ).tobytes()
4040

4141

42-
def image_to_BGR(image: Image.Image) -> bytes:
42+
def image_to_BGR(image: Image.Image) -> (bytes, int):
4343
if image.mode not in ["RGB", "RGBA"]:
4444
# we need the first 3 channels to be R, G and B
4545
image = image.convert("RGB")
4646
rgb = np.asarray(image)
4747
# same as rgb[:, :, [2, 1, 0]] but faster
4848
bgr = np.take(rgb, (2, 1, 0), axis=-1)
49-
return bgr.tobytes()
49+
return bgr.tobytes(), 3
5050

5151

52-
def image_to_BGRA(image: Image.Image) -> bytes:
52+
def image_to_BGRA(image: Image.Image) -> (bytes, int):
5353
if image.mode != "RGBA":
5454
image = image.convert("RGBA")
5555
rgba = np.asarray(image)
5656
# same as rgba[:, :, [2, 1, 0, 3]] but faster
5757
bgra = np.take(rgba, (2, 1, 0, 3), axis=-1)
58-
return bgra.tobytes()
58+
return bgra.tobytes(), 4
59+
60+
61+
# FIXME: to optimize like other functions above
62+
def image_to_compressed_BGRA(image: Image.Image) -> (bytes, int):
63+
compressed_bgra = bytearray()
64+
image_data = image.convert("RGBA").load()
65+
for h in range(image.height):
66+
for w in range(image.width):
67+
# r = pixel[0], g = pixel[1], b = pixel[2], a = pixel[3]
68+
pixel = image_data[w, h]
69+
a = pixel[3] >> 4
70+
compressed_bgra.append(pixel[2] & 0xFC | a >> 2)
71+
compressed_bgra.append(pixel[1] & 0xFC | a & 2)
72+
compressed_bgra.append(pixel[0])
73+
return bytes(compressed_bgra), 3

0 commit comments

Comments
 (0)