Skip to content

Commit 4b3faa3

Browse files
Finialize battery icon
1 parent 77853bc commit 4b3faa3

File tree

2 files changed

+70
-32
lines changed

2 files changed

+70
-32
lines changed

internal_filesystem/lib/mpos/battery_voltage.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,68 @@
1616
ADC_MAX = 4095 # 12-bit ADC resolution
1717
VOLTAGE_DIVIDER = 3 # (R1 + R2) / R2 = (200k + 100k) / 100k = 3
1818

19+
MIN_VOLTAGE = 3.7
20+
MAX_VOLTAGE = 4.2
21+
22+
# USB connected, full battery: VBAT 4.179 5V: 5.1
23+
# read_battery_voltage raw_value: 1598.4
24+
# read_battery_voltage raw_value: 1598.1
25+
# read_battery_voltage raw_value: 1596.5
26+
# on display: 1596.8
27+
# => FULL is 1597 at 4.193V so * 2.6255 / 1000
28+
#
29+
# unplugged: VBAT: 4.157 5V: 0
30+
# 1591.3: 4.157 2.6123
31+
# 1588.5: 4.156
32+
# 1580.4: 4.14 2.619
33+
# 1577.8: 4.12
34+
# 1561.2: 4.08 2.61
35+
# 1555: 4.06
36+
# 1505: 3.95 2.624
37+
# 1489: 3.90
38+
# 1470: 3.85 2.61
39+
# 1454: 3.8
40+
# 1445: 3.81
41+
# 1412 should be empty 3.7
42+
#
43+
# USB connected, no battery:
44+
# 1566 * 0.00261 = 4.08V = 75%
45+
# 1566 * 0.00241 = 3.77V = 14%
46+
# 1564-1567
47+
#
48+
# quite empty and charging:
49+
# 1594: 4.18V
50+
# 16..
51+
#
52+
# logically, it should be * 0.00241758241758 but emperically, it seems to be * 0.00261 which is 8% higher
53+
# => Let's go with 0.00262
1954
def read_battery_voltage():
2055
if not have_adc:
2156
import random
22-
return random.randint(370,420) / 100
57+
random_voltage = random.randint(round(MIN_VOLTAGE*100),round(MAX_VOLTAGE*100)) / 100
58+
#print(f"returning random voltage: {random_voltage}")
59+
return random_voltage
2360
# Read raw ADC value
2461
total = 0
2562
# Read multiple times to try to reduce variability.
2663
# Reading 10 times takes around 3ms so it's fine...
2764
for _ in range(10):
2865
total = total + adc.read()
2966
raw_value = total / 10
67+
#print(f"read_battery_voltage raw_value: {raw_value}")
3068
# Convert to voltage, accounting for divider and reference
31-
voltage = (raw_value / ADC_MAX) * VREF * VOLTAGE_DIVIDER
69+
#voltage = (raw_value / ADC_MAX) * VREF * VOLTAGE_DIVIDER
70+
voltage = raw_value * 262 / 100000
3271
# Clamp to 0–4.2V range for LiPo battery
33-
voltage = max(0, min(voltage, 4.2))
72+
voltage = max(0, min(voltage, MAX_VOLTAGE))
73+
#return raw_value
3474
return voltage
3575

76+
# Could be interesting to keep a "rolling average" of the percentage so that it doesn't fluctuate too quickly
77+
def get_battery_percentage():
78+
return (read_battery_voltage() - MIN_VOLTAGE) * 100 / (MAX_VOLTAGE - MIN_VOLTAGE)
79+
80+
3681
# Main loop to read and print battery voltage
3782
if False:
3883
#for _ in range(10):

internal_filesystem/lib/mpos/ui/__init__.py

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
CLOCK_UPDATE_INTERVAL = 1000 # 10 or even 1 ms doesn't seem to change the framerate but 100ms is enough
1818
WIFI_ICON_UPDATE_INTERVAL = 1500
19-
BATTERY_ICON_UPDATE_INTERVAL = 30000
19+
BATTERY_ICON_UPDATE_INTERVAL = 5000
2020
TEMPERATURE_UPDATE_INTERVAL = 2000
2121
MEMFREE_UPDATE_INTERVAL = 5000 # not too frequent because there's a forced gc.collect() to give it a reliable value
2222

@@ -136,15 +136,8 @@ def init_rootscreen():
136136
rootlabel.set_text("Welcome to MicroPythonOS")
137137
rootlabel.center()
138138

