[FL-1783] Power service refactoring (#718)
* settings power: introduce power settings app * power: move power API to separate file * settings power: implement reboot scene * settings power: add power off scene * assets: add crying dolphin, fix Subghz assets names * settings power: fix power off scene GUI * settings power: add battery info scene * power: add cli to application on start hook * power: remove power from main menu * power: move to power service folder * power service: rework power off logic * power: add pubsub events * bt: subscribe to battery level change, update characteristic * application: change order of Settings applications * gui: add bubble element * power: gui improvements * application: rename Notification -> LCD and notifications * Applications: menu order according to documentation and add missing power cli init * settings power: add disconnect USB scene * power cli: notify user to disconnect USB after poweroff * Power: update poweroff message in cli Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
		| @@ -0,0 +1,17 @@ | ||||
| #include "../power_settings_app.h" | ||||
|  | ||||
| void power_settings_scene_usb_disconnect_on_enter(void* context) { | ||||
|     PowerSettingsApp* app = context; | ||||
|  | ||||
|     dialog_ex_set_header( | ||||
|         app->dialog, "Disconnect USB for safe\nshutdown", 64, 26, AlignCenter, AlignTop); | ||||
|  | ||||
|     view_dispatcher_switch_to_view(app->view_dispatcher, PowerSettingsAppViewDialog); | ||||
| } | ||||
|  | ||||
| bool power_settings_scene_usb_disconnect_on_event(void* context, SceneManagerEvent event) { | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| void power_settings_scene_usb_disconnect_on_exit(void* context) { | ||||
| } | ||||
| @@ -0,0 +1,30 @@ | ||||
| #include "power_settings_scene.h" | ||||
|  | ||||
| // Generate scene on_enter handlers array | ||||
| #define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter, | ||||
| void (*const power_settings_on_enter_handlers[])(void*) = { | ||||
| #include "power_settings_scene_config.h" | ||||
| }; | ||||
| #undef ADD_SCENE | ||||
|  | ||||
| // Generate scene on_event handlers array | ||||
| #define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event, | ||||
| bool (*const power_settings_on_event_handlers[])(void* context, SceneManagerEvent event) = { | ||||
| #include "power_settings_scene_config.h" | ||||
| }; | ||||
| #undef ADD_SCENE | ||||
|  | ||||
| // Generate scene on_exit handlers array | ||||
| #define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit, | ||||
| void (*const power_settings_on_exit_handlers[])(void* context) = { | ||||
| #include "power_settings_scene_config.h" | ||||
| }; | ||||
| #undef ADD_SCENE | ||||
|  | ||||
| // Initialize scene handlers configuration structure | ||||
| const SceneManagerHandlers power_settings_scene_handlers = { | ||||
|     .on_enter_handlers = power_settings_on_enter_handlers, | ||||
|     .on_event_handlers = power_settings_on_event_handlers, | ||||
|     .on_exit_handlers = power_settings_on_exit_handlers, | ||||
|     .scene_num = PowerSettingsAppSceneNum, | ||||
| }; | ||||
| @@ -0,0 +1,29 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <gui/scene_manager.h> | ||||
|  | ||||
| // Generate scene id and total number | ||||
| #define ADD_SCENE(prefix, name, id) PowerSettingsAppScene##id, | ||||
| typedef enum { | ||||
| #include "power_settings_scene_config.h" | ||||
|     PowerSettingsAppSceneNum, | ||||
| } PowerSettingsAppScene; | ||||
| #undef ADD_SCENE | ||||
|  | ||||
| extern const SceneManagerHandlers power_settings_scene_handlers; | ||||
|  | ||||
| // Generate scene on_enter handlers declaration | ||||
| #define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*); | ||||
| #include "power_settings_scene_config.h" | ||||
| #undef ADD_SCENE | ||||
|  | ||||
| // Generate scene on_event handlers declaration | ||||
| #define ADD_SCENE(prefix, name, id) \ | ||||
|     bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event); | ||||
| #include "power_settings_scene_config.h" | ||||
| #undef ADD_SCENE | ||||
|  | ||||
| // Generate scene on_exit handlers declaration | ||||
| #define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context); | ||||
| #include "power_settings_scene_config.h" | ||||
| #undef ADD_SCENE | ||||
| @@ -0,0 +1,34 @@ | ||||
| #include "../power_settings_app.h" | ||||
|  | ||||
| static void power_settings_scene_battery_info_update_model(PowerSettingsApp* app) { | ||||
|     power_get_info(app->power, &app->info); | ||||
|     BatteryInfoModel battery_info_data = { | ||||
|         .vbus_voltage = app->info.voltage_vbus, | ||||
|         .gauge_voltage = app->info.voltage_gauge, | ||||
|         .gauge_current = app->info.current_gauge, | ||||
|         .gauge_temperature = app->info.temperature_gauge, | ||||
|         .charge = app->info.charge, | ||||
|         .health = app->info.health, | ||||
|     }; | ||||
|     battery_info_set_data(app->batery_info, &battery_info_data); | ||||
| } | ||||
|  | ||||
| void power_settings_scene_battery_info_on_enter(void* context) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     power_settings_scene_battery_info_update_model(app); | ||||
|     view_dispatcher_switch_to_view(app->view_dispatcher, PowerSettingsAppViewBatteryInfo); | ||||
| } | ||||
|  | ||||
| bool power_settings_scene_battery_info_on_event(void* context, SceneManagerEvent event) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     bool consumed = false; | ||||
|  | ||||
|     if(event.type == SceneManagerEventTypeTick) { | ||||
|         power_settings_scene_battery_info_update_model(app); | ||||
|         consumed = true; | ||||
|     } | ||||
|     return consumed; | ||||
| } | ||||
|  | ||||
| void power_settings_scene_battery_info_on_exit(void* context) { | ||||
| } | ||||
| @@ -0,0 +1,5 @@ | ||||
| ADD_SCENE(power_settings, start, Start) | ||||
| ADD_SCENE(power_settings, battery_info, BatteryInfo) | ||||
| ADD_SCENE(power_settings, reboot, Reboot) | ||||
| ADD_SCENE(power_settings, power_off, PowerOff) | ||||
| ADD_SCENE(power_settings, usb_disconnect, UsbDisconnect) | ||||
| @@ -0,0 +1,48 @@ | ||||
| #include "../power_settings_app.h" | ||||
|  | ||||
| void power_settings_scene_power_off_dialog_callback(DialogExResult result, void* context) { | ||||
|     furi_assert(context); | ||||
|     PowerSettingsApp* app = context; | ||||
|     view_dispatcher_send_custom_event(app->view_dispatcher, result); | ||||
| } | ||||
|  | ||||
| void power_settings_scene_power_off_on_enter(void* context) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     DialogEx* dialog = app->dialog; | ||||
|  | ||||
|     dialog_ex_set_header(dialog, "Turn off Device?", 64, 2, AlignCenter, AlignTop); | ||||
|     dialog_ex_set_text( | ||||
|         dialog, "   I will be\nwaiting for\n you here...", 78, 16, AlignLeft, AlignTop); | ||||
|     dialog_ex_set_icon(dialog, 21, 13, &I_Cry_dolph_55x52); | ||||
|     dialog_ex_set_left_button_text(dialog, "Back"); | ||||
|     dialog_ex_set_right_button_text(dialog, "OFF"); | ||||
|     dialog_ex_set_result_callback(dialog, power_settings_scene_power_off_dialog_callback); | ||||
|     dialog_ex_set_context(dialog, app); | ||||
|  | ||||
|     view_dispatcher_switch_to_view(app->view_dispatcher, PowerSettingsAppViewDialog); | ||||
| } | ||||
|  | ||||
| bool power_settings_scene_power_off_on_event(void* context, SceneManagerEvent event) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     bool consumed = false; | ||||
|  | ||||
|     if(event.type == SceneManagerEventTypeCustom) { | ||||
|         if(event.event == DialogExResultLeft) { | ||||
|             scene_manager_previous_scene(app->scene_manager); | ||||
|         } else if(event.event == DialogExResultRight) { | ||||
|             power_off(); | ||||
|             // Check if USB is connected | ||||
|             power_get_info(app->power, &app->info); | ||||
|             if(app->info.voltage_vbus > 4.0f) { | ||||
|                 scene_manager_next_scene(app->scene_manager, PowerSettingsAppSceneUsbDisconnect); | ||||
|             } | ||||
|         } | ||||
|         consumed = true; | ||||
|     } | ||||
|     return consumed; | ||||
| } | ||||
|  | ||||
| void power_settings_scene_power_off_on_exit(void* context) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     dialog_ex_clean(app->dialog); | ||||
| } | ||||
							
								
								
									
										52
									
								
								applications/power/power_settings_app/scenes/power_settings_scene_reboot.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										52
									
								
								applications/power/power_settings_app/scenes/power_settings_scene_reboot.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| #include "../power_settings_app.h" | ||||
