mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-08-14 00:17:39 +00:00
Battery info switcher (#2230)
This commit is contained in:
@@ -27,9 +27,6 @@
|
||||
namespace battery {
|
||||
namespace ads1110 {
|
||||
|
||||
constexpr uint16_t BATTERY_MIN_VOLTAGE = 3000;
|
||||
constexpr uint16_t BATTERY_MAX_VOLTAGE = 4000;
|
||||
|
||||
void ADS1110::init() {
|
||||
if (!detected_) {
|
||||
detected_ = detect();
|
||||
@@ -118,15 +115,8 @@ uint16_t ADS1110::readVoltage() {
|
||||
return (uint16_t)voltage;
|
||||
}
|
||||
|
||||
void ADS1110::getBatteryInfo(uint8_t& valid_mask, uint8_t& batteryPercentage, uint16_t& voltage) {
|
||||
void ADS1110::getBatteryInfo(uint8_t& valid_mask, uint16_t& voltage) {
|
||||
voltage = readVoltage();
|
||||
|
||||
// Calculate the remaining battery percentage
|
||||
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;
|
||||
// ToDo: if its > 4, then 100%, if < 3 then 0%
|
||||
valid_mask = 1; // BATT_VALID_VOLTAGE
|
||||
}
|
||||
|
||||
|
@@ -42,7 +42,7 @@ class ADS1110 {
|
||||
bool isDetected() const { return detected_; }
|
||||
|
||||
uint16_t readVoltage();
|
||||
void getBatteryInfo(uint8_t& valid_mask, uint8_t& batteryPercentage, uint16_t& voltage);
|
||||
void getBatteryInfo(uint8_t& valid_mask, uint16_t& voltage);
|
||||
|
||||
private:
|
||||
I2C& bus;
|
||||
|
@@ -18,6 +18,7 @@ ads1110::ADS1110 battery_ads1110{portapack::i2c0, 0x48};
|
||||
max17055::MAX17055 battery_max17055{portapack::i2c0, 0x36};
|
||||
|
||||
Thread* BatteryManagement::thread = nullptr;
|
||||
bool BatteryManagement::calcOverride = false;
|
||||
|
||||
void BatteryManagement::detect() {
|
||||
// try to detect supported modules
|
||||
@@ -43,22 +44,33 @@ void BatteryManagement::detect() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void BatteryManagement::init() {
|
||||
void BatteryManagement::init(bool override) {
|
||||
calcOverride = override;
|
||||
detect();
|
||||
// sets timer to query and broadcats this info
|
||||
create_thread();
|
||||
}
|
||||
|
||||
// set if the default percentage calculation should be overrided by voltage based one
|
||||
void BatteryManagement::set_calc_override(bool override) {
|
||||
calcOverride = override;
|
||||
}
|
||||
|
||||
// sets the values, it the currend module supports it.
|
||||
void BatteryManagement::getBatteryInfo(uint8_t& valid_mask, uint8_t& batteryPercentage, uint16_t& voltage, int32_t& current) {
|
||||
if (detected_ == BATT_NONE) {
|
||||
valid_mask = BATT_VALID_NONE;
|
||||
return;
|
||||
} else if (detected_ == BATT_ADS1110) {
|
||||
battery_ads1110.getBatteryInfo(valid_mask, batteryPercentage, voltage);
|
||||
battery_ads1110.getBatteryInfo(valid_mask, voltage);
|
||||
batteryPercentage = calc_percent_voltage(voltage);
|
||||
return;
|
||||
} else if (detected_ == BATT_MAX17055) {
|
||||
battery_max17055.getBatteryInfo(valid_mask, batteryPercentage, voltage, current);
|
||||
if (calcOverride) {
|
||||
valid_mask &= ~BATT_VALID_PERCENT; // indicate it is voltage based
|
||||
batteryPercentage = calc_percent_voltage(voltage);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// add new module query here
|
||||
@@ -70,7 +82,7 @@ void BatteryManagement::getBatteryInfo(uint8_t& valid_mask, uint8_t& batteryPerc
|
||||
voltage = rand() % 1000 + 3000; // mV
|
||||
current = rand() % 150; // mA
|
||||
isCharging = rand() % 2;
|
||||
valid_mask = 3;
|
||||
valid_mask = 7;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
@@ -100,6 +112,18 @@ uint8_t BatteryManagement::getPercent() {
|
||||
int32_t current = 0;
|
||||
getBatteryInfo(validity, batteryPercentage, voltage, current);
|
||||
if ((validity & BATT_VALID_VOLTAGE) != BATT_VALID_VOLTAGE) return 102;
|
||||
if (calcOverride || ((validity & BATT_VALID_PERCENT) != BATT_VALID_PERCENT)) {
|
||||
validity &= ~BATT_VALID_PERCENT; // indicate it is voltage based
|
||||
batteryPercentage = calc_percent_voltage(voltage);
|
||||
}
|
||||
return batteryPercentage;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@@ -27,6 +27,10 @@
|
||||
|
||||
namespace battery {
|
||||
|
||||
#define BATTERY_MIN_VOLTAGE 3000.0
|
||||
#define BATTERY_MAX_VOLTAGE 4170.0
|
||||
#define BATTERY_DESIGN_CAP 2500
|
||||
|
||||
class BatteryManagement {
|
||||
public:
|
||||
enum BatteryModules {
|
||||
@@ -39,8 +43,9 @@ class BatteryManagement {
|
||||
BATT_VALID_NONE = 0,
|
||||
BATT_VALID_VOLTAGE = 1,
|
||||
BATT_VALID_CURRENT = 2,
|
||||
BATT_VALID_PERCENT = 4,
|
||||
};
|
||||
static void init();
|
||||
static void init(bool override = false);
|
||||
static void detect();
|
||||
static bool isDetected() { return detected_ != BATT_NONE; }
|
||||
static BatteryModules detectedModule() { return detected_; }
|
||||
@@ -49,12 +54,15 @@ class BatteryManagement {
|
||||
static uint8_t getPercent();
|
||||
static uint16_t read_register(const uint8_t reg);
|
||||
static bool write_register(const uint8_t reg, const uint16_t value);
|
||||
static void set_calc_override(bool override);
|
||||
static uint8_t calc_percent_voltage(uint16_t); // calculates battery percentage from the voltage
|
||||
|
||||
private:
|
||||
static void create_thread();
|
||||
static msg_t timer_fn(void* arg);
|
||||
static Thread* thread;
|
||||
static BatteryModules detected_; // if there is any batt management system
|
||||
static bool calcOverride; // if set to true, it'll override the battery percent calculation based on current voltage.
|
||||
};
|
||||
}; // namespace battery
|
||||
#endif
|
@@ -166,7 +166,7 @@ void MAX17055::getBatteryInfo(uint8_t& valid_mask, uint8_t& batteryPercentage, u
|
||||
}
|
||||
batteryPercentage = stateOfCharge();
|
||||
current = instantCurrent();
|
||||
valid_mask = 3; // BATT_VALID_VOLTAGE + CURRENT
|
||||
valid_mask = 7; // BATT_VALID_VOLTAGE + CURRENT + PERCENT
|
||||
} else {
|
||||
// let's indicate the data is wrong. ui will handle this by display UNK values.
|
||||
valid_mask = 0;
|
||||
|
@@ -26,6 +26,7 @@
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
#include "battery.hpp"
|
||||
#include "i2c_pp.hpp"
|
||||
|
||||
#define MAX17055_POR 0
|
||||
@@ -47,7 +48,7 @@
|
||||
|
||||
// Define Battery Capacity
|
||||
#ifndef __MAX17055_Design_Capacity__
|
||||
#define __MAX17055_Design_Capacity__ 2500 // Battery Capacity
|
||||
#define __MAX17055_Design_Capacity__ BATTERY_DESIGN_CAP // Battery Capacity
|
||||
#endif
|
||||
|
||||
// Define Gauge Resistor
|
||||
@@ -57,17 +58,17 @@
|
||||
|
||||
// Define Minimum Voltage
|
||||
#ifndef __MAX17055_Min_Voltage__
|
||||
#define __MAX17055_Min_Voltage__ 3.0 // Minimum Voltage
|
||||
#define __MAX17055_Min_Voltage__ BATTERY_MIN_VOLTAGE / 1000.0 // Minimum Voltage
|
||||
#endif
|
||||
|
||||
// Define Maximum Voltage
|
||||
#ifndef __MAX17055_Max_Voltage__
|
||||
#define __MAX17055_Max_Voltage__ 4.17 // Maximum Voltage
|
||||
#define __MAX17055_Max_Voltage__ BATTERY_MAX_VOLTAGE / 1000.0 // Maximum Voltage
|
||||
#endif
|
||||
|
||||
// Define Empty Voltage
|
||||
#ifndef __MAX17055_Empty_Voltage__
|
||||
#define __MAX17055_Empty_Voltage__ 3.0 // Empty Voltage
|
||||
#define __MAX17055_Empty_Voltage__ BATTERY_MIN_VOLTAGE / 1000.0 // Empty Voltage
|
||||
#endif
|
||||
|
||||
// Define Recovery Voltage
|
||||
|
@@ -133,7 +133,7 @@ struct ui_config2_t {
|
||||
bool hide_fake_brightness : 1;
|
||||
bool hide_numeric_battery : 1;
|
||||
bool hide_battery_icon : 1;
|
||||
bool UNUSED_3 : 1;
|
||||
bool override_batt_calc : 1;
|
||||
bool UNUSED_4 : 1;
|
||||
bool UNUSED_5 : 1;
|
||||
bool UNUSED_6 : 1;
|
||||
@@ -965,6 +965,10 @@ uint8_t ui_theme_id() {
|
||||
return data->ui_config2.theme_id;
|
||||
}
|
||||
|
||||
bool ui_override_batt_calc() {
|
||||
return data->ui_config2.override_batt_calc;
|
||||
}
|
||||
|
||||
void set_ui_hide_speaker(bool v) {
|
||||
data->ui_config2.hide_speaker = v;
|
||||
}
|
||||
@@ -1006,6 +1010,9 @@ void set_ui_hide_battery_icon(bool v) {
|
||||
void set_ui_theme_id(uint8_t theme_id) {
|
||||
data->ui_config2.theme_id = theme_id;
|
||||
}
|
||||
void set_ui_override_batt_calc(bool v) {
|
||||
data->ui_config2.override_batt_calc = v;
|
||||
}
|
||||
|
||||
/* Converter */
|
||||
bool config_converter() {
|
||||
|
@@ -339,6 +339,7 @@ bool ui_hide_numeric_battery();
|
||||
bool ui_hide_battery_icon();
|
||||
bool ui_hide_sd_card();
|
||||
uint8_t ui_theme_id();
|
||||
bool ui_override_batt_calc();
|
||||
void set_ui_hide_speaker(bool v);
|
||||
void set_ui_hide_mute(bool v);
|
||||
void set_ui_hide_converter(bool v);
|
||||
@@ -352,6 +353,7 @@ void set_ui_hide_numeric_battery(bool v);
|
||||
void set_ui_hide_battery_icon(bool v);
|
||||
void set_ui_hide_sd_card(bool v);
|
||||
void set_ui_theme_id(uint8_t v);
|
||||
void set_ui_override_batt_calc(bool v);
|
||||
|
||||
// sd persisting settings
|
||||
bool should_use_sdcard_for_pmem();
|
||||
|
Reference in New Issue
Block a user