Add simple button handler

Long press brings it into bootloader mode.
This commit is contained in:
Thomas Farstrike
2025-05-06 15:56:40 +02:00
parent d4b8a7d74b
commit a58b3381d2
5 changed files with 60 additions and 5 deletions
+3
View File
@@ -4,8 +4,11 @@ pushd internal_filesystem/
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp boot.py :/boot.py
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp main.py :/main.py
#~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp main.py :/system/button.py
#~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp autorun.py :/autorun.py
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp -r system :/
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp -r apps :/
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp -r builtin :/
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp -r lib :/
+3 -2
View File
@@ -395,8 +395,9 @@ def restart_launcher():
# No need to stop the other launcher first, because it exits after building the screen
start_app_by_name("com.example.launcher", True)
# Execute this if it exists
execute_script_new_thread("/autorun.py", True, False, False)
# Execute these if they exist:
execute_script_new_thread("/autorun.py", True, False, False) # Generic run-at-boot script, for development
execute_script_new_thread("/system/button.py", True, False, False) # Button handling through IRQ
try:
import freezefs_mount_builtin
+50
View File
@@ -0,0 +1,50 @@
print("button.py running")
from machine import Pin, Timer
import time
# Configure IO0 as input with pull-up resistor
button = Pin(0, Pin.IN, Pin.PULL_UP)
# Variables for long press detection
long_press_duration = 4000
press_start_time = 0
is_pressed = False
# Timer for checking long press
timer = Timer(-1)
def on_long_press(t): # Callback for when long press duration is reached.
timer.deinit() # Stop the timer
global is_pressed
if is_pressed and button.value() == 0: # Ensure button is still pressed
print("Button IO0 long pressed, going into bootloader mode...")
import machine
machine.bootloader()
else:
is_pressed = False
def button_handler(pin):
"""Interrupt handler for button press and release."""
global press_start_time, is_pressed
# Debounce: Ignore interrupts within 50ms of the last event
if time.ticks_diff(time.ticks_ms(), press_start_time) < 50:
return
if button.value() == 0: # Button pressed (LOW due to pull-up)
print("Button IO0 pressed.")
press_start_time = time.ticks_ms()
is_pressed = True
# Start timer to check for long press after long_press_duration
timer.init(mode=Timer.ONE_SHOT, period=long_press_duration, callback=on_long_press)
else: # Button released (HIGH)
print("Button IO0 released.")
timer.deinit() # Cancel timer if button is released early
is_pressed = False
# Set up interrupt for both falling (press) and rising (release) edges
button.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=button_handler)
print("button.py finished")
+1
View File
@@ -1,4 +1,5 @@
freeze('internal_filesystem/', 'boot.py') # Hardware initialization
freeze('internal_filesystem/', 'main.py') # User Interface initialization
freeze('internal_filesystem/lib', '') # Additional libraries
freeze('internal_filesystem/system', '') # Additional libraries
freeze('/home/user/sources/freezeFS/', 'freezefs_mount_builtin.py') # Built-in apps
+3 -3
View File
@@ -755,15 +755,15 @@ extern void *mp_lv_roots;
#endif
/*API for memory-mapped file access. */
#define LV_USE_FS_MEMFS 1
#define LV_USE_FS_MEMFS 0
#if LV_USE_FS_MEMFS
#define LV_FS_MEMFS_LETTER '\0' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
#endif
/*API for LittleFs. */
#define LV_USE_FS_LITTLEFS 1
#define LV_USE_FS_LITTLEFS 0
#if LV_USE_FS_LITTLEFS
#define LV_FS_LITTLEFS_LETTER 'L' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
#define LV_FS_LITTLEFS_LETTER '\0' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
#endif
/*API for Arduino LittleFs. */