Skip to content

Commit

Permalink
Battery info switcher (#2230)
Browse files Browse the repository at this point in the history
  • Loading branch information
htotoo committed Aug 28, 2024
1 parent 1a0555f commit 0ae7768
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 29 deletions.
22 changes: 21 additions & 1 deletion firmware/application/apps/ui_battinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void BattinfoView::update_result() {
text_voltage.set("UNKNOWN");
text_current.set("-");
text_charge.set("-");
text_method.set("-");
return;
}
bool uichg = false;
Expand Down Expand Up @@ -78,6 +79,13 @@ void BattinfoView::update_result() {
text_current.hidden(true);
text_charge.hidden(true);
}
if ((valid_mask & battery::BatteryManagement::BATT_VALID_PERCENT) == battery::BatteryManagement::BATT_VALID_PERCENT) {
text_method.set("IC");
button_mode.set_text("Volt");
} else {
text_method.set("Voltage");
button_mode.set_text("IC");
}
if (uichg) set_dirty();
// to update status bar too, send message in behalf of batt manager
BatteryStateMessage msg{valid_mask, percent, current >= 0, voltage};
Expand All @@ -92,12 +100,24 @@ BattinfoView::BattinfoView(NavigationView& nav)
&text_voltage,
&text_current,
&text_charge,
&text_method,
&button_mode,
&button_exit});

button_exit.on_select = [this, &nav](Button&) {
nav.pop();
};

button_mode.on_select = [this, &nav](Button&) {
if (button_mode.text() == "IC") {
battery::BatteryManagement::set_calc_override(false);
persistent_memory::set_ui_override_batt_calc(false);
button_mode.set_text("Volt");
} else {
battery::BatteryManagement::set_calc_override(true);
persistent_memory::set_ui_override_batt_calc(true);
button_mode.set_text("IC");
}
};
update_result();
if (thread == nullptr) thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, BattinfoView::static_fn, this);
}
Expand Down
20 changes: 15 additions & 5 deletions firmware/application/apps/ui_battinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,34 @@ class BattinfoView : public View {

Labels labels{
{{2 * 8, 1 * 16}, "Percent:", Theme::getInstance()->fg_light->foreground},
{{2 * 8, 2 * 16}, "Voltage:", Theme::getInstance()->fg_light->foreground}};
{{2 * 8, 2 * 16}, "Voltage:", Theme::getInstance()->fg_light->foreground},
{{2 * 8, 3 * 16}, "Method:", Theme::getInstance()->fg_light->foreground},
{{2 * 8, 7 * 16}, "Change method:", Theme::getInstance()->fg_light->foreground},
};

Labels labels_opt{
{{2 * 8, 3 * 16}, "Current:", Theme::getInstance()->fg_light->foreground},
{{2 * 8, 4 * 16}, "Charge:", Theme::getInstance()->fg_light->foreground}};
{{2 * 8, 4 * 16}, "Current:", Theme::getInstance()->fg_light->foreground},
{{2 * 8, 5 * 16}, "Charge:", Theme::getInstance()->fg_light->foreground}};

Text text_percent{
{13 * 8, 1 * 16, 10 * 16, 16},
"-"};
Text text_voltage{
{13 * 8, 2 * 16, 10 * 16, 16},
"-"};
Text text_current{
Text text_method{
{13 * 8, 3 * 16, 10 * 16, 16},
"-"};
Text text_charge{
Text text_current{
{13 * 8, 4 * 16, 10 * 16, 16},
"-"};
Text text_charge{
{13 * 8, 5 * 16, 10 * 16, 16},
"-"};

Button button_mode{
{2 * 8, 8 * 16 + 5, 5 * 16, 32},
"Volt"};

Button button_exit{
{72, 17 * 16, 96, 32},
Expand Down
27 changes: 27 additions & 0 deletions firmware/application/apps/ui_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,32 @@ void SetThemeView::focus() {
options.focus();
}

/* SetBatteryView ************************************/

SetBatteryView::SetBatteryView(NavigationView& nav) {
add_children({&labels,
&button_save,
&button_cancel,
&checkbox_overridebatt});

button_save.on_select = [&nav, this](Button&) {
pmem::set_ui_override_batt_calc(checkbox_overridebatt.value());
battery::BatteryManagement::set_calc_override(checkbox_overridebatt.value());
send_system_refresh();
nav.pop();
};

checkbox_overridebatt.set_value(pmem::ui_override_batt_calc());

button_cancel.on_select = [&nav, this](Button&) {
nav.pop();
};
}

void SetBatteryView::focus() {
button_cancel.focus();
}

/* SettingsMenuView **************************************/

SettingsMenuView::SettingsMenuView(NavigationView& nav)
Expand Down Expand Up @@ -978,6 +1004,7 @@ void SettingsMenuView::on_populate() {
{"Theme", ui::Color::dark_cyan(), &bitmap_icon_setup, [this]() { nav_.push<SetThemeView>(); }},
{"Autostart", ui::Color::dark_cyan(), &bitmap_icon_setup, [this]() { nav_.push<SetAutostartView>(); }},
});
if (battery::BatteryManagement::isDetected()) add_item({"Battery", ui::Color::dark_cyan(), &bitmap_icon_setup, [this]() { nav_.push<SetBatteryView>(); }});
}

} /* namespace ui */
29 changes: 29 additions & 0 deletions firmware/application/apps/ui_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,35 @@ class SetThemeView : public View {
};
};

class SetBatteryView : public View {
public:
SetBatteryView(NavigationView& nav);

void focus() override;

std::string title() const override { return "Battery"; };

private:
int32_t selected = 0;
Labels labels{
{{1 * 8, 1 * 16}, "Override batt calculation", Theme::getInstance()->fg_light->foreground},
{{1 * 8, 2 * 16}, "method to voltage based", Theme::getInstance()->fg_light->foreground}};

Button button_save{
{2 * 8, 16 * 16, 12 * 8, 32},
"Save"};

Checkbox checkbox_overridebatt{
{2 * 8, 6 * 16},
23,
"Override"};

Button button_cancel{
{16 * 8, 16 * 16, 12 * 8, 32},
"Cancel",
};
};

class SettingsMenuView : public BtnGridView {
public:
SettingsMenuView(NavigationView& nav);
Expand Down
2 changes: 1 addition & 1 deletion firmware/application/portapack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ init_status_t init() {
chThdSleepMilliseconds(10);

audio::init(portapack_audio_codec());
battery::BatteryManagement::init();
battery::BatteryManagement::init(persistent_memory::ui_override_batt_calc());

if (lcd_fast_setup)
draw_splash_screen_icon(4, ui::bitmap_icon_speaker);
Expand Down
12 changes: 1 addition & 11 deletions firmware/common/ads1110.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion firmware/common/ads1110.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 27 additions & 3 deletions firmware/common/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 9 additions & 1 deletion firmware/common/battery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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_; }
Expand All @@ -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
2 changes: 1 addition & 1 deletion firmware/common/max17055.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions firmware/common/max17055.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <array>
#include <string>

#include "battery.hpp"
#include "i2c_pp.hpp"

#define MAX17055_POR 0
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion firmware/common/portapack_persistent_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit 0ae7768

Please sign in to comment.