Skip to content

Commit d83e12a

Browse files
boot_unix: try gesture detection before giving up
1 parent d87ccd8 commit d83e12a

File tree

1 file changed

+159
-4
lines changed

1 file changed

+159
-4
lines changed

internal_filesystem/boot_unix.py

Lines changed: 159 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,25 @@
3333
#keyboard.add_event_cb(keyboard_cb, lv.EVENT.ALL, None)
3434

3535

36-
# Swipe detection state
3736
# Swipe detection state
3837
start_y = None # Store the starting Y-coordinate of the mouse press
3938
start_x = None # Store the starting X-coordinate for left-edge swipe
4039

40+
# the problem is, this whole thing is called after the click/release has been processed by the screen...
4141
def swipe_read_cb(indev_drv, data):
4242
global start_y, start_x
4343
global indev
4444
global mouse
45+
46+
mouseindev = mouse._indevs[0]
47+
#print(indev) # none...
48+
#print(mouse.get_event_count())
49+
#print(mouseindev.get_event_count()) # 0
50+
#mouseindev.stop_processing()
51+
#mouseindev.enable(False) # well this works...
52+
#mouseindev.__x = -1 # works
53+
#mouseindev.__y = -1 # works
54+
#print(mouse._indevs[0])
4555

4656
pressed = mouse.get_state() # Get mouse/touch pressed state
4757
point = lv.point_t()
@@ -91,6 +101,8 @@ def swipe_read_cb(indev_drv, data):
91101
print("Long swipe right")
92102
if start_x <= 20: # Confirm swipe started near left edge
93103
print("Swipe Right Detected from Left Edge")
104+
#mouseindev.enable(False) # well this works...
105+
mouseindev.wait_release()
94106
mpos.ui.back_screen() # Call custom method for left menu
95107
start_y = None # Reset Y after swipe
96108
start_x = None # Reset X after swipe
@@ -99,10 +111,153 @@ def swipe_read_cb(indev_drv, data):
99111
start_y = None
100112
start_x = None
101113

