2024-04-29 11:38:27 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
2024-10-07 05:55:11 -04:00
|
|
|
* Copyright (C) 2024 HTotoo
|
2024-04-29 11:38:27 -04:00
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_battinfo.hpp"
|
|
|
|
|
|
|
|
#include "event_m0.hpp"
|
|
|
|
#include "portapack.hpp"
|
2024-07-17 05:18:57 -04:00
|
|
|
#include "battery.hpp"
|
2024-04-29 11:38:27 -04:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
void BattinfoView::focus() {
|
|
|
|
button_exit.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// called each 1/60th of second, so 6 = 100ms
|
|
|
|
void BattinfoView::on_timer() {
|
|
|
|
if (++timer_counter == timer_period) {
|
|
|
|
timer_counter = 0;
|
|
|
|
update_result();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 05:55:11 -04:00
|
|
|
void BattinfoView::update_result() {
|
|
|
|
if (!battery::BatteryManagement::isDetected()) {
|
2024-04-29 11:38:27 -04:00
|
|
|
text_percent.set("UNKNOWN");
|
|
|
|
text_voltage.set("UNKNOWN");
|
2024-10-07 05:55:11 -04:00
|
|
|
text_current.set("-");
|
|
|
|
text_charge.set("-");
|
2024-11-19 05:23:33 -05:00
|
|
|
// text_cycles.set("-");
|
2024-10-07 05:55:11 -04:00
|
|
|
text_ttef.set("-");
|
|
|
|
text_method.set("-");
|
2024-11-20 09:02:02 -05:00
|
|
|
// text_warn.set("");
|
2024-10-07 05:55:11 -04:00
|
|
|
return;
|
2024-04-29 11:38:27 -04:00
|
|
|
}
|
|
|
|
bool uichg = false;
|
2024-07-17 05:18:57 -04:00
|
|
|
uint8_t valid_mask = 0;
|
2024-10-07 05:55:11 -04:00
|
|
|
battery::BatteryManagement::getBatteryInfo(valid_mask, percent, voltage, current);
|
2024-04-29 11:38:27 -04:00
|
|
|
// update text fields
|
2024-10-07 05:55:11 -04:00
|
|
|
if (percent <= 100 && (valid_mask & battery::BatteryManagement::BATT_VALID_VOLTAGE) == battery::BatteryManagement::BATT_VALID_VOLTAGE)
|
2024-04-29 11:38:27 -04:00
|
|
|
text_percent.set(to_string_dec_uint(percent) + " %");
|
|
|
|
else
|
|
|
|
text_percent.set("UNKNOWN");
|
2024-10-07 05:55:11 -04:00
|
|
|
if (voltage > 1 && (valid_mask & battery::BatteryManagement::BATT_VALID_VOLTAGE) == battery::BatteryManagement::BATT_VALID_VOLTAGE) {
|
2024-04-29 11:38:27 -04:00
|
|
|
text_voltage.set(to_string_decimal(voltage / 1000.0, 3) + " V");
|
|
|
|
} else {
|
|
|
|
text_voltage.set("UNKNOWN");
|
|
|
|
}
|
2024-10-07 05:55:11 -04:00
|
|
|
if ((valid_mask & battery::BatteryManagement::BATT_VALID_CURRENT) == battery::BatteryManagement::BATT_VALID_CURRENT) {
|
|
|
|
if (labels_opt.hidden()) uichg = true;
|
|
|
|
labels_opt.hidden(false);
|
|
|
|
text_current.hidden(false);
|
|
|
|
text_charge.hidden(false);
|
|
|
|
text_current.set(to_string_dec_int(current) + " mA");
|
2024-10-30 07:19:17 -04:00
|
|
|
if (current >= 25) // when >25mA it is charging in any scenario
|
|
|
|
text_charge.set("Charging");
|
|
|
|
else {
|
|
|
|
if (current > -25) { // between -25 and 25
|
|
|
|
if (voltage > 4060 || percent == 100) { // the voltage is high enough, so it is full, that's why no mA
|
|
|
|
text_charge.set("Full");
|
|
|
|
} else {
|
|
|
|
text_charge.set("Holding"); // not enough voltage, so should charge. maybe the batttery is off, or USB power is not enough
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
text_charge.set("Discharging"); // less then -25mA, so it is discharging
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 05:55:11 -04:00
|
|
|
labels_opt.hidden(false);
|
|
|
|
|
|
|
|
text_ttef.hidden(false);
|
|
|
|
} else {
|
|
|
|
if (!labels_opt.hidden()) uichg = true;
|
|
|
|
labels_opt.hidden(true);
|
|
|
|
text_current.hidden(true);
|
|
|
|
text_charge.hidden(true);
|
2024-11-19 05:23:33 -05:00
|
|
|
// text_cycles.hidden(true);
|
2024-10-07 05:55:11 -04:00
|
|
|
text_ttef.hidden(true);
|
2024-11-20 09:02:02 -05:00
|
|
|
// text_warn.set("");
|
2024-10-07 05:55:11 -04:00
|
|
|
}
|
2024-11-20 09:02:02 -05:00
|
|
|
/* if ((valid_mask & battery::BatteryManagement::BATT_VALID_CYCLES) == battery::BatteryManagement::BATT_VALID_CYCLES) {
|
2024-11-19 05:23:33 -05:00
|
|
|
// text_cycles.hidden(false);
|
|
|
|
uint16_t cycles = 0; // battery::BatteryManagement::get_cycles();
|
2024-10-07 05:55:11 -04:00
|
|
|
if (cycles < 2)
|
2024-11-19 05:23:33 -05:00
|
|
|
text_warn.set("SoC improves after each cycles");
|
2024-10-07 05:55:11 -04:00
|
|
|
else
|
|
|
|
text_warn.set("");
|
2024-11-19 05:23:33 -05:00
|
|
|
// text_cycles.set(to_string_dec_uint(cycles));
|
2024-09-20 05:59:17 -04:00
|
|
|
} else {
|
2024-11-19 05:23:33 -05:00
|
|
|
// text_cycles.hidden(true);
|
2024-10-07 05:55:11 -04:00
|
|
|
text_warn.set("");
|
2024-11-20 09:02:02 -05:00
|
|
|
} */
|
2024-10-07 05:55:11 -04:00
|
|
|
if ((valid_mask & battery::BatteryManagement::BATT_VALID_TTEF) == battery::BatteryManagement::BATT_VALID_TTEF) {
|
|
|
|
text_ttef.hidden(false);
|
|
|
|
float ttef = 0;
|
2024-10-30 07:19:17 -04:00
|
|
|
if (current <= 0) { // we keep this yet
|
2024-10-07 05:55:11 -04:00
|
|
|
ttef = battery::BatteryManagement::get_tte();
|
|
|
|
} else {
|
|
|
|
ttef = battery::BatteryManagement::get_ttf();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert ttef to hours and minutes
|
|
|
|
uint8_t hours = static_cast<uint8_t>(ttef);
|
|
|
|
uint8_t minutes = static_cast<uint8_t>((ttef - hours) * 60 + 0.5); // +0.5 for rounding
|
|
|
|
|
|
|
|
// Create the formatted string
|
|
|
|
std::string formatted_time;
|
|
|
|
if (hours > 0) {
|
|
|
|
formatted_time += to_string_dec_uint(hours) + "h ";
|
|
|
|
}
|
|
|
|
formatted_time += to_string_dec_uint(minutes) + "m";
|
|
|
|
|
|
|
|
text_ttef.set(formatted_time);
|
|
|
|
} else {
|
|
|
|
text_ttef.hidden(true);
|
2024-04-29 11:38:27 -04:00
|
|
|
}
|
2024-10-07 05:55:11 -04:00
|
|
|
if ((valid_mask & battery::BatteryManagement::BATT_VALID_PERCENT) == battery::BatteryManagement::BATT_VALID_PERCENT) {
|
2024-08-28 05:32:24 -04:00
|
|
|
text_method.set("IC");
|
|
|
|
button_mode.set_text("Volt");
|
|
|
|
} else {
|
|
|
|
text_method.set("Voltage");
|
|
|
|
button_mode.set_text("IC");
|
|
|
|
}
|
2024-04-29 11:38:27 -04:00
|
|
|
if (uichg) set_dirty();
|
|
|
|
// to update status bar too, send message in behalf of batt manager
|
2024-10-30 07:19:17 -04:00
|
|
|
BatteryStateMessage msg{valid_mask, percent, current >= 25, voltage};
|
2024-04-29 11:38:27 -04:00
|
|
|
EventDispatcher::send_message(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
BattinfoView::BattinfoView(NavigationView& nav)
|
|
|
|
: nav_{nav} {
|
|
|
|
add_children({&labels,
|
|
|
|
&labels_opt,
|
|
|
|
&text_percent,
|
|
|
|
&text_voltage,
|
|
|
|
&text_current,
|
|
|
|
&text_charge,
|
2024-08-28 05:32:24 -04:00
|
|
|
&text_method,
|
|
|
|
&button_mode,
|
2024-09-20 05:59:17 -04:00
|
|
|
&button_exit,
|
2024-11-19 05:23:33 -05:00
|
|
|
// &text_cycles,
|
2024-11-20 09:02:02 -05:00
|
|
|
// &text_warn,
|
2024-09-20 05:59:17 -04:00
|
|
|
&text_ttef});
|
2024-04-29 11:38:27 -04:00
|
|
|
|
|
|
|
button_exit.on_select = [this, &nav](Button&) {
|
|
|
|
nav.pop();
|
|
|
|
};
|
2024-08-28 05:32:24 -04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
};
|
2024-04-29 11:38:27 -04:00
|
|
|
update_result();
|
2024-07-17 05:18:57 -04:00
|
|
|
if (thread == nullptr) thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, BattinfoView::static_fn, this);
|
2024-04-29 11:38:27 -04:00
|
|
|
}
|
|
|
|
|
2024-05-11 04:52:35 -04:00
|
|
|
msg_t BattinfoView::static_fn(void* arg) {
|
|
|
|
auto obj = static_cast<BattinfoView*>(arg);
|
2024-07-17 05:18:57 -04:00
|
|
|
while (!chThdShouldTerminate()) {
|
2024-05-11 04:52:35 -04:00
|
|
|
chThdSleepMilliseconds(16);
|
|
|
|
obj->on_timer();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BattinfoView::~BattinfoView() {
|
|
|
|
if (thread) {
|
|
|
|
chThdTerminate(thread);
|
|
|
|
chThdWait(thread);
|
|
|
|
thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 05:55:11 -04:00
|
|
|
} // namespace ui
|