40 lines
890 B
C++
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);
|
|
}
|