|
2 | 2 |
|
3 | 3 | from machine import Pin, Timer |
4 | 4 | import time |
| 5 | +import _thread |
5 | 6 |
|
6 | 7 | # Configure IO0 as input with pull-up resistor |
7 | 8 | button = Pin(0, Pin.IN, Pin.PULL_UP) |
|
14 | 15 | # Timer for checking long press |
15 | 16 | timer = Timer(-1) |
16 | 17 |
|
| 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() |
17 | 31 |
|
18 | 32 | def on_long_press(t): # Callback for when long press duration is reached. |
| 33 | + global timer |
19 | 34 | timer.deinit() # Stop the timer |
20 | 35 | global is_pressed |
21 | 36 | 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, ()) |
25 | 38 | else: |
26 | 39 | is_pressed = False |
27 | 40 |
|
28 | 41 |
|
29 | 42 | def button_handler(pin): |
30 | 43 | """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 |
35 | 45 | if button.value() == 0: # Button pressed (LOW due to pull-up) |
36 | | - print("Button IO0 pressed.") |
| 46 | + print("Button IO0 pressed") |
37 | 47 | press_start_time = time.ticks_ms() |
38 | 48 | is_pressed = True |
39 | 49 | # Start timer to check for long press after long_press_duration |
40 | 50 | timer.init(mode=Timer.ONE_SHOT, period=long_press_duration, callback=on_long_press) |
41 | 51 | else: # Button released (HIGH) |
42 | | - print("Button IO0 released.") |
| 52 | + print("Button IO0 released") |
43 | 53 | timer.deinit() # Cancel timer if button is released early |
44 | 54 | is_pressed = False |
45 | 55 |
|
|
0 commit comments