Skip to content

Commit 3addeb8

Browse files
button.py: show feedback on display
1 parent a58b338 commit 3addeb8

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

internal_filesystem/system/button.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from machine import Pin, Timer
44
import time
5+
import _thread
56

67
# Configure IO0 as input with pull-up resistor
78
button = Pin(0, Pin.IN, Pin.PULL_UP)
@@ -14,32 +15,41 @@
1415
# Timer for checking long press
1516
timer = Timer(-1)
1617

18+
message = "Bootloader mode activated.\nYou can now install firmware over USB.\n\n"
19+
20+
def handle_long_press():
21+
print(message)
22+
import lvgl as lv
23+
screen = lv.obj()
24+
label = lv.label(screen)
25+
label.set_text(message)
26+
label.center()
27+
lv.screen_load(screen)
28+
time.sleep(1)
29+
import machine
30+
machine.bootloader()
1731

1832
def on_long_press(t): # Callback for when long press duration is reached.
33+
global timer
1934
timer.deinit() # Stop the timer
2035
global is_pressed
2136
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()
37+
_thread.start_new_thread(handle_long_press, ())
2538
else:
2639
is_pressed = False
2740

2841

2942
def button_handler(pin):
3043
"""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
44+
global press_start_time, is_pressed, timer
3545
if button.value() == 0: # Button pressed (LOW due to pull-up)
36-
print("Button IO0 pressed.")
46+
print("Button IO0 pressed")
3747
press_start_time = time.ticks_ms()
3848
is_pressed = True
3949
# Start timer to check for long press after long_press_duration
4050
timer.init(mode=Timer.ONE_SHOT, period=long_press_duration, callback=on_long_press)
4151
else: # Button released (HIGH)
42-
print("Button IO0 released.")
52+
print("Button IO0 released")
4353
timer.deinit() # Cancel timer if button is released early
4454
is_pressed = False
4555

0 commit comments

Comments
 (0)