Commit 7d98182e authored by koalo's avatar koalo

Reset confirmation

parent ccbb0da1
......@@ -12,6 +12,9 @@ import os
FACTORY_RESET_CMD = "! RESET !"
STATE_MAIN_MENU = "MAIN"
STATE_RESET = "RESET"
STATE_RESET_ARMED = "RESET_ARMED"
def list_apps():
......@@ -24,11 +27,29 @@ def list_apps():
if "menu.py" in apps:
apps.remove("menu.py")
apps.append(FACTORY_RESET_CMD)
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():
"""Iterate over button presses (event-loop)."""
yield 0
......@@ -55,13 +76,13 @@ def button_events():
COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE)
def draw_menu(disp, applist, idx, offset):
def draw_menu(disp, menulist, idx, offset):
disp.clear()
# 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(
" " + applist[i % len(applist)] + " ",
" " + menulist[i % len(menulist)] + " ",
posy=offset + y * 20 - 40,
bg=COLOR1 if i % 2 == 0 else COLOR2,
)
......@@ -70,38 +91,61 @@ def draw_menu(disp, applist, idx, offset):
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():
disp = display.open()
applist = list_apps()
numapps = len(applist)
menulist = list_apps()
menulist += list_extra_entries()
numentries = len(menulist)
current = 0
state = STATE_MAIN_MENU
for ev in button_events():
if ev == buttons.BOTTOM_RIGHT:
# Scroll down
draw_menu(disp, applist, current, -8)
current = (current + 1) % numapps
elif ev == buttons.BOTTOM_LEFT:
# Scroll up
draw_menu(disp, applist, current, 8)
current = (current + numapps - 1) % numapps
elif ev == buttons.TOP_RIGHT:
# Select & start
disp.clear().update()
disp.close()
if applist[current] == FACTORY_RESET_CMD:
# Button handling
if state == STATE_MAIN_MENU:
if ev == buttons.BOTTOM_RIGHT:
# Scroll down
draw_menu(disp, menulist, current, -8)
current = (current + 1) % numentries
elif ev == buttons.BOTTOM_LEFT:
# Scroll up
draw_menu(disp, menulist, current, 8)
current = (current + numentries - 1) % numentries
elif ev == buttons.TOP_RIGHT:
# Select & start
state = activate_menu_entry(menulist[current])
elif state == STATE_RESET:
if ev == buttons.BOTTOM_LEFT:
state = STATE_RESET_ARMED
else:
state = STATE_MAIN_MENU
elif state == STATE_RESET_ARMED:
if ev == buttons.BOTTOM_RIGHT:
files = os.listdir(".")
for f in files:
os.unlink(f)
os.exit(0)
else:
try:
os.exec(applist[current])
except OSError as e:
print("Loading failed: ", e)
os.exit(1)
draw_menu(disp, applist, current, 0)
state = STATE_MAIN_MENU
# Output handling
if state == STATE_MAIN_MENU:
draw_menu(disp, menulist, current, 0)
elif state == STATE_RESET:
draw_reset(disp, False)
elif state == STATE_RESET_ARMED:
draw_reset(disp, True)
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment