Power: more info (#274)

This commit is contained in:
あく 2020-12-22 21:16:32 +03:00 committed by GitHub
parent daf18c11c8
commit 9a257b048e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 20 deletions

View file

@ -23,9 +23,14 @@ struct Power {
Cli* cli;
MenuItem* menu;
float current;
float voltage;
float temperature;
float current_charger;
float current_gauge;
float voltage_charger;
float voltage_gauge;
uint32_t capacity_remaining;
uint32_t capacity_full;
float temperature_charger;
float temperature_gauge;
uint8_t charge;
};
@ -71,20 +76,38 @@ void power_draw_callback(Canvas* canvas, void* context) {
char buffer[64];
canvas_set_font(canvas, FontSecondary);
snprintf(buffer, 64, "Current: %ldmA", (int32_t)(power->current * 1000));
snprintf(
buffer,
64,
"Current: %ld/%ldmA",
(int32_t)(power->current_gauge * 1000),
(int32_t)(power->current_charger * 1000));
canvas_draw_str(canvas, 5, 22, buffer);
snprintf(buffer, 64, "Voltage: %ldmV", (uint32_t)(power->voltage * 1000));
snprintf(
buffer,
64,
"Voltage: %ld/%ldmV",
(uint32_t)(power->voltage_gauge * 1000),
(uint32_t)(power->voltage_charger * 1000));
canvas_draw_str(canvas, 5, 32, buffer);
snprintf(buffer, 64, "Charge: %ld%%", (uint32_t)(power->charge));
canvas_draw_str(canvas, 5, 42, buffer);
snprintf(buffer, 64, "Temperature: %ldC", (uint32_t)(power->temperature));
snprintf(
buffer, 64, "Capacity: %ld of %ldmAh", power->capacity_remaining, power->capacity_full);
canvas_draw_str(canvas, 5, 52, buffer);
snprintf(
buffer,
64,
"Temperature: %ld/%ldC",
(uint32_t)(power->temperature_gauge),
(uint32_t)(power->temperature_charger));
canvas_draw_str(canvas, 5, 62, buffer);
}
void power_input_callback(InputEvent* event, void* context) {
Power* power = context;
if(!event->state) return;
if(!event->state || event->input != InputBack) return;
widget_enabled_set(power->widget, false);
}
@ -199,9 +222,14 @@ void power_task(void* p) {
while(1) {
power->charge = api_hal_power_get_pct();
power->current = api_hal_power_get_battery_current();
power->voltage = api_hal_power_get_battery_voltage();
power->temperature = api_hal_power_get_battery_temperature();
power->capacity_remaining = api_hal_power_get_battery_remaining_capacity();
power->capacity_full = api_hal_power_get_battery_full_capacity();
power->current_charger = api_hal_power_get_battery_current(ApiHalPowerICCharger);
power->current_gauge = api_hal_power_get_battery_current(ApiHalPowerICFuelGauge);
power->voltage_charger = api_hal_power_get_battery_voltage(ApiHalPowerICCharger);
power->voltage_gauge = api_hal_power_get_battery_voltage(ApiHalPowerICFuelGauge);
power->temperature_charger = api_hal_power_get_battery_temperature(ApiHalPowerICCharger);
power->temperature_gauge = api_hal_power_get_battery_temperature(ApiHalPowerICFuelGauge);
widget_update(power->widget);
widget_enabled_set(power->usb_widget, api_hal_power_is_charging());
osDelay(1000);

View file

@ -8,6 +8,11 @@
extern "C" {
#endif
typedef enum {
ApiHalPowerICCharger,
ApiHalPowerICFuelGauge,
} ApiHalPowerIC;
/* Initialize drivers */
void api_hal_power_init();
@ -26,14 +31,26 @@ void api_hal_power_enable_otg();
/* OTG disable */
void api_hal_power_disable_otg();
/* Get remaining battery battery capacity in mAh */
uint32_t api_hal_power_get_battery_remaining_capacity();
/* Get full charge battery capacity in mAh */
uint32_t api_hal_power_get_battery_full_capacity();
/* Get battery voltage in V */
float api_hal_power_get_battery_voltage();
float api_hal_power_get_battery_voltage(ApiHalPowerIC ic);
/* Get battery current in A */
float api_hal_power_get_battery_current();
float api_hal_power_get_battery_current(ApiHalPowerIC ic);
/* Get temperature in C */
float api_hal_power_get_battery_temperature();
float api_hal_power_get_battery_temperature(ApiHalPowerIC ic);
/* Get System voltage in V */
float api_hal_power_get_system_voltage();
/* Get USB voltage in V */
float api_hal_power_get_usb_voltage();
/* Get power system component state */
void api_hal_power_dump_state(string_t buffer);

View file

@ -34,16 +34,44 @@ void api_hal_power_disable_otg() {
bq25896_disable_otg();
}
float api_hal_power_get_battery_voltage() {
return (float)bq27220_get_voltage() / 1000.0f;
uint32_t api_hal_power_get_battery_remaining_capacity() {
return bq27220_get_remaining_capacity();
}
float api_hal_power_get_battery_current() {
return (float)bq27220_get_current() / 1000.0f;
uint32_t api_hal_power_get_battery_full_capacity() {
return bq27220_get_full_charge_capacity();
}
float api_hal_power_get_battery_temperature() {
return ((float)bq27220_get_temperature() - 2731.0f) / 10.0f;
float api_hal_power_get_battery_voltage(ApiHalPowerIC ic) {
if (ic == ApiHalPowerICCharger) {
return (float)bq25896_get_vbat_voltage() / 1000.0f;
} else if (ic == ApiHalPowerICFuelGauge) {
return (float)bq27220_get_voltage() / 1000.0f;
} else {
return 0.0f;
}
}
float api_hal_power_get_battery_current(ApiHalPowerIC ic) {
if (ic == ApiHalPowerICCharger) {
return (float)bq25896_get_vbat_current() / 1000.0f;
} else if (ic == ApiHalPowerICFuelGauge) {
return (float)bq27220_get_current() / 1000.0f;
} else {
return 0.0f;
}
}
float api_hal_power_get_battery_temperature(ApiHalPowerIC ic) {
if (ic == ApiHalPowerICCharger) {
// Linear approximation, +/- 5 C
return (71.0f - (float)bq25896_get_ntc_mpct()/1000) / 0.6f;
} else if (ic == ApiHalPowerICFuelGauge) {
return ((float)bq27220_get_temperature() - 2731.0f) / 10.0f;
} else {
return 0.0f;
}
}
void api_hal_power_dump_state(string_t buffer) {
@ -81,7 +109,7 @@ void api_hal_power_dump_state(string_t buffer) {
);
string_cat_printf(buffer,
"bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n",
bq27220_get_voltage(), bq27220_get_current(), (int)api_hal_power_get_battery_temperature()
bq27220_get_voltage(), bq27220_get_current(), (int)api_hal_power_get_battery_temperature(ApiHalPowerICFuelGauge)
);
}