2022-02-22 13:45:11 +00:00
|
|
|
import gc
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import math
|
|
|
|
import machine
|
|
|
|
import badger2040
|
2022-02-23 16:08:06 +00:00
|
|
|
import launchericons
|
2022-02-22 13:45:11 +00:00
|
|
|
|
|
|
|
page = 0
|
2022-02-23 16:08:06 +00:00
|
|
|
font_size = 1
|
|
|
|
|
|
|
|
icons = bytearray(launchericons.data())
|
2022-02-22 13:45:11 +00:00
|
|
|
|
|
|
|
examples = [
|
2022-02-23 16:08:06 +00:00
|
|
|
("clock.py", 0),
|
|
|
|
("fonts.py", 1),
|
|
|
|
("ebook.py", 2),
|
|
|
|
("image.py", 3),
|
|
|
|
("list.py", 4),
|
|
|
|
("badge.py", 5)
|
2022-02-22 13:45:11 +00:00
|
|
|
]
|
|
|
|
|
2022-02-23 16:08:06 +00:00
|
|
|
font_sizes = (0.5, 0.7, 0.9)
|
|
|
|
|
|
|
|
# Approximate center lines for buttons A, B and C
|
|
|
|
centers = (41, 147, 253)
|
|
|
|
|
2022-02-22 13:45:11 +00:00
|
|
|
MAX_PAGE = math.ceil(len(examples) / 3)
|
|
|
|
|
|
|
|
button_a = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN)
|
|
|
|
button_b = machine.Pin(badger2040.BUTTON_B, machine.Pin.IN, machine.Pin.PULL_DOWN)
|
|
|
|
button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN)
|
|
|
|
button_up = machine.Pin(badger2040.BUTTON_UP, machine.Pin.IN, machine.Pin.PULL_DOWN)
|
|
|
|
button_down = machine.Pin(badger2040.BUTTON_DOWN, machine.Pin.IN, machine.Pin.PULL_DOWN)
|
|
|
|
|
2022-02-23 16:08:06 +00:00
|
|
|
# Inverted. For reasons.
|
|
|
|
button_user = machine.Pin(badger2040.BUTTON_USER, machine.Pin.IN, machine.Pin.PULL_UP)
|
2022-02-22 13:45:11 +00:00
|
|
|
|
2022-02-23 16:08:06 +00:00
|
|
|
display = badger2040.Badger2040()
|
|
|
|
display.update_speed(1)
|
2022-02-22 13:45:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render():
|
2022-02-23 16:08:06 +00:00
|
|
|
display.pen(15)
|
|
|
|
display.clear()
|
|
|
|
display.pen(0)
|
|
|
|
display.thickness(2)
|
2022-02-22 14:39:45 +00:00
|
|
|
|
2022-02-22 13:45:11 +00:00
|
|
|
max_icons = min(3, len(examples[(page * 3):]))
|
|
|
|
|
|
|
|
for i in range(max_icons):
|
2022-02-23 16:08:06 +00:00
|
|
|
x = centers[i]
|
|
|
|
label, icon = examples[i + (page * 3)]
|
|
|
|
label = label.replace(".py", "")
|
|
|
|
display.pen(0)
|
|
|
|
display.icon(icons, icon, 384, 64, x - 32, 24)
|
|
|
|
w = display.measure_text(label, font_sizes[font_size])
|
|
|
|
display.text(label, x - int(w / 2), 16 + 80, font_sizes[font_size])
|
2022-02-22 14:39:45 +00:00
|
|
|
|
2022-02-22 13:45:11 +00:00
|
|
|
for i in range(MAX_PAGE):
|
|
|
|
x = 286
|
|
|
|
y = int((128 / 2) - (MAX_PAGE * 10 / 2) + (i * 10))
|
2022-02-23 16:08:06 +00:00
|
|
|
display.pen(0)
|
|
|
|
display.rectangle(x, y, 8, 8)
|
2022-02-22 13:45:11 +00:00
|
|
|
if page != i:
|
2022-02-23 16:08:06 +00:00
|
|
|
display.pen(15)
|
|
|
|
display.rectangle(x + 1, y + 1, 6, 6)
|
2022-02-22 13:45:11 +00:00
|
|
|
|
2022-02-23 16:08:06 +00:00
|
|
|
display.pen(0)
|
|
|
|
|
|
|
|
display.update()
|
2022-02-22 13:45:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def launch(file):
|
|
|
|
for k in locals().keys():
|
|
|
|
if k not in ("gc", "file"):
|
|
|
|
del locals()[k]
|
|
|
|
gc.collect()
|
|
|
|
__import__(file)
|
2022-02-23 16:08:06 +00:00
|
|
|
sys.exit(0)
|
2022-02-22 13:45:11 +00:00
|
|
|
|
2022-02-22 14:39:45 +00:00
|
|
|
|
|
|
|
def launch_example(index):
|
2022-02-22 13:45:11 +00:00
|
|
|
try:
|
2022-02-23 16:08:06 +00:00
|
|
|
launch(examples[(page * 3) + index][0])
|
2022-02-22 14:39:45 +00:00
|
|
|
return True
|
2022-02-22 13:45:11 +00:00
|
|
|
except IndexError:
|
2022-02-22 14:39:45 +00:00
|
|
|
return False
|
2022-02-22 13:45:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def button(pin):
|
2022-02-23 16:08:06 +00:00
|
|
|
global page, font_size
|
|
|
|
if pin == button_user:
|
|
|
|
font_size += 1
|
|
|
|
if font_size == len(font_sizes):
|
|
|
|
font_size = 0
|
|
|
|
render()
|
2022-02-22 13:45:11 +00:00
|
|
|
if pin == button_a:
|
2022-02-22 14:39:45 +00:00
|
|
|
launch_example(0)
|
2022-02-22 13:45:11 +00:00
|
|
|
if pin == button_b:
|
2022-02-22 14:39:45 +00:00
|
|
|
launch_example(1)
|
2022-02-22 13:45:11 +00:00
|
|
|
if pin == button_c:
|
2022-02-22 14:39:45 +00:00
|
|
|
launch_example(2)
|
2022-02-22 13:45:11 +00:00
|
|
|
if pin == button_up:
|
|
|
|
if page > 0:
|
|
|
|
page -= 1
|
|
|
|
render()
|
|
|
|
if pin == button_down:
|
|
|
|
if page < MAX_PAGE - 1:
|
|
|
|
page += 1
|
|
|
|
render()
|
|
|
|
|
|
|
|
|
2022-02-22 20:26:00 +00:00
|
|
|
render()
|
|
|
|
|
|
|
|
# Wait for wakeup button to be released
|
|
|
|
while button_a.value() or button_b.value() or button_c.value() or button_up.value() or button_down.value():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-02-22 13:45:11 +00:00
|
|
|
while True:
|
2022-02-23 16:08:06 +00:00
|
|
|
if button_a.value():
|
|
|
|
button(button_a)
|
|
|
|
if button_b.value():
|
|
|
|
button(button_b)
|
|
|
|
if button_c.value():
|
|
|
|
button(button_c)
|
|
|
|
if button_up.value():
|
|
|
|
button(button_up)
|
|
|
|
if button_down.value():
|
|
|
|
button(button_down)
|
|
|
|
if not button_user.value():
|
|
|
|
button(button_user)
|
|
|
|
|
|
|
|
time.sleep(0.01)
|