30 lines
1.1 KiB
C++
Raw Normal View History

#include "battery.hpp"
#include "portapack.hpp"
#include "i2cdevmanager.hpp"
namespace battery {
2024-08-28 11:32:24 +02:00
bool BatteryManagement::calcOverride = false;
2024-08-28 11:32:24 +02:00
// set if the default percentage calculation should be overrided by voltage based one
void BatteryManagement::set_calc_override(bool override) {
calcOverride = override;
}
// Helper function to checkif there is ANY battery management ic present.
bool BatteryManagement::isDetected() {
auto dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_MAX17055);
if (dev) return true;
dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_ADS1110);
if (dev) return true;
2024-07-16 08:17:55 +12:00
return false;
}
2024-08-28 11:32:24 +02:00
uint8_t BatteryManagement::calc_percent_voltage(uint16_t voltage) {
// Calculate the remaining battery percentage
uint8_t batteryPercentage = (float)(voltage - BATTERY_MIN_VOLTAGE) / (float)(BATTERY_MAX_VOLTAGE - BATTERY_MIN_VOLTAGE) * 100.0;
// Limit the values to the valid range
batteryPercentage = (batteryPercentage > 100) ? 100 : batteryPercentage;
return batteryPercentage;
}
} // namespace battery