114+
def get_xy():
115+
indev = lv.indev_active()
116+
if indev:
117+
point = lv.point_t()
118+
indev.get_point(point)
119+
return point.x, point.y
120+
else:
121+
return indev_error_x,indev_error_y # make it visible that this occurred
122+
123+
def test_indev_cb(event):
124+
print("test_indev_cb")
125+
126+
def test_cb(event):
127+
print("test_cb")
128+
event_code=event.get_code()
129+
name = mpos.ui.get_event_name(event_code)
130+
print(f"lv_event_t: code={event_code}, name={name}") # target={event.get_target()}, user_data={event.get_user_data()}, param={event.get_param()}
131+
x, y = get_xy()
132+
print(f"x,y = {x},{y}")
133+
# try to disable the event
134+
event.stop_processing()
135+
event.stop_bubbling()
136+
#global mouse
137+
#mouseindev = mouse._indevs[0]
138+
#mouseindev.__x = -1 # works
139+
#mouseindev.__y = -1 # works
140+
#mouseindev.wait_release() # results in only PRESSED and then it stops...
141+
#if event_code == lv.EVENT.RELEASED or event_code == lv.EVENT.CLICKED:
142+
# mouseindev.wait_release() # results in only PRESSED and then it stops...
143+
#mouseindev.enable(False) # well this works...
144+
145+
gesture_start_x = None
146+
gesture_start_y = None
147+
def detect_gesture(event, pressed, x, y):
148+
global mouse
149+
global gesture_start_x, gesture_start_y
150+
151+
mouseindev = mouse._indevs[0]
152+
event.stop_processing()
153+
event.stop_bubbling()
154+
#event.remove_all()
155+
156+
if pressed and gesture_start_x is None :
157+
# Mouse/touch pressed (start of potential swipe)
158+
gesture_start_y = y # Store Y for vertical swipe detection
159+
gesture_start_x = x # Store X for horizontal swipe detection
160+
#print(f"Mouse press at X={start_x}, Y={start_y}")
161+
162+
# Check if press is in notification bar (for swipe down)
163+
if y <= mpos.ui.NOTIFICATION_BAR_HEIGHT:
164+
print(f"Press in notification bar at Y={start_y}")
165+
# Check if press is near left edge (for swipe right)
166+
if x <= 20: # Adjust threshold for left edge (e.g., 20 pixels)
167+
print(f"Press near left edge at X={start_x}")
168+
elif pressed and gesture_start_x is not None:
169+
# Mouse/touch dragged while pressed (potential swipe in progress)
170+
print("tracking pressed")
171+
if gesture_start_x <= 20: # Confirm swipe started near left edge
172+
print("tracking pressed with swipe started left")
173+
#mpos.ui.back_screen() # Call custom method for left menu
174+
mouse.wait_release() # causes only presses to be detected anymore
175+
#mouseindev.wait_release() # causes only presses to be detected anymore
176+
#event.stop_processing()
177+
#event.stop_bubbling()
178+
#mouseindev.__x = -1
179+
#mouseindev.__y = -1
180+
#mouseindev.enable(False) # causes no pressed to be detected anymore because no events
181+
# Idea: show a full-screen widget that captures the presses?
182+
else:
183+
# Check for rightward swipe from left edge (x increased significantly)
184+
if gesture_start_x is not None and x > gesture_start_x + 50: # Threshold for swipe right
185+
print("Long swipe right")
186+
if gesture_start_x <= 20: # Confirm swipe started near left edge
187+
print("!!! Swipe Right Detected from Left Edge")
188+
mpos.ui.back_screen() # Call custom method for left menu
189+
#mouse.wait_release()
190+
#mouseindev.wait_release()
191+
#event.stop_processing()
192+
#event.stop_bubbling()
193+
#mouseindev.__x = -1
194+
#mouseindev.__y = -1
195+
#mouseindev.enable(False)
196+
197+
# Reset both coordinates on release
198+
gesture_start_y = None
199+
gesture_start_x = None
200+
#mouseindev.enable(True) # well this works...
201+
202+
203+
def detect_swipe(event):
204+
global mouse
205+
pressed = mouse.get_state() # Get mouse/touch pressed state
206+
point = lv.point_t()
207+
mouse.get_point(point) # Get current coordinates
208+
x, y = point.x, point.y
209+
code = event.get_code()
210+
name = mpos.ui.get_event_name(code)
211+
print(f"detect_swipe got {pressed},{x},{y} with {code},{name}")
212+
event.stop_processing()
213+
# crashes: target_obj = event.get_target_obj() # Get the widget that triggered the event
214+
target = event.get_target() # Get the widget that triggered the event
215+
print("Event triggered by target:", target)
216+
ctarget = event.get_current_target() # Get the widget that triggered the event
217+
print("Event triggered by ctarget:", ctarget)
218+
#target_obj = lv.obj.cast(target)
219+
target_obj = lv.obj(target) # Wrap the Blob as an lv.obj
220+
print("Event triggered by object:", target_obj)
221+
# Example: Check if the object is a button
222+
if isinstance(target_obj, lv.button):
223+
print("This is a button!")
224+
elif isinstance(target_obj, lv.obj):
225+
print("This is a generic LVGL object")
226+
#event.remove()
227+
#event.removeall()
228+
# this also works:
229+
#mouseindev = mouse._indevs[0]
230+
#mouseindev.get_point(point) # Always returns 0,0
231+
#x, y = point.x, point.y
232+
#print(f"detect_swipe got {x},{y} from mouseindev")
233+
234+
detect_gesture(event, pressed, x, y)
235+
236+
237+
# this seems to work for capturing mouse events:
238+
#mouse.add_event_cb(detect_swipe, lv.EVENT.ALL, None)
239+
240+
def re_enable_upon_release(indev_drv, data):
241+
#print("re_enable_upon_release")
242+
global mouse
243+
#pressed = mouse.get_state() # doesn't change if mouseindev has been disabled
244+
#print(f"re_enable_upon_release {pressed}")
245+
#if not pressed:
246+
mouseindev = mouse._indevs[0]
247+
mouseindev.enable(True) # causes no pressed to be detected anymore because no events
248+
102249
# Register the custom read callback with the input device
103-
indev = lv.indev_create()
104-
indev.set_type(lv.INDEV_TYPE.POINTER)
105-
indev.set_read_cb(swipe_read_cb)
250+
#indev = lv.indev_create()
251+
#indev.set_type(lv.INDEV_TYPE.POINTER)
252+
#indev.set_read_cb(re_enable_upon_release)
253+
254+
#indev = lv.indev_active()
255+
#if not indev:
256+
# print("ERROR: could not get active indev?!")
257+
#else:
258+
# indev.set_read_cb(test_indev_cb)
259+
260+
106261

107262
print("boot_unix.py finished")
108263

0 commit comments

Comments
 (0)