22
33import lvgl as lv
44import mpos .apps
5+ import mpos .battery_voltage
56import mpos .wifi
67from mpos .ui .anim import WidgetAnimator
78
1516
1617CLOCK_UPDATE_INTERVAL = 1000 # 10 or even 1 ms doesn't seem to change the framerate but 100ms is enough
1718WIFI_ICON_UPDATE_INTERVAL = 1500
19+ BATTERY_ICON_UPDATE_INTERVAL = 30000
1820TEMPERATURE_UPDATE_INTERVAL = 2000
1921MEMFREE_UPDATE_INTERVAL = 5000 # not too frequent because there's a forced gc.collect() to give it a reliable value
2022
@@ -157,10 +159,10 @@ def create_notification_bar():
157159time_label .align (lv .ALIGN .LEFT_MID ,0 ,0 )
158160temp_label = lv .label (notification_bar )
159161temp_label .set_text ("00°C" )
160- temp_label .align_to (time_label ,lv .ALIGN .OUT_RIGHT_MID ,NOTIFICATION_BAR_HEIGHT ,0 )
162+ temp_label .align_to (time_label ,lv .ALIGN .OUT_RIGHT_MID ,mpos . ui . pct_of_display_width ( 7 ) ,0 )
161163memfree_label = lv .label (notification_bar )
162164memfree_label .set_text ("" )
163- memfree_label .align_to (temp_label ,lv .ALIGN .OUT_RIGHT_MID ,NOTIFICATION_BAR_HEIGHT ,0 )
165+ memfree_label .align_to (temp_label ,lv .ALIGN .OUT_RIGHT_MID ,mpos . ui . pct_of_display_width ( 7 ) ,0 )
164166#style = lv.style_t()
165167#style.init()
166168#style.set_text_font(lv.font_montserrat_8) # tiny font
@@ -169,19 +171,21 @@ def create_notification_bar():
169171#notif_icon = lv.label(notification_bar)
170172#notif_icon.set_text(lv.SYMBOL.BELL)
171173#notif_icon.align_to(time_label, lv.ALIGN.OUT_RIGHT_MID, PADDING_TINY, 0)
174+ # Battery percentage
175+ battery_label = lv .label (notification_bar )
176+ battery_label .set_text ("100%" )
177+ battery_label .align (lv .ALIGN .RIGHT_MID ,0 ,0 )
178+ battery_label .add_flag (lv .obj .FLAG .HIDDEN )
172179# Battery icon
173180battery_icon = lv .label (notification_bar )
174181battery_icon .set_text (lv .SYMBOL .BATTERY_FULL )
175- battery_icon .align (lv .ALIGN .RIGHT_MID ,0 ,0 )
182+ battery_icon .align_to (battery_label ,lv .ALIGN .OUT_LEFT_MID ,0 ,0 )
183+ battery_icon .add_flag (lv .obj .FLAG .HIDDEN )
176184# WiFi icon
177185wifi_icon = lv .label (notification_bar )
178186wifi_icon .set_text (lv .SYMBOL .WIFI )
179- wifi_icon .align_to (battery_icon ,lv .ALIGN .OUT_LEFT_MID ,- NOTIFICATION_BAR_HEIGHT ,0 )
187+ wifi_icon .align_to (battery_icon ,lv .ALIGN .OUT_LEFT_MID ,- mpos . ui . pct_of_display_width ( 7 ) ,0 )
180188wifi_icon .add_flag (lv .obj .FLAG .HIDDEN )
181- # Battery percentage - not shown to conserve space
182- #battery_label = lv.label(notification_bar)
183- #battery_label.set_text("100%")
184- #battery_label.align(lv.ALIGN.RIGHT_MID, 0, 0)
185189# Update time
186190import time
187191def update_time (timer ):
@@ -197,6 +201,25 @@ def update_time(timer):
197201except Exception as e :
198202print ("Warning: could not check WLAN status:" ,str (e ))
199203
204+ def update_battery_icon (timer = None ):
205+ volt = mpos .battery_voltage .read_battery_voltage ()
206+ percent = (volt - 3.7 )* 100 / (4.2 - 3.7 )
207+ battery_label .set_text (f"{ round (percent )} %" )
208+ battery_label .remove_flag (lv .obj .FLAG .HIDDEN )
209+ # 3.7 - 4.15 => 0.5V diff / 3 = 0.015
210+ if volt > 4.15 :
211+ battery_icon .set_text (lv .SYMBOL .BATTERY_FULL )
212+ elif volt > 4 :
213+ battery_icon .set_text (lv .SYMBOL .BATTERY_3 )
214+ elif volt > 3.85 :
215+ battery_icon .set_text (lv .SYMBOL .BATTERY_2 )
216+ elif volt > 3.75 :
217+ battery_icon .set_text (lv .SYMBOL .BATTERY_1 )
218+ else :
219+ battery_icon .set_text (lv .SYMBOL .BATTERY_EMPTY )
220+ battery_icon .remove_flag (lv .obj .FLAG .HIDDEN )
221+ update_battery_icon ()# run it immediately instead of waiting for the timer
222+
200223def update_wifi_icon (timer ):
201224if mpos .wifi .WifiService .is_connected ():
202225wifi_icon .remove_flag (lv .obj .FLAG .HIDDEN )
@@ -229,6 +252,7 @@ def update_memfree(timer):
229252timer2 = lv .timer_create (update_temperature ,TEMPERATURE_UPDATE_INTERVAL ,None )
230253timer3 = lv .timer_create (update_memfree ,MEMFREE_UPDATE_INTERVAL ,None )
231254timer4 = lv .timer_create (update_wifi_icon ,WIFI_ICON_UPDATE_INTERVAL ,None )
255+ timer5 = lv .timer_create (update_battery_icon ,BATTERY_ICON_UPDATE_INTERVAL ,None )
232256
233257# hide bar animation
234258global hide_bar_animation
@@ -371,7 +395,7 @@ def poweroff_cb(e):
371395import machine
372396# DON'T configure BOOT button (Pin 0) as wake-up source because it wakes up immediately.
373397# Luckily, the RESET button can be used to wake it up.
374- #wake_pin = Pin(0, machine.Pin.IN, machine.Pin.PULL_UP) # Pull-up enabled, active low
398+ #wake_pin =machine. Pin(0, machine.Pin.IN, machine.Pin.PULL_UP) # Pull-up enabled, active low
375399#import esp32
376400#esp32.wake_on_ext0(pin=wake_pin, level=esp32.WAKEUP_ALL_LOW)
377401print ("Entering deep sleep. Press BOOT button to wake up." )