Skip to content

Commit 593870f

Browse files
Move swipe detection to ui
1 parent ed68ab5 commit 593870f

File tree

3 files changed

+145
-19
lines changed

3 files changed

+145
-19
lines changed

internal_filesystem/apps/com.example.draw/assets/draw.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
import mpos.ui
22

3-
import lvgl as lv
4-
53
indev_error_x = 160
64
indev_error_y = 120
75

86
DARKPINK = lv.color_hex(0xEC048C)
97

10-
def get_xy():
11-
indev = lv.indev_active()
12-
if indev:
13-
point = lv.point_t()
14-
indev.get_point(point)
15-
return point.x, point.y
16-
else:
17-
return indev_error_x,indev_error_y # make it visible that this occurred
18-
198
# doesnt work:
209
def draw_line(x, y):
2110
global canvas
@@ -57,7 +46,7 @@ def touch_cb(event):
5746
#print(f"lv_event_t: code={event_code}, name={name}") # target={event.get_target()}, user_data={event.get_user_data()}, param={event.get_param()}
5847
if event_code == lv.EVENT.PRESSING: # this is probably enough
5948
#if event_code in [lv.EVENT.PRESSED, lv.EVENT.PRESSING, lv.EVENT.LONG_PRESSED, lv.EVENT.LONG_PRESSED_REPEAT]:
60-
x, y = get_xy()
49+
x, y = mpos.ui.get_pointer_xy()
6150
# drawing a point works:
6251
#canvas.set_px(x,y,lv.color_black(),lv.OPA.COVER)
6352
#

internal_filesystem/lib/mpos/ui.py

Lines changed: 142 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@
3030
foreground_app_name=None
3131

3232

33+
def get_pointer_xy():
34+
indev = lv.indev_active()
35+
if indev:
36+
point = lv.point_t()
37+
indev.get_point(point)
38+
return point.x, point.y
39+
else:
40+
return indev_error_x,indev_error_y # make it visible that this occurred
41+
42+
43+
drawer_swipe_start_y = 0
44+
def drawer_swipe_cb(event):
45+
global drawer_swipe_start_y
46+
event_code = event.get_code()
47+
#name = mpos.ui.get_event_name(event_code)
48+
#print(f"drawer_swipe_cb {event_code} and {name}")
49+
if event_code == lv.EVENT.PRESSED:
50+
x, drawer_swipe_start_y = get_pointer_xy()
51+
elif event_code == lv.EVENT.RELEASED:
52+
x, end_y = get_pointer_xy()
53+
if end_y < drawer_swipe_start_y - NOTIFICATION_BAR_HEIGHT:
54+
mpos.ui.close_drawer()
55+
56+
3357
# Shutdown function to run in main thread
3458
def shutdown():
3559
print("Shutting down...")
@@ -81,10 +105,13 @@ def close_bar():
81105
hide_bar_animation.start()
82106

83107
def show_launcher():
84-
global rootscreen
85-
set_foreground_app("com.example.launcher")
86-
open_bar()
87-
lv.screen_load(rootscreen)
108+
mpos.apps.restart_launcher()
109+
#global rootscreen
110+
#set_foreground_app("com.example.launcher")
111+
#open_bar()
112+
#prevscreen = screen_stack[0] # load previous screen
113+
#lv.screen_load(prevscreen)
114+
#lv.screen_load(rootscreen)
88115

89116
def create_rootscreen():
90117
global rootscreen
@@ -218,6 +245,7 @@ def update_memfree(timer):
218245
show_bar_animation.set_custom_exec_cb(lambda not_used, value : notification_bar.set_y(value))
219246

220247

248+
221249
def create_drawer(display=None):
222250
global drawer
223251
drawer=lv.obj(lv.layer_top())
@@ -226,7 +254,9 @@ def create_drawer(display=None):
226254
drawer.set_scroll_dir(lv.DIR.NONE)
227255
drawer.set_style_pad_all(0, 0)
228256
drawer.add_flag(lv.obj.FLAG.HIDDEN)
229-
257+
#drawer.add_flag(lv.obj.FLAG.GESTURE_BUBBLE) # no gestures are received... maybe not supported by SDL Pointer Driver...
258+
drawer.add_event_cb(drawer_swipe_cb, lv.EVENT.PRESSED, None)
259+
drawer.add_event_cb(drawer_swipe_cb, lv.EVENT.RELEASED, None)
230260
slider_label=lv.label(drawer)
231261
slider_label.set_text(f"{100}%") # TODO: restore this from configuration
232262
slider_label.align(lv.ALIGN.TOP_MID,0,lv.pct(4))
@@ -251,7 +281,7 @@ def slider_event(e):
251281
def wifi_event(e):
252282
global drawer_open
253283
close_drawer()
254-
start_app_by_name("com.example.wificonf")
284+
mpos.apps.start_app_by_name("com.example.wificonf")
255285

256286
wifi_btn.add_event_cb(wifi_event,lv.EVENT.CLICKED,None)
257287
#
@@ -313,6 +343,7 @@ def poweroff_cb(e):
313343
sys.exit(0)
314344

315345
poweroff_btn.add_event_cb(poweroff_cb,lv.EVENT.CLICKED,None)
346+
316347

317348

318349

@@ -451,7 +482,7 @@ def back_screen():
451482
#print("Adding notification bar and drawer to top layer")
452483
#mpos.ui.create_notification_bar()
453484
#mpos.ui.create_drawer()
454-
close_top_layer_msgboxes() # would be nicer to "cancel" all input events
485+
#close_top_layer_msgboxes() # would be nicer to "cancel" all input events
455486

