Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
battery.h
Go to the documentation of this file.
1
5#ifndef COMPONENT_BATTERY_H
6#define COMPONENT_BATTERY_H
7
8#include <cstdint>
9
11
13
14namespace lampda {
15namespace component {
16
18namespace battery {
19
21extern uint16_t get_raw_battery_voltage_mv();
22
34extern bool can_battery_be_charged();
35
42inline uint16_t liion_mv_to_battery_percent(const uint16_t liionLevel_mv, const uint8_t batteryCountSerie)
43{
45 static curve_t liionVoltagePercentToRealPercent({// low end of the curve, sharp drop
46 curve_t::point_t {3000, 0},
47 curve_t::point_t {3210, 500},
48 curve_t::point_t {3350, 1000},
49 curve_t::point_t {3430, 1500},
50 curve_t::point_t {3470, 2000},
51 // linear approximation works well enough in range 20-90%
52 curve_t::point_t {4080, 9000},
53 curve_t::point_t {4110, 9500},
54 curve_t::point_t {4180, 10000}});
55
56 // sample the curve
57 return liionVoltagePercentToRealPercent.sample(liionLevel_mv / batteryCountSerie);
58}
59
65inline uint16_t get_level_percent(const uint16_t batteryVoltage_mV, const uint8_t cellCount = batteryCount)
66{
67 return liion_mv_to_battery_percent(batteryVoltage_mV, cellCount);
68}
69
73inline uint16_t get_level_safe(const uint16_t battery_mv, const uint8_t cellCount = batteryCount)
74{
75 // save the init values
76 static const uint16_t minSafeLevel_percent = get_level_percent(batteryMinVoltageSafe_mV);
77 static const uint16_t maxSafeLevel_percent = get_level_percent(batteryMaxVoltageSafe_mV);
78
79 // get the result of the total battery life, map it to the safe battery level
80 // indicated by user
81 return lmpd_constrain<uint16_t>(
82 lmpd_map<uint16_t>(
83 get_level_percent(battery_mv, cellCount), minSafeLevel_percent, maxSafeLevel_percent, 0, 10000),
84 0,
85 10000);
86}
87
89inline uint16_t get_battery_level()
90{
91 // get the result of the total battery life, map it to the safe battery level
92 // indicated by user
94}
95
100
105
106} // namespace battery
107} // namespace component
108} // namespace lampda
109
110#endif
Given a set of points, will fit multiple linear segments to it.
Definition: curves.h:35
U sample(const T x) const
Sample a point Y from a given x.
Definition: curves.h:82
Define curves types, that can be sampled.
uint16_t get_raw_battery_voltage_mv()
Return the battery voltage and raise the battery incoherent alert if needed.
Definition: battery.cpp:55
uint16_t get_level_safe(const uint16_t battery_mv, const uint8_t cellCount=batteryCount)
returns the battery level, mapped to the desired safe battery level
Definition: battery.h:73
uint16_t get_level_percent(const uint16_t batteryVoltage_mV, const uint8_t cellCount=batteryCount)
transform a battery voltage measurment to a percent * 100 estimate
Definition: battery.h:65
uint16_t liion_mv_to_battery_percent(const uint16_t liionLevel_mv, const uint8_t batteryCountSerie)
convert a single liion battery voltage to a percent level model
Definition: battery.h:42
uint16_t get_battery_minimum_cell_level()
Return the level of the cell of the stack with the minimum battery.
Definition: battery.cpp:91
uint16_t get_battery_level()
returns the battery level, corresponding to user safe choice (0-10000)
Definition: battery.h:89
bool can_battery_be_charged()
Return true if this battery can be charged Check only for validity, not voltage. If this is false,...
Definition: battery.cpp:89
bool is_battery_usable_as_power_source()
return true if the battery pack can be used as a energy source This status is updated after calling g...
Definition: battery.cpp:87
uint16_t get_battery_maximum_cell_level()
Return the level of the cell of the stack with the maximum battery.
Definition: battery.cpp:120
Program scope.
Definition: control_fixed_modes.hpp:12
static constexpr uint8_t batteryCount
number of batteries for this model
Definition: constants.h:54
static constexpr uint16_t batteryMaxVoltageSafe_mV
max voltage of a li-ion cell to maximise lifetime
Definition: constants.h:111
static constexpr uint16_t batteryMinVoltageSafe_mV
min voltage of a li-ion cell to maximise lifetime
Definition: constants.h:117
Define useful functions.