Skip to content

Commit

Permalink
Merge pull request #857 from Dmitry422/dev
Browse files Browse the repository at this point in the history
Auto_poweroff_timer moved to power service. Power service have own config file.
  • Loading branch information
xMasterX authored Jan 14, 2025
2 parents f054d05 + cf50875 commit b4c7cde
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 138 deletions.
78 changes: 16 additions & 62 deletions applications/services/desktop/desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
#include "scenes/desktop_scene.h"
#include "scenes/desktop_scene_locked.h"

#include "furi_hal_power.h"

#define TAG "Desktop"

// dublicate constants from desktop_setting_scene_start.c
#define USB_INHIBIT_AUTOLOCK_OFF 0
#define USB_INHIBIT_AUTOLOCK_ON 1
#define USB_INHIBIT_AUTOLOCK_RPC 2

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 <power/power_service/power.h>
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);
Expand Down Expand Up @@ -137,30 +138,28 @@ 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);

} 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) {
if(!desktop->app_running && !desktop->locked) {
// if usb_inhibit_autolock enabled and device charging or device charged but still connected to USB then break desktop locking.
if ((desktop->settings.usb_inhibit_auto_lock == USB_INHIBIT_AUTOLOCK_ON) && ((furi_hal_power_is_charging()) || (furi_hal_power_is_charging_done()))){
return(0);
}
// if usb_inhibit_autolock set to RPC and we have F0 connected to phone or PC app then break desktop locking.
if (desktop->settings.usb_inhibit_auto_lock == USB_INHIBIT_AUTOLOCK_RPC){
return(0);
}
desktop_lock(desktop);
}
} else if(event == DesktopGlobalAutoPowerOff) {
if(!desktop->app_running) {
Power* power = furi_record_open(RECORD_POWER);
power_off(power);
}
} else if(event == DesktopGlobalSaveSettings) {
desktop_settings_save(&desktop->settings);
desktop_apply_settings(desktop);
Expand Down Expand Up @@ -195,9 +194,6 @@ static void desktop_input_event_callback(const void* value, void* context) {
Desktop* desktop = context;
if(event->type == InputTypePress) {
desktop_start_auto_lock_timer(desktop);
//--- auto_power_off_timer
desktop_start_auto_poweroff_timer(desktop);
//---
}
}

Expand Down Expand Up @@ -234,41 +230,6 @@ static void desktop_auto_lock_inhibit(Desktop* desktop) {
}
}

//--- auto_power_off_timer
static void desktop_auto_poweroff_timer_callback(void* context) {
furi_assert(context);
Desktop* desktop = context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalAutoPowerOff);
}

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) {
if(!desktop->input_events_subscription) {
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;
Expand All @@ -294,9 +255,6 @@ 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;
Expand Down Expand Up @@ -432,10 +390,6 @@ 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();

Expand Down
3 changes: 0 additions & 3 deletions applications/services/desktop/desktop_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ struct Desktop {

FuriTimer* auto_lock_timer;
FuriTimer* update_clock_timer;
//--- auto_power_off_timer
FuriTimer* auto_poweroff_timer;
//---

AnimationManager* animation_manager;
FuriSemaphore* animation_semaphore;
Expand Down
33 changes: 17 additions & 16 deletions applications/services/desktop/desktop_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

#define TAG "DesktopSettings"

#define DESKTOP_SETTINGS_VER_14 (14)
#define DESKTOP_SETTINGS_VER (15)
#define DESKTOP_SETTINGS_VER_15 (15)
#define DESKTOP_SETTINGS_VER (16)

#define DESKTOP_SETTINGS_PATH INT_PATH(DESKTOP_SETTINGS_FILE_NAME)
#define DESKTOP_SETTINGS_MAGIC (0x17)

typedef struct {
uint32_t auto_lock_delay_ms;
uint32_t auto_poweroff_delay_ms;
uint8_t displayBatteryPercentage;
uint8_t dummy_mode;
uint8_t display_clock;
FavoriteApp favorite_apps[FavoriteAppNumber];
FavoriteApp dummy_apps[DummyAppNumber];
} DesktopSettingsV14;
} DesktopSettingsV15;

// Actual size of DesktopSettings v13
//static_assert(sizeof(DesktopSettingsV13) == 1234);
Expand All @@ -41,31 +42,31 @@ void desktop_settings_load(DesktopSettings* settings) {
DESKTOP_SETTINGS_MAGIC,
DESKTOP_SETTINGS_VER);

} else if(version == DESKTOP_SETTINGS_VER_14) {
DesktopSettingsV14* settings_v14 = malloc(sizeof(DesktopSettingsV14));
} else if(version == DESKTOP_SETTINGS_VER_15) {
DesktopSettingsV15* settings_v15 = malloc(sizeof(DesktopSettingsV15));

success = saved_struct_load(
DESKTOP_SETTINGS_PATH,
settings_v14,
sizeof(DesktopSettingsV14),
settings_v15,
sizeof(DesktopSettingsV15),
DESKTOP_SETTINGS_MAGIC,
DESKTOP_SETTINGS_VER_14);
DESKTOP_SETTINGS_VER_15);

if(success) {
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;
settings->auto_lock_delay_ms = settings_v15->auto_lock_delay_ms;
settings->usb_inhibit_auto_lock = 0;
settings->displayBatteryPercentage = settings_v15->displayBatteryPercentage;
settings->dummy_mode = settings_v15->dummy_mode;
settings->display_clock = settings_v15->display_clock;
memcpy(
settings->favorite_apps,
settings_v14->favorite_apps,
settings_v15->favorite_apps,
sizeof(settings->favorite_apps));
memcpy(
settings->dummy_apps, settings_v14->dummy_apps, sizeof(settings->dummy_apps));
settings->dummy_apps, settings_v15->dummy_apps, sizeof(settings->dummy_apps));
}

free(settings_v14);
free(settings_v15);
}

} while(false);
Expand Down
4 changes: 1 addition & 3 deletions applications/services/desktop/desktop_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ typedef struct {

typedef struct {
uint32_t auto_lock_delay_ms;
//--- auto_power_off_timer
uint32_t auto_poweroff_delay_ms;
//---
uint8_t usb_inhibit_auto_lock;
uint8_t displayBatteryPercentage;
uint8_t dummy_mode;
uint8_t display_clock;
Expand Down
1 change: 0 additions & 1 deletion applications/services/desktop/views/desktop_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,4 @@ typedef enum {
DesktopGlobalApiUnlock,
DesktopGlobalSaveSettings,
DesktopGlobalReloadSettings,
DesktopGlobalAutoPowerOff,
} DesktopEvent;
Loading

0 comments on commit b4c7cde

Please sign in to comment.