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():
157159 time_label .align (lv .ALIGN .LEFT_MID , 0 , 0 )
158160 temp_label = lv .label (notification_bar )
159161 temp_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 )
161163 memfree_label = lv .label (notification_bar )
162164 memfree_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
173180 battery_icon = lv .label (notification_bar )
174181 battery_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
177185 wifi_icon = lv .label (notification_bar )
178186 wifi_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 )
180188 wifi_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
186190 import time
187191 def update_time (timer ):
@@ -197,6 +201,25 @@ def update_time(timer):
197201 except Exception as e :
198202 print ("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+
200223 def update_wifi_icon (timer ):
201224 if mpos .wifi .WifiService .is_connected ():
202225 wifi_icon .remove_flag (lv .obj .FLAG .HIDDEN )
@@ -229,6 +252,7 @@ def update_memfree(timer):
229252 timer2 = lv .timer_create (update_temperature , TEMPERATURE_UPDATE_INTERVAL , None )
230253 timer3 = lv .timer_create (update_memfree , MEMFREE_UPDATE_INTERVAL , None )
231254 timer4 = 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
234258 global hide_bar_animation
@@ -371,7 +395,7 @@ def poweroff_cb(e):
371395 import 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)
377401 print ("Entering deep sleep. Press BOOT button to wake up." )
0 commit comments