139-
140-
timer1 = None
141-
timer2 = None
142-
timer3 = None
143-
timer4 = None
144-
145139
def create_notification_bar():
146140
global notification_bar
147-
global timer1, timer2, timer3, timer4
148141
# Create notification bar
149142
notification_bar = lv.obj(lv.layer_top())
150143
notification_bar.set_size(lv.pct(100), NOTIFICATION_BAR_HEIGHT)
@@ -172,15 +165,16 @@ def create_notification_bar():
172165
#notif_icon.set_text(lv.SYMBOL.BELL)
173166
#notif_icon.align_to(time_label, lv.ALIGN.OUT_RIGHT_MID, PADDING_TINY, 0)
174167
# 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)
168+
#battery_label = lv.label(notification_bar)
169+
#battery_label.set_text("100%")
170+
#battery_label.align(lv.ALIGN.RIGHT_MID, 0, 0)
171+
#battery_label.add_flag(lv.obj.FLAG.HIDDEN)
179172
# Battery icon
180173
battery_icon = lv.label(notification_bar)
181174
battery_icon.set_text(lv.SYMBOL.BATTERY_FULL)
182-
battery_icon.align_to(battery_label, lv.ALIGN.OUT_LEFT_MID, 0, 0)
183-
battery_icon.add_flag(lv.obj.FLAG.HIDDEN)
175+
#battery_icon.align_to(battery_label, lv.ALIGN.OUT_LEFT_MID, 0, 0)
176+
battery_icon.align(lv.ALIGN.RIGHT_MID, 0, 0)
177+
battery_icon.add_flag(lv.obj.FLAG.HIDDEN) # keep it hidden until it has a correct value
184178
# WiFi icon
185179
wifi_icon = lv.label(notification_bar)
186180
wifi_icon.set_text(lv.SYMBOL.WIFI)
@@ -202,22 +196,21 @@ def update_time(timer):
202196
print("Warning: could not check WLAN status:", str(e))
203197

204198
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:
199+
percent = mpos.battery_voltage.get_battery_percentage()
200+
if percent > 80: # 4.1V
211201
battery_icon.set_text(lv.SYMBOL.BATTERY_FULL)
212-
elif volt > 4:
202+
elif percent > 60: # 4.0V
213203
battery_icon.set_text(lv.SYMBOL.BATTERY_3)
214-
elif volt > 3.85:
204+
elif percent > 40: # 3.9V
215205
battery_icon.set_text(lv.SYMBOL.BATTERY_2)
216-
elif volt > 3.75:
206+
elif percent > 20: # 3.8V
217207
battery_icon.set_text(lv.SYMBOL.BATTERY_1)
218-
else:
208+
else: # > 3.7V
219209
battery_icon.set_text(lv.SYMBOL.BATTERY_EMPTY)
220210
battery_icon.remove_flag(lv.obj.FLAG.HIDDEN)
211+
# Percentage is not shown for now:
212+
#battery_label.set_text(f"{round(percent)}%")
213+
#battery_label.remove_flag(lv.obj.FLAG.HIDDEN)
221214
update_battery_icon() # run it immediately instead of waiting for the timer
222215

223216
def update_wifi_icon(timer):
@@ -248,11 +241,11 @@ def update_memfree(timer):
248241
percentage = round(free * 100 / (free + used))
249242
memfree_label.set_text(f"{percentage}%")
250243

251-
timer1 = lv.timer_create(update_time, CLOCK_UPDATE_INTERVAL, None)
252-
timer2 = lv.timer_create(update_temperature, TEMPERATURE_UPDATE_INTERVAL, None)
253-
timer3 = lv.timer_create(update_memfree, MEMFREE_UPDATE_INTERVAL, None)
254-
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)
244+
lv.timer_create(update_time, CLOCK_UPDATE_INTERVAL, None)
245+
lv.timer_create(update_temperature, TEMPERATURE_UPDATE_INTERVAL, None)
246+
lv.timer_create(update_memfree, MEMFREE_UPDATE_INTERVAL, None)
247+
lv.timer_create(update_wifi_icon, WIFI_ICON_UPDATE_INTERVAL, None)
248+
lv.timer_create(update_battery_icon, BATTERY_ICON_UPDATE_INTERVAL, None)
256249

257250
# hide bar animation
258251
global hide_bar_animation

0 commit comments

Comments
 (0)