-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower.cpp
More file actions
60 lines (52 loc) · 1.78 KB
/
Copy pathPower.cpp
File metadata and controls
60 lines (52 loc) · 1.78 KB
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
#include "Power.h"
using LogPower = Tiny::TILogTarget<TinyCon::PowerLogLevel>;
void TinyCon::PowerController::Init()
{
#if defined(ADAFRUIT_FEATHER_ESP32S2)
pinMode(PIN_I2C_POWER, INPUT);
delay(1);
bool polarity = digitalRead(PIN_I2C_POWER);
pinMode(PIN_I2C_POWER, OUTPUT);
digitalWrite(PIN_I2C_POWER, !polarity);
#endif
#ifndef ESP32
analogReference(AR_INTERNAL_3_0);
#endif
pinMode(USBAdcPin, INPUT);
pinMode(BatteryAdcPin, INPUT);
#if USE_LC709203
if ((LC709203FPresent = LC709203F.begin(i2c)))
{
LC709203F.setThermistorB(3950);
LC709203F.setPackSize(LC709203F_APA_100MAH);
LC709203F.setAlarmVoltage(3.8);
}
#endif
Update();
}
void TinyCon::PowerController::Update()
{
PowerSource = PowerSources::I2C;
#if USE_LC709203
if (LC709203FPresent)
{
Battery.Voltage = LC709203F.cellVoltage();
Battery.Percentage = LC709203F.cellPercent();
}
else
#endif
if constexpr (BatteryAdcPin >= 0)
{
Battery.Voltage = analogRead(BatteryAdcPin) * (3.0f / 1024) * 2;
Battery.Percentage = 2.0f / Battery.Voltage - pow(4.3f - Battery.Voltage, 2) + 0.55f;
Battery.Percentage = min(1.0f, max(0.0f, Battery.Percentage));
if (Battery.Voltage > BatteryPresentVoltage) PowerSource = PowerSources::Battery;
}
if constexpr (USBAdcPin >= 0)
{
USBPowerVoltage = analogRead(USBAdcPin) * (3.0f / 1024) * 2;
if (USBPowerVoltage > USBPowerPresentVoltage) PowerSource = PowerSources::USB;
}
LogPower::Info("Power: ", PowerSource == PowerSources::USB ? "USB" : PowerSource == PowerSources::Battery ? "Battery" : "I2C",
", USB: ", USBPowerVoltage, 2, "V, Battery: ", Battery.Voltage, 2, "V, ", Battery.Percentage * 100, 0, "%", Tiny::TIEndl);
}