WatchyWatchFace/src/WatchFeatures/Battery.cpp
leblane 93d7bde7dc
All checks were successful
Compile / Compile (push) Successful in 1m23s
Slightly tweak battery curve. 0% should be 3.65V now.
2023-06-15 19:46:28 +03:00

40 lines
890 B
C++

#include "../config.h"
#include "Battery.h"
#include <Wire.h>
#include <Arduino.h>
#include <limits>
float WatchFeatures::Battery::m_previousVoltage = std::numeric_limits<float>::infinity();
WatchFeatures::Battery::Battery()
{
}
float WatchFeatures::Battery::GetVoltage()
{
float voltage = analogReadMilliVolts(BATT_ADC_PIN) / 1000.0f * 2.0f;
if (m_previousVoltage == std::numeric_limits<float>::infinity()) {
m_previousVoltage = voltage;
}
float averageVoltage = (m_previousVoltage + voltage) / 2.0f;
m_previousVoltage = voltage;
return averageVoltage;
}
uint8_t WatchFeatures::Battery::GetPercentage()
{
float voltage = GetVoltage();
float level = (GetVoltage() - 3.65f) / 0.55f;
if (level < 0.0f) {
level = 0.0f;
} else if (level > 1.0f) {
level = 1.0f;
}
level = level * level * (3.0f - 2.0f * level);
return std::round(level * 100.0f);
}