feat: menu demo
This commit is contained in:
@@ -5,6 +5,7 @@ from time import sleep, time_ns
|
||||
|
||||
from machine import SPI, Pin
|
||||
|
||||
from encoder import Encoder
|
||||
from ili9341 import Display, color565
|
||||
|
||||
colors = {
|
||||
@@ -18,6 +19,12 @@ colors = {
|
||||
"NAVY": (0, 0, 128),
|
||||
}
|
||||
|
||||
menu = [
|
||||
"Option 1",
|
||||
"Option 2",
|
||||
"Option 3",
|
||||
]
|
||||
|
||||
|
||||
def display():
|
||||
bg = Pin(33, Pin.OUT)
|
||||
@@ -26,70 +33,80 @@ def display():
|
||||
|
||||
bg.on()
|
||||
|
||||
while True:
|
||||
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,
|
||||
10,
|
||||
"ILI9341 Display Test",
|
||||
y,
|
||||
text,
|
||||
color565(255, 255, 255),
|
||||
color565(0, 0, 0),
|
||||
270,
|
||||
)
|
||||
display.draw_text8x8(
|
||||
10,
|
||||
280,
|
||||
"PPAP",
|
||||
color565(255, 255, 255),
|
||||
color565(0, 0, 0),
|
||||
270,
|
||||
)
|
||||
sleep(1)
|
||||
# Calculate valid hlines parameters for display clear method
|
||||
valid_hlines = []
|
||||
for i in range(1, display.height):
|
||||
if display.height % i == 0:
|
||||
valid_hlines.append(i)
|
||||
# Ensure only 13 entries, truncate or repeat the last one
|
||||
valid_hlines = valid_hlines[:8]
|
||||
if len(valid_hlines) < 8:
|
||||
valid_hlines += [valid_hlines[-1]] * (8 - len(valid_hlines))
|
||||
# Ensure only 13 entries, truncate or repeat the last one
|
||||
valid_hlines = valid_hlines[:8]
|
||||
if len(valid_hlines) < 8:
|
||||
valid_hlines += [valid_hlines[-1]] * (8 - len(valid_hlines))
|
||||
|
||||
print("Clearing to black...")
|
||||
start = time_ns()
|
||||
display.clear()
|
||||
end = time_ns()
|
||||
print(f"Display cleared in {end - start} ms.")
|
||||
sleep(2)
|
||||
last_registered: int = time_ns()
|
||||
|
||||
print("Clearing to white...")
|
||||
start = time_ns()
|
||||
display.clear(color565(255, 255, 255))
|
||||
end = time_ns()
|
||||
print(f"Display cleared in {end - start} ms.")
|
||||
sleep(2)
|
||||
menu_index = 0
|
||||
|
||||
for hlines, (color, rgb) in zip(valid_hlines, colors.items()):
|
||||
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,
|
||||
10,
|
||||
f"Next color: {color}",
|
||||
18 + (i + 1) * 8,
|
||||
get_cursor(i) + line,
|
||||
color565(255, 255, 255),
|
||||
color565(0, 0, 0),
|
||||
270,
|
||||
)
|
||||
sleep(1)
|
||||
print(f"Clearing display to {color}, hlines={hlines}...")
|
||||
try:
|
||||
start = time_ns()
|
||||
display.clear(hlines=hlines, color=color565(*rgb))
|
||||
end = time_ns()
|
||||
print(f"Display cleared in {end - start} ms.")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
sleep(1)
|
||||
|
||||
sleep(5)
|
||||
refresh_display()
|
||||
|
||||
Reference in New Issue
Block a user