BatterySense
Arduino battery sensing library
Battery.h
1/*
2 Battery.h - Battery library
3 Copyright (c) 2014 Roberto Lo Giacco.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef BATTERY_H_
20#define BATTERY_H_
21
22#include <Arduino.h>
23
24typedef uint8_t(*mapFn_t)(uint16_t, uint16_t, uint16_t);
25
26class Battery {
27 public:
37 Battery(uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin, uint8_t adcBits = 10);
38
47 void begin(uint16_t refVoltage, float dividerRatio, mapFn_t = 0);
48
57 void onDemand(uint8_t activationPin, uint8_t activationMode = LOW);
58
62 static const uint8_t ON_DEMAND_DISABLE = 0xFF;
63
68 uint8_t level();
69 uint8_t level(uint16_t voltage);
70
75 uint16_t voltage(uint8_t delay = 2);
76
77 private:
78 uint16_t refVoltage;
79 uint16_t minVoltage;
80 uint16_t maxVoltage;
81 float dividerRatio;
82 uint8_t sensePin;
83 uint8_t activationPin;
84 uint8_t activationMode;
85 mapFn_t mapFunction;
86
87 const uint16_t adc;
88};
89
90//
91// Plots of the functions below available at
92// https://www.desmos.com/calculator/x0esk5bsrk
93//
94
101static inline uint8_t sigmoidal(uint16_t voltage, uint16_t minVoltage, uint16_t maxVoltage) {
102 // slow
103 // uint8_t result = 110 - (110 / (1 + pow(1.468 * (voltage - minVoltage)/(maxVoltage - minVoltage), 6)));
104
105 // steep
106 // uint8_t result = 102 - (102 / (1 + pow(1.621 * (voltage - minVoltage)/(maxVoltage - minVoltage), 8.1)));
107
108 // normal
109 uint8_t result = 105 - (105 / (1 + pow(1.724 * (voltage - minVoltage)/(maxVoltage - minVoltage), 5.5)));
110 return result >= 100 ? 100 : result;
111}
112
119static inline uint8_t asigmoidal(uint16_t voltage, uint16_t minVoltage, uint16_t maxVoltage) {
120 uint8_t result = 101 - (101 / pow(1 + pow(1.33 * (voltage - minVoltage)/(maxVoltage - minVoltage) ,4.5), 3));
121 return result >= 100 ? 100 : result;
122}
123
130static inline uint8_t linear(uint16_t voltage, uint16_t minVoltage, uint16_t maxVoltage) {
131 return (unsigned long)(voltage - minVoltage) * 100 / (maxVoltage - minVoltage);
132}
133#endif // BATTERY_H_
Definition: Battery.h:26
uint8_t level()
Definition: Battery.cpp:45
uint16_t voltage(uint8_t delay=2)
Definition: Battery.cpp:59
Battery(uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin, uint8_t adcBits=10)
Definition: Battery.cpp:22
static const uint8_t ON_DEMAND_DISABLE
Definition: Battery.h:62
void onDemand(uint8_t activationPin, uint8_t activationMode=LOW)
Definition: Battery.cpp:36
void begin(uint16_t refVoltage, float dividerRatio, mapFn_t=0)
Definition: Battery.cpp:29