From c9bc05199e24e059a394f97cb36f90a2927b2d4d Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:18:51 +0300 Subject: [PATCH] add auto power off timer setting [ci skip] by @Dmitry422 with little fixes by @xMasterX --- applications/services/desktop/desktop.c | 55 +++++++++++++++++++ applications/services/desktop/desktop_i.h | 3 + .../services/desktop/desktop_settings.c | 40 ++++++++++---- .../services/desktop/desktop_settings.h | 3 + .../scenes/desktop_settings_scene_start.c | 35 ++++++++++++ 5 files changed, 124 insertions(+), 12 deletions(-) diff --git a/applications/services/desktop/desktop.c b/applications/services/desktop/desktop.c index 1132760d5..8b24e38f0 100644 --- a/applications/services/desktop/desktop.c +++ b/applications/services/desktop/desktop.c @@ -19,6 +19,12 @@ static void desktop_auto_lock_arm(Desktop*); static void desktop_auto_lock_inhibit(Desktop*); static void desktop_start_auto_lock_timer(Desktop*); static void desktop_apply_settings(Desktop*); +//--- auto_power_off_timer +#include +static void desktop_start_auto_poweroff_timer(Desktop*); +static void desktop_auto_poweroff_arm(Desktop*); +static void desktop_auto_poweroff_inhibit(Desktop*); +//--- static void desktop_loader_callback(const void* message, void* context) { furi_assert(context); @@ -131,6 +137,9 @@ static bool desktop_custom_event_callback(void* context, uint32_t event) { } desktop_auto_lock_inhibit(desktop); + //--- auto_power_off_timer + desktop_auto_poweroff_inhibit(desktop); + //-- desktop->app_running = true; furi_semaphore_release(desktop->animation_semaphore); @@ -138,6 +147,9 @@ static bool desktop_custom_event_callback(void* context, uint32_t event) { } else if(event == DesktopGlobalAfterAppFinished) { animation_manager_load_and_continue_animation(desktop->animation_manager); desktop_auto_lock_arm(desktop); + //--- auto_power_off_timer + desktop_auto_poweroff_arm(desktop); + //--- desktop->app_running = false; } else if(event == DesktopGlobalAutoLock) { @@ -179,6 +191,9 @@ static void desktop_input_event_callback(const void* value, void* context) { Desktop* desktop = context; if(event->type == InputTypePress) { desktop_start_auto_lock_timer(desktop); + //--- m96yda + desktop_start_auto_poweroff_timer(desktop); + //--- } } @@ -213,6 +228,39 @@ static void desktop_auto_lock_inhibit(Desktop* desktop) { } } +//--- auto_power_off_timer +static void desktop_auto_poweroff_timer_callback(void* context) { + UNUSED(context); + Power* power = furi_record_open(RECORD_POWER); + power_off(power); +} + +static void desktop_start_auto_poweroff_timer(Desktop* desktop) { + furi_timer_start( + desktop->auto_poweroff_timer, furi_ms_to_ticks(desktop->settings.auto_poweroff_delay_ms)); +} + +static void desktop_stop_auto_poweroff_timer(Desktop* desktop) { + furi_timer_stop(desktop->auto_poweroff_timer); +} + +static void desktop_auto_poweroff_arm(Desktop* desktop) { + if(desktop->settings.auto_poweroff_delay_ms) { + desktop->input_events_subscription = furi_pubsub_subscribe( + desktop->input_events_pubsub, desktop_input_event_callback, desktop); + desktop_start_auto_poweroff_timer(desktop); + } +} + +static void desktop_auto_poweroff_inhibit(Desktop* desktop) { + desktop_stop_auto_poweroff_timer(desktop); + if(desktop->input_events_subscription) { + furi_pubsub_unsubscribe(desktop->input_events_pubsub, desktop->input_events_subscription); + desktop->input_events_subscription = NULL; + } +} +//--- + static void desktop_clock_timer_callback(void* context) { furi_assert(context); Desktop* desktop = context; @@ -238,6 +286,9 @@ static void desktop_apply_settings(Desktop* desktop) { if(!desktop->app_running && !desktop->locked) { desktop_auto_lock_arm(desktop); + //--- auto_power_off_timer + desktop_auto_poweroff_arm(desktop); + //--- } desktop->in_transition = false; @@ -373,6 +424,10 @@ static Desktop* desktop_alloc(void) { desktop->auto_lock_timer = furi_timer_alloc(desktop_auto_lock_timer_callback, FuriTimerTypeOnce, desktop); + //--- auto_power_off_timer + desktop->auto_poweroff_timer = + furi_timer_alloc(desktop_auto_poweroff_timer_callback, FuriTimerTypeOnce, desktop); + //--- desktop->status_pubsub = furi_pubsub_alloc(); diff --git a/applications/services/desktop/desktop_i.h b/applications/services/desktop/desktop_i.h index 1dc7c7d21..abf5cd579 100644 --- a/applications/services/desktop/desktop_i.h +++ b/applications/services/desktop/desktop_i.h @@ -75,6 +75,9 @@ struct Desktop { FuriTimer* auto_lock_timer; FuriTimer* update_clock_timer; + //--- auto_power_off_timer + FuriTimer* auto_poweroff_timer; + //--- AnimationManager* animation_manager; FuriSemaphore* animation_semaphore; diff --git a/applications/services/desktop/desktop_settings.c b/applications/services/desktop/desktop_settings.c index 828ec5f0d..45b9e5ee3 100644 --- a/applications/services/desktop/desktop_settings.c +++ b/applications/services/desktop/desktop_settings.c @@ -6,16 +6,20 @@ #define TAG "DesktopSettings" -#define DESKTOP_SETTINGS_VER_13 (13) -#define DESKTOP_SETTINGS_VER (14) +#define DESKTOP_SETTINGS_VER_14 (14) +#define DESKTOP_SETTINGS_VER (15) #define DESKTOP_SETTINGS_PATH INT_PATH(DESKTOP_SETTINGS_FILE_NAME) #define DESKTOP_SETTINGS_MAGIC (0x17) typedef struct { - uint8_t reserved[11]; - DesktopSettings settings; -} DesktopSettingsV13; + uint32_t auto_lock_delay_ms; + uint8_t displayBatteryPercentage; + uint8_t dummy_mode; + uint8_t display_clock; + FavoriteApp favorite_apps[FavoriteAppNumber]; + FavoriteApp dummy_apps[DummyAppNumber]; +} DesktopSettingsV14; // Actual size of DesktopSettings v13 //static_assert(sizeof(DesktopSettingsV13) == 1234); @@ -37,21 +41,33 @@ void desktop_settings_load(DesktopSettings* settings) { DESKTOP_SETTINGS_MAGIC, DESKTOP_SETTINGS_VER); - } else if(version == DESKTOP_SETTINGS_VER_13) { - DesktopSettingsV13* settings_v13 = malloc(sizeof(DesktopSettingsV13)); + } else if(version == DESKTOP_SETTINGS_VER_14) { + DesktopSettingsV14* settings_v14 = malloc(sizeof(DesktopSettingsV14)); success = saved_struct_load( DESKTOP_SETTINGS_PATH, - settings_v13, - sizeof(DesktopSettingsV13), + settings_v14, + sizeof(DesktopSettingsV14), DESKTOP_SETTINGS_MAGIC, - DESKTOP_SETTINGS_VER_13); + DESKTOP_SETTINGS_VER_14); if(success) { - *settings = settings_v13->settings; + settings->auto_lock_delay_ms = settings_v14->auto_lock_delay_ms; + settings->auto_poweroff_delay_ms = 0; + settings->displayBatteryPercentage = settings_v14->displayBatteryPercentage; + settings->dummy_mode = settings_v14->dummy_mode; + settings->display_clock = settings_v14->display_clock; + memcpy( + settings_v14->favorite_apps, + settings->favorite_apps, + sizeof(settings_v14->favorite_apps)); + memcpy( + settings_v14->dummy_apps, + settings->dummy_apps, + sizeof(settings_v14->dummy_apps)); } - free(settings_v13); + free(settings_v14); } } while(false); diff --git a/applications/services/desktop/desktop_settings.h b/applications/services/desktop/desktop_settings.h index ba5a78840..e2e91d2dd 100644 --- a/applications/services/desktop/desktop_settings.h +++ b/applications/services/desktop/desktop_settings.h @@ -38,6 +38,9 @@ typedef struct { typedef struct { uint32_t auto_lock_delay_ms; + //--- auto_power_off_timer + uint32_t auto_poweroff_delay_ms; + //--- uint8_t displayBatteryPercentage; uint8_t dummy_mode; uint8_t display_clock; diff --git a/applications/settings/desktop_settings/scenes/desktop_settings_scene_start.c b/applications/settings/desktop_settings/scenes/desktop_settings_scene_start.c index 04cb0182b..9954a2c28 100644 --- a/applications/settings/desktop_settings/scenes/desktop_settings_scene_start.c +++ b/applications/settings/desktop_settings/scenes/desktop_settings_scene_start.c @@ -28,6 +28,15 @@ typedef enum { DesktopSettingsDummyOkLong, } DesktopSettingsEntry; +// --- auto_power_off_timer +#define AUTO_POWEROFF_DELAY_COUNT 8 +const char* const auto_poweroff_delay_text[AUTO_POWEROFF_DELAY_COUNT] = + {"OFF", "5min", "10min", "15min", "30min", "45min", "60min", "90min"}; + +const uint32_t auto_poweroff_delay_value[AUTO_POWEROFF_DELAY_COUNT] = + {0, 300000, 600000, 900000, 1800000, 2700000, 3600000, 5400000}; +// --- + #define AUTO_LOCK_DELAY_COUNT 9 const char* const auto_lock_delay_text[AUTO_LOCK_DELAY_COUNT] = { "OFF", @@ -86,6 +95,16 @@ static void desktop_settings_scene_start_clock_enable_changed(VariableItem* item app->settings.display_clock = index; } +// --- auto_power_off_timer +static void desktop_settings_scene_start_auto_poweroff_delay_changed(VariableItem* item) { + DesktopSettingsApp* app = variable_item_get_context(item); + uint8_t index = variable_item_get_current_value_index(item); + + variable_item_set_current_value_text(item, auto_poweroff_delay_text[index]); + app->settings.auto_poweroff_delay_ms = auto_poweroff_delay_value[index]; +} +// --- + static void desktop_settings_scene_start_auto_lock_delay_changed(VariableItem* item) { DesktopSettingsApp* app = variable_item_get_context(item); uint8_t index = variable_item_get_current_value_index(item); @@ -115,6 +134,22 @@ void desktop_settings_scene_start_on_enter(void* context) { variable_item_set_current_value_index(item, value_index); variable_item_set_current_value_text(item, auto_lock_delay_text[value_index]); + // --- auto_power_off_timer + item = variable_item_list_add( + variable_item_list, + "Auto PowerOff", + AUTO_POWEROFF_DELAY_COUNT, + desktop_settings_scene_start_auto_poweroff_delay_changed, + app); + + value_index = value_index_uint32( + app->settings.auto_poweroff_delay_ms, + auto_poweroff_delay_value, + AUTO_POWEROFF_DELAY_COUNT); + variable_item_set_current_value_index(item, value_index); + variable_item_set_current_value_text(item, auto_poweroff_delay_text[value_index]); + // --- + item = variable_item_list_add( variable_item_list, "Battery View",