Skip to content

Commit a58b338

Browse files
Add simple button handler
Long press brings it into bootloader mode.
1 parent d4b8a7d commit a58b338

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

install.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ pushd internal_filesystem/
44

55
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp boot.py :/boot.py
66
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp main.py :/main.py
7+
8+
#~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp main.py :/system/button.py
79
#~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp autorun.py :/autorun.py
810

11+
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp -r system :/
912
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp -r apps :/
1013
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp -r builtin :/
1114
~/sources/lvgl_micropython/lib/micropython/tools/mpremote/mpremote.py fs cp -r lib :/

internal_filesystem/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,9 @@ def restart_launcher():
395395
# No need to stop the other launcher first, because it exits after building the screen
396396
start_app_by_name("com.example.launcher", True)
397397

398-
# Execute this if it exists
399-
execute_script_new_thread("/autorun.py", True, False, False)
398+
# Execute these if they exist:
399+
execute_script_new_thread("/autorun.py", True, False, False) # Generic run-at-boot script, for development
400+
execute_script_new_thread("/system/button.py", True, False, False) # Button handling through IRQ
400401

401402
try:
402403
import freezefs_mount_builtin
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
print("button.py running")
2+
3+
from machine import Pin, Timer
4+
import time
5+
6+
# Configure IO0 as input with pull-up resistor
7+
button = Pin(0, Pin.IN, Pin.PULL_UP)
8+
9+
# Variables for long press detection
10+
long_press_duration = 4000
11+
press_start_time = 0
12+
is_pressed = False
13+
14+
# Timer for checking long press
15+
timer = Timer(-1)
16+
17+
18+
def on_long_press(t): # Callback for when long press duration is reached.
19+
timer.deinit() # Stop the timer
20+
global is_pressed
21+
if is_pressed and button.value() == 0: # Ensure button is still pressed
22+
print("Button IO0 long pressed, going into bootloader mode...")
23+
import machine
24+
machine.bootloader()
25+
else:
26+
is_pressed = False
27+
28+
29+
def button_handler(pin):
30+
"""Interrupt handler for button press and release."""
31+
global press_start_time, is_pressed
32+
# Debounce: Ignore interrupts within 50ms of the last event
33+
if time.ticks_diff(time.ticks_ms(), press_start_time) < 50:
34+
return
35+
if button.value() == 0: # Button pressed (LOW due to pull-up)
36+
print("Button IO0 pressed.")
37+
press_start_time = time.ticks_ms()
38+
is_pressed = True
39+
# Start timer to check for long press after long_press_duration
40+
timer.init(mode=Timer.ONE_SHOT, period=long_press_duration, callback=on_long_press)
41+
else: # Button released (HIGH)
42+
print("Button IO0 released.")
43+
timer.deinit() # Cancel timer if button is released early
44+
is_pressed = False
45+
46+
47+
# Set up interrupt for both falling (press) and rising (release) edges
48+
button.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=button_handler)
49+
50+
print("button.py finished")

manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
freeze('internal_filesystem/', 'boot.py') # Hardware initialization
22
freeze('internal_filesystem/', 'main.py') # User Interface initialization
33
freeze('internal_filesystem/lib', '') # Additional libraries
4+
freeze('internal_filesystem/system', '') # Additional libraries
45
freeze('/home/user/sources/freezeFS/', 'freezefs_mount_builtin.py') # Built-in apps

patches/lv_conf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,15 +755,15 @@ extern void *mp_lv_roots;
755755
#endif
756756

757757
/*API for memory-mapped file access. */
758-
#define LV_USE_FS_MEMFS 1
758+
#define LV_USE_FS_MEMFS 0
759759
#if LV_USE_FS_MEMFS
760760
#define LV_FS_MEMFS_LETTER '\0' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
761761
#endif
762762

763763
/*API for LittleFs. */
764-
#define LV_USE_FS_LITTLEFS 1
764+
#define LV_USE_FS_LITTLEFS 0
765765
#if LV_USE_FS_LITTLEFS
766-
#define LV_FS_LITTLEFS_LETTER 'L' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
766+
#define LV_FS_LITTLEFS_LETTER '\0' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
767767
#endif
768768

769769
/*API for Arduino LittleFs. */

0 commit comments

Comments
 (0)