import gc import os import time from time import sleep, time_ns from machine import SPI, Pin from encoder import Encoder from ili9341 import Display, color565 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), "DARK_GREEN": (0, 128, 0), "NAVY": (0, 0, 128), } menu = [ "Option 1", "Option 2", "Option 3", ] def display(): bg = Pin(33, Pin.OUT) spi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13)) display = Display(spi, dc=Pin(27), cs=Pin(25), rst=Pin(26)) bg.on() encoder_a = Pin(2, Pin.IN, Pin.PULL_UP) encoder_b = Pin(4, Pin.IN, Pin.PULL_UP) def draw_debug_text(text: str, y: int): display.draw_text8x8( 10, y, text, color565(255, 255, 255), color565(0, 0, 0), ) last_registered: int = time_ns() menu_index = 0 def handle_button_pres(dir: str): nonlocal last_registered nonlocal menu_index if (time_ns() - last_registered) < 100000000: return last_registered = time_ns() if dir == "Left": menu_index = (menu_index - 1) % 3 elif dir == "Right": menu_index = (menu_index + 1) % 3 else: return refresh_display() encoder = Encoder( encoder_a, encoder_b, on_left=lambda x: handle_button_pres("Left"), on_right=lambda x: handle_button_pres("Right"), ) display.clear(color=color565(*colors["BLUE"])) display.draw_text8x8( 10, 10, "Menu:", color565(255, 255, 255), color565(0, 0, 0), ) display.draw_text8x8( 230 - (8 * 4), 10, "PPAP", color565(255, 255, 255), color565(0, 0, 0), ) def get_cursor(line: int): nonlocal menu_index if line == menu_index: return "> " else: return " " def refresh_display(): for i, line in enumerate(menu): display.draw_text8x8( 10, 18 + (i + 1) * 8, get_cursor(i) + line, color565(255, 255, 255), color565(0, 0, 0), ) refresh_display()