|
RSeries astromech firmware
|
|
Go to the documentation of this file.
4 #ifndef BatteryMonitor_h
5 #define BatteryMonitor_h
22 uint16_t minVoltage = 3000,
23 uint16_t maxVoltage = 4200,
24 float scale = 2 * 3.3 * 1000,
25 unsigned (*mapping)(
unsigned,
unsigned,
unsigned) =
sigmoidal) :
27 fMinVoltage(minVoltage),
28 fMaxVoltage(maxVoltage),
46 fRefreshMillis = millis;
50 inline void setMapping(
unsigned (*mapping)(
unsigned,
unsigned,
unsigned))
70 return (*fMapping)(
voltage, fMinVoltage, fMaxVoltage);
79 uint32_t mV = (analogRead(fPin) * fScale) / 4096;
81 uint32_t mV = (analogRead(fPin) * fScale) / 1024;
90 if (fCallback !=
nullptr && fNextRefresh < millis())
93 fNextRefresh = millis() + fRefreshMillis;
105 unsigned result = 105 - (105 / (1 + pow(1.724 * (
voltage - minVoltage)/(maxVoltage - minVoltage), 5.5)));
106 return result >= 100 ? 100 : result;
117 uint8_t result = 101 - (101 / pow(1 + pow(1.33 * (
voltage - minVoltage)/(maxVoltage - minVoltage) ,4.5), 3));
118 return result >= 100 ? 100 : result;
127 static inline unsigned linear(
unsigned voltage,
unsigned minVoltage,
unsigned maxVoltage)
129 return (
unsigned long)(
voltage - minVoltage) * 100 / (maxVoltage - minVoltage);
134 unsigned fMinVoltage;
135 unsigned fMaxVoltage;
137 unsigned (*fMapping)(unsigned, unsigned, unsigned);
138 unsigned fMilliVolts = 0;
139 uint32_t fRefreshMillis = 1000;
140 uint32_t fNextRefresh = 0;
141 void (*fCallback)() =
nullptr;
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
unsigned level()
Returns the current battery level as a number between 0 and 100, with 0 indicating an empty battery a...
Definition: BatteryMonitor.h:59
void setRefreshCallback(uint32_t millis, void(*callback)())
Definition: BatteryMonitor.h:44
static unsigned linear(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
Linear mapping https://www.desmos.com/calculator/sowyhttjta.
Definition: BatteryMonitor.h:127
virtual void animate() override
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: BatteryMonitor.h:88
static unsigned sigmoidal(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
Symmetric sigmoidal approximation https://www.desmos.com/calculator/7m9lu26vpy.
Definition: BatteryMonitor.h:103
Definition: BatteryMonitor.h:7
uint32_t voltage()
Returns the current battery voltage in millivolts.
Definition: BatteryMonitor.h:76
unsigned level(uint32_t voltage)
Definition: BatteryMonitor.h:64
bool isBatteryPowered()
Definition: BatteryMonitor.h:34
void setMapping(unsigned(*mapping)(unsigned, unsigned, unsigned))
Definition: BatteryMonitor.h:50
bool isBatteryPowered(uint32_t voltage)
Definition: BatteryMonitor.h:39
static unsigned asigmoidal(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
Asymmetric sigmoidal approximation https://www.desmos.com/calculator/oyhpsu8jnw.
Definition: BatteryMonitor.h:115
BatteryMonitor(uint8_t pin, uint16_t minVoltage=3000, uint16_t maxVoltage=4200, float scale=2 *3.3 *1000, unsigned(*mapping)(unsigned, unsigned, unsigned)=sigmoidal)
Creates an instance to monitor battery voltage and level.
Definition: BatteryMonitor.h:20