456487
print("Loading previous screen")
457488
screen_stack.pop() # Remove current screen
@@ -461,3 +492,107 @@ def back_screen():
461492
open_bar()
462493
else:
463494
print("Warning: can't go back because screen_stack is empty.")
495+
496+
497+
498+
# Would be better to somehow save other events, like clicks, and pass them down to the layers below if released with x < 60
499+
def back_swipe_cb(event):
500+
#global rect
501+
502+
event_code = event.get_code()
503+
name = mpos.ui.get_event_name(event_code)
504+
print(f"back_swipe_cb {event_code} and {name}")
505+
506+
#xa = rect.get_x_aligned()
507+
#ya = rect.get_y_aligned()
508+
#print(f"xa, ya: {xa},{ya}")
509+
510+
#obj = e.get_target()
511+
#lvobj = lv.obj(obj)
512+
#pos = lvobj.get_pos() # Get current position
513+
#print(f"pos: {lvobj.get_x()}, {lvobj.get_y()}")
514+
515+
indev = lv.indev_active()
516+
if indev:
517+
point = lv.point_t()
518+
indev.get_point(point)
519+
x = point.x
520+
y = point.y
521+
print(f"pos: {x}, {y}")
522+
#rect.set_pos(x, 0)
523+
if event_code == lv.EVENT.RELEASED and x > 60: # TODO: use display_width / 3 here
524+
mpos.ui.back_screen()
525+
#rect.set_pos(0,0)
526+
#rect.set_pos(xa + point.x, ya + point.y)
527+
#rect.set_pos(point.x, point.y)
528+
529+
530+
def handle_back_swipe():
531+
rect = lv.obj(lv.layer_top())
532+
rect.set_size(NOTIFICATION_BAR_HEIGHT, lv.layer_top().get_height()-NOTIFICATION_BAR_HEIGHT)
533+
rect.set_scrollbar_mode(lv.SCROLLBAR_MODE.OFF)
534+
rect.set_pos(0, NOTIFICATION_BAR_HEIGHT)
535+
style = lv.style_t()
536+
style.init()
537+
style.set_bg_opa(lv.OPA.TRANSP)
538+
#style.set_bg_opa(15)
539+
style.set_border_width(0)
540+
style.set_radius(0)
541+
#style.set_border_color(lv.color_hex(0xFF0000)) # White border for visibility
542+
#style.set_border_opa(lv.OPA._50) # 50% opacity for the border
543+
rect.add_style(style, 0)
544+
#rect.add_flag(lv.obj.FLAG.CLICKABLE) # Make the object clickable
545+
#rect.add_flag(lv.obj.FLAG.GESTURE_BUBBLE) # Allow dragging
546+
#rect.add_event_cb(drag_event_cb, lv.EVENT.PRESSING, None)
547+
rect.add_event_cb(back_swipe_cb, lv.EVENT.RELEASED, None)
548+
549+
# Would be better to somehow save other events, like clicks, and pass them down to the layers below if released with x < 60
550+
def top_swipe_cb(event):
551+
#global rect
552+
553+
event_code = event.get_code()
554+
name = mpos.ui.get_event_name(event_code)
555+
print(f"top_swipe_cb {event_code} and {name}")
556+
557+
#xa = rect.get_x_aligned()
558+
#ya = rect.get_y_aligned()
559+
#print(f"xa, ya: {xa},{ya}")
560+
561+
#obj = e.get_target()
562+
#lvobj = lv.obj(obj)
563+
#pos = lvobj.get_pos() # Get current position
564+
#print(f"pos: {lvobj.get_x()}, {lvobj.get_y()}")
565+
566+
indev = lv.indev_active()
567+
if indev:
568+
point = lv.point_t()
569+
indev.get_point(point)
570+
x = point.x
571+
y = point.y
572+
print(f"pos: {x}, {y}")
573+
#rect.set_pos(x, 0)
574+
if event_code == lv.EVENT.RELEASED and y > 60: # TODO: use display_height / 3 here
575+
mpos.ui.open_drawer()
576+
#rect.set_pos(0,0)
577+
#rect.set_pos(xa + point.x, ya + point.y)
578+
#rect.set_pos(point.x, point.y)
579+
580+
581+
def handle_top_swipe():
582+
rect = lv.obj(lv.layer_top())
583+
rect.set_size(lv.pct(100), NOTIFICATION_BAR_HEIGHT)
584+
rect.set_pos(0, 0)
585+
rect.set_scrollbar_mode(lv.SCROLLBAR_MODE.OFF)
586+
style = lv.style_t()
587+
style.init()
588+
style.set_bg_opa(lv.OPA.TRANSP)
589+
#style.set_bg_opa(15)
590+
style.set_border_width(0)
591+
style.set_radius(0)
592+
#style.set_border_color(lv.color_hex(0xFF0000)) # White border for visibility
593+
#style.set_border_opa(lv.OPA._50) # 50% opacity for the border
594+
rect.add_style(style, 0)
595+
#rect.add_flag(lv.obj.FLAG.CLICKABLE) # Make the object clickable
596+
#rect.add_flag(lv.obj.FLAG.GESTURE_BUBBLE) # Allow dragging
597+
#rect.add_event_cb(drag_event_cb, lv.EVENT.PRESSING, None)
598+
rect.add_event_cb(top_swipe_cb, lv.EVENT.RELEASED, None)

internal_filesystem/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
mpos.ui.create_rootscreen()
66
mpos.ui.create_notification_bar()
77
mpos.ui.create_drawer(display)
8+
mpos.ui.handle_back_swipe()
9+
mpos.ui.handle_top_swipe()
810
mpos.ui.th = task_handler.TaskHandler(duration=5) # 5ms is recommended for MicroPython+LVGL on desktop
911

1012
try:

0 commit comments

Comments
 (0)