Skip to content

Commit 43a6c0d

Browse files
Move UI code to mpos/ui
1 parent ef4eba9 commit 43a6c0d

4 files changed

Lines changed: 269 additions & 256 deletions

File tree

internal_filesystem/boot_unix.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import lvgl as lv
33
import sdl_display
44
import sdl_pointer
5+
from mpos import ui
56

67
TFT_HOR_RES=320
78
TFT_VER_RES=240
@@ -30,23 +31,23 @@ def swipe_read_cb(indev_drv, data):
3031
if pressed and start_y is None:
3132
start_y = y
3233
# Mouse button pressed (start of potential swipe)
33-
if y <= NOTIFICATION_BAR_HEIGHT:
34+
if y <= ui.NOTIFICATION_BAR_HEIGHT:
3435
# Store starting Y if press is in the notification bar area
3536
print(f"Mouse press at Y={start_y}")
3637
elif pressed and start_y is not None:
3738
# Mouse dragged while pressed (potential swipe in progress)
3839
# Check for downward swipe (y increased significantly)
3940
if y > start_y + 50: # Threshold for swipe detection (adjust as needed)
4041
print("long swipe down")
41-
if start_y <= NOTIFICATION_BAR_HEIGHT:
42+
if start_y <= ui.NOTIFICATION_BAR_HEIGHT:
4243
print("Swipe Down Detected from Notification Bar")
43-
open_drawer()
44+
ui.open_drawer()
4445
start_y = None # Reset after swipe
4546
else:
4647
# Mouse button released
4748
if start_y is not None and y < start_y - 50: # Threshold for swipe-up
4849
print("Swipe Up Detected")
49-
close_drawer()
50+
ui.close_drawer()
5051
start_y = None # Reset on release
5152

5253
# Register the custom read callback with the input device
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
def is_launcher(app_name):
3+
print(f"checking is_launcher for {app_name}")
4+
# Simple check, could be more elaborate by checking the MANIFEST.JSON for the app...
5+
return "launcher" in app_name