|  | ||||
| enum PowerSettingsRebootSubmenuIndex { | ||||
|     PowerSettingsRebootSubmenuIndexDfu, | ||||
|     PowerSettingsRebootSubmenuIndexOs, | ||||
| }; | ||||
|  | ||||
| void power_settings_scene_reboot_submenu_callback(void* context, uint32_t index) { | ||||
|     furi_assert(context); | ||||
|     PowerSettingsApp* app = context; | ||||
|     view_dispatcher_send_custom_event(app->view_dispatcher, index); | ||||
| } | ||||
|  | ||||
| void power_settings_scene_reboot_on_enter(void* context) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     Submenu* submenu = app->submenu; | ||||
|  | ||||
|     submenu_set_header(submenu, "Reboot type"); | ||||
|     submenu_add_item( | ||||
|         submenu, | ||||
|         "Firmware upgrade", | ||||
|         PowerSettingsRebootSubmenuIndexDfu, | ||||
|         power_settings_scene_reboot_submenu_callback, | ||||
|         app); | ||||
|     submenu_add_item( | ||||
|         submenu, | ||||
|         "Flipper OS", | ||||
|         PowerSettingsRebootSubmenuIndexOs, | ||||
|         power_settings_scene_reboot_submenu_callback, | ||||
|         app); | ||||
|  | ||||
|     view_dispatcher_switch_to_view(app->view_dispatcher, PowerSettingsAppViewSubmenu); | ||||
| } | ||||
|  | ||||
| bool power_settings_scene_reboot_on_event(void* context, SceneManagerEvent event) { | ||||
|     bool consumed = false; | ||||
|  | ||||
|     if(event.type == SceneManagerEventTypeCustom) { | ||||
|         if(event.event == PowerSettingsRebootSubmenuIndexDfu) { | ||||
|             power_reboot(PowerBootModeDfu); | ||||
|         } else if(event.event == PowerSettingsRebootSubmenuIndexOs) { | ||||
|             power_reboot(PowerBootModeNormal); | ||||
|         } | ||||
|         consumed = true; | ||||
|     } | ||||
|     return consumed; | ||||
| } | ||||
|  | ||||
| void power_settings_scene_reboot_on_exit(void* context) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     submenu_clean(app->submenu); | ||||
| } | ||||
							
								
								
									
										64
									
								
								applications/power/power_settings_app/scenes/power_settings_scene_start.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										64
									
								
								applications/power/power_settings_app/scenes/power_settings_scene_start.c
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| #include "../power_settings_app.h" | ||||
|  | ||||
| enum PowerSettingsSubmenuIndex { | ||||
|     PowerSettingsSubmenuIndexBatteryInfo, | ||||
|     PowerSettingsSubmenuIndexReboot, | ||||
|     PowerSettingsSubmenuIndexOff, | ||||
| }; | ||||
|  | ||||
| static void power_settings_scene_start_submenu_callback(void* context, uint32_t index) { | ||||
|     furi_assert(context); | ||||
|     PowerSettingsApp* app = context; | ||||
|     view_dispatcher_send_custom_event(app->view_dispatcher, index); | ||||
| } | ||||
|  | ||||
| void power_settings_scene_start_on_enter(void* context) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     Submenu* submenu = app->submenu; | ||||
|  | ||||
|     submenu_add_item( | ||||
|         submenu, | ||||
|         "Battery info", | ||||
|         PowerSettingsSubmenuIndexBatteryInfo, | ||||
|         power_settings_scene_start_submenu_callback, | ||||
|         app); | ||||
|     submenu_add_item( | ||||
|         submenu, | ||||
|         "Reboot", | ||||
|         PowerSettingsSubmenuIndexReboot, | ||||
|         power_settings_scene_start_submenu_callback, | ||||
|         app); | ||||
|     submenu_add_item( | ||||
|         submenu, | ||||
|         "Power OFF", | ||||
|         PowerSettingsSubmenuIndexOff, | ||||
|         power_settings_scene_start_submenu_callback, | ||||
|         app); | ||||
|     submenu_set_selected_item( | ||||
|         submenu, scene_manager_get_scene_state(app->scene_manager, PowerSettingsAppSceneStart)); | ||||
|  | ||||
|     view_dispatcher_switch_to_view(app->view_dispatcher, PowerSettingsAppViewSubmenu); | ||||
| } | ||||
|  | ||||
| bool power_settings_scene_start_on_event(void* context, SceneManagerEvent event) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     bool consumed = false; | ||||
|  | ||||
|     if(event.type == SceneManagerEventTypeCustom) { | ||||
|         if(event.event == PowerSettingsSubmenuIndexBatteryInfo) { | ||||
|             scene_manager_next_scene(app->scene_manager, PowerSettingsAppSceneBatteryInfo); | ||||
|         } else if(event.event == PowerSettingsSubmenuIndexReboot) { | ||||
|             scene_manager_next_scene(app->scene_manager, PowerSettingsAppSceneReboot); | ||||
|         } else if(event.event == PowerSettingsSubmenuIndexOff) { | ||||
|             scene_manager_next_scene(app->scene_manager, PowerSettingsAppScenePowerOff); | ||||
|         } | ||||
|         scene_manager_set_scene_state(app->scene_manager, PowerSettingsAppSceneStart, event.event); | ||||
|         consumed = true; | ||||
|     } | ||||
|     return consumed; | ||||
| } | ||||
|  | ||||
| void power_settings_scene_start_on_exit(void* context) { | ||||
|     PowerSettingsApp* app = context; | ||||
|     submenu_clean(app->submenu); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user