Skip to content
Snippets Groups Projects
Commit 5c2303e9 authored by q3k's avatar q3k Committed by q3k
Browse files

st3m: expose battery charge status

parent 1e6b42dc
No related branches found
No related tags found
1 merge request!188Q3k/fancy 1.2 features
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "py/obj.h" #include "py/obj.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "st3m_console.h" #include "st3m_console.h"
#include "st3m_io.h"
#include "st3m_usb.h" #include "st3m_usb.h"
#include "st3m_version.h" #include "st3m_version.h"
...@@ -372,6 +373,13 @@ STATIC mp_obj_t mp_i2c_scan(void) { ...@@ -372,6 +373,13 @@ STATIC mp_obj_t mp_i2c_scan(void) {
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_i2c_scan_obj, mp_i2c_scan); STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_i2c_scan_obj, mp_i2c_scan);
STATIC mp_obj_t mp_battery_charging(void) {
bool res = st3m_io_charger_state_get();
return mp_obj_new_bool(!res);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_battery_charging_obj, mp_battery_charging);
STATIC const mp_rom_map_elem_t globals_table[] = { STATIC const mp_rom_map_elem_t globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_scheduler_snapshot), { MP_ROM_QSTR(MP_QSTR_scheduler_snapshot),
MP_ROM_PTR(&mp_scheduler_snapshot_obj) }, MP_ROM_PTR(&mp_scheduler_snapshot_obj) },
...@@ -385,6 +393,8 @@ STATIC const mp_rom_map_elem_t globals_table[] = { ...@@ -385,6 +393,8 @@ STATIC const mp_rom_map_elem_t globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_usb_console_active), { MP_ROM_QSTR(MP_QSTR_usb_console_active),
MP_ROM_PTR(&mp_usb_console_active_obj) }, MP_ROM_PTR(&mp_usb_console_active_obj) },
{ MP_ROM_QSTR(MP_QSTR_i2c_scan), MP_ROM_PTR(&mp_i2c_scan_obj) }, { MP_ROM_QSTR(MP_QSTR_i2c_scan), MP_ROM_PTR(&mp_i2c_scan_obj) },
{ MP_ROM_QSTR(MP_QSTR_battery_charging),
MP_ROM_PTR(&mp_battery_charging_obj) },
{ MP_ROM_QSTR(MP_QSTR_RUNNING), MP_ROM_INT(eRunning) }, { MP_ROM_QSTR(MP_QSTR_RUNNING), MP_ROM_INT(eRunning) },
{ MP_ROM_QSTR(MP_QSTR_READY), MP_ROM_INT(eReady) }, { MP_ROM_QSTR(MP_QSTR_READY), MP_ROM_INT(eReady) },
......
...@@ -58,3 +58,7 @@ uint8_t st3m_io_badge_link_disable(uint8_t pin_mask); ...@@ -58,3 +58,7 @@ uint8_t st3m_io_badge_link_disable(uint8_t pin_mask);
* a while. Warn user. * a while. Warn user.
*/ */
uint8_t st3m_io_badge_link_enable(uint8_t pin_mask); uint8_t st3m_io_badge_link_enable(uint8_t pin_mask);
/* Returns true if the battery is currently being charged.
*/
bool st3m_io_charger_state_get();
...@@ -71,3 +71,4 @@ def usb_console_active() -> bool: ... ...@@ -71,3 +71,4 @@ def usb_console_active() -> bool: ...
def hardware_version() -> str: ... def hardware_version() -> str: ...
def firmware_version() -> str: ... def firmware_version() -> str: ...
def i2c_scan() -> list[int]: ... def i2c_scan() -> list[int]: ...
def battery_charging() -> bool: ...
import machine import machine
import time import time
import sys_kernel
class Power: class Power:
...@@ -29,3 +30,42 @@ class Power: ...@@ -29,3 +30,42 @@ class Power:
def battery_voltage(self) -> float: def battery_voltage(self) -> float:
self._update() self._update()
return self._battery_voltage return self._battery_voltage
@property
def battery_charging(self) -> bool:
"""
True if the battery is currently being charged.
"""
return sys_kernel.battery_charging()
def approximate_battery_percentage(voltage: float) -> float:
"""
Returns approximate battery percentage ([0,100]) based on battery voltage
(in volts).
"""
if voltage > 4.20:
return 100
piecewise = [
(100, 4.20),
(90, 4.06),
(80, 3.98),
(70, 3.92),
(60, 3.87),
(50, 3.82),
(40, 3.79),
(30, 3.77),
(20, 3.74),
(10, 3.68),
(5, 3.45),
(0, 3.00),
]
for (p1, v1), (p2, v2) in zip(piecewise, piecewise[1:]):
if voltage > v1 or voltage < v2:
continue
vr = v1 - v2
pr = p1 - p2
vd = voltage - v2
p = vd / vr
return pr * p + p2
return 0
...@@ -31,3 +31,7 @@ def freertos_sleep(ms): ...@@ -31,3 +31,7 @@ def freertos_sleep(ms):
def i2c_scan(): def i2c_scan():
return [16, 44, 45, 85, 109, 110] return [16, 44, 45, 85, 109, 110]
def battery_charging():
return True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment