WatchyWatchFace/src/WatchFeatures/Battery.cpp
Lewis Jackson 5993fda85e
All checks were successful
Compile / Compile (push) Successful in 1m21s
Add averaging to reading battery levels
2023-06-01 16:17:40 +03:00

40 lines
888 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.6f) / 0.6f;
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);
}