forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatterySensor.h
346 lines (289 loc) · 9.74 KB
/
BatterySensor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __BATTERYSENSOR_H__
#define __BATTERYSENSOR_H__
#include <Debug.h>
#include <AlarmClock.h>
#ifdef ARDUINO_ARCH_AVR
#include <avr/power.h>
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
#define ADMUX_VCCWRT1V1 (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1))
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
#define ADMUX_VCCWRT1V1 (_BV(MUX5) | _BV(MUX0))
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#define ADMUX_VCCWRT1V1 (_BV(MUX3) | _BV(MUX2))
#else
#define ADMUX_VCCWRT1V1 (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1))
#endif
#endif
namespace as {
class NoBattery {
public:
uint8_t current () const { return 0; }
bool critical () const { return false; }
bool low () const { return false; }
void setIdle () {}
void unsetIdle () {}
};
#ifdef ARDUINO_ARCH_AVR
#define ADMUX_ADCMASK ((1 << MUX3)|(1 << MUX2)|(1 << MUX1)|(1 << MUX0))
#define ADMUX_REFMASK ((1 << REFS1)|(1 << REFS0))
#define ADMUX_REF_AREF ((0 << REFS1)|(0 << REFS0))
#define ADMUX_REF_AVCC ((0 << REFS1)|(1 << REFS0))
#define ADMUX_REF_RESV ((1 << REFS1)|(0 << REFS0))
#define ADMUX_REF_VBG ((1 << REFS1)|(1 << REFS0))
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
#define ADMUX_ADC_VBG ((1 << MUX4)|(1 << MUX3)|(1 << MUX2)|(1 << MUX1)|(0 << MUX0))
#else
#define ADMUX_ADC_VBG ((1 << MUX3)|(1 << MUX2)|(1 << MUX1)|(0 << MUX0))
#endif
#endif
class InternalVCC {
public:
typedef uint16_t ValueType;
static const int DefaultDelay = 0;
void init () {
#ifdef ARDUINO_ARCH_STM32F1
adc_reg_map *regs = ADC1->regs;
regs->CR2 |= ADC_CR2_TSVREFE; // enable VREFINT and temp sensor
regs->SMPR1 = ADC_SMPR1_SMP17; // sample rate for VREFINT ADC channel
#endif
}
void start () {}
uint16_t finish () {
uint16_t vcc=0;
#ifdef ARDUINO_ARCH_AVR
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
ADMUX &= ~(ADMUX_REFMASK | ADMUX_ADCMASK);
ADMUX |= ADMUX_REF_AVCC; // select AVCC as reference
ADMUX |= ADMUX_ADC_VBG; // measure bandgap reference voltage
_delay_us(350);
ADCSRA |= (1 << ADSC); // start conversion
while (ADCSRA & (1 << ADSC)) ; // wait to finish
vcc = 1100UL * 1024 / ADC;
#elif defined ARDUINO_ARCH_STM32F1
vcc = 1200 * 4096 / adc_read(ADC1, 17); // ADC sample to millivolts
#endif
DPRINT(F("iVcc: ")); DDECLN(vcc);
return vcc;
}
};
template<uint8_t SENSPIN, uint8_t ACTIVATIONPIN, uint8_t ACTIVATIONSTATE=LOW, uint16_t VCC=3300, uint8_t FACTOR=57>
class ExternalVCC : public InternalVCC {
public:
static const int DefaultDelay = 250;
void init () {
pinMode(SENSPIN, INPUT);
pinMode(ACTIVATIONPIN, INPUT);
}
void start () {
pinMode(ACTIVATIONPIN, OUTPUT);
digitalWrite(ACTIVATIONPIN, ACTIVATIONSTATE==LOW ? LOW : HIGH);
digitalWrite(SENSPIN,LOW);
// analogRead(SENSPIN);
}
uint16_t finish () {
uint32_t value = analogRead(SENSPIN);
digitalWrite(SENSPIN,HIGH);
digitalWrite(ACTIVATIONPIN, ACTIVATIONSTATE==LOW ? HIGH : LOW);
pinMode(ACTIVATIONPIN,INPUT);
uint16_t refvcc = VCC;
if( refvcc == 0 ) {
InternalVCC::start(); // in case we add something here later
refvcc = InternalVCC::finish();
}
uint16_t vin = (value * refvcc * FACTOR) / 1024 / 10;
DPRINT(F("eVcc: ")); DDECLN(vin);
return vin;
}
};
template <class SENSOR,int DELAY=SENSOR::DefaultDelay>
class SyncMeter {
SENSOR m_Sensor;
volatile typename SENSOR::ValueType m_Value;
public:
SyncMeter () : m_Value(0) {}
void start () {
sensor().start();
if( DELAY > 0 ) {
_delay_ms(DELAY);
}
typename SENSOR::ValueType tmp = sensor().finish();
if( m_Value == 0 || tmp < m_Value ) {
m_Value = tmp;
}
}
typename SENSOR::ValueType value () const { return m_Value; }
SENSOR& sensor () { return m_Sensor; }
typename SENSOR::ValueType measure () {
start();
return value();
}
};
template <class SENSOR,int DELAY=SENSOR::DefaultDelay>
class AsyncMeter : public Alarm {
SENSOR m_Sensor;
volatile typename SENSOR::ValueType m_Value;
public:
AsyncMeter () : Alarm(0), m_Value(0) {}
virtual ~AsyncMeter () {}
virtual void trigger (__attribute__((unused)) AlarmClock& clock) {
typename SENSOR::ValueType tmp = sensor().finish();
if( m_Value == 0 || tmp < m_Value ) {
m_Value = tmp;
}
}
void start () {
sensor().start();
set(millis2ticks(DELAY));
sysclock.add(*this);
}
typename SENSOR::ValueType value () const { return m_Value; }
SENSOR& sensor () { return m_Sensor; }
typename SENSOR::ValueType measure () {
sensor().start();
_delay_ms(DELAY);
m_Value = sensor().finish();
return value();
}
};
template <class METER>
class BattSensor : public Alarm {
uint32_t m_Period;
uint8_t m_Low, m_Critical;
METER m_Meter;
public:
BattSensor () : Alarm(0), m_Period(0), m_Low(0), m_Critical(0) {}
virtual ~BattSensor() {}
virtual void trigger (AlarmClock& clock) {
tick = m_Period;
clock.add(*this);
m_Meter.start();
}
uint8_t current () const { return (m_Meter.value() + 50) / 100; }
bool critical () const { return current() < m_Critical; }
void critical (uint8_t value ) { m_Critical = value; }
bool low () const { return current() < m_Low; }
void low (uint8_t value ) { m_Low = value; }
void init(uint32_t period,AlarmClock& clock) {
m_Meter.sensor().init();
m_Meter.measure();
m_Period = period;
set(m_Period);
clock.add(*this);
}
// for backward compatibility
uint16_t voltageHighRes() { return m_Meter.value(); }
uint8_t voltage() { return current(); }
METER& meter () { return m_Meter; }
void setIdle () {}
void unsetIdle () {}
};
typedef BattSensor<SyncMeter<InternalVCC> > BatterySensor;
template <uint8_t SENSPIN,uint8_t ACTIVATIONPIN,uint16_t VCC=3300>
class BatterySensorUni : public BattSensor<SyncMeter<ExternalVCC<SENSPIN,ACTIVATIONPIN,LOW,VCC,57> > > {
public:
BatterySensorUni () {}
virtual ~BatterySensorUni () {}
};
/**
* Measure on analog pin
* See https://github.com/rlogiacco/BatterySense for setup
*/
template <uint8_t SENSPIN,uint8_t ACTIVATIONPIN,uint16_t VCC=3300>
class BatterySensorExt : public BatterySensor {
uint8_t m_SensePin;
uint8_t m_ActivationPin;
uint8_t m_DividerRatio;
uint16_t m_RefVoltage;
public:
BatterySensorExt () : BatterySensor (),
m_SensePin(SENSPIN), m_ActivationPin(ACTIVATIONPIN), m_DividerRatio(2), m_RefVoltage(VCC) {}
virtual ~BatterySensorExt () {}
void init(uint32_t period,AlarmClock& clock,uint16_t refvolt=VCC,uint8_t divider=2) {
m_DividerRatio=divider;
m_RefVoltage = refvolt;
pinMode(m_SensePin, INPUT);
if (m_ActivationPin < 0xFF) {
pinMode(m_ActivationPin, OUTPUT);
}
BatterySensor::init(period,clock);
}
virtual uint8_t voltage () {
if (m_ActivationPin != 0xFF) {
digitalWrite(m_ActivationPin, HIGH);
_delay_us(10); // copes with slow switching activation circuits
}
analogRead(m_SensePin);
_delay_ms(2); // allow the ADC to stabilize
uint32_t value = analogRead(m_SensePin);
uint16_t vcc = (value * m_DividerRatio * m_RefVoltage) / 1024 / 100;
if (m_ActivationPin != 0xFF) {
digitalWrite(m_ActivationPin, LOW);
}
DPRINT(F("Bat: ")); DDECLN(vcc);
return (uint8_t)vcc;
}
};
#ifdef ARDUINO_ARCH_AVR
extern volatile uint16_t __gb_BatCurrent;
extern void (*__gb_BatIrq)();
class IrqInternalBatt {
uint8_t m_Low, m_Critical;
public:
IrqInternalBatt () : m_Low(0), m_Critical(0) {}
~IrqInternalBatt() {}
uint8_t current () const { return (__gb_BatCurrent + 50) / 100; }
bool critical () const { return current() < m_Critical; }
void critical (uint8_t value ) { m_Critical = value; }
bool low () const { return current() < m_Low; }
void low (uint8_t value ) { m_Low = value; }
void init(__attribute__((unused)) uint32_t period,__attribute__((unused)) AlarmClock& clock) {
pinMode(17,OUTPUT); // debug interrupt
unsetIdle();
}
// for backward compatibility
uint16_t voltageHighRes() { return __gb_BatCurrent; }
uint8_t voltage() { return current(); }
void setIdle () {
__gb_BatIrq = 0;
ADCSRA &= ~((1 << ADIE) | (1 << ADIF)); // disable interrupt
while (ADCSRA & (1 << ADSC)) ; // wait finish
irq(); // ensure value is read
}
void unsetIdle () {
//DDECLN(__gb_BatCurrent);
__gb_BatIrq = irq;
ADMUX &= ~(ADMUX_REFMASK | ADMUX_ADCMASK);
ADMUX |= ADMUX_REF_AVCC; // select AVCC as reference
ADMUX |= ADMUX_ADC_VBG; // measure bandgap reference voltage
ADCSRA |= (1 << ADIE); // enable interrupt
ADCSRA |= (1 << ADSC); // start conversion
}
static void irq () {
uint16_t v = 1100UL * 1024 / ADC;
if( __gb_BatCurrent == 0 ) {
__gb_BatCurrent = v;
}
else {
v = (__gb_BatCurrent + v) / 2;
if( v < __gb_BatCurrent ) {
__gb_BatCurrent = v;
}
}
digitalWrite(17, digitalRead(17)==LOW?HIGH:LOW); // debug
if( __gb_BatIrq != 0 )
ADCSRA |= (1 << ADSC); // start conversion again
}
};
ISR(ADC_vect) {
if( __gb_BatIrq != 0 ) {
__gb_BatIrq();
}
}
#endif
}
#endif