Skip to content
Snippets Groups Projects
Commit 7d98182e authored by koalo's avatar koalo
Browse files

Reset confirmation

parent ccbb0da1
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,9 @@ import os ...@@ -12,6 +12,9 @@ import os
FACTORY_RESET_CMD = "! RESET !" FACTORY_RESET_CMD = "! RESET !"
STATE_MAIN_MENU = "MAIN"
STATE_RESET = "RESET"
STATE_RESET_ARMED = "RESET_ARMED"
def list_apps(): def list_apps():
...@@ -24,11 +27,29 @@ def list_apps(): ...@@ -24,11 +27,29 @@ def list_apps():
if "menu.py" in apps: if "menu.py" in apps:
apps.remove("menu.py") apps.remove("menu.py")
apps.append(FACTORY_RESET_CMD)
return apps return apps
def list_extra_entries():
return [FACTORY_RESET_CMD]
def activate_menu_entry(entryy):
if entryy == FACTORY_RESET_CMD:
return STATE_RESET
disp.clear().update()
disp.close()
try:
os.exec(entryy)
except OSError as e:
print("Loading failed: ", e)
os.exit(1)
return STATE_MAIN_MENU
def button_events(): def button_events():
"""Iterate over button presses (event-loop).""" """Iterate over button presses (event-loop)."""
yield 0 yield 0
...@@ -55,13 +76,13 @@ def button_events(): ...@@ -55,13 +76,13 @@ def button_events():
COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE) COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE)
def draw_menu(disp, applist, idx, offset): def draw_menu(disp, menulist, idx, offset):
disp.clear() disp.clear()
# Wrap around the app-list and draw entries from idx - 3 to idx + 4 # Wrap around the app-list and draw entries from idx - 3 to idx + 4
for y, i in enumerate(range(len(applist) + idx - 3, len(applist) + idx + 4)): for y, i in enumerate(range(len(menulist) + idx - 3, len(menulist) + idx + 4)):
disp.print( disp.print(
" " + applist[i % len(applist)] + " ", " " + menulist[i % len(menulist)] + " ",
posy=offset + y * 20 - 40, posy=offset + y * 20 - 40,
bg=COLOR1 if i % 2 == 0 else COLOR2, bg=COLOR1 if i % 2 == 0 else COLOR2,
) )
...@@ -70,38 +91,61 @@ def draw_menu(disp, applist, idx, offset): ...@@ -70,38 +91,61 @@ def draw_menu(disp, applist, idx, offset):
disp.update() disp.update()
def draw_reset(disp, armed):
disp.clear()
button = "LEFT" if not armed else "RIGHT"
disp.print("Press %s"%button, posy=0)
disp.print("to perform", posy=20)
disp.print("factory", posy=40)
disp.print("reset", posy=60)
disp.update()
def main(): def main():
disp = display.open() disp = display.open()
applist = list_apps() menulist = list_apps()
numapps = len(applist) menulist += list_extra_entries()
numentries = len(menulist)
current = 0 current = 0
state = STATE_MAIN_MENU
for ev in button_events(): for ev in button_events():
# Button handling
if state == STATE_MAIN_MENU:
if ev == buttons.BOTTOM_RIGHT: if ev == buttons.BOTTOM_RIGHT:
# Scroll down # Scroll down
draw_menu(disp, applist, current, -8) draw_menu(disp, menulist, current, -8)
current = (current + 1) % numapps current = (current + 1) % numentries
elif ev == buttons.BOTTOM_LEFT: elif ev == buttons.BOTTOM_LEFT:
# Scroll up # Scroll up
draw_menu(disp, applist, current, 8) draw_menu(disp, menulist, current, 8)
current = (current + numapps - 1) % numapps current = (current + numentries - 1) % numentries
elif ev == buttons.TOP_RIGHT: elif ev == buttons.TOP_RIGHT:
# Select & start # Select & start
disp.clear().update() state = activate_menu_entry(menulist[current])
disp.close() elif state == STATE_RESET:
if ev == buttons.BOTTOM_LEFT:
if applist[current] == FACTORY_RESET_CMD: state = STATE_RESET_ARMED
else:
state = STATE_MAIN_MENU
elif state == STATE_RESET_ARMED:
if ev == buttons.BOTTOM_RIGHT:
files = os.listdir(".") files = os.listdir(".")
for f in files: for f in files:
os.unlink(f) os.unlink(f)
os.exit(0) os.exit(0)
else: else:
try: state = STATE_MAIN_MENU
os.exec(applist[current])
except OSError as e: # Output handling
print("Loading failed: ", e) if state == STATE_MAIN_MENU:
os.exit(1) draw_menu(disp, menulist, current, 0)
elif state == STATE_RESET:
draw_menu(disp, applist, current, 0) draw_reset(disp, False)
elif state == STATE_RESET_ARMED:
draw_reset(disp, True)
if __name__ == "__main__": if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment