You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
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 270 and 310... The full range is unknown, yet. But with the new calculation it looks more realistic, then before ;)
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
"icon_url": "https://apps.micropythonos.com/apps/com.micropythonos.showbattery/icons/com.micropythonos.showbattery_0.1.1_64x64.png",
|
||||
"download_url": "https://apps.micropythonos.com/apps/com.micropythonos.showbattery/mpks/com.micropythonos.showbattery_0.1.1.mpk",
|
||||
"fullname": "com.micropythonos.showbattery",
|
||||
"version": "0.1.1",
|
||||
"version": "0.2.0",
|
||||
"category": "development",
|
||||
"activities": [
|
||||
{
|
||||
|
||||
@@ -40,8 +40,7 @@ I want application that will show big time (hour, minutes), with smaller seconds
|
||||
"""
|
||||
|
||||
import lvgl as lv
|
||||
import time
|
||||
|
||||
import mpos.time
|
||||
from mpos import Activity, BatteryManager
|
||||
|
||||
HISTORY_LEN = 60
|
||||
@@ -56,11 +55,13 @@ class ShowBattery(Activity):
|
||||
# Widgets
|
||||
lbl_time = None
|
||||
lbl_sec = None
|
||||
lbl_date = None
|
||||
lbl_text = None
|
||||
|
||||
bat_outline = None
|
||||
bat_fill = None
|
||||
|
||||
clear_cache_checkbox = None # Add reference to checkbox
|
||||
|
||||
history_v = []
|
||||
history_p = []
|
||||
|
||||
@@ -69,16 +70,21 @@ class ShowBattery(Activity):
|
||||
|
||||
# --- TIME ---
|
||||
self.lbl_time = lv.label(scr)
|
||||
self.lbl_time.set_style_text_font(lv.font_montserrat_48, 0)
|
||||
self.lbl_time.set_style_text_font(lv.font_montserrat_40, 0)
|
||||
self.lbl_time.align(lv.ALIGN.TOP_LEFT, 5, 5)
|
||||
|
||||
self.lbl_sec = lv.label(scr)
|
||||
self.lbl_sec.set_style_text_font(lv.font_montserrat_24, 0)
|
||||
self.lbl_sec.align_to(self.lbl_time, lv.ALIGN.OUT_RIGHT_BOTTOM, 24, -4)
|
||||
|
||||
self.lbl_date = lv.label(scr)
|
||||
self.lbl_date.set_style_text_font(lv.font_montserrat_24, 0)
|
||||
self.lbl_date.align(lv.ALIGN.TOP_LEFT, 5, 60)
|
||||
# --- CHECKBOX ---
|
||||
self.clear_cache_checkbox = lv.checkbox(scr)
|
||||
self.clear_cache_checkbox.set_text("Real-time values")
|
||||
self.clear_cache_checkbox.align(lv.ALIGN.TOP_LEFT, 5, 50)
|
||||
|
||||
self.lbl_text = lv.label(scr)
|
||||
self.lbl_text.set_style_text_font(lv.font_montserrat_16, 0)
|
||||
self.lbl_text.align(lv.ALIGN.TOP_LEFT, 5, 80)
|
||||
|
||||
# --- BATTERY ICON ---
|
||||
self.bat_outline = lv.obj(scr)
|
||||
@@ -126,19 +132,26 @@ class ShowBattery(Activity):
|
||||
super().onResume(screen)
|
||||
|
||||
def update(timer):
|
||||
now = time.localtime()
|
||||
now = mpos.time.localtime()
|
||||
|
||||
hour, minute, second = now[3], now[4], now[5]
|
||||
date = f"{now[0]}-{now[1]:02}-{now[2]:02}"
|
||||
|
||||
if self.clear_cache_checkbox.get_state() & lv.STATE.CHECKED:
|
||||
# Get "real-time" values by clearing the cache before reading
|
||||
BatteryManager.clear_cache()
|
||||
|
||||
voltage = BatteryManager.read_battery_voltage()
|
||||
percent = BatteryManager.get_battery_percentage()
|
||||
|
||||
# --- TIME ---
|
||||
self.lbl_time.set_text(f"{hour:02}:{minute:02}")
|
||||
self.lbl_sec.set_text(f":{second:02}")
|
||||
|
||||
# --- BATTERY VALUES ---
|
||||
date += f"\n{voltage:.2f}V {percent:.0f}%"
|
||||
self.lbl_date.set_text(date)
|
||||
date += f"\nRaw ADC: {BatteryManager.read_raw_adc()}"
|
||||
self.lbl_text.set_text(date)
|
||||
|
||||
# --- BATTERY ICON ---
|
||||
fill_h = int((percent / 100) * (self.bat_size * 0.9))
|
||||
|
||||
@@ -48,7 +48,6 @@ CROSSBAR_Y = const(35)
|
||||
# Misc settings:
|
||||
LED_BLUE = const(2)
|
||||
BATTERY_PIN = const(36)
|
||||
BATTERY_RESISTANCE_NUM = const(2)
|
||||
SPEAKER_ENABLE_PIN = const(25)
|
||||
SPEAKER_PIN = const(26)
|
||||
|
||||
@@ -105,8 +104,22 @@ print("odroid_go.py Battery initialization...")
|
||||
from mpos import BatteryManager
|
||||
|
||||
|
||||
def adc_to_voltage(adc_value):
|
||||
return adc_value * BATTERY_RESISTANCE_NUM
|
||||
def adc_to_voltage(raw_adc_value):
|
||||
"""
|
||||
The percentage calculation uses MIN_VOLTAGE = 3.15 and MAX_VOLTAGE = 4.15
|
||||
0% at 3.15V -> raw_adc_value = 270
|
||||
100% at 4.15V -> raw_adc_value = 310
|
||||
|
||||
4.15 - 3.15 = 1V
|
||||
310 - 270 = 40 raw ADC steps
|
||||
|
||||
So each raw ADC step is 1V / 40 = 0.025V
|
||||
Offset calculation:
|
||||
270 * 0.025 = 6.75V. but we want it to be 3.15V
|
||||
So the offset is 3.15V - 6.75V = -3.6V
|
||||
"""
|
||||
voltage = raw_adc_value * 0.025 - 3.6
|
||||
return voltage
|
||||
|
||||
|
||||
BatteryManager.init_adc(BATTERY_PIN, adc_to_voltage)
|
||||
@@ -183,6 +196,7 @@ def input_callback(indev, data):
|
||||
current_key = lv.KEY.ESC
|
||||
elif button_volume.value() == 0:
|
||||
print("Volume button pressed -> reset")
|
||||
blue_led.on()
|
||||
machine.reset()
|
||||
elif button_select.value() == 0:
|
||||
current_key = lv.KEY.BACKSPACE
|
||||
|
||||
Reference in New Issue
Block a user