-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathpowerswitch.cpp
More file actions
39 lines (35 loc) · 872 Bytes
/
powerswitch.cpp
File metadata and controls
39 lines (35 loc) · 872 Bytes
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
#include <Arduino.h>
#include "powerswitch.h"
#include "config.h"
#include "batterystate.h"
#include "log.h"
#include "battery.h"
int lastState = LOW;
void powerSwitchInit() {
pinMode(POWER_ENABLE_PIN, OUTPUT);
digitalWrite(POWER_ENABLE_PIN, LOW);
}
void powerSwitchUpdate() {
float vBatCell = batteryStateGetVoltageAverage() / VBAT_CELL_COUNT;
const Battery& battery = getBattery();
int newState;
if (vBatCell > battery.vRecovery) {
newState = HIGH;
} else if (vBatCell < battery.vCritical) {
newState = LOW;
} else {
newState = lastState;
}
if (newState != lastState) {
digitalWrite(POWER_ENABLE_PIN, newState);
lastState = newState;
#ifdef POWER_SWITCH_LOG
batteryStateLog();
if (newState == HIGH) {
logLn("power turned on");
} else {
logLn("power turned off");
}
#endif
}
}