From a3b4defa44f039c47732df265c3fa57b2d2b66d2 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Wed, 31 May 2023 17:33:05 +0100 Subject: [PATCH] Revert "Revert "Feature: Custom user set charging cap (#265)"" This reverts commit 31f624d662e425486705d0d1925ad6d667fe4f99. --- .../xtreme_app/scenes/xtreme_app_scene_misc.c | 23 +++++++++++++++++++ .../services/power/power_service/power.c | 18 +++++++++++++++ .../services/power/power_service/power.h | 1 + lib/xtreme/settings.c | 4 ++++ lib/xtreme/xtreme.h | 1 + 5 files changed, 47 insertions(+) diff --git a/applications/main/xtreme_app/scenes/xtreme_app_scene_misc.c b/applications/main/xtreme_app/scenes/xtreme_app_scene_misc.c index 9d44f4d5b..35bc24d09 100644 --- a/applications/main/xtreme_app/scenes/xtreme_app_scene_misc.c +++ b/applications/main/xtreme_app/scenes/xtreme_app_scene_misc.c @@ -45,6 +45,17 @@ static void xtreme_app_scene_misc_butthurt_timer_changed(VariableItem* item) { app->require_reboot = true; } +#define CHARGE_CAP_INTV 5 +static void xtreme_app_scene_misc_charge_cap_changed(VariableItem* item) { + XtremeApp* app = variable_item_get_context(item); + char cap_str[6]; + uint32_t value = (variable_item_get_current_value_index(item) + 1) * CHARGE_CAP_INTV; + snprintf(cap_str, 6, "%lu%%", value); + variable_item_set_current_value_text(item, cap_str); + XTREME_SETTINGS()->charge_cap = value; + app->save_settings = true; +} + static void xtreme_app_scene_misc_lcd_color_changed(VariableItem* item) { XtremeApp* app = variable_item_get_context(item); uint8_t index = variable_item_get_current_value_index(item); @@ -96,6 +107,18 @@ void xtreme_app_scene_misc_on_enter(void* context) { variable_item_set_current_value_index(item, value_index); variable_item_set_current_value_text(item, butthurt_timer_names[value_index]); + char cap_str[6]; + value_index = xtreme_settings->charge_cap / CHARGE_CAP_INTV; + snprintf(cap_str, 6, "%lu%%", (uint32_t)value_index * CHARGE_CAP_INTV); + item = variable_item_list_add( + var_item_list, + "Charge Cap", + 100 / CHARGE_CAP_INTV, + xtreme_app_scene_misc_charge_cap_changed, + app); + variable_item_set_current_value_index(item, value_index - 1); + variable_item_set_current_value_text(item, cap_str); + item = variable_item_list_add(var_item_list, "RGB Backlight", 1, NULL, app); variable_item_set_current_value_text(item, xtreme_settings->rgb_backlight ? "ON" : "OFF"); diff --git a/applications/services/power/power_service/power.c b/applications/services/power/power_service/power.c index c7e00beb6..423f767b9 100644 --- a/applications/services/power/power_service/power.c +++ b/applications/services/power/power_service/power.c @@ -472,6 +472,20 @@ static void power_check_battery_level_change(Power* power) { } } +static void power_check_charge_cap(Power* power) { + if(power->info.charge >= XTREME_SETTINGS()->charge_cap) { + if(!power->info.is_charge_capped) { // Suppress charging if charge reaches custom cap + power->info.is_charge_capped = true; + furi_hal_power_suppress_charge_enter(); + } + } else { + if(power->info.is_charge_capped) { // Start charging again if charge below custom cap + power->info.is_charge_capped = false; + furi_hal_power_suppress_charge_exit(); + } + } +} + void power_trigger_ui_update(Power* power) { view_port_update(power->battery_view_port); } @@ -491,6 +505,7 @@ int32_t power_srv(void* p) { } power_auto_shutdown_arm(power); power_update_info(power); + power->info.is_charge_capped = false; // default false furi_record_create(RECORD_POWER, power); while(1) { @@ -506,6 +521,9 @@ int32_t power_srv(void* p) { // Check and notify about battery level change power_check_battery_level_change(power); + // Check charge cap, compare with user setting and suppress/unsuppress charging + power_check_charge_cap(power); + // Update battery view port if(need_refresh) { view_port_update(power->battery_view_port); diff --git a/applications/services/power/power_service/power.h b/applications/services/power/power_service/power.h index 4a8f07a89..8a9d5947d 100644 --- a/applications/services/power/power_service/power.h +++ b/applications/services/power/power_service/power.h @@ -48,6 +48,7 @@ typedef struct { typedef struct { bool gauge_is_ok; bool is_charging; + bool is_charge_capped; float current_charger; float current_gauge; diff --git a/lib/xtreme/settings.c b/lib/xtreme/settings.c index aeab97e8a..2ced296a4 100644 --- a/lib/xtreme/settings.c +++ b/lib/xtreme/settings.c @@ -29,6 +29,7 @@ XtremeSettings xtreme_settings = { .bad_bt = false, // USB .bad_bt_remember = false, // OFF .butthurt_timer = 21600, // 6 H + .charge_cap = 100, // 100% .rgb_backlight = false, // OFF }; @@ -90,6 +91,8 @@ void XTREME_SETTINGS_LOAD() { flipper_format_rewind(file); flipper_format_read_int32(file, "butthurt_timer", &x->butthurt_timer, 1); flipper_format_rewind(file); + flipper_format_read_uint32(file, "charge_cap", &x->charge_cap, 1); + flipper_format_rewind(file); flipper_format_read_bool(file, "rgb_backlight", &x->rgb_backlight, 1); } flipper_format_free(file); @@ -126,6 +129,7 @@ void XTREME_SETTINGS_SAVE() { flipper_format_write_bool(file, "bad_bt", &x->bad_bt, 1); flipper_format_write_bool(file, "bad_bt_remember", &x->bad_bt_remember, 1); flipper_format_write_int32(file, "butthurt_timer", &x->butthurt_timer, 1); + flipper_format_write_uint32(file, "charge_cap", &x->charge_cap, 1); flipper_format_write_bool(file, "rgb_backlight", &x->rgb_backlight, 1); } flipper_format_free(file); diff --git a/lib/xtreme/xtreme.h b/lib/xtreme/xtreme.h index b1eb408bf..bb5b265e9 100644 --- a/lib/xtreme/xtreme.h +++ b/lib/xtreme/xtreme.h @@ -37,6 +37,7 @@ typedef struct { bool bad_bt; bool bad_bt_remember; int32_t butthurt_timer; + uint32_t charge_cap; bool rgb_backlight; } XtremeSettings;