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; Cli* cli;
MenuItem* menu; MenuItem* menu;
float current; float current_charger;
float voltage; float current_gauge;
float temperature; float voltage_charger;
float voltage_gauge;
uint32_t capacity_remaining;
uint32_t capacity_full;
float temperature_charger;
float temperature_gauge;
uint8_t charge; uint8_t charge;
}; };
@ -71,20 +76,38 @@ void power_draw_callback(Canvas* canvas, void* context) {
char buffer[64]; char buffer[64];
canvas_set_font(canvas, FontSecondary); 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); 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); canvas_draw_str(canvas, 5, 32, buffer);
snprintf(buffer, 64, "Charge: %ld%%", (uint32_t)(power->charge)); snprintf(buffer, 64, "Charge: %ld%%", (uint32_t)(power->charge));
canvas_draw_str(canvas, 5, 42, buffer); 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); 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) { void power_input_callback(InputEvent* event, void* context) {
Power* power = context; Power* power = context;
if(!event->state) return; if(!event->state || event->input != InputBack) return;
widget_enabled_set(power->widget, false); widget_enabled_set(power->widget, false);
} }
@ -199,9 +222,14 @@ void power_task(void* p) {
while(1) { while(1) {
power->charge = api_hal_power_get_pct(); power->charge = api_hal_power_get_pct();
power->current = api_hal_power_get_battery_current(); power->capacity_remaining = api_hal_power_get_battery_remaining_capacity();
power->voltage = api_hal_power_get_battery_voltage(); power->capacity_full = api_hal_power_get_battery_full_capacity();
power->temperature = api_hal_power_get_battery_temperature(); 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_update(power->widget);
widget_enabled_set(power->usb_widget, api_hal_power_is_charging()); widget_enabled_set(power->usb_widget, api_hal_power_is_charging());
osDelay(1000); osDelay(1000);

View File

@ -8,6 +8,11 @@
extern "C" { extern "C" {
#endif #endif
typedef enum {
ApiHalPowerICCharger,
ApiHalPowerICFuelGauge,
} ApiHalPowerIC;
/* Initialize drivers */ /* Initialize drivers */
void api_hal_power_init(); void api_hal_power_init();
@ -26,14 +31,26 @@ void api_hal_power_enable_otg();
/* OTG disable */ /* OTG disable */
void api_hal_power_disable_otg(); 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 */ /* 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 */ /* 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 */ /* 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 */ /* Get power system component state */
void api_hal_power_dump_state(string_t buffer); void api_hal_power_dump_state(string_t buffer);

View File

@ -34,16 +34,44 @@ void api_hal_power_disable_otg() {
bq25896_disable_otg(); bq25896_disable_otg();
} }
float api_hal_power_get_battery_voltage() { uint32_t api_hal_power_get_battery_remaining_capacity() {
return bq27220_get_remaining_capacity();
}
uint32_t api_hal_power_get_battery_full_capacity() {
return bq27220_get_full_charge_capacity();
}
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; return (float)bq27220_get_voltage() / 1000.0f;
} else {
return 0.0f;
}
} }
float api_hal_power_get_battery_current() { 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; return (float)bq27220_get_current() / 1000.0f;
} else {
return 0.0f;
}
} }
float api_hal_power_get_battery_temperature() { 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; return ((float)bq27220_get_temperature() - 2731.0f) / 10.0f;
} else {
return 0.0f;
}
} }
void api_hal_power_dump_state(string_t buffer) { 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, string_cat_printf(buffer,
"bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n", "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)
); );
} }