2020-10-29 07:11:16 +00:00
|
|
|
#include "power.h"
|
|
|
|
|
|
|
|
#include <flipper_v2.h>
|
2020-11-11 06:31:35 +00:00
|
|
|
|
|
|
|
#include <menu/menu.h>
|
|
|
|
#include <menu/menu_item.h>
|
2020-10-29 07:11:16 +00:00
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <gui/widget.h>
|
|
|
|
#include <assets_icons.h>
|
2020-11-11 06:31:35 +00:00
|
|
|
#include <api-hal-power.h>
|
2020-11-16 10:16:34 +00:00
|
|
|
#include <cli/cli.h>
|
2020-10-29 07:11:16 +00:00
|
|
|
|
|
|
|
struct Power {
|
|
|
|
Icon* usb_icon;
|
|
|
|
Widget* usb_widget;
|
|
|
|
|
|
|
|
Icon* battery_icon;
|
|
|
|
Widget* battery_widget;
|
|
|
|
|
2020-12-18 18:03:28 +00:00
|
|
|
Widget* widget;
|
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
ValueMutex* menu_vm;
|
2020-11-16 10:16:34 +00:00
|
|
|
Cli* cli;
|
2020-11-11 06:31:35 +00:00
|
|
|
MenuItem* menu;
|
|
|
|
|
2020-12-22 18:16:32 +00:00
|
|
|
float current_charger;
|
|
|
|
float current_gauge;
|
|
|
|
float voltage_charger;
|
|
|
|
float voltage_gauge;
|
|
|
|
uint32_t capacity_remaining;
|
|
|
|
uint32_t capacity_full;
|
|
|
|
float temperature_charger;
|
|
|
|
float temperature_gauge;
|
2020-12-18 18:03:28 +00:00
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
uint8_t charge;
|
2020-10-29 07:11:16 +00:00
|
|
|
};
|
|
|
|
|
2020-12-14 10:50:32 +00:00
|
|
|
void power_draw_usb_callback(Canvas* canvas, void* context) {
|
2020-10-29 07:11:16 +00:00
|
|
|
assert(context);
|
|
|
|
Power* power = context;
|
2020-12-14 10:50:32 +00:00
|
|
|
canvas_draw_icon(canvas, 0, 0, power->usb_icon);
|
2020-10-29 07:11:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 10:50:32 +00:00
|
|
|
void power_draw_battery_callback(Canvas* canvas, void* context) {
|
2020-10-29 07:11:16 +00:00
|
|
|
assert(context);
|
|
|
|
Power* power = context;
|
|
|
|
|
2020-12-14 10:50:32 +00:00
|
|
|
canvas_draw_icon(canvas, 0, 0, power->battery_icon);
|
|
|
|
canvas_draw_box(canvas, 2, 2, (float)power->charge / 100 * 14, 4);
|
2020-10-29 07:11:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
void power_off_callback(void* context) {
|
|
|
|
api_hal_power_off();
|
|
|
|
}
|
2020-10-29 07:11:16 +00:00
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
void power_enable_otg_callback(void* context) {
|
|
|
|
api_hal_power_enable_otg();
|
|
|
|
}
|
2020-10-29 07:11:16 +00:00
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
void power_disable_otg_callback(void* context) {
|
|
|
|
api_hal_power_disable_otg();
|
2020-10-29 07:11:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:03:28 +00:00
|
|
|
void power_info_callback(void* context) {
|
|
|
|
Power* power = context;
|
|
|
|
widget_enabled_set(power->widget, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void power_draw_callback(Canvas* canvas, void* context) {
|
|
|
|
Power* power = context;
|
|
|
|
|
|
|
|
canvas_clear(canvas);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
canvas_draw_str(canvas, 2, 10, "Power state:");
|
|
|
|
|
|
|
|
char buffer[64];
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
2020-12-22 18:16:32 +00:00
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
64,
|
|
|
|
"Current: %ld/%ldmA",
|
|
|
|
(int32_t)(power->current_gauge * 1000),
|
|
|
|
(int32_t)(power->current_charger * 1000));
|
2020-12-18 18:03:28 +00:00
|
|
|
canvas_draw_str(canvas, 5, 22, buffer);
|
2020-12-22 18:16:32 +00:00
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
64,
|
|
|
|
"Voltage: %ld/%ldmV",
|
|
|
|
(uint32_t)(power->voltage_gauge * 1000),
|
|
|
|
(uint32_t)(power->voltage_charger * 1000));
|
2020-12-18 18:03:28 +00:00
|
|
|
canvas_draw_str(canvas, 5, 32, buffer);
|
|
|
|
snprintf(buffer, 64, "Charge: %ld%%", (uint32_t)(power->charge));
|
|
|
|
canvas_draw_str(canvas, 5, 42, buffer);
|
2020-12-22 18:16:32 +00:00
|
|
|
snprintf(
|
|
|
|
buffer, 64, "Capacity: %ld of %ldmAh", power->capacity_remaining, power->capacity_full);
|
2020-12-18 18:03:28 +00:00
|
|
|
canvas_draw_str(canvas, 5, 52, buffer);
|
2020-12-22 18:16:32 +00:00
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
64,
|
|
|
|
"Temperature: %ld/%ldC",
|
|
|
|
(uint32_t)(power->temperature_gauge),
|
|
|
|
(uint32_t)(power->temperature_charger));
|
|
|
|
canvas_draw_str(canvas, 5, 62, buffer);
|
2020-12-18 18:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void power_input_callback(InputEvent* event, void* context) {
|
|
|
|
Power* power = context;
|
|
|
|
|
2020-12-22 18:16:32 +00:00
|
|
|
if(!event->state || event->input != InputBack) return;
|
2020-12-18 18:03:28 +00:00
|
|
|
|
|
|
|
widget_enabled_set(power->widget, false);
|
|
|
|
}
|
|
|
|
|
2020-10-29 07:11:16 +00:00
|
|
|
Power* power_alloc() {
|
|
|
|
Power* power = furi_alloc(sizeof(Power));
|
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
power->menu_vm = furi_open("menu");
|
|
|
|
furi_check(power->menu_vm);
|
|
|
|
|
2020-11-16 10:16:34 +00:00
|
|
|
power->cli = furi_open("cli");
|
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
power->menu = menu_item_alloc_menu("Power", NULL);
|
|
|
|
menu_item_subitem_add(
|
|
|
|
power->menu, menu_item_alloc_function("Poweroff", NULL, power_off_callback, power));
|
|
|
|
menu_item_subitem_add(
|
|
|
|
power->menu,
|
|
|
|
menu_item_alloc_function("Enable OTG", NULL, power_enable_otg_callback, power));
|
|
|
|
menu_item_subitem_add(
|
|
|
|
power->menu,
|
|
|
|
menu_item_alloc_function("Disable OTG", NULL, power_disable_otg_callback, power));
|
2020-12-18 18:03:28 +00:00
|
|
|
menu_item_subitem_add(
|
|
|
|
power->menu, menu_item_alloc_function("Info", NULL, power_info_callback, power));
|
2020-11-11 06:31:35 +00:00
|
|
|
|
2020-10-29 07:11:16 +00:00
|
|
|
power->usb_icon = assets_icons_get(I_USBConnected_15x8);
|
|
|
|
power->usb_widget = widget_alloc();
|
|
|
|
widget_set_width(power->usb_widget, icon_get_width(power->usb_icon));
|
|
|
|
widget_draw_callback_set(power->usb_widget, power_draw_usb_callback, power);
|
|
|
|
|
2020-12-18 18:03:28 +00:00
|
|
|
power->widget = widget_alloc();
|
|
|
|
widget_draw_callback_set(power->widget, power_draw_callback, power);
|
|
|
|
widget_input_callback_set(power->widget, power_input_callback, power);
|
|
|
|
widget_enabled_set(power->widget, false);
|
|
|
|
|
2020-10-29 07:11:16 +00:00
|
|
|
power->battery_icon = assets_icons_get(I_Battery_19x8);
|
|
|
|
power->battery_widget = widget_alloc();
|
|
|
|
widget_set_width(power->battery_widget, icon_get_width(power->battery_icon));
|
|
|
|
widget_draw_callback_set(power->battery_widget, power_draw_battery_callback, power);
|
|
|
|
|
|
|
|
return power;
|
|
|
|
}
|
|
|
|
|
|
|
|
void power_free(Power* power) {
|
|
|
|
assert(power);
|
|
|
|
free(power);
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:16:34 +00:00
|
|
|
void power_cli_poweroff(string_t args, void* context) {
|
2020-11-17 17:08:31 +00:00
|
|
|
cli_print("Poweroff in 3 seconds");
|
|
|
|
osDelay(3000);
|
2020-11-16 10:16:34 +00:00
|
|
|
api_hal_power_off();
|
|
|
|
}
|
|
|
|
|
2020-11-17 17:08:31 +00:00
|
|
|
void power_cli_reset(string_t args, void* context) {
|
|
|
|
cli_print("NVIC System Reset in 3 seconds");
|
|
|
|
osDelay(3000);
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void power_cli_dfu(string_t args, void* context) {
|
|
|
|
cli_print("NVIC System Reset to DFU mode in 3 seconds");
|
|
|
|
api_hal_boot_set_mode(ApiHalBootModeDFU);
|
|
|
|
osDelay(3000);
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
2020-12-02 10:47:13 +00:00
|
|
|
void power_cli_test(string_t args, void* context) {
|
|
|
|
string_t buffer;
|
|
|
|
string_init(buffer);
|
|
|
|
api_hal_power_dump_state(buffer);
|
|
|
|
cli_print(string_get_cstr(buffer));
|
|
|
|
string_clear(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void power_cli_otg_on(string_t args, void* context) {
|
|
|
|
api_hal_power_enable_otg();
|
|
|
|
}
|
|
|
|
|
|
|
|
void power_cli_otg_off(string_t args, void* context) {
|
|
|
|
api_hal_power_disable_otg();
|
|
|
|
}
|
|
|
|
|
2020-10-29 07:11:16 +00:00
|
|
|
void power_task(void* p) {
|
|
|
|
(void)p;
|
|
|
|
Power* power = power_alloc();
|
|
|
|
|
2020-11-16 17:12:05 +00:00
|
|
|
if(power->cli) {
|
|
|
|
cli_add_command(power->cli, "poweroff", power_cli_poweroff, power);
|
2020-11-17 17:08:31 +00:00
|
|
|
cli_add_command(power->cli, "reset", power_cli_reset, power);
|
|
|
|
cli_add_command(power->cli, "dfu", power_cli_dfu, power);
|
2020-12-02 10:47:13 +00:00
|
|
|
cli_add_command(power->cli, "power_test", power_cli_test, power);
|
|
|
|
cli_add_command(power->cli, "power_otg_on", power_cli_otg_on, power);
|
|
|
|
cli_add_command(power->cli, "power_otg_off", power_cli_otg_off, power);
|
2020-11-16 17:12:05 +00:00
|
|
|
}
|
2020-11-16 10:16:34 +00:00
|
|
|
|
2020-12-14 10:50:32 +00:00
|
|
|
Gui* gui = furi_open("gui");
|
2020-12-18 18:03:28 +00:00
|
|
|
gui_add_widget(gui, power->widget, GuiLayerFullscreen);
|
2020-12-14 10:50:32 +00:00
|
|
|
gui_add_widget(gui, power->usb_widget, GuiLayerStatusBarLeft);
|
|
|
|
gui_add_widget(gui, power->battery_widget, GuiLayerStatusBarRight);
|
2020-10-29 07:11:16 +00:00
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
with_value_mutex(
|
|
|
|
power->menu_vm, (Menu * menu) { menu_item_add(menu, power->menu); });
|
|
|
|
|
2020-10-29 07:11:16 +00:00
|
|
|
if(!furi_create("power", power)) {
|
|
|
|
printf("[power_task] unable to create power record\n");
|
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
api_hal_power_init();
|
|
|
|
|
2020-10-29 07:11:16 +00:00
|
|
|
furiac_ready();
|
|
|
|
|
|
|
|
while(1) {
|
2020-11-11 06:31:35 +00:00
|
|
|
power->charge = api_hal_power_get_pct();
|
2020-12-22 18:16:32 +00:00
|
|
|
power->capacity_remaining = api_hal_power_get_battery_remaining_capacity();
|
|
|
|
power->capacity_full = api_hal_power_get_battery_full_capacity();
|
|
|
|
power->current_charger = api_hal_power_get_battery_current(ApiHalPowerICCharger);
|
|
|
|
power->current_gauge = api_hal_power_get_battery_current(ApiHalPowerICFuelGauge);
|
|
|
|
power->voltage_charger = api_hal_power_get_battery_voltage(ApiHalPowerICCharger);
|
|
|
|
power->voltage_gauge = api_hal_power_get_battery_voltage(ApiHalPowerICFuelGauge);
|
|
|
|
power->temperature_charger = api_hal_power_get_battery_temperature(ApiHalPowerICCharger);
|
|
|
|
power->temperature_gauge = api_hal_power_get_battery_temperature(ApiHalPowerICFuelGauge);
|
2020-12-18 18:03:28 +00:00
|
|
|
widget_update(power->widget);
|
2020-11-11 06:31:35 +00:00
|
|
|
widget_enabled_set(power->usb_widget, api_hal_power_is_charging());
|
2020-10-29 07:11:16 +00:00
|
|
|
osDelay(1000);
|
|
|
|
}
|
|
|
|
}
|