Badger2040: Update list app to use new system

This commit is contained in:
Mike Bell 2022-03-26 19:29:24 +00:00
parent 522c83dc19
commit 721da69adc
1 changed files with 61 additions and 106 deletions

View File

@ -1,12 +1,9 @@
import badger2040
import machine
import time
import badger_os
# **** Put your list title and contents here *****
list_title = "Checklist"
list_content = ["Badger", "Badger", "Badger", "Badger", "Badger", "Mushroom", "Mushroom", "Snake"]
list_states = [False] * len(list_content)
list_file = "checklist.txt"
list_items = ["Badger", "Badger", "Badger", "Badger", "Badger", "Mushroom", "Mushroom", "Snake"]
# Global Constants
@ -29,16 +26,6 @@ LIST_WIDTH = WIDTH - LIST_PADDING - LIST_PADDING - ARROW_WIDTH
LIST_HEIGHT = HEIGHT - LIST_START - LIST_PADDING - ARROW_HEIGHT
def save_list():
with open(list_file, "w") as f:
f.write(list_title + "\n")
for i in range(len(list_content)):
list_item = list_content[i]
if list_states[i]:
list_item += " X"
f.write(list_item + "\n")
# ------------------------------
# Drawing functions
# ------------------------------
@ -135,35 +122,40 @@ def draw_checkbox(x, y, size, background, foreground, thickness, tick, padding):
# Program setup
# ------------------------------
changed = not badger2040.woken_by_button()
state = {
"current_item": 0,
}
badger_os.state_load("list", state)
if "items" not in state or state["items"] != list_items:
# Item list changed, or not yet written reset the list
state["current_item"] = 0
state["items"] = list_items
state["checked"] = [False] * len(list_items)
changed = True
# Global variables
update = True
needs_save = False
current_item = 0
items_per_page = 0
# Create a new Badger and set it to update FAST
display = badger2040.Badger2040()
display.led(128)
display.update_speed(badger2040.UPDATE_FAST)
# Set up the buttons
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)
if changed:
display.update_speed(badger2040.UPDATE_FAST)
else:
display.update_speed(badger2040.UPDATE_TURBO)
# Find out what the longest item is
longest_item = 0
for i in range(len(list_content)):
for i in range(len(state["items"])):
while True:
item = list_content[i]
item = state["items"][i]
item_length = display.measure_text(item, ITEM_TEXT_SIZE)
if item_length > 0 and item_length > LIST_WIDTH - ITEM_SPACING:
list_content[i] = item[:-1]
state["items"][i] = item[:-1]
else:
break
longest_item = max(longest_item, display.measure_text(list_content[i], ITEM_TEXT_SIZE))
longest_item = max(longest_item, display.measure_text(state["items"][i], ITEM_TEXT_SIZE))
# And use that to calculate the number of columns we can fit onscreen and how many items that would give
@ -174,78 +166,41 @@ while longest_item + ITEM_SPACING < (LIST_WIDTH // (list_columns + 1)):
items_per_page = ((LIST_HEIGHT // ITEM_SPACING) + 1) * list_columns
# Button handling function
def button(pin):
global update, current_item, needs_save
time.sleep(0.05)
if not pin.value():
return
if button_a.value() and button_c.value():
machine.reset()
if len(list_content) > 0 and not update:
if pin == button_a:
if current_item > 0:
current_item = max(current_item - (items_per_page) // list_columns, 0)
update = True
return
if pin == button_b:
list_states[current_item] = not list_states[current_item]
needs_save = True
update = True
return
if pin == button_c:
if current_item < len(list_content) - 1:
current_item = min(current_item + (items_per_page) // list_columns, len(list_content) - 1)
update = True
return
if pin == button_up:
if current_item > 0:
current_item -= 1
update = True
return
if pin == button_down:
if current_item < len(list_content) - 1:
current_item += 1
update = True
return
# Register the button handling function with the buttons
button_a.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_b.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_c.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_up.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_down.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
# ------------------------------
# Main program loop
# ------------------------------
try:
with open(list_file, "r") as f:
list_content = f.read().strip().split("\n")
list_title = list_content.pop(0)
list_states = [False] * len(list_content)
for i in range(len(list_content)):
list_content[i] = list_content[i].strip()
if list_content[i].endswith(" X"):
list_states[i] = True
list_content[i] = list_content[i][:-2]
except OSError:
save_list()
while True:
if needs_save:
save_list()
needs_save = False
if len(state["items"]) > 0:
if display.pressed(badger2040.BUTTON_A):
if state["current_item"] > 0:
page = state["current_item"] // items_per_page
state["current_item"] = max(state["current_item"] - (items_per_page) // list_columns, 0)
if page != state["current_item"] // items_per_page:
display.update_speed(badger2040.UPDATE_FAST)
changed = True
if display.pressed(badger2040.BUTTON_B):
state["checked"][state["current_item"]] = not state["checked"][state["current_item"]]
changed = True
if display.pressed(badger2040.BUTTON_C):
if state["current_item"] < len(state["items"]) - 1:
page = state["current_item"] // items_per_page
state["current_item"] = min(state["current_item"] + (items_per_page) // list_columns, len(state["items"]) - 1)
if page != state["current_item"] // items_per_page:
display.update_speed(badger2040.UPDATE_FAST)
changed = True
if display.pressed(badger2040.BUTTON_UP):
if state["current_item"] > 0:
state["current_item"] -= 1
changed = True
if display.pressed(badger2040.BUTTON_DOWN):
if state["current_item"] < len(state["items"]) - 1:
state["current_item"] += 1
changed = True
if changed:
badger_os.state_save("list", state)
if update:
display.pen(15)
display.clear()
@ -263,15 +218,15 @@ while True:
display.thickness(2)
display.line(LIST_PADDING, y, WIDTH - LIST_PADDING - ARROW_WIDTH, y)
if len(list_content) > 0:
if len(state["items"]) > 0:
page_item = 0
if items_per_page > 0:
page_item = (current_item // items_per_page) * items_per_page
page_item = (state["current_item"] // items_per_page) * items_per_page
# Draw the list
display.pen(0)
display.thickness(2)
draw_list(list_content, list_states, page_item, current_item, LIST_PADDING, LIST_START,
draw_list(state["items"], state["checked"], page_item, state["current_item"], LIST_PADDING, LIST_START,
LIST_WIDTH, LIST_HEIGHT, ITEM_SPACING, list_columns)
# Draw the interaction button icons
@ -279,26 +234,26 @@ while True:
display.thickness(ARROW_THICKNESS)
# Previous item
if current_item > 0:
if state["current_item"] > 0:
draw_up(WIDTH - ARROW_WIDTH, (HEIGHT // 4) - (ARROW_HEIGHT // 2),
ARROW_WIDTH, ARROW_HEIGHT, ARROW_THICKNESS, ARROW_PADDING)
# Next item
if current_item < (len(list_content) - 1):
if state["current_item"] < (len(state["items"]) - 1):
draw_down(WIDTH - ARROW_WIDTH, ((HEIGHT * 3) // 4) - (ARROW_HEIGHT // 2),
ARROW_WIDTH, ARROW_HEIGHT, ARROW_THICKNESS, ARROW_PADDING)
# Previous column
if current_item > 0:
if state["current_item"] > 0:
draw_left((WIDTH // 7) - (ARROW_WIDTH // 2), HEIGHT - ARROW_HEIGHT,
ARROW_WIDTH, ARROW_HEIGHT, ARROW_THICKNESS, ARROW_PADDING)
# Next column
if current_item < (len(list_content) - 1):
if state["current_item"] < (len(state["items"]) - 1):
draw_right(((WIDTH * 6) // 7) - (ARROW_WIDTH // 2), HEIGHT - ARROW_HEIGHT,
ARROW_WIDTH, ARROW_HEIGHT, ARROW_THICKNESS, ARROW_PADDING)
if list_states[current_item]:
if state["checked"][state["current_item"]]:
# Tick off item
draw_cross((WIDTH // 2) - (ARROW_WIDTH // 2), HEIGHT - ARROW_HEIGHT,
ARROW_HEIGHT, ARROW_HEIGHT, ARROW_THICKNESS, ARROW_PADDING)
@ -314,6 +269,6 @@ while True:
display.update()
display.update_speed(badger2040.UPDATE_TURBO)
update = False
changed = False
time.sleep(0.1)
display.halt()