Skip to content

Commit 1ff653e

Browse files
Debounce buttons to workaround PRESSING focus loss
In LVGL 9.2.2, the lv_keyboard widget seems to lose focus when a PRESSING (long press) is received.
1 parent 5e23c22 commit 1ff653e

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

internal_filesystem/boot_fri3d-2024.py

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,53 +116,74 @@ def read_joystick():
116116
return None # No key triggered
117117

118118

119+
# Track button states for debouncing (only for buttons, not joystick)
120+
last_button_key = None
121+
last_button_state = lv.INDEV_STATE.RELEASED
122+
119123
# Read callback
120124
# Warning: This gets called several times per second, and if it outputs continuous debugging on the serial line,
121125
# that will break tools like mpremote from working properly to upload new files over the serial line, thus needing a reflash.
122126
def keypad_read_cb(indev, data):
123-
data.continue_reading = False # No more data to read
124-
# Check GPIOs and set key/state (only one key at a time)
127+
global last_button_key, last_button_state
128+
data.continue_reading = False
129+
130+
# Check buttons and joystick
131+
current_key = None
132+
is_joystick = False
133+
134+
# Check buttons first (debounced)
125135
if btn_x.value() == 0:
126-
data.key = lv.KEY.ESC
127-
data.state = lv.INDEV_STATE.PRESSED
128-
mpos.ui.back_screen()
136+
current_key = lv.KEY.ESC
129137
elif btn_y.value() == 0:
130-
data.key = lv.KEY.ESC
131-
data.state = lv.INDEV_STATE.PRESSED
132-
mpos.ui.back_screen()
138+
current_key = lv.KEY.PREV
133139
elif btn_a.value() == 0:
134-
#print("A pressed")
135-
data.key = lv.KEY.ENTER
136-
data.state = lv.INDEV_STATE.PRESSED
140+
current_key = lv.KEY.NEXT
137141
elif btn_b.value() == 0:
138-
#print("B pressed")
139-
data.key = lv.KEY.ENTER
140-
data.state = lv.INDEV_STATE.PRESSED
142+
current_key = lv.KEY.ENTER
141143
elif btn_menu.value() == 0:
142-
data.key = lv.KEY.HOME
143-
data.state = lv.INDEV_STATE.PRESSED
144+
current_key = lv.KEY.HOME
144145
elif btn_start.value() == 0:
145-
data.key = lv.KEY.END
146-
data.state = lv.INDEV_STATE.PRESSED
146+
current_key = lv.KEY.END
147147
else:
148-
data.state = lv.INDEV_STATE.RELEASED
149-
if data.state == lv.INDEV_STATE.RELEASED:
148+
# Check joystick (not debounced)
150149
joystick = read_joystick()
151150
if joystick == "LEFT":
152-
data.key = lv.KEY.PREV
153-
data.state = lv.INDEV_STATE.PRESSED
151+
current_key = lv.KEY.LEFT
152+
is_joystick = True
154153
elif joystick == "RIGHT":
155-
data.key = lv.KEY.NEXT
156-
data.state = lv.INDEV_STATE.PRESSED
154+
current_key = lv.KEY.RIGHT
155+
is_joystick = True
157156
elif joystick == "UP":
158-
data.key = lv.KEY.UP
159-
data.state = lv.INDEV_STATE.PRESSED
157+
current_key = lv.KEY.UP
158+
is_joystick = True
160159
elif joystick == "DOWN":
161-
data.key = lv.KEY.DOWN
160+
current_key = lv.KEY.DOWN
161+
is_joystick = True
162+
163+
# Handle joystick (continuous pressing allowed)
164+
if is_joystick and current_key:
165+
data.key = current_key
166+
data.state = lv.INDEV_STATE.PRESSED # Always send PRESSED for joystick
167+
# Handle buttons (debounced)
168+
elif current_key:
169+
if current_key != last_button_key:
170+
data.key = current_key
162171
data.state = lv.INDEV_STATE.PRESSED
172+
last_button_key = current_key
173+
last_button_state = lv.INDEV_STATE.PRESSED
163174
else:
164-
data.state = lv.INDEV_STATE.RELEASED
175+
data.state = lv.INDEV_STATE.RELEASED # Avoid continuous PRESSED
176+
else:
177+
# No input
178+
data.key = last_button_key if last_button_key else lv.KEY.ENTER
179+
data.state = lv.INDEV_STATE.RELEASED
180+
last_button_key = None
181+
last_button_state = lv.INDEV_STATE.RELEASED
165182

183+
# Handle ESC for back navigation
184+
if current_key == lv.KEY.ESC and last_button_state == lv.INDEV_STATE.PRESSED:
185+
import mpos
186+
mpos.ui.back_screen()
166187

167188
group = lv.group_create()
168189
group.set_default()

0 commit comments

Comments
 (0)