From a4f7d05fe5ba18cb26e93881aae68603977b53ef Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 22 Feb 2022 20:26:00 +0000 Subject: [PATCH] Badger2040: Move startup block on held buttons to launcher.py Blocking on held buttons is bad for MicroPython, since it makes nothing happen on startup- We need the button to be held but are waiting for a button release after some arbitrary amount of time. Additionally running code while holding down a button would block normal MicroPython event execution and crash the host's USB bus quite severely. --- libraries/badger2040/badger2040.cpp | 2 ++ micropython/examples/badger2040/launcher.py | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libraries/badger2040/badger2040.cpp b/libraries/badger2040/badger2040.cpp index e9300de0..18d764ff 100644 --- a/libraries/badger2040/badger2040.cpp +++ b/libraries/badger2040/badger2040.cpp @@ -41,6 +41,7 @@ namespace pimoroni { gpio_set_dir(VBUS_DETECT, GPIO_IN); gpio_put(VBUS_DETECT, 1); +/* // read initial button states uint32_t mask = (1UL << A) | (1UL << B) | (1UL << C) | (1UL << D) | (1UL << E); _wake_button_states |= gpio_get_all() & mask; @@ -49,6 +50,7 @@ namespace pimoroni { while(gpio_get_all() & mask) { tight_loop_contents(); } +*/ // led control pin pwm_config cfg = pwm_get_default_config(); diff --git a/micropython/examples/badger2040/launcher.py b/micropython/examples/badger2040/launcher.py index 491748d2..5b94c928 100644 --- a/micropython/examples/badger2040/launcher.py +++ b/micropython/examples/badger2040/launcher.py @@ -25,8 +25,9 @@ button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOW 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) -# Early exit if a button is pressed -if button_a.value() or button_b.value() or button_c.value(): +# Early exit if b + c button is pressed +# A button must be held briefly to power on, so we use a combo instead +if button_b.value() and button_c.value(): sys.exit(0) @@ -94,13 +95,19 @@ def button(pin): render() +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 + + 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) -render() while True: time.sleep(1.0)