Skip to content

Commit

Permalink
Quit app on A+C pressed using an interrupt handler.
Browse files Browse the repository at this point in the history
Note if apps set up their own interrupt handlers then they take precedence, but in that case the app should be responsible for quitting.
  • Loading branch information
MichaelBell committed Mar 25, 2022
1 parent e19fe9c commit 39a9f05
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
5 changes: 0 additions & 5 deletions micropython/examples/badger2040/badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,6 @@ def draw_badge():
draw_badge()

while True:
# Pressing A and C together quits the app
if display.pressed(badger2040.BUTTON_A) and display.pressed(badger2040.BUTTON_C):
break

if display.pressed(badger2040.BUTTON_A) or display.pressed(badger2040.BUTTON_B) or display.pressed(badger2040.BUTTON_C) or display.pressed(badger2040.BUTTON_UP) or display.pressed(badger2040.BUTTON_DOWN):
draw_overlay("To change the text, connect Badger2040 to a PC, load up Thonny, and modify badge.txt",
WIDTH - OVERLAY_BORDER, HEIGHT - OVERLAY_BORDER, OVERLAY_SPACING, OVERLAY_TEXT_SIZE)
Expand All @@ -230,4 +226,3 @@ def draw_badge():

# If on battery, halt the Badger to save power, it will wake up if any of the front buttons are pressed
display.halt()
time.sleep(0.1)
16 changes: 16 additions & 0 deletions micropython/examples/badger2040/badger_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ def launch(file):
if k not in ("gc", "file", "machine"):
del locals()[k]
gc.collect()

button_a = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN)

def quit_to_launcher(pin):
if button_a.value() and button_c.value():
import os
try:
os.remove(STATE_FILE)
except OSError:
pass
machine.reset()

button_a.irq(trigger=machine.Pin.IRQ_RISING, handler=quit_to_launcher)
button_c.irq(trigger=machine.Pin.IRQ_RISING, handler=quit_to_launcher)

try:
__import__(file[1:]) # Try to import _[file] (drop underscore prefix)
except ImportError:
Expand Down
5 changes: 0 additions & 5 deletions micropython/examples/badger2040/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ def show_image(n):


while True:
# Pressing A and C together quits the app
if display.pressed(badger2040.BUTTON_A) and display.pressed(badger2040.BUTTON_C):
break

if display.pressed(badger2040.BUTTON_UP):
if current_image > 0:
current_image -= 1
Expand Down Expand Up @@ -158,4 +154,3 @@ def show_image(n):

# Halt the Badger to save power, it will wake up if any of the front buttons are pressed
display.halt()
time.sleep(0.1)
4 changes: 3 additions & 1 deletion micropython/examples/badger2040/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ def button(pin):
render()

display.update_speed(badger2040.UPDATE_FAST)

if not changed and not display.woken():
render()

while True:
if display.pressed(badger2040.BUTTON_A):
button(badger2040.BUTTON_A)
Expand Down

0 comments on commit 39a9f05

Please sign in to comment.