From a58b3381d2c974f6ee4e20fc8f32a79d7b0a265a Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Tue, 6 May 2025 15:56:40 +0200 Subject: [PATCH] Add simple button handler Long press brings it into bootloader mode. --- install.sh | 3 ++ internal_filesystem/main.py | 5 +-- internal_filesystem/system/button.py | 50 ++++++++++++++++++++++++++++ manifest.py | 1 + patches/lv_conf.h | 6 ++-- 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 internal_filesystem/system/button.py diff --git a/install.sh b/install.sh index ae05e9c4..ece059ca 100755 --- a/install.sh +++ b/install.sh @@ -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 :/ diff --git a/internal_filesystem/main.py b/internal_filesystem/main.py index 0b75f815..acc27462 100644 --- a/internal_filesystem/main.py +++ b/internal_filesystem/main.py @@ -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 diff --git a/internal_filesystem/system/button.py b/internal_filesystem/system/button.py new file mode 100644 index 00000000..29ca11df --- /dev/null +++ b/internal_filesystem/system/button.py @@ -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") diff --git a/manifest.py b/manifest.py index 549bfcea..115dc813 100644 --- a/manifest.py +++ b/manifest.py @@ -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 diff --git a/patches/lv_conf.h b/patches/lv_conf.h index 56758548..1e324861 100644 --- a/patches/lv_conf.h +++ b/patches/lv_conf.h @@ -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. */