From 3690e7b093975d49eaa3339e2db9209ec595f9c5 Mon Sep 17 00:00:00 2001 From: rdagger Date: Thu, 4 May 2023 15:14:52 -0700 Subject: [PATCH] Modify clear code for memory allocation. --- demo_clear.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ ili9341.py | 22 ++++++++++++++----- 2 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 demo_clear.py diff --git a/demo_clear.py b/demo_clear.py new file mode 100644 index 0000000..2d4b21a --- /dev/null +++ b/demo_clear.py @@ -0,0 +1,61 @@ +"""ILI9341 demo (clear).""" +from time import sleep, ticks_ms +from ili9341 import Display, color565 +from machine import Pin, SPI +import gc + +valid_hlines = [1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 160] + +colors = { + "RED": (255, 0, 0), + "GREEN": (0, 255, 0), + "BLUE": (0, 0, 255), + "YELLOW": (255, 255, 0), + "AQUA": (0, 255, 255), + "MAROON": (128, 0, 0), + "DARKGREEN": (0, 128, 0), + "NAVY": (0, 0, 128), + "TEAL": (0, 128, 128), + "PURPLE": (128, 0, 128), + "ORANGE": (255, 128, 0), + "DEEP_PINK": (255, 0, 128), + "CYAN": (128, 255, 255), +} + +def test(): + """Test code.""" + # Baud rate of 40000000 seems about the max + spi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13)) + display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17)) + + print('Clearing to black...') + start = ticks_ms() + display.clear() + end = ticks_ms() + print(f'Display cleared in {end - start} ms.') + sleep(2) + + print('Clearing to white...') + start = ticks_ms() + display.clear(color565(255, 255, 255)) + end = ticks_ms() + print(f'Display cleared in {end - start} ms.') + sleep(2) + + for hlines, (color, rgb) in zip(valid_hlines, colors.items()): + gc.collect() + print(f'Clearing display to {color}, hlines={hlines}') + try: + start = ticks_ms() + display.clear(hlines=hlines, color=color565(*rgb)) + end = ticks_ms() + print(f'Display cleared in {end - start} ms.') + except Exception as e: + print(e) + sleep(1) + + sleep(5) + display.cleanup() + + +test() diff --git a/ili9341.py b/ili9341.py index e9a504d..b81dd37 100644 --- a/ili9341.py +++ b/ili9341.py @@ -182,21 +182,31 @@ class Display(object): self.spi.deinit() print('display off') - def clear(self, color=0): + def clear(self, color=0, hlines=8): """Clear display. Args: color (Optional int): RGB565 color value (Default: 0 = Black). + hlines (Optional int): # of horizontal lines per chunk (Default: 8) + Note: + hlines was introduced to deal with memory allocation on some + boards. Smaller values allocate less memory but take longer + to execute. hlines must be a factor of the display height. + For example, for a 240 pixel height, valid values for hline + would be 1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 160. + Higher values may result in memory allocation errors. """ w = self.width h = self.height - # Clear display in 1024 byte blocks + assert hlines > 0 and h % hlines == 0, ( + "hlines must be a non-zero factor of height.") + # Clear display if color: - line = color.to_bytes(2, 'big') * (w * 8) + line = color.to_bytes(2, 'big') * (w * hlines) else: - line = bytearray(w * 16) - for y in range(0, h, 8): - self.block(0, y, w - 1, y + 7, line) + line = bytearray(w * 2 * hlines) + for y in range(0, h, hlines): + self.block(0, y, w - 1, y + hlines - 1, line) def display_off(self): """Turn display off."""