mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-07-29 01:29:03 -04:00
parent
83b65ba6ce
commit
b9771b2350
6 changed files with 124 additions and 96 deletions
|
@ -1,6 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
||||||
* Copyright (C) 2016 Furrtek
|
* Copyright (C) 2016 Furrtek
|
||||||
|
* Copyright (C) 2024 HTotoo
|
||||||
*
|
*
|
||||||
* This file is part of PortaPack.
|
* This file is part of PortaPack.
|
||||||
*
|
*
|
||||||
|
@ -43,51 +44,32 @@ void BattinfoView::on_timer() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BattinfoView::update_results_ads1110(i2cdev::I2cDev_ADS1110* dev) {
|
void BattinfoView::update_result() {
|
||||||
bool uichg = false;
|
if (!battery::BatteryManagement::isDetected()) {
|
||||||
auto voltage = dev->readVoltage();
|
|
||||||
auto percent = battery::BatteryManagement::calc_percent_voltage(voltage);
|
|
||||||
if (percent <= 100)
|
|
||||||
text_percent.set(to_string_dec_uint(percent) + " %");
|
|
||||||
else
|
|
||||||
text_percent.set("UNKNOWN");
|
text_percent.set("UNKNOWN");
|
||||||
if (voltage > 1) {
|
|
||||||
text_voltage.set(to_string_decimal(voltage / 1000.0, 3) + " V");
|
|
||||||
} else {
|
|
||||||
text_voltage.set("UNKNOWN");
|
text_voltage.set("UNKNOWN");
|
||||||
}
|
text_current.set("-");
|
||||||
|
text_charge.set("-");
|
||||||
// ui hide:
|
text_cycles.set("-");
|
||||||
if (!labels_opt.hidden()) uichg = true;
|
text_ttef.set("-");
|
||||||
labels_opt.hidden(true);
|
text_method.set("-");
|
||||||
text_current.hidden(true);
|
|
||||||
text_charge.hidden(true);
|
|
||||||
labels_opt.hidden(true);
|
|
||||||
text_ttef.hidden(true);
|
|
||||||
text_cycles.hidden(true);
|
|
||||||
text_warn.set("");
|
text_warn.set("");
|
||||||
text_ttef.hidden(true);
|
return;
|
||||||
text_method.set("Voltage");
|
|
||||||
button_mode.set_text("Voltage");
|
|
||||||
if (uichg) set_dirty();
|
|
||||||
BatteryStateMessage msg{1, percent, false, voltage};
|
|
||||||
EventDispatcher::send_message(msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BattinfoView::update_results_max17055(i2cdev::I2cDev_MAX17055* dev) {
|
|
||||||
bool uichg = false;
|
bool uichg = false;
|
||||||
uint8_t valid_mask = 0;
|
uint8_t valid_mask = 0;
|
||||||
dev->getBatteryInfo(valid_mask, percent, voltage, current);
|
battery::BatteryManagement::getBatteryInfo(valid_mask, percent, voltage, current);
|
||||||
// update text fields
|
// update text fields
|
||||||
if (percent <= 100)
|
if (percent <= 100 && (valid_mask & battery::BatteryManagement::BATT_VALID_VOLTAGE) == battery::BatteryManagement::BATT_VALID_VOLTAGE)
|
||||||
text_percent.set(to_string_dec_uint(percent) + " %");
|
text_percent.set(to_string_dec_uint(percent) + " %");
|
||||||
else
|
else
|
||||||
text_percent.set("UNKNOWN");
|
text_percent.set("UNKNOWN");
|
||||||
if (voltage > 1) {
|
if (voltage > 1 && (valid_mask & battery::BatteryManagement::BATT_VALID_VOLTAGE) == battery::BatteryManagement::BATT_VALID_VOLTAGE) {
|
||||||
text_voltage.set(to_string_decimal(voltage / 1000.0, 3) + " V");
|
text_voltage.set(to_string_decimal(voltage / 1000.0, 3) + " V");
|
||||||
} else {
|
} else {
|
||||||
text_voltage.set("UNKNOWN");
|
text_voltage.set("UNKNOWN");
|
||||||
}
|
}
|
||||||
|
if ((valid_mask & battery::BatteryManagement::BATT_VALID_CURRENT) == battery::BatteryManagement::BATT_VALID_CURRENT) {
|
||||||
if (labels_opt.hidden()) uichg = true;
|
if (labels_opt.hidden()) uichg = true;
|
||||||
labels_opt.hidden(false);
|
labels_opt.hidden(false);
|
||||||
text_current.hidden(false);
|
text_current.hidden(false);
|
||||||
|
@ -95,34 +77,54 @@ void BattinfoView::update_results_max17055(i2cdev::I2cDev_MAX17055* dev) {
|
||||||
text_current.set(to_string_dec_int(current) + " mA");
|
text_current.set(to_string_dec_int(current) + " mA");
|
||||||
text_charge.set(current >= 0 ? "Charging" : "Discharging");
|
text_charge.set(current >= 0 ? "Charging" : "Discharging");
|
||||||
labels_opt.hidden(false);
|
labels_opt.hidden(false);
|
||||||
|
|
||||||
text_ttef.hidden(false);
|
text_ttef.hidden(false);
|
||||||
// cycles
|
} else {
|
||||||
|
if (!labels_opt.hidden()) uichg = true;
|
||||||
|
labels_opt.hidden(true);
|
||||||
|
text_current.hidden(true);
|
||||||
|
text_charge.hidden(true);
|
||||||
|
text_cycles.hidden(true);
|
||||||
|
text_ttef.hidden(true);
|
||||||
|
text_warn.set("");
|
||||||
|
}
|
||||||
|
if ((valid_mask & battery::BatteryManagement::BATT_VALID_CYCLES) == battery::BatteryManagement::BATT_VALID_CYCLES) {
|
||||||
text_cycles.hidden(false);
|
text_cycles.hidden(false);
|
||||||
uint16_t cycles = (uint16_t)dev->getValue("Cycles");
|
uint16_t cycles = battery::BatteryManagement::get_cycles();
|
||||||
if (cycles < 2)
|
if (cycles < 2)
|
||||||
text_warn.set("SoC improves after 2 cycles");
|
text_warn.set("SoC improves after 2 cycles");
|
||||||
else
|
else
|
||||||
text_warn.set("");
|
text_warn.set("");
|
||||||
text_cycles.set(to_string_dec_uint(cycles));
|
text_cycles.set(to_string_dec_uint(cycles));
|
||||||
// ttef
|
} else {
|
||||||
|
text_cycles.hidden(true);
|
||||||
|
text_warn.set("");
|
||||||
|
}
|
||||||
|
if ((valid_mask & battery::BatteryManagement::BATT_VALID_TTEF) == battery::BatteryManagement::BATT_VALID_TTEF) {
|
||||||
text_ttef.hidden(false);
|
text_ttef.hidden(false);
|
||||||
float ttef = 0;
|
float ttef = 0;
|
||||||
if (current <= 0) {
|
if (current <= 0) {
|
||||||
ttef = dev->getValue("TTE");
|
ttef = battery::BatteryManagement::get_tte();
|
||||||
} else {
|
} else {
|
||||||
ttef = dev->getValue("TTF");
|
ttef = battery::BatteryManagement::get_ttf();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert ttef to hours and minutes
|
// Convert ttef to hours and minutes
|
||||||
uint8_t hours = static_cast<uint8_t>(ttef);
|
uint8_t hours = static_cast<uint8_t>(ttef);
|
||||||
uint8_t minutes = static_cast<uint8_t>((ttef - hours) * 60 + 0.5); // +0.5 for rounding
|
uint8_t minutes = static_cast<uint8_t>((ttef - hours) * 60 + 0.5); // +0.5 for rounding
|
||||||
|
|
||||||
// Create the formatted string
|
// Create the formatted string
|
||||||
std::string formatted_time;
|
std::string formatted_time;
|
||||||
if (hours > 0) {
|
if (hours > 0) {
|
||||||
formatted_time += to_string_dec_uint(hours) + "h ";
|
formatted_time += to_string_dec_uint(hours) + "h ";
|
||||||
}
|
}
|
||||||
formatted_time += to_string_dec_uint(minutes) + "m";
|
formatted_time += to_string_dec_uint(minutes) + "m";
|
||||||
|
|
||||||
text_ttef.set(formatted_time);
|
text_ttef.set(formatted_time);
|
||||||
if (!battery::BatteryManagement::calcOverride) {
|
} else {
|
||||||
|
text_ttef.hidden(true);
|
||||||
|
}
|
||||||
|
if ((valid_mask & battery::BatteryManagement::BATT_VALID_PERCENT) == battery::BatteryManagement::BATT_VALID_PERCENT) {
|
||||||
text_method.set("IC");
|
text_method.set("IC");
|
||||||
button_mode.set_text("Volt");
|
button_mode.set_text("Volt");
|
||||||
} else {
|
} else {
|
||||||
|
@ -135,30 +137,6 @@ void BattinfoView::update_results_max17055(i2cdev::I2cDev_MAX17055* dev) {
|
||||||
EventDispatcher::send_message(msg);
|
EventDispatcher::send_message(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BattinfoView::update_result() {
|
|
||||||
auto dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_MAX17055);
|
|
||||||
if (dev) {
|
|
||||||
update_results_max17055((i2cdev::I2cDev_MAX17055*)dev);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_ADS1110);
|
|
||||||
if (dev) {
|
|
||||||
update_results_ads1110((i2cdev::I2cDev_ADS1110*)dev);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// no dev found
|
|
||||||
text_percent.set("UNKNOWN");
|
|
||||||
text_voltage.set("UNKNOWN");
|
|
||||||
text_current.set("-");
|
|
||||||
text_charge.set("-");
|
|
||||||
text_cycles.set("-");
|
|
||||||
text_ttef.set("-");
|
|
||||||
text_method.set("-");
|
|
||||||
text_warn.set("");
|
|
||||||
}
|
|
||||||
|
|
||||||
BattinfoView::BattinfoView(NavigationView& nav)
|
BattinfoView::BattinfoView(NavigationView& nav)
|
||||||
: nav_{nav} {
|
: nav_{nav} {
|
||||||
add_children({&labels,
|
add_children({&labels,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
||||||
* Copyright (C) 2016 Furrtek
|
* Copyright (C) 2016 Furrtek
|
||||||
|
* Copyright (C) 2024 HTotoo
|
||||||
*
|
*
|
||||||
* This file is part of PortaPack.
|
* This file is part of PortaPack.
|
||||||
*
|
*
|
||||||
|
@ -28,8 +29,6 @@
|
||||||
#include "ui_navigation.hpp"
|
#include "ui_navigation.hpp"
|
||||||
#include "string_format.hpp"
|
#include "string_format.hpp"
|
||||||
#include "i2cdevmanager.hpp"
|
#include "i2cdevmanager.hpp"
|
||||||
#include "i2cdev_max17055.hpp"
|
|
||||||
#include "i2cdev_ads1110.hpp"
|
|
||||||
|
|
||||||
namespace ui {
|
namespace ui {
|
||||||
class BattinfoView : public View {
|
class BattinfoView : public View {
|
||||||
|
@ -46,8 +45,6 @@ class BattinfoView : public View {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void update_result();
|
void update_result();
|
||||||
void update_results_max17055(i2cdev::I2cDev_MAX17055* dev);
|
|
||||||
void update_results_ads1110(i2cdev::I2cDev_ADS1110* dev);
|
|
||||||
void on_timer();
|
void on_timer();
|
||||||
NavigationView& nav_;
|
NavigationView& nav_;
|
||||||
uint16_t timer_period = 60;
|
uint16_t timer_period = 60;
|
||||||
|
|
|
@ -14,7 +14,7 @@ class BatteryCapacityView : public View {
|
||||||
public:
|
public:
|
||||||
BatteryCapacityView(NavigationView& nav);
|
BatteryCapacityView(NavigationView& nav);
|
||||||
void focus() override;
|
void focus() override;
|
||||||
std::string title() const override { return "MAX17055 Registers"; }
|
std::string title() const override { return "Battery Registers"; }
|
||||||
|
|
||||||
bool on_encoder(const EncoderEvent delta) override;
|
bool on_encoder(const EncoderEvent delta) override;
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@
|
||||||
#include "sd_card.hpp"
|
#include "sd_card.hpp"
|
||||||
#include "external_app.hpp"
|
#include "external_app.hpp"
|
||||||
#include "view_factory.hpp"
|
#include "view_factory.hpp"
|
||||||
|
#include "battery.hpp"
|
||||||
|
|
||||||
// for incrementing fake date when RTC battery is dead
|
// for incrementing fake date when RTC battery is dead
|
||||||
#define DATE_FILEFLAG u"/SETTINGS/DATE_FILEFLAG"
|
#define DATE_FILEFLAG u"/SETTINGS/DATE_FILEFLAG"
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include "battery.hpp"
|
#include "battery.hpp"
|
||||||
#include "portapack.hpp"
|
#include "portapack.hpp"
|
||||||
#include "i2cdevmanager.hpp"
|
#include "i2cdev_max17055.hpp"
|
||||||
|
#include "i2cdev_ads1110.hpp"
|
||||||
|
|
||||||
namespace battery {
|
namespace battery {
|
||||||
|
|
||||||
bool BatteryManagement::calcOverride = false;
|
bool BatteryManagement::calcOverride = false;
|
||||||
|
@ -27,4 +29,49 @@ uint8_t BatteryManagement::calc_percent_voltage(uint16_t voltage) {
|
||||||
return batteryPercentage;
|
return batteryPercentage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helper function to get data from ANY batt management ic
|
||||||
|
void BatteryManagement::getBatteryInfo(uint8_t& valid_mask, uint8_t& percent, uint16_t& voltage, int32_t& current) {
|
||||||
|
auto dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_MAX17055);
|
||||||
|
if (dev) {
|
||||||
|
((i2cdev::I2cDev_MAX17055*)dev)->getBatteryInfo(valid_mask, percent, voltage, current);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_ADS1110);
|
||||||
|
if (dev) {
|
||||||
|
voltage = ((i2cdev::I2cDev_ADS1110*)dev)->readVoltage();
|
||||||
|
percent = calc_percent_voltage(voltage);
|
||||||
|
valid_mask = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
valid_mask = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// helper function to get data from ANY batt management ic
|
||||||
|
uint16_t BatteryManagement::get_cycles() {
|
||||||
|
auto dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_MAX17055);
|
||||||
|
if (dev) {
|
||||||
|
return ((i2cdev::I2cDev_MAX17055*)dev)->getValue("Cycles");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// helper function to get data from ANY batt management ic
|
||||||
|
float BatteryManagement::get_tte() {
|
||||||
|
auto dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_MAX17055);
|
||||||
|
if (dev) {
|
||||||
|
return ((i2cdev::I2cDev_MAX17055*)dev)->getValue("TTE");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// helper function to get data from ANY batt management ic
|
||||||
|
float BatteryManagement::get_ttf() {
|
||||||
|
auto dev = i2cdev::I2CDevManager::get_dev_by_model(I2CDEVMDL_MAX17055);
|
||||||
|
if (dev) {
|
||||||
|
return ((i2cdev::I2cDev_MAX17055*)dev)->getValue("TTF");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace battery
|
} // namespace battery
|
|
@ -45,6 +45,11 @@ class BatteryManagement {
|
||||||
static void set_calc_override(bool override);
|
static void set_calc_override(bool override);
|
||||||
static uint8_t calc_percent_voltage(uint16_t); // calculates battery percentage from the voltage
|
static uint8_t calc_percent_voltage(uint16_t); // calculates battery percentage from the voltage
|
||||||
static bool calcOverride; // if set to true, it'll override the battery percent calculation based on current voltage.
|
static bool calcOverride; // if set to true, it'll override the battery percent calculation based on current voltage.
|
||||||
|
static void getBatteryInfo(uint8_t& valid_mask, uint8_t& percent, uint16_t& voltage, int32_t& current);
|
||||||
|
static uint16_t get_cycles();
|
||||||
|
static float get_tte();
|
||||||
|
static float get_ttf();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
}; // namespace battery
|
}; // namespace battery
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue