From a0020068f53e715cff4960809cb62424c1fcd0b2 Mon Sep 17 00:00:00 2001 From: rdagger Date: Sat, 13 Aug 2022 12:15:19 -0700 Subject: [PATCH] Add support for MicroPython built-in 8x8 font. --- .gitignore | 4 +++- demo_fonts8x8.py | 43 +++++++++++++++++++++++++++++++++++ ili9341.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 demo_fonts8x8.py diff --git a/.gitignore b/.gitignore index 885376a..7a13564 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .flake8 .ftpconfig -.vscode/ \ No newline at end of file +.vscode/ +main.py +ftp.py \ No newline at end of file diff --git a/demo_fonts8x8.py b/demo_fonts8x8.py new file mode 100644 index 0000000..1f913c2 --- /dev/null +++ b/demo_fonts8x8.py @@ -0,0 +1,43 @@ +"""ILI9341 demo demo (fonts 8x8).""" +from time import sleep +from ili9341 import Display, color565 +from machine import Pin, SPI # type: ignore + + +def test(): + """Test code.""" + spi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13)) + display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17)) + + x_center = display.width // 2 + y_center = display.height // 2 + + display.draw_text8x8(0, 0, 'Built-in', color565(255, 0, 255)) + display.draw_text8x8(16, 16, 'MicroPython', color565(255, 255, 0)) + display.draw_text8x8(32, 32, '8x8 Font', color565(0, 0, 255)) + + display.draw_text8x8(x_center - 40, 120, "Rotate = 0", + color565(0, 255, 0)) + display.draw_text8x8(0, y_center - 44, "Rotate = 90", + color565(255, 0, 0), rotate=90) + display.draw_text8x8(x_center - 48, display.height - 9, "Rotate = 180", + color565(0, 255, 255), rotate=180) + display.draw_text8x8(display.width - 9, y_center - 48, "Rotate = 270", + color565(255, 255, 255), rotate=270) + + display.draw_text8x8(x_center - 40, 140, "Rotate = 0", + color565(0, 255, 0), background=color565(255, 0, 0)) + display.draw_text8x8(20, y_center - 44, "Rotate = 90", color565(255, 0, 0), + rotate=90, background=color565(0, 255, 0)) + display.draw_text8x8(x_center - 48, display.height - 29, "Rotate = 180", + color565(0, 255, 255), rotate=180, + background=color565(0, 0, 255)) + display.draw_text8x8(display.width - 29, y_center - 48, "Rotate = 270", + color565(255, 255, 255), rotate=270, + background=color565(255, 0, 255)) + + sleep(15) + display.cleanup() + + +test() diff --git a/ili9341.py b/ili9341.py index 58ca24f..eab78fc 100644 --- a/ili9341.py +++ b/ili9341.py @@ -2,7 +2,8 @@ from time import sleep from math import cos, sin, pi, radians from sys import implementation -import ustruct +from framebuf import FrameBuffer, RGB565 # type: ignore +import ustruct # type: ignore def color565(r, g, b): @@ -559,6 +560,62 @@ class Display(object): # # Position x for next letter # x += w + spacing + def draw_text8x8(self, x, y, text, color, background=0, + rotate=0): + """Draw text using built-in MicroPython 8x8 bit font. + + Args: + x (int): Starting X position. + y (int): Starting Y position. + text (string): Text to draw. + color (int): RGB565 color value. + background (int): RGB565 background color (default: black). + rotate(int): 0, 90, 180, 270 + """ + w = len(text) * 8 + h = 8 + # Confirm coordinates in boundary + if self.is_off_grid(x, y, x + 7, y + 7): + return + # Rearrange color + r = (color & 0xF800) >> 8 + g = (color & 0x07E0) >> 3 + b = (color & 0x1F) << 3 + buf = bytearray(w * 16) + fbuf = FrameBuffer(buf, w, h, RGB565) + if background != 0: + bg_r = (background & 0xF800) >> 8 + bg_g = (background & 0x07E0) >> 3 + bg_b = (background & 0x1F) << 3 + fbuf.fill(color565(bg_b, bg_r, bg_g)) + fbuf.text(text, 0, 0, color565(b, r, g)) + if rotate == 0: + self.block(x, y, x + w - 1, y + (h - 1), buf) + elif rotate == 90: + buf2 = bytearray(w * 16) + fbuf2 = FrameBuffer(buf2, h, w, RGB565) + for y1 in range(h): + for x1 in range(w): + fbuf2.pixel(y1, x1, + fbuf.pixel(x1, (h - 1) - y1)) + self.block(x, y, x + (h - 1), y + w - 1, buf2) + elif rotate == 180: + buf2 = bytearray(w * 16) + fbuf2 = FrameBuffer(buf2, w, h, RGB565) + for y1 in range(h): + for x1 in range(w): + fbuf2.pixel(x1, y1, + fbuf.pixel((w - 1) - x1, (h - 1) - y1)) + self.block(x, y, x + w - 1, y + (h - 1), buf2) + elif rotate == 270: + buf2 = bytearray(w * 16) + fbuf2 = FrameBuffer(buf2, h, w, RGB565) + for y1 in range(h): + for x1 in range(w): + fbuf2.pixel(y1, x1, + fbuf.pixel((w - 1) - x1, y1)) + self.block(x, y, x + (h - 1), y + w - 1, buf2) + def draw_vline(self, x, y, h, color): """Draw a vertical line.