Created
September 27, 2023 20:30
-
-
Save jepler/997be7787fa569ff5b6804983135e4f8 to your computer and use it in GitHub Desktop.
Convert Arduino_GFX style TFT initialization strings to CircuitPython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
BEGIN_WRITE, | |
WRITE_COMMAND_8, | |
WRITE_COMMAND_16, | |
WRITE_DATA_8, | |
WRITE_DATA_16, | |
WRITE_BYTES, | |
WRITE_C8_D8, | |
WRITE_C8_D16, | |
WRITE_C16_D16, | |
END_WRITE, | |
DELAY, | |
) = range(11) | |
class Encoder: | |
def __init__(self): | |
self.content = bytearray() | |
self.reset() | |
def command(self, command): | |
if self.pending_command is not None: | |
self.flush() | |
self.pending_command = command | |
self.pending_data = bytearray() | |
def data(self, data): | |
self.pending_data.append(data) | |
def delay(self, value): | |
self.pending_delay = value | |
def flush(self): | |
self.content.append(self.pending_command) | |
len_and_delay = len(self.pending_data) | (bool(self.pending_delay) << 7) | |
self.content.append(len_and_delay) | |
self.content.extend(self.pending_data) | |
if self.pending_delay: | |
self.content.append(self.pending_delay) | |
print(f" {bytes(self.content)}") | |
self.reset() | |
def reset(self): | |
self.pending_command = None | |
self.pending_data = bytearray() | |
self.pending_delay = 0 | |
self.content = bytearray() | |
def translate_init_operations(*initcode): | |
initcode = iter(initcode) | |
encoder = Encoder() | |
print("init_code = bytes((") | |
for op in initcode: | |
if op in (BEGIN_WRITE, END_WRITE): | |
continue | |
elif op == WRITE_COMMAND_8: | |
encoder.command(next(initcode)) | |
elif op == WRITE_C8_D8: | |
encoder.command(next(initcode)) | |
encoder.data(next(initcode)) | |
elif op == WRITE_C8_D16: | |
encoder.command(next(initcode)) | |
encoder.data(next(initcode)) | |
encoder.data(next(initcode)) | |
elif op == WRITE_BYTES: | |
for _ in range(next(initcode)): | |
encoder.data(next(initcode)) | |
elif op == DELAY: | |
encoder.delay(next(initcode)) | |
else: | |
raise ValueError(f"Invalid operation 0x{op:02x}") | |
encoder.flush() | |
print("))") | |
""" | |
from convert_initcode import * | |
translate_init_operations( | |
WRITE_COMMAND_8, 0xFF, | |
WRITE_BYTES, 5, 0x77, 0x01, 0x00, 0x00, 0x13, | |
WRITE_C8_D8, 0xEF, 0x08, | |
... | |
) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment