Api-hal-power: reset routine. Power: safe to poweroff message, cleanup. (#436)

This commit is contained in:
あく 2021-04-30 07:51:48 +03:00 committed by GitHub
parent eb82748d50
commit 43ff095724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 77 additions and 38 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -2,16 +2,18 @@
#include <api-hal.h>
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);
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <cli/cli.h>
#include "power.h"
void power_cli_init(Cli* cli);
void power_cli_init(Cli* cli, Power* power);

View File

@ -1,4 +1,5 @@
#include "power_views.h"
#include <gui/elements.h>
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");
}

View File

@ -6,7 +6,7 @@
#include <gui/canvas.h>
#include <gui/view.h>
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);

View File

@ -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();

View File

@ -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();
}