From 43ff095724bced15ff4ffdd817c29bda94bd3ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=81=8F?= Date: Fri, 30 Apr 2021 07:51:48 +0300 Subject: [PATCH] Api-hal-power: reset routine. Power: safe to poweroff message, cleanup. (#436) --- applications/power/power.c | 50 ++++++++++--------- applications/power/power.h | 16 ++++++ applications/power/power_cli.c | 24 +++++---- applications/power/power_cli.h | 3 +- applications/power/power_views.c | 7 +++ applications/power/power_views.h | 4 +- .../targets/api-hal-include/api-hal-power.h | 5 +- firmware/targets/f5/api-hal/api-hal-power.c | 6 ++- 8 files changed, 77 insertions(+), 38 deletions(-) diff --git a/applications/power/power.c b/applications/power/power.c index cd417be4..a27231c2 100644 --- a/applications/power/power.c +++ b/applications/power/power.c @@ -22,9 +22,7 @@ struct Power { ViewDispatcher* view_dispatcher; View* info_view; View* off_view; - - // Icon* usb_icon; - // ViewPort* usb_view_port; + View* disconnect_view; Icon* battery_icon; ViewPort* battery_view_port; @@ -36,12 +34,6 @@ struct Power { MenuItem* menu; }; -// void power_draw_usb_callback(Canvas* canvas, void* context) { -// furi_assert(context); -// Power* power = context; -// canvas_draw_icon(canvas, 0, 0, power->usb_icon); -// } - void power_draw_battery_callback(Canvas* canvas, void* context) { furi_assert(context); Power* power = context; @@ -58,16 +50,16 @@ uint32_t power_info_back_callback(void* context) { } void power_menu_off_callback(void* context) { - api_hal_power_off(); + Power* power = context; + power_off(power); } void power_menu_reset_dialog_result(DialogResult result, void* context) { + Power* power = context; if(result == DialogResultLeft) { - api_hal_boot_set_mode(ApiHalBootModeDFU); - NVIC_SystemReset(); + power_reset(power, PowerBootModeDfu); } else if(result == DialogResultRight) { - api_hal_boot_set_mode(ApiHalBootModeNormal); - NVIC_SystemReset(); + power_reset(power, PowerBootModeNormal); } } @@ -100,7 +92,7 @@ Power* power_alloc() { power->menu_vm = furi_record_open("menu"); power->cli = furi_record_open("cli"); - power_cli_init(power->cli); + power_cli_init(power->cli, power); power->menu = menu_item_alloc_menu("Power", assets_icons_get(A_Power_14)); menu_item_subitem_add( @@ -128,16 +120,15 @@ Power* power_alloc() { view_set_draw_callback(power->off_view, power_off_draw_callback); view_dispatcher_add_view(power->view_dispatcher, PowerViewOff, power->off_view); + power->disconnect_view = view_alloc(); + view_set_draw_callback(power->disconnect_view, power_disconnect_draw_callback); + view_dispatcher_add_view(power->view_dispatcher, PowerViewDisconnect, power->disconnect_view); + power->dialog = dialog_alloc(); dialog_set_context(power->dialog, power); view_dispatcher_add_view( power->view_dispatcher, PowerViewDialog, dialog_get_view(power->dialog)); - // power->usb_icon = assets_icons_get(I_USBConnected_15x8); - // power->usb_view_port = view_port_alloc(); - // view_port_set_width(power->usb_view_port, icon_get_width(power->usb_icon)); - // view_port_draw_callback_set(power->usb_view_port, power_draw_usb_callback, power); - power->battery_icon = assets_icons_get(I_Battery_26x8); power->battery_view_port = view_port_alloc(); @@ -151,6 +142,21 @@ void power_free(Power* power) { free(power); } +void power_off(Power* power) { + furi_assert(power); + api_hal_power_off(); + view_dispatcher_switch_to_view(power->view_dispatcher, PowerViewDisconnect); +} + +void power_reset(Power* power, PowerBootMode mode) { + if(mode == PowerBootModeNormal) { + api_hal_boot_set_mode(ApiHalBootModeNormal); + } else if(mode == PowerBootModeDfu) { + api_hal_boot_set_mode(ApiHalBootModeDFU); + } + api_hal_power_reset(); +} + static void power_charging_indication_handler() { if(api_hal_power_is_charging()) { if(api_hal_power_get_pct() == 100) { @@ -173,7 +179,6 @@ int32_t power_task(void* p) { Power* power = power_alloc(); Gui* gui = furi_record_open("gui"); - //gui_add_view_port(gui, power->usb_view_port, GuiLayerStatusBarLeft); gui_add_view_port(gui, power->battery_view_port, GuiLayerStatusBarRight); view_dispatcher_attach_to_gui(power->view_dispatcher, gui, ViewDispatcherTypeFullscreen); @@ -216,7 +221,7 @@ int32_t power_task(void* p) { osKernelGetTickCount() + osKernelGetTickFreq() * POWER_OFF_TIMEOUT; } else { if(osKernelGetTickCount() > model->poweroff_tick) { - api_hal_power_off(); + power_off(power); } } } else { @@ -234,7 +239,6 @@ int32_t power_task(void* p) { power_charging_indication_handler(); view_port_update(power->battery_view_port); - //view_port_enabled_set(power->usb_view_port, api_hal_power_is_charging()); osDelay(1024); } diff --git a/applications/power/power.h b/applications/power/power.h index f43b9c3f..fef130a8 100644 --- a/applications/power/power.h +++ b/applications/power/power.h @@ -1,3 +1,19 @@ #pragma once typedef struct Power Power; + +typedef enum { + PowerBootModeNormal, + PowerBootModeDfu, +} PowerBootMode; + +/** Power off device + * @param power - Power instance + */ +void power_off(Power* power); + +/** Reset device + * @param power - Power instance + * @param mode - PowerBootMode + */ +void power_reset(Power* power, PowerBootMode mode); diff --git a/applications/power/power_cli.c b/applications/power/power_cli.c index f33a7d61..ea7caf58 100644 --- a/applications/power/power_cli.c +++ b/applications/power/power_cli.c @@ -2,16 +2,18 @@ #include void power_cli_poweroff(string_t args, void* context) { - api_hal_power_off(); + Power* power = context; + power_off(power); } void power_cli_reset(string_t args, void* context) { - NVIC_SystemReset(); + Power* power = context; + power_reset(power, PowerBootModeNormal); } void power_cli_dfu(string_t args, void* context) { - api_hal_boot_set_mode(ApiHalBootModeDFU); - NVIC_SystemReset(); + Power* power = context; + power_reset(power, PowerBootModeDfu); } void power_cli_test(string_t args, void* context) { @@ -26,11 +28,11 @@ void power_cli_otg_off(string_t args, void* context) { api_hal_power_disable_otg(); } -void power_cli_init(Cli* cli) { - cli_add_command(cli, "poweroff", power_cli_poweroff, NULL); - cli_add_command(cli, "reset", power_cli_reset, NULL); - cli_add_command(cli, "dfu", power_cli_dfu, NULL); - cli_add_command(cli, "power_test", power_cli_test, NULL); - cli_add_command(cli, "power_otg_on", power_cli_otg_on, NULL); - cli_add_command(cli, "power_otg_off", power_cli_otg_off, NULL); +void power_cli_init(Cli* cli, Power* power) { + cli_add_command(cli, "poweroff", power_cli_poweroff, power); + cli_add_command(cli, "reset", power_cli_reset, power); + cli_add_command(cli, "dfu", power_cli_dfu, power); + cli_add_command(cli, "power_test", power_cli_test, power); + cli_add_command(cli, "power_otg_on", power_cli_otg_on, power); + cli_add_command(cli, "power_otg_off", power_cli_otg_off, power); } diff --git a/applications/power/power_cli.h b/applications/power/power_cli.h index ca4e6bcb..5d8f208c 100644 --- a/applications/power/power_cli.h +++ b/applications/power/power_cli.h @@ -1,5 +1,6 @@ #pragma once #include +#include "power.h" -void power_cli_init(Cli* cli); \ No newline at end of file +void power_cli_init(Cli* cli, Power* power); \ No newline at end of file diff --git a/applications/power/power_views.c b/applications/power/power_views.c index 6508026c..b851f6f8 100644 --- a/applications/power/power_views.c +++ b/applications/power/power_views.c @@ -1,4 +1,5 @@ #include "power_views.h" +#include static void draw_stat(Canvas* canvas, int x, int y, IconName icon, char* val) { canvas_draw_frame(canvas, x - 7, y + 7, 30, 13); @@ -122,3 +123,9 @@ void power_off_draw_callback(Canvas* canvas, void* context) { (model->poweroff_tick - osKernelGetTickCount()) / osKernelGetTickFreq()); canvas_draw_str(canvas, 5, 42, buffer); } + +void power_disconnect_draw_callback(Canvas* canvas, void* context) { + canvas_set_font(canvas, FontPrimary); + elements_multiline_text_aligned( + canvas, 64, 32, AlignCenter, AlignCenter, "It's now safe to turn off\nyour flipper"); +} diff --git a/applications/power/power_views.h b/applications/power/power_views.h index c7ea27f2..2aaa114f 100644 --- a/applications/power/power_views.h +++ b/applications/power/power_views.h @@ -6,7 +6,7 @@ #include #include -typedef enum { PowerViewInfo, PowerViewDialog, PowerViewOff } PowerView; +typedef enum { PowerViewInfo, PowerViewDialog, PowerViewOff, PowerViewDisconnect } PowerView; typedef struct { float current_charger; @@ -34,3 +34,5 @@ typedef struct { void power_info_draw_callback(Canvas* canvas, void* context); void power_off_draw_callback(Canvas* canvas, void* context); + +void power_disconnect_draw_callback(Canvas* canvas, void* context); \ No newline at end of file diff --git a/firmware/targets/api-hal-include/api-hal-power.h b/firmware/targets/api-hal-include/api-hal-power.h index 04198199..25b20d5e 100644 --- a/firmware/targets/api-hal-include/api-hal-power.h +++ b/firmware/targets/api-hal-include/api-hal-power.h @@ -57,9 +57,12 @@ uint8_t api_hal_power_get_bat_health_pct(); /** Get charging status */ bool api_hal_power_is_charging(); -/** Poweroff system */ +/** Poweroff device */ void api_hal_power_off(); +/** Reset device */ +void api_hal_power_reset(); + /** OTG enable */ void api_hal_power_enable_otg(); diff --git a/firmware/targets/f5/api-hal/api-hal-power.c b/firmware/targets/f5/api-hal/api-hal-power.c index f13cc29d..e90ebd80 100644 --- a/firmware/targets/f5/api-hal/api-hal-power.c +++ b/firmware/targets/f5/api-hal/api-hal-power.c @@ -48,7 +48,7 @@ const ParamCEDV cedv = { void HAL_RCC_CSSCallback(void) { // TODO: notify user about issue with HSE - NVIC_SystemReset(); + api_hal_power_reset(); } void api_hal_power_init() { @@ -150,6 +150,10 @@ void api_hal_power_off() { bq25896_poweroff(); } +void api_hal_power_reset() { + NVIC_SystemReset(); +} + void api_hal_power_enable_otg() { bq25896_enable_otg(); }