Skip to content

Commit 7ae8580

Browse files
committed
Enhance ShowBattery and fixes for Odroid-GO battery
Add a "Real-time values" checkbox to ShowBattery app. If checked, then the cache will be clear on every cycle. Bugfix: Use `mpos.time.localtime()` to get the "correct" local time with timezone offset. Changes for Odroid-GO: The seen "2400" values are at startup the Odroid-GO... After a while the values are somewhere between 290 and 310... The full range is unknown, yet. But with the new calculation it looks more realistic, then before ;)
1 parent 8143dd7 commit 7ae8580

3 files changed

Lines changed: 39 additions & 13 deletions

File tree

internal_filesystem/apps/com.micropythonos.showbattery/META-INF/MANIFEST.JSON

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"icon_url": "https://apps.micropythonos.com/apps/com.micropythonos.showbattery/icons/com.micropythonos.showbattery_0.1.1_64x64.png",
77
"download_url": "https://apps.micropythonos.com/apps/com.micropythonos.showbattery/mpks/com.micropythonos.showbattery_0.1.1.mpk",
88
"fullname": "com.micropythonos.showbattery",
9-
"version": "0.1.1",
9+
"version": "0.2.0",
1010
"category": "development",
1111
"activities": [
1212
{

internal_filesystem/apps/com.micropythonos.showbattery/assets/show_battery.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
"""
4141

4242
import lvgl as lv
43-
import time
44-
43+
import mpos.time
4544
from mpos import Activity, BatteryManager
4645

4746
HISTORY_LEN = 60
@@ -56,11 +55,13 @@ class ShowBattery(Activity):
5655
# Widgets
5756
lbl_time = None
5857
lbl_sec = None
59-
lbl_date = None
58+
lbl_text = None
6059

6160
bat_outline = None
6261
bat_fill = None
6362

63+
clear_cache_checkbox = None # Add reference to checkbox
64+
6465
history_v = []
6566
history_p = []
6667

@@ -69,16 +70,21 @@ def onCreate(self):
6970

7071
# --- TIME ---
7172
self.lbl_time = lv.label(scr)
72-
self.lbl_time.set_style_text_font(lv.font_montserrat_48, 0)
73+
self.lbl_time.set_style_text_font(lv.font_montserrat_40, 0)
7374
self.lbl_time.align(lv.ALIGN.TOP_LEFT, 5, 5)
7475

7576
self.lbl_sec = lv.label(scr)
7677
self.lbl_sec.set_style_text_font(lv.font_montserrat_24, 0)
7778
self.lbl_sec.align_to(self.lbl_time, lv.ALIGN.OUT_RIGHT_BOTTOM, 24, -4)
7879

79-
self.lbl_date = lv.label(scr)
80-
self.lbl_date.set_style_text_font(lv.font_montserrat_24, 0)
81-
self.lbl_date.align(lv.ALIGN.TOP_LEFT, 5, 60)
80+
# --- CHECKBOX ---
81+
self.clear_cache_checkbox = lv.checkbox(scr)
82+
self.clear_cache_checkbox.set_text("Real-time values")
83+
self.clear_cache_checkbox.align(lv.ALIGN.TOP_LEFT, 5, 50)
84+
85+
self.lbl_text = lv.label(scr)
86+
self.lbl_text.set_style_text_font(lv.font_montserrat_16, 0)
87+
self.lbl_text.align(lv.ALIGN.TOP_LEFT, 5, 80)
8288

8389
# --- BATTERY ICON ---
8490
self.bat_outline = lv.obj(scr)
@@ -126,19 +132,26 @@ def onResume(self, screen):
126132
super().onResume(screen)
127133

128134
def update(timer):
129-
now = time.localtime()
135+
now = mpos.time.localtime()
130136

131137
hour, minute, second = now[3], now[4], now[5]
132138
date = f"{now[0]}-{now[1]:02}-{now[2]:02}"
133139

140+
if self.clear_cache_checkbox.get_state() & lv.STATE.CHECKED:
141+
# Get "real-time" values by clearing the cache before reading
142+
BatteryManager.clear_cache()
143+
134144
voltage = BatteryManager.read_battery_voltage()
135145
percent = BatteryManager.get_battery_percentage()
136146

137147
# --- TIME ---
138148
self.lbl_time.set_text(f"{hour:02}:{minute:02}")
139149
self.lbl_sec.set_text(f":{second:02}")
150+
151+
# --- BATTERY VALUES ---
140152
date += f"\n{voltage:.2f}V {percent:.0f}%"
141-
self.lbl_date.set_text(date)
153+
date += f"\nRaw ADC: {BatteryManager.read_raw_adc()}"
154+
self.lbl_text.set_text(date)
142155

143156
# --- BATTERY ICON ---
144157
fill_h = int((percent / 100) * (self.bat_size * 0.9))

internal_filesystem/lib/mpos/board/odroid_go.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
# Misc settings:
4949
LED_BLUE = const(2)
5050
BATTERY_PIN = const(36)
51-
BATTERY_RESISTANCE_NUM = const(2)
5251
SPEAKER_ENABLE_PIN = const(25)
5352
SPEAKER_PIN = const(26)
5453

@@ -105,8 +104,21 @@
105104
from mpos import BatteryManager
106105

107106

108-
def adc_to_voltage(adc_value):
109-
return adc_value * BATTERY_RESISTANCE_NUM
107+
def adc_to_voltage(raw_adc_value):
108+
"""
109+
The percentage calculation uses MIN_VOLTAGE = 3.15 and MAX_VOLTAGE = 4.15
110+
0% at 3.15V -> raw_adc_value = 290
111+
100% at 4.15V -> raw_adc_value = 310
112+
113+
4.15 - 3.15 = 1V
114+
310 - 290 = 20 raw ADC steps
115+
116+
So each raw ADC step is 1V / 20 = 0.05V
117+
Offset calculation:
118+
290 * 0.05 = 14.5V, but we want it to be 3.15V, so the offset is 3.15V - 14.5V = -11.35V
119+
"""
120+
voltage = raw_adc_value * 0.05 - 11.35
121+
return voltage
110122

111123

112124
BatteryManager.init_adc(BATTERY_PIN, adc_to_voltage)
@@ -183,6 +195,7 @@ def input_callback(indev, data):
183195
current_key = lv.KEY.ESC
184196
elif button_volume.value() == 0:
185197
print("Volume button pressed -> reset")
198+
blue_led.on()
186199
machine.reset()
187200
elif button_select.value() == 0:
188201
current_key = lv.KEY.BACKSPACE

0 commit comments

Comments
 (0)