2022-09-14 18:04:04 +00:00
|
|
|
#include "desktop/desktop_settings.h"
|
2023-01-09 14:15:00 +00:00
|
|
|
#include "power_i.h"
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
#define POWER_OFF_TIMEOUT 90
|
2023-02-26 15:15:26 +00:00
|
|
|
#define TAG "Power"
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
void power_draw_battery_callback(Canvas* canvas, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
Power* power = context;
|
2022-05-06 17:35:52 +00:00
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Battery_26x8);
|
2022-04-18 23:09:11 +00:00
|
|
|
|
|
|
|
if(power->info.gauge_is_ok) {
|
2022-10-17 08:14:31 +00:00
|
|
|
char batteryPercentile[4];
|
2022-09-08 10:52:18 +00:00
|
|
|
snprintf(batteryPercentile, sizeof(batteryPercentile), "%d", power->info.charge);
|
2023-01-09 14:15:00 +00:00
|
|
|
if((power->displayBatteryPercentage == DISPLAY_BATTERY_PERCENT) &&
|
2022-09-08 15:24:25 +00:00
|
|
|
(power->state !=
|
|
|
|
PowerStateCharging)) { //if display battery percentage, black background white text
|
2022-09-08 10:52:18 +00:00
|
|
|
canvas_set_font(canvas, FontBatteryPercent);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
2022-09-08 15:24:25 +00:00
|
|
|
canvas_draw_box(canvas, 1, 1, 22, 6);
|
2022-09-08 10:52:18 +00:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
2022-10-17 08:14:31 +00:00
|
|
|
canvas_draw_str_aligned(canvas, 11, 4, AlignCenter, AlignCenter, batteryPercentile);
|
2022-09-08 15:24:25 +00:00
|
|
|
} else if(
|
2023-01-09 14:15:00 +00:00
|
|
|
(power->displayBatteryPercentage == DISPLAY_BATTERY_INVERTED_PERCENT) &&
|
2022-09-08 15:24:25 +00:00
|
|
|
(power->state !=
|
|
|
|
PowerStateCharging)) { //if display inverted percentage, white background black text
|
2022-09-08 10:52:18 +00:00
|
|
|
canvas_set_font(canvas, FontBatteryPercent);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
2022-10-17 08:14:31 +00:00
|
|
|
canvas_draw_str_aligned(canvas, 11, 4, AlignCenter, AlignCenter, batteryPercentile);
|
2022-09-08 15:24:25 +00:00
|
|
|
} else if(
|
2023-01-09 14:15:00 +00:00
|
|
|
(power->displayBatteryPercentage == DISPLAY_BATTERY_RETRO_3) &&
|
2022-09-08 15:24:25 +00:00
|
|
|
(power->state != PowerStateCharging)) { //Retro style segmented display, 3 parts
|
2022-09-08 10:52:18 +00:00
|
|
|
if(power->info.charge > 25) {
|
|
|
|
canvas_draw_box(canvas, 2, 2, 6, 4);
|
|
|
|
}
|
|
|
|
if(power->info.charge > 50) {
|
2022-09-08 15:24:25 +00:00
|
|
|
canvas_draw_box(canvas, 9, 2, 6, 4);
|
2022-09-08 10:52:18 +00:00
|
|
|
}
|
|
|
|
if(power->info.charge > 75) {
|
2022-09-08 15:24:25 +00:00
|
|
|
canvas_draw_box(canvas, 16, 2, 6, 4);
|
2022-09-08 10:52:18 +00:00
|
|
|
}
|
2022-09-08 15:24:25 +00:00
|
|
|
} else if(
|
2023-01-09 14:15:00 +00:00
|
|
|
(power->displayBatteryPercentage == DISPLAY_BATTERY_RETRO_5) &&
|
2022-09-08 15:24:25 +00:00
|
|
|
(power->state != PowerStateCharging)) { //Retro style segmented display, 5 parts
|
2022-09-08 10:52:18 +00:00
|
|
|
if(power->info.charge > 10) {
|
|
|
|
canvas_draw_box(canvas, 2, 2, 3, 4);
|
|
|
|
}
|
|
|
|
if(power->info.charge > 30) {
|
|
|
|
canvas_draw_box(canvas, 6, 2, 3, 4);
|
|
|
|
}
|
|
|
|
if(power->info.charge > 50) {
|
|
|
|
canvas_draw_box(canvas, 10, 2, 3, 4);
|
|
|
|
}
|
|
|
|
if(power->info.charge > 70) {
|
|
|
|
canvas_draw_box(canvas, 14, 2, 3, 4);
|
|
|
|
}
|
|
|
|
if(power->info.charge > 90) {
|
|
|
|
canvas_draw_box(canvas, 18, 2, 3, 4);
|
|
|
|
}
|
2023-01-09 14:15:00 +00:00
|
|
|
} else if(
|
|
|
|
(power->displayBatteryPercentage == DISPLAY_BATTERY_BAR_PERCENT) &&
|
|
|
|
(power->state != PowerStateCharging) && // Default bar display with percentage
|
2023-02-26 18:24:38 +00:00
|
|
|
(power->info.voltage_battery_charge_limit >=
|
2023-01-18 16:19:49 +00:00
|
|
|
4.2)) { // not looking nice with low voltage indicator
|
|
|
|
canvas_set_font(canvas, FontBatteryPercent);
|
2023-01-09 14:15:00 +00:00
|
|
|
|
2023-02-20 15:55:53 +00:00
|
|
|
// align charge display value with digits to draw
|
2023-01-09 14:15:00 +00:00
|
|
|
uint8_t bar_charge = power->info.charge;
|
|
|
|
if(bar_charge > 23 && bar_charge < 38) {
|
|
|
|
bar_charge = 23;
|
|
|
|
} else if(bar_charge >= 38 && bar_charge < 62) {
|
|
|
|
bar_charge = 50;
|
|
|
|
} else if(bar_charge >= 62 && bar_charge < 74) {
|
|
|
|
bar_charge = 74;
|
|
|
|
}
|
|
|
|
|
|
|
|
// drawing digits
|
2023-01-12 08:35:42 +00:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_box(canvas, 1, 1, (bar_charge * 22) / 100, 6);
|
2023-01-09 14:15:00 +00:00
|
|
|
if(bar_charge < 38) { // both digits are black
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 11, 4, AlignCenter, AlignCenter, batteryPercentile);
|
|
|
|
} else if(bar_charge >= 38 && bar_charge < 74) { // first digit is white
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
|
|
|
|
// first
|
|
|
|
char batteryPercentileFirstDigit[2];
|
|
|
|
snprintf(
|
|
|
|
batteryPercentileFirstDigit,
|
|
|
|
sizeof(batteryPercentileFirstDigit),
|
|
|
|
"%c",
|
|
|
|
batteryPercentile[0]);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 9, 4, AlignCenter, AlignCenter, batteryPercentileFirstDigit);
|
|
|
|
|
|
|
|
// second
|
|
|
|
char batteryPercentileSecondDigit[2];
|
|
|
|
snprintf(
|
|
|
|
batteryPercentileSecondDigit,
|
|
|
|
sizeof(batteryPercentileSecondDigit),
|
|
|
|
"%c",
|
|
|
|
batteryPercentile[1]);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 15, 4, AlignCenter, AlignCenter, batteryPercentileSecondDigit);
|
2023-01-12 08:35:42 +00:00
|
|
|
} else { // charge >= 74, both digits are white
|
2023-01-09 14:15:00 +00:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 11, 4, AlignCenter, AlignCenter, batteryPercentile);
|
|
|
|
}
|
|
|
|
|
2022-09-08 10:52:18 +00:00
|
|
|
} else { //default bar display, added here to serve as fallback/default behaviour.
|
|
|
|
canvas_draw_box(canvas, 2, 2, (power->info.charge + 4) / 5, 4);
|
|
|
|
}
|
2022-09-08 15:24:25 +00:00
|
|
|
|
2022-12-09 17:00:39 +00:00
|
|
|
// TODO: Verify if it displays correctly with custom battery skins !!!
|
2023-02-26 17:23:39 +00:00
|
|
|
if(power->info.voltage_battery_charge_limit < 4.2) {
|
2022-12-09 17:00:39 +00:00
|
|
|
// Battery charging voltage is modified, indicate with cross pattern
|
|
|
|
canvas_invert_color(canvas);
|
|
|
|
uint8_t battery_bar_width = (power->info.charge + 4) / 5;
|
|
|
|
bool cross_odd = false;
|
|
|
|
// Start 1 further in from the battery bar's x position
|
|
|
|
for(uint8_t x = 3; x <= battery_bar_width; x++) {
|
|
|
|
// Cross pattern is from the center of the battery bar
|
|
|
|
// y = 2 + 1 (inset) + 1 (for every other)
|
|
|
|
canvas_draw_dot(canvas, x, 3 + (uint8_t)cross_odd);
|
|
|
|
cross_odd = !cross_odd;
|
|
|
|
}
|
|
|
|
canvas_invert_color(canvas);
|
|
|
|
}
|
|
|
|
|
2022-04-18 23:09:11 +00:00
|
|
|
if(power->state == PowerStateCharging) {
|
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2022-05-06 17:35:52 +00:00
|
|
|
// TODO: replace -1 magic for uint8_t with re-framing
|
2023-01-12 08:35:42 +00:00
|
|
|
if(power->displayBatteryPercentage == DISPLAY_BATTERY_PERCENT) {
|
2022-11-22 05:51:47 +00:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_box(canvas, 1, 1, 22, 6);
|
|
|
|
canvas_draw_icon(canvas, 2, -1, &I_Charging_lightning_9x10);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_icon(canvas, 2, -1, &I_Charging_lightning_mask_9x10);
|
|
|
|
canvas_set_font(canvas, FontBatteryPercent);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 16, 4, AlignCenter, AlignCenter, batteryPercentile);
|
2023-01-09 14:15:00 +00:00
|
|
|
} else if(power->displayBatteryPercentage == DISPLAY_BATTERY_INVERTED_PERCENT) {
|
2022-11-22 05:51:47 +00:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_box(canvas, 1, 1, 22, 6);
|
|
|
|
canvas_draw_icon(canvas, 2, -1, &I_Charging_lightning_9x10);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_icon(canvas, 2, -1, &I_Charging_lightning_mask_9x10);
|
|
|
|
canvas_set_font(canvas, FontBatteryPercent);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 16, 4, AlignCenter, AlignCenter, batteryPercentile);
|
2023-01-12 08:35:42 +00:00
|
|
|
} else if(power->displayBatteryPercentage == DISPLAY_BATTERY_BAR_PERCENT) {
|
|
|
|
// clean-up default charging bar display
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_box(canvas, 1, 1, 22, 6);
|
|
|
|
|
2023-02-20 15:55:53 +00:00
|
|
|
// align charge display value with digits to draw
|
2023-01-12 08:35:42 +00:00
|
|
|
uint8_t bar_charge = power->info.charge;
|
|
|
|
|
|
|
|
if(bar_charge > 48 && bar_charge < 63) {
|
|
|
|
bar_charge = 48;
|
|
|
|
} else if(bar_charge >= 63 && bar_charge < 84) {
|
|
|
|
bar_charge = 75;
|
|
|
|
} else if(bar_charge >= 84 && bar_charge < 96) {
|
|
|
|
bar_charge = 96;
|
|
|
|
}
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_box(canvas, 1, 1, (bar_charge * 22) / 100, 6);
|
|
|
|
|
|
|
|
// drawing charge icon
|
|
|
|
canvas_draw_icon(canvas, 2, -1, &I_Charging_lightning_9x10);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_icon(canvas, 2, -1, &I_Charging_lightning_mask_9x10);
|
|
|
|
|
|
|
|
// drawing digits
|
|
|
|
canvas_set_font(canvas, FontBatteryPercent);
|
|
|
|
if(bar_charge < 64) { // both digits are black
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 16, 4, AlignCenter, AlignCenter, batteryPercentile);
|
|
|
|
} else if(bar_charge >= 64 && bar_charge < 84) { // first digit is white
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
|
|
|
|
// first
|
|
|
|
char batteryPercentileFirstDigit[2];
|
|
|
|
snprintf(
|
|
|
|
batteryPercentileFirstDigit,
|
|
|
|
sizeof(batteryPercentileFirstDigit),
|
|
|
|
"%c",
|
|
|
|
batteryPercentile[0]);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 14, 4, AlignCenter, AlignCenter, batteryPercentileFirstDigit);
|
|
|
|
|
|
|
|
// second
|
|
|
|
char batteryPercentileSecondDigit[2];
|
|
|
|
snprintf(
|
|
|
|
batteryPercentileSecondDigit,
|
|
|
|
sizeof(batteryPercentileSecondDigit),
|
|
|
|
"%c",
|
|
|
|
batteryPercentile[1]);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 20, 4, AlignCenter, AlignCenter, batteryPercentileSecondDigit);
|
|
|
|
} else { // charge >= 84, both digits are white
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 16, 4, AlignCenter, AlignCenter, batteryPercentile);
|
|
|
|
}
|
2022-11-22 05:51:47 +00:00
|
|
|
} else {
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_icon(canvas, 8, -1, &I_Charging_lightning_mask_9x10);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_icon(canvas, 8, -1, &I_Charging_lightning_9x10);
|
|
|
|
}
|
2022-04-18 23:09:11 +00:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-06 17:35:52 +00:00
|
|
|
canvas_draw_box(canvas, 8, 3, 8, 2);
|
2021-12-24 20:00:45 +00:00
|
|
|
}
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ViewPort* power_battery_view_port_alloc(Power* power) {
|
|
|
|
ViewPort* battery_view_port = view_port_alloc();
|
|
|
|
view_port_set_width(battery_view_port, icon_get_width(&I_Battery_26x8));
|
|
|
|
view_port_draw_callback_set(battery_view_port, power_draw_battery_callback, power);
|
|
|
|
gui_add_view_port(power->gui, battery_view_port, GuiLayerStatusBarRight);
|
|
|
|
return battery_view_port;
|
|
|
|
}
|
|
|
|
|
|
|
|
Power* power_alloc() {
|
2022-02-18 19:53:46 +00:00
|
|
|
Power* power = malloc(sizeof(Power));
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
// Records
|
2022-07-26 12:21:51 +00:00
|
|
|
power->notification = furi_record_open(RECORD_NOTIFICATION);
|
|
|
|
power->gui = furi_record_open(RECORD_GUI);
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
// Pubsub
|
2021-11-01 20:35:54 +00:00
|
|
|
power->event_pubsub = furi_pubsub_alloc();
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
// State initialization
|
|
|
|
power->state = PowerStateNotCharging;
|
|
|
|
power->battery_low = false;
|
|
|
|
power->power_off_timeout = POWER_OFF_TIMEOUT;
|
2022-07-20 10:56:33 +00:00
|
|
|
power->api_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
// Gui
|
|
|
|
power->view_dispatcher = view_dispatcher_alloc();
|
|
|
|
power->power_off = power_off_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
power->view_dispatcher, PowerViewOff, power_off_get_view(power->power_off));
|
2022-08-22 17:54:01 +00:00
|
|
|
power->power_unplug_usb = power_unplug_usb_alloc();
|
|
|
|
view_dispatcher_add_view(
|
|
|
|
power->view_dispatcher,
|
|
|
|
PowerViewUnplugUsb,
|
|
|
|
power_unplug_usb_get_view(power->power_unplug_usb));
|
2021-09-24 16:28:02 +00:00
|
|
|
view_dispatcher_attach_to_gui(
|
|
|
|
power->view_dispatcher, power->gui, ViewDispatcherTypeFullscreen);
|
|
|
|
|
|
|
|
// Battery view port
|
|
|
|
power->battery_view_port = power_battery_view_port_alloc(power);
|
2022-01-02 22:52:45 +00:00
|
|
|
power->show_low_bat_level_message = true;
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
return power;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void power_check_charging_state(Power* power) {
|
|
|
|
if(furi_hal_power_is_charging()) {
|
2022-09-26 15:34:59 +00:00
|
|
|
if((power->info.charge == 100) || (furi_hal_power_is_charging_done())) {
|
2021-09-24 16:28:02 +00:00
|
|
|
if(power->state != PowerStateCharged) {
|
|
|
|
notification_internal_message(power->notification, &sequence_charged);
|
|
|
|
power->state = PowerStateCharged;
|
|
|
|
power->event.type = PowerEventTypeFullyCharged;
|
2021-11-01 20:35:54 +00:00
|
|
|
furi_pubsub_publish(power->event_pubsub, &power->event);
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(power->state != PowerStateCharging) {
|
|
|
|
notification_internal_message(power->notification, &sequence_charging);
|
|
|
|
power->state = PowerStateCharging;
|
|
|
|
power->event.type = PowerEventTypeStartCharging;
|
2021-11-01 20:35:54 +00:00
|
|
|
furi_pubsub_publish(power->event_pubsub, &power->event);
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(power->state != PowerStateNotCharging) {
|
|
|
|
notification_internal_message(power->notification, &sequence_not_charging);
|
|
|
|
power->state = PowerStateNotCharging;
|
|
|
|
power->event.type = PowerEventTypeStopCharging;
|
2021-11-01 20:35:54 +00:00
|
|
|
furi_pubsub_publish(power->event_pubsub, &power->event);
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 17:00:56 +00:00
|
|
|
static bool power_update_info(Power* power) {
|
|
|
|
PowerInfo info;
|
|
|
|
|
2022-09-14 13:29:17 +00:00
|
|
|
info.is_charging = furi_hal_power_is_charging();
|
2022-04-18 23:09:11 +00:00
|
|
|
info.gauge_is_ok = furi_hal_power_gauge_is_ok();
|
2023-07-18 10:46:38 +00:00
|
|
|
info.is_shutdown_requested = furi_hal_power_is_shutdown_requested();
|
2021-10-02 17:00:56 +00:00
|
|
|
info.charge = furi_hal_power_get_pct();
|
|
|
|
info.health = furi_hal_power_get_bat_health_pct();
|
|
|
|
info.capacity_remaining = furi_hal_power_get_battery_remaining_capacity();
|
|
|
|
info.capacity_full = furi_hal_power_get_battery_full_capacity();
|
|
|
|
info.current_charger = furi_hal_power_get_battery_current(FuriHalPowerICCharger);
|
|
|
|
info.current_gauge = furi_hal_power_get_battery_current(FuriHalPowerICFuelGauge);
|
2023-02-26 17:23:39 +00:00
|
|
|
info.voltage_battery_charge_limit = furi_hal_power_get_battery_charge_voltage_limit();
|
2021-10-02 17:00:56 +00:00
|
|
|
info.voltage_charger = furi_hal_power_get_battery_voltage(FuriHalPowerICCharger);
|
|
|
|
info.voltage_gauge = furi_hal_power_get_battery_voltage(FuriHalPowerICFuelGauge);
|
|
|
|
info.voltage_vbus = furi_hal_power_get_usb_voltage();
|
|
|
|
info.temperature_charger = furi_hal_power_get_battery_temperature(FuriHalPowerICCharger);
|
|
|
|
info.temperature_gauge = furi_hal_power_get_battery_temperature(FuriHalPowerICFuelGauge);
|
2021-09-24 16:28:02 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(power->api_mtx, FuriWaitForever);
|
2021-10-02 17:00:56 +00:00
|
|
|
bool need_refresh = power->info.charge != info.charge;
|
2022-09-14 13:29:17 +00:00
|
|
|
need_refresh |= power->info.is_charging != info.is_charging;
|
2021-10-02 17:00:56 +00:00
|
|
|
power->info = info;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(power->api_mtx);
|
2021-10-02 17:00:56 +00:00
|
|
|
|
|
|
|
return need_refresh;
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void power_check_low_battery(Power* power) {
|
2022-04-18 23:09:11 +00:00
|
|
|
if(!power->info.gauge_is_ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-24 16:28:02 +00:00
|
|
|
// Check battery charge and vbus voltage
|
2023-07-18 10:46:38 +00:00
|
|
|
if((power->info.is_shutdown_requested) && (power->info.voltage_vbus < 4.0f) &&
|
2022-01-02 22:52:45 +00:00
|
|
|
power->show_low_bat_level_message) {
|
2021-09-24 16:28:02 +00:00
|
|
|
if(!power->battery_low) {
|
2021-10-06 15:41:22 +00:00
|
|
|
view_dispatcher_send_to_front(power->view_dispatcher);
|
2021-09-24 16:28:02 +00:00
|
|
|
view_dispatcher_switch_to_view(power->view_dispatcher, PowerViewOff);
|
|
|
|
}
|
|
|
|
power->battery_low = true;
|
|
|
|
} else {
|
|
|
|
if(power->battery_low) {
|
|
|
|
view_dispatcher_switch_to_view(power->view_dispatcher, VIEW_NONE);
|
|
|
|
power->power_off_timeout = POWER_OFF_TIMEOUT;
|
|
|
|
}
|
|
|
|
power->battery_low = false;
|
|
|
|
}
|
|
|
|
// If battery low, update view and switch off power after timeout
|
|
|
|
if(power->battery_low) {
|
2022-05-19 16:07:45 +00:00
|
|
|
PowerOffResponse response = power_off_get_response(power->power_off);
|
|
|
|
if(response == PowerOffResponseDefault) {
|
|
|
|
if(power->power_off_timeout) {
|
|
|
|
power_off_set_time_left(power->power_off, power->power_off_timeout--);
|
|
|
|
} else {
|
|
|
|
power_off(power);
|
|
|
|
}
|
|
|
|
} else if(response == PowerOffResponseOk) {
|
2021-10-06 15:41:22 +00:00
|
|
|
power_off(power);
|
2022-05-19 16:07:45 +00:00
|
|
|
} else if(response == PowerOffResponseHide) {
|
|
|
|
view_dispatcher_switch_to_view(power->view_dispatcher, VIEW_NONE);
|
|
|
|
if(power->power_off_timeout) {
|
|
|
|
power_off_set_time_left(power->power_off, power->power_off_timeout--);
|
|
|
|
} else {
|
|
|
|
power_off(power);
|
|
|
|
}
|
|
|
|
} else if(response == PowerOffResponseCancel) {
|
|
|
|
view_dispatcher_switch_to_view(power->view_dispatcher, VIEW_NONE);
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void power_check_battery_level_change(Power* power) {
|
|
|
|
if(power->battery_level != power->info.charge) {
|
|
|
|
power->battery_level = power->info.charge;
|
|
|
|
power->event.type = PowerEventTypeBatteryLevelChanged;
|
|
|
|
power->event.data.battery_level = power->battery_level;
|
2021-11-01 20:35:54 +00:00
|
|
|
furi_pubsub_publish(power->event_pubsub, &power->event);
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-21 12:55:06 +00:00
|
|
|
void power_trigger_ui_update(Power* power) {
|
|
|
|
DesktopSettings* settings = malloc(sizeof(DesktopSettings));
|
|
|
|
bool is_loaded = DESKTOP_SETTINGS_LOAD(settings);
|
|
|
|
if(is_loaded) {
|
|
|
|
power->displayBatteryPercentage = settings->displayBatteryPercentage;
|
|
|
|
} else {
|
|
|
|
power->displayBatteryPercentage = DISPLAY_BATTERY_BAR;
|
|
|
|
}
|
|
|
|
free(settings);
|
|
|
|
view_port_update(power->battery_view_port);
|
|
|
|
}
|
|
|
|
|
2021-09-24 16:28:02 +00:00
|
|
|
int32_t power_srv(void* p) {
|
2022-08-03 15:47:10 +00:00
|
|
|
UNUSED(p);
|
2023-02-26 15:15:26 +00:00
|
|
|
|
|
|
|
if(furi_hal_rtc_get_boot_mode() != FuriHalRtcBootModeNormal) {
|
|
|
|
FURI_LOG_W(TAG, "Skipping start in special boot mode");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-24 16:28:02 +00:00
|
|
|
Power* power = power_alloc();
|
2021-11-29 12:51:15 +00:00
|
|
|
power_update_info(power);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_create(RECORD_POWER, power);
|
2022-09-08 15:24:25 +00:00
|
|
|
|
2022-09-08 05:31:57 +00:00
|
|
|
DesktopSettings* settings = malloc(sizeof(DesktopSettings));
|
2022-09-16 21:43:21 +00:00
|
|
|
DESKTOP_SETTINGS_LOAD(settings);
|
2022-09-08 05:31:57 +00:00
|
|
|
power->displayBatteryPercentage = settings->displayBatteryPercentage;
|
|
|
|
free(settings);
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
// Update data from gauge and charger
|
2021-10-02 17:00:56 +00:00
|
|
|
bool need_refresh = power_update_info(power);
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
// Check low battery level
|
|
|
|
power_check_low_battery(power);
|
|
|
|
|
|
|
|
// Check and notify about charging state
|
|
|
|
power_check_charging_state(power);
|
|
|
|
|
|
|
|
// Check and notify about battery level change
|
|
|
|
power_check_battery_level_change(power);
|
|
|
|
|
|
|
|
// Update battery view port
|
2022-09-08 05:31:57 +00:00
|
|
|
if(need_refresh) {
|
|
|
|
view_port_update(power->battery_view_port);
|
|
|
|
}
|
2021-09-24 16:28:02 +00:00
|
|
|
|
2022-02-16 17:52:34 +00:00
|
|
|
// Check OTG status and disable it in case of fault
|
|
|
|
if(furi_hal_power_is_otg_enabled()) {
|
|
|
|
furi_hal_power_check_otg_status();
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_ms(1000);
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 15:17:12 +00:00
|
|
|
furi_crash("That was unexpected");
|
2021-09-24 16:28:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|