RSeries astromech firmware
BatteryMonitor.h
Go to the documentation of this file.
1 #include "ReelTwo.h"
2 #include "core/AnimatedEvent.h"
3 
4 #ifndef BatteryMonitor_h
5 #define BatteryMonitor_h
6 
8 {
9 public:
21  uint8_t pin,
22  uint16_t minVoltage = 3000,
23  uint16_t maxVoltage = 4200,
24  float scale = 2 * 3.3 * 1000,
25  unsigned (*mapping)(unsigned, unsigned, unsigned) = sigmoidal) :
26  fPin(pin),
27  fMinVoltage(minVoltage),
28  fMaxVoltage(maxVoltage),
29  fScale(scale),
30  fMapping(mapping)
31  {
32  }
33 
34  inline bool isBatteryPowered()
35  {
36  return isBatteryPowered(voltage());
37  }
38 
39  inline bool isBatteryPowered(uint32_t voltage)
40  {
41  return voltage < fMaxVoltage;
42  }
43 
44  inline void setRefreshCallback(uint32_t millis, void (*callback)())
45  {
46  fRefreshMillis = millis;
47  fCallback = callback;
48  }
49 
50  inline void setMapping(unsigned (*mapping)(unsigned, unsigned, unsigned))
51  {
52  fMapping = mapping;
53  }
54 
59  inline unsigned level()
60  {
61  return level(voltage());
62  }
63 
64  unsigned level(uint32_t voltage)
65  {
66  if (voltage <= fMinVoltage)
67  return 0;
68  if (voltage >= fMaxVoltage)
69  return 100;
70  return (*fMapping)(voltage, fMinVoltage, fMaxVoltage);
71  }
72 
76  uint32_t voltage()
77  {
78  #ifdef ESP32
79  uint32_t mV = (analogRead(fPin) * fScale) / 4096;
80  #else
81  uint32_t mV = (analogRead(fPin) * fScale) / 1024;
82  #endif
83  // Cache value
84  fMilliVolts = mV;
85  return mV;
86  }
87 
88  virtual void animate() override
89  {
90  if (fCallback != nullptr && fNextRefresh < millis())
91  {
92  fCallback();
93  fNextRefresh = millis() + fRefreshMillis;
94  }
95  }
96 
103  static unsigned sigmoidal(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
104  {
105  unsigned result = 105 - (105 / (1 + pow(1.724 * (voltage - minVoltage)/(maxVoltage - minVoltage), 5.5)));
106  return result >= 100 ? 100 : result;
107  }
108 
115  static inline unsigned asigmoidal(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
116  {
117  uint8_t result = 101 - (101 / pow(1 + pow(1.33 * (voltage - minVoltage)/(maxVoltage - minVoltage) ,4.5), 3));
118  return result >= 100 ? 100 : result;
119  }
120 
127  static inline unsigned linear(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
128  {
129  return (unsigned long)(voltage - minVoltage) * 100 / (maxVoltage - minVoltage);
130  }
131 
132 private:
133  uint8_t fPin;
134  unsigned fMinVoltage;
135  unsigned fMaxVoltage;
136  float fScale;
137  unsigned (*fMapping)(unsigned, unsigned, unsigned);
138  unsigned fMilliVolts = 0;
139  uint32_t fRefreshMillis = 1000;
140  uint32_t fNextRefresh = 0;
141  void (*fCallback)() = nullptr;
142 };
143 
144 #endif
ReelTwo.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
BatteryMonitor::level
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
BatteryMonitor::setRefreshCallback
void setRefreshCallback(uint32_t millis, void(*callback)())
Definition: BatteryMonitor.h:44
BatteryMonitor::linear
static unsigned linear(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
Linear mapping https://www.desmos.com/calculator/sowyhttjta.
Definition: BatteryMonitor.h:127
AnimatedEvent.h
BatteryMonitor::animate
virtual void animate() override
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: BatteryMonitor.h:88
BatteryMonitor::sigmoidal
static unsigned sigmoidal(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
Symmetric sigmoidal approximation https://www.desmos.com/calculator/7m9lu26vpy.
Definition: BatteryMonitor.h:103
BatteryMonitor
Definition: BatteryMonitor.h:7
BatteryMonitor::voltage
uint32_t voltage()
Returns the current battery voltage in millivolts.
Definition: BatteryMonitor.h:76
BatteryMonitor::level
unsigned level(uint32_t voltage)
Definition: BatteryMonitor.h:64
BatteryMonitor::isBatteryPowered
bool isBatteryPowered()
Definition: BatteryMonitor.h:34
BatteryMonitor::setMapping
void setMapping(unsigned(*mapping)(unsigned, unsigned, unsigned))
Definition: BatteryMonitor.h:50
BatteryMonitor::isBatteryPowered
bool isBatteryPowered(uint32_t voltage)
Definition: BatteryMonitor.h:39
BatteryMonitor::asigmoidal
static unsigned asigmoidal(unsigned voltage, unsigned minVoltage, unsigned maxVoltage)
Asymmetric sigmoidal approximation https://www.desmos.com/calculator/oyhpsu8jnw.
Definition: BatteryMonitor.h:115
BatteryMonitor::BatteryMonitor
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