2024-04-29 17:38:27 +02:00
|
|
|
#include "battery.hpp"
|
|
|
|
#include "portapack.hpp"
|
2024-10-06 22:14:27 +02:00
|
|
|
#include "i2cdevmanager.hpp"
|
2024-04-29 17:38:27 +02:00
|
|
|
namespace battery {
|
|
|
|
|
2024-08-28 11:32:24 +02:00
|
|
|
bool BatteryManagement::calcOverride = false;
|
2024-04-29 17:38:27 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-10-06 22:14:27 +02:00
|
|
|
// 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;
|
2024-04-29 17:38:27 +02:00
|
|
|
return batteryPercentage;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace battery
|