Skip to content

Commit d4ded9f

Browse files
committed
Use numpy for rev C
1 parent be8e26c commit d4ded9f

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

library/lcd/lcd_comm_rev_c.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from serial.tools.list_ports import comports
3030

3131
from library.lcd.lcd_comm import Orientation, LcdComm
32+
from library.lcd.serialize import image_to_BGRA, image_to_BGR, chunked
3233
from library.log import logger
3334

3435

@@ -315,15 +316,9 @@ def _generate_full_image(self, image: Image.Image) -> bytes:
315316
elif self.orientation == Orientation.REVERSE_LANDSCAPE:
316317
image = image.rotate(180)
317318

318-
image_data = image.convert("RGBA").load()
319-
image_ret = ''
320-
for y in range(image.height):
321-
for x in range(image.width):
322-
pixel = image_data[x, y]
323-
image_ret += f'{pixel[2]:02x}{pixel[1]:02x}{pixel[0]:02x}{pixel[3]:02x}'
319+
bgra_data = image_to_BGRA(image)
324320

325-
hex_data = bytearray.fromhex(image_ret)
326-
return b'\x00'.join(hex_data[i:i + 249] for i in range(0, len(hex_data), 249))
321+
return b'\x00'.join(chunked(bgra_data, 249))
327322

328323
def _generate_update_image(
329324
self, image: Image.Image, x: int, y: int, count: int, cmd: Optional[Command] = None
@@ -343,28 +338,26 @@ def _generate_update_image(
343338
elif self.orientation == Orientation.LANDSCAPE:
344339
x0, y0 = y, x
345340

346-
img_raw_data = []
347-
image_data = image.convert("RGBA").load()
348-
for h in range(image.height):
349-
img_raw_data.append(f'{((x0 + h) * self.display_height) + y0:06x}{image.width:04x}')
350-
for w in range(image.width):
351-
current_pixel = image_data[w, h]
352-
img_raw_data.append(f'{current_pixel[2]:02x}{current_pixel[1]:02x}{current_pixel[0]:02x}')
341+
img_raw_data = bytearray()
342+
bgr_data = image_to_BGR(image)
343+
for h, line in enumerate(chunked(bgr_data, image.width * 3)):
344+
img_raw_data += int(((x0 + h) * self.display_height) + y0).to_bytes(3, "big")
345+
img_raw_data += int(image.width).to_bytes(2, "big")
346+
img_raw_data += line
353347

354-
image_msg = ''.join(img_raw_data)
355-
image_size = f'{int((len(image_msg) / 2) + 2):06x}' # The +2 is for the "ef69" that will be added later.
348+
image_size = int(len(img_raw_data) + 2).to_bytes(3, "big") # The +2 is for the "ef69" that will be added later.
356349

357350
# logger.debug("Render Count: {}".format(count))
358351
payload = bytearray()
359352

360353
if cmd:
361354
payload.extend(cmd.value)
362-
payload.extend(bytearray.fromhex(image_size))
355+
payload.extend(image_size)
363356
payload.extend(Padding.NULL.value * 3)
364357
payload.extend(count.to_bytes(4, 'big'))
365358

366-
if len(image_msg) > 500:
367-
image_msg = '00'.join(image_msg[i:i + 498] for i in range(0, len(image_msg), 498))
368-
image_msg += 'ef69'
359+
if len(img_raw_data) > 250:
360+
img_raw_data = bytearray(b'\x00').join(chunked(bytes(img_raw_data), 249))
361+
img_raw_data += b'\xef\x69'
369362

370-
return bytearray.fromhex(image_msg), payload
363+
return img_raw_data, payload

library/lcd/serialize.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ def image_to_RGB565(image: Image.Image, endianness: Literal["big", "little"]) ->
3737
else:
3838
typ = "<u2"
3939
return rgb565.astype(typ).tobytes()
40+
41+
42+
def image_to_BGR(image: Image.Image) -> bytes:
43+
if image.mode not in ["RGB", "RGBA"]:
44+
# we need the first 3 channels to be R, G and B
45+
image = image.convert("RGB")
46+
rgb = np.asarray(image)
47+
# same as rgb[:, :, [2, 1, 0]] but faster
48+
bgr = np.take(rgb, (2, 1, 0), axis=-1)
49+
return bgr.tobytes()
50+
51+
52+
def image_to_BGRA(image: Image.Image) -> bytes:
53+
if image.mode != "RGBA":
54+
image = image.convert("RGBA")
55+
rgba = np.asarray(image)
56+
# same as rgba[:, :, [2, 1, 0, 3]] but faster
57+
bgra = np.take(rgba, (2, 1, 0, 3), axis=-1)
58+
return bgra.tobytes()

0 commit comments

Comments
 (0)