-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathvariant.cpp
38 lines (32 loc) · 904 Bytes
/
variant.cpp
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
#include "Arduino.h"
float getBatteryVoltage() {
int analogVolt = analogReadMilliVolts(1);
float voltage = analogVolt / 1000.0;
voltage = voltage * (100.0+200.0) / 200.0;
return voltage;
}
float getBatteryCapacity() {
float voltage = getBatteryVoltage();
float capacity = (voltage - 3.3) / (4.2 - 3.3) * 100.0;
capacity = constrain(capacity, 0, 100);
return capacity;
}
bool getChargingState() {
pinMode(CHG, INPUT_PULLUP);
return !digitalRead(CHG);
}
void (*__onChargeStart__)();
void (*__onChargeEnd__)();
void setOnChargeStart(void (*func)()) { __onChargeStart__ = func; }
void setOnChargeEnd(void (*func)()) { __onChargeEnd__ = func; }
void ARDUINO_ISR_ATTR chargeIsr() {
if (getChargingState()) {
__onChargeStart__();
} else {
__onChargeEnd__();
}
}
extern "C" void initVariant(void){
attachInterrupt(CHG, chargeIsr, CHANGE);
analogReadResolution(12);
}