internal_filesystem/lib/mpos/ui.py

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
import lvgl as lv
2+
from mpos import apps
3+
4+
NOTIFICATION_BAR_HEIGHT=24
5+
6+
CLOCK_UPDATE_INTERVAL = 1000 # 10 or even 1 ms doesn't seem to change the framerate but 100ms is enough
7+
WIFI_ICON_UPDATE_INTERVAL = 1500
8+
TEMPERATURE_UPDATE_INTERVAL = 2000
9+
MEMFREE_UPDATE_INTERVAL = 5000 # not too frequent because there's a forced gc.collect() to give it a reliable value
10+
11+
DRAWER_ANIM_DURATION=300
12+
13+
drawer=None
14+
rootscreen = None
15+
16+
drawer_open=False
17+
bar_open=True
18+
19+
hide_bar_animation = None
20+
show_bar_animation = None
21+
22+
foreground_app_name=None
23+
24+
def set_foreground_app(appname):
25+
global foreground_app_name
26+
print(f"foreground app is: {foreground_app_name}")
27+
foreground_app_name = appname
28+
29+
def open_drawer():
30+
global drawer_open, drawer
31+
if not drawer_open:
32+
open_bar()
33+
drawer_open=True
34+
drawer.remove_flag(lv.obj.FLAG.HIDDEN)
35+
36+
def close_drawer(to_launcher=False):
37+
global drawer_open, drawer
38+
if drawer_open:
39+
drawer_open=False
40+
drawer.add_flag(lv.obj.FLAG.HIDDEN)
41+
if not to_launcher and not apps.is_launcher(foreground_app_name):
42+
close_bar()
43+
44+
def open_bar():
45+
global bar_open, show_bar_animation
46+
if not bar_open:
47+
bar_open=True
48+
show_bar_animation.start()
49+
50+
def close_bar():
51+
global bar_open, hide_bar_animation
52+
if bar_open:
53+
bar_open=False
54+
hide_bar_animation.start()
55+
56+
def show_launcher():
57+
global rootscreen
58+
open_bar()
59+
lv.screen_load(rootscreen)
60+
61+
def create_rootscreen():
62+
global rootscreen
63+
rootscreen = lv.screen_active()
64+
rootlabel = lv.label(rootscreen)
65+
rootlabel.set_text("Welcome!")
66+
rootlabel.align(lv.ALIGN.CENTER, 0, 0)
67+
68+
69+
def create_notification_bar():
70+
# Create notification bar
71+
notification_bar = lv.obj(lv.layer_top())
72+
notification_bar.set_size(lv.pct(100), NOTIFICATION_BAR_HEIGHT)
73+
notification_bar.set_pos(0, 0)
74+
notification_bar.set_scrollbar_mode(lv.SCROLLBAR_MODE.OFF)
75+
notification_bar.set_scroll_dir(lv.DIR.VER)
76+
notification_bar.set_style_border_width(0, 0)
77+
notification_bar.set_style_radius(0, 0)
78+
# Time label
79+
time_label = lv.label(notification_bar)
80+
time_label.set_text("00:00:00")
81+
time_label.align(lv.ALIGN.LEFT_MID, 0, 0)
82+
temp_label = lv.label(notification_bar)
83+
temp_label.set_text("00°C")
84+
temp_label.align_to(time_label, lv.ALIGN.OUT_RIGHT_MID, NOTIFICATION_BAR_HEIGHT , 0)
85+
memfree_label = lv.label(notification_bar)
86+
memfree_label.set_text("")
87+
memfree_label.align_to(temp_label, lv.ALIGN.OUT_RIGHT_MID, NOTIFICATION_BAR_HEIGHT, 0)
88+
#style = lv.style_t()
89+
#style.init()
90+
#style.set_text_font(lv.font_montserrat_8) # tiny font
91+
#memfree_label.add_style(style, 0)
92+
# Notification icon (bell)
93+
#notif_icon = lv.label(notification_bar)
94+
#notif_icon.set_text(lv.SYMBOL.BELL)
95+
#notif_icon.align_to(time_label, lv.ALIGN.OUT_RIGHT_MID, PADDING_TINY, 0)
96+
# Battery icon
97+
battery_icon = lv.label(notification_bar)
98+
battery_icon.set_text(lv.SYMBOL.BATTERY_FULL)
99+
battery_icon.align(lv.ALIGN.RIGHT_MID, 0, 0)
100+
# WiFi icon
101+
wifi_icon = lv.label(notification_bar)
102+
wifi_icon.set_text(lv.SYMBOL.WIFI)
103+
wifi_icon.align_to(battery_icon, lv.ALIGN.OUT_LEFT_MID, -NOTIFICATION_BAR_HEIGHT, 0)
104+
wifi_icon.add_flag(lv.obj.FLAG.HIDDEN)
105+
# Battery percentage - not shown to conserve space
106+
#battery_label = lv.label(notification_bar)
107+
#battery_label.set_text("100%")
108+
#battery_label.align(lv.ALIGN.RIGHT_MID, 0, 0)
109+
# Update time
110+
import time
111+
def update_time(timer):
112+
ticks = time.ticks_ms()
113+
hours = (ticks // 3600000) % 24
114+
minutes = (ticks // 60000) % 60
115+
seconds = (ticks // 1000) % 60
116+
#milliseconds = ticks % 1000
117+
time_label.set_text(f"{hours:02d}:{minutes:02d}:{seconds:02d}")
118+
119+
can_check_network = False
120+
try:
121+
import network
122+
can_check_network = True
123+
except Exception as e:
124+
print("Warning: could not check WLAN status:", str(e))
125+
126+
def update_wifi_icon(timer):
127+
if not can_check_network or network.WLAN(network.STA_IF).isconnected():
128+
wifi_icon.remove_flag(lv.obj.FLAG.HIDDEN)
129+
else:
130+
wifi_icon.add_flag(lv.obj.FLAG.HIDDEN)
131+
132+
can_check_temperature = False
133+
try:
134+
import esp32
135+
except Exception as e:
136+
print("Warning: can't check temperature sensor:", str(e))
137+
138+
def update_temperature(timer):
139+
if can_check_temperature:
140+
temp_label.set_text(f"{esp32.mcu_temperature()}°C")
141+
else:
142+
temp_label.set_text("42°C")
143+
144+
145+
import gc
146+
def update_memfree(timer):
147+
gc.collect()
148+
memfree_label.set_text(f"{gc.mem_free()}")
149+
150+
timer1 = lv.timer_create(update_time, CLOCK_UPDATE_INTERVAL, None)
151+
timer2 = lv.timer_create(update_temperature, TEMPERATURE_UPDATE_INTERVAL, None)
152+
timer3 = lv.timer_create(update_memfree, MEMFREE_UPDATE_INTERVAL, None)
153+
timer4 = lv.timer_create(update_wifi_icon, WIFI_ICON_UPDATE_INTERVAL, None)
154+
155+
# hide bar animation
156+
global hide_bar_animation
157+
hide_bar_animation = lv.anim_t()
158+
hide_bar_animation.init()
159+
hide_bar_animation.set_var(notification_bar)
160+
hide_bar_animation.set_values(0, -NOTIFICATION_BAR_HEIGHT)
161+
hide_bar_animation.set_time(2000)
162+
hide_bar_animation.set_custom_exec_cb(lambda not_used, value : notification_bar.set_y(value))
163+
164+
# show bar animation
165+
global show_bar_animation
166+
show_bar_animation = lv.anim_t()
167+
show_bar_animation.init()
168+
show_bar_animation.set_var(notification_bar)
169+
show_bar_animation.set_values(-NOTIFICATION_BAR_HEIGHT, 0)
170+
show_bar_animation.set_time(1000)
171+
show_bar_animation.set_custom_exec_cb(lambda not_used, value : notification_bar.set_y(value))
172+
173+
174+
def create_drawer(display):
175+
global drawer
176+
drawer=lv.obj(lv.layer_top())
177+
drawer.set_size(lv.pct(100),lv.pct(90))
178+
drawer.set_pos(0,NOTIFICATION_BAR_HEIGHT)
179+
drawer.set_scroll_dir(lv.DIR.NONE)
180+
drawer.set_style_pad_all(0, 0)
181+
drawer.add_flag(lv.obj.FLAG.HIDDEN)
182+
183+
slider_label=lv.label(drawer)
184+
slider_label.set_text(f"{100}%") # TODO: restore this from configuration
185+
slider_label.align(lv.ALIGN.TOP_MID,0,lv.pct(4))
186+
slider=lv.slider(drawer)
187+
slider.set_range(1,100)
188+
slider.set_value(100,False)
189+
slider.set_width(lv.pct(80))
190+
slider.align_to(slider_label,lv.ALIGN.OUT_BOTTOM_MID,0,lv.pct(4))
191+
def slider_event(e):
192+
value=slider.get_value()
193+
slider_label.set_text(f"{value}%")
194+
display.set_backlight(value)
195+
196+
slider.add_event_cb(slider_event,lv.EVENT.VALUE_CHANGED,None)
197+
wifi_btn=lv.button(drawer)
198+
wifi_btn.set_size(lv.pct(40),lv.SIZE_CONTENT)
199+
wifi_btn.align(lv.ALIGN.LEFT_MID,0,0)
200+
wifi_label=lv.label(wifi_btn)
201+
wifi_label.set_text(lv.SYMBOL.WIFI+" WiFi")
202+
wifi_label.center()
203+
def wifi_event(e):
204+
global drawer_open
205+
close_drawer()
206+
start_app_by_name("com.example.wificonf")
207+
208+
wifi_btn.add_event_cb(wifi_event,lv.EVENT.CLICKED,None)
209+
#
210+
#settings_btn=lv.button(drawer)
211+
#settings_btn.set_size(BUTTON_WIDTH,BUTTON_HEIGHT)
212+
#settings_btn.align(lv.ALIGN.RIGHT_MID,-PADDING_MEDIUM,0)
213+
#settings_label=lv.label(settings_btn)
214+
#settings_label.set_text(lv.SYMBOL.SETTINGS+" Settings")
215+
#settings_label.center()
216+
#def settings_event(e):
217+
# global drawer_open
218+
# close_drawer()
219+
220+
#settings_btn.add_event_cb(settings_event,lv.EVENT.CLICKED,None)
221+
#
222+
launcher_btn=lv.button(drawer)
223+
launcher_btn.set_size(lv.pct(40),lv.SIZE_CONTENT)
224+
launcher_btn.align(lv.ALIGN.BOTTOM_LEFT,0,0)
225+
launcher_label=lv.label(launcher_btn)
226+
launcher_label.set_text(lv.SYMBOL.HOME+" Launcher")
227+
launcher_label.center()
228+
def launcher_event(e):
229+
print("Launcher button pressed!")
230+
global drawer_open
231+
close_drawer(True)
232+
show_launcher()
233+
234+
launcher_btn.add_event_cb(launcher_event,lv.EVENT.CLICKED,None)
235+
#
236+
restart_btn=lv.button(drawer)
237+
restart_btn.set_size(lv.pct(40),lv.SIZE_CONTENT)
238+
restart_btn.align(lv.ALIGN.RIGHT_MID,0,0)
239+
restart_label=lv.label(restart_btn)
240+
restart_label.set_text(lv.SYMBOL.POWER+" Reset")
241+
restart_label.center()
242+
restart_btn.add_event_cb(lambda event: machine.reset(),lv.EVENT.CLICKED,None)
243+

0 commit comments

Comments
 (0)