unleashed-firmware/applications/services/power/power_service/power.c
Georgii Surkov f4122a924a
[FL-3841] FuriEventLoop Pt.2 (#3703)
* Abstract primitive type from main logic in FuriEventLoop
* Remove message_queue_i.h
* Add stream buffer support for event loop
* Add semaphore support for event loop
* Add temporary unit test workaround
* Make the linter happy
* Add mutex support for event loop
* Implement event subscription and unsubscription while the event loop is running
* Implement edge events
* Fix leftover logical errors
* Add event loop timer example application
* Implement flag-based edge trigger and one-shot mode
* Add event loop mutex example application
* Only notify the event loop if stream buffer is at or above its trigger level
* Reformat comments
* Add event loop stream buffer example application
* Add event loop multiple elements example application
* Improve event loop flag names
* Remove redundant signal handler as it is already handled by the event loop
* Refactor Power service, improve ViewHolder
* Use ViewHolder instead of ViewDispatcher in About app
* Enable ViewDispatcher queue on construction, deprecate view_dispatcher_enable_queue()
* Remove all invocations of view_dispatcher_enable_queue()
* Remove app-scened-template
* Remove missing library from target.json
* Port Accessor app to ViewHolder
* Make the linter happy
* Add example_view_holder application, update ViewHolder docs
* Add example_view_dispatcher application, update ViewDispatcher docs
* Replace FuriSemaphore with FuriApiLock, remove workaround delay
* Fix logical error
* Fix another logical error
* Use the sources directive to speed up compilation
* Use constant define macro
* Improve FuriEventLoop documentation
* Improve FuriEventLoop documentation once more
* Bump API Version
* Gui: remove redundant checks from ViewDispatcher
* Gui: remove dead ifs from ViewDispatcher

Co-authored-by: Silent <CookiePLMonster@users.noreply.github.com>
Co-authored-by: hedger <hedger@users.noreply.github.com>
Co-authored-by: あく <alleteam@gmail.com>
2024-08-07 12:49:41 +09:00

303 lines
11 KiB
C

#include "power_i.h"
#include <furi.h>
#include <furi_hal.h>
#include <update_util/update_operation.h>
#include <notification/notification_messages.h>
#define TAG "Power"
#define POWER_OFF_TIMEOUT_S (90U)
#define POWER_POLL_PERIOD_MS (1000UL)
#define POWER_VBUS_LOW_THRESHOLD (4.0f)
#define POWER_HEALTH_LOW_THRESHOLD (70U)
static void power_draw_battery_callback(Canvas* canvas, void* context) {
furi_assert(context);
Power* power = context;
canvas_draw_icon(canvas, 0, 0, &I_Battery_26x8);
if(power->info.gauge_is_ok) {
canvas_draw_box(canvas, 2, 2, (power->info.charge + 4) / 5, 4);
if(power->info.voltage_battery_charge_limit < 4.2f) {
// Battery charge voltage limit is modified, indicate with cross pattern
canvas_invert_color(canvas);
uint8_t battery_bar_width = (power->info.charge + 4) / 5;
bool cross_odd = false;
// Start 1 further in from the battery bar's x position
for(uint8_t x = 3; x <= battery_bar_width; x++) {
// Cross pattern is from the center of the battery bar
// y = 2 + 1 (inset) + 1 (for every other)
canvas_draw_dot(canvas, x, 3 + (uint8_t)cross_odd);
cross_odd = !cross_odd;
}
canvas_invert_color(canvas);
}
if(power->state == PowerStateCharging) {
canvas_set_bitmap_mode(canvas, 1);
canvas_set_color(canvas, ColorWhite);
// -1 used here to overflow u8 number and render is outside of the area
canvas_draw_icon(canvas, 8, -1, &I_Charging_lightning_mask_9x10);
canvas_set_color(canvas, ColorBlack);
canvas_draw_icon(canvas, 8, -1, &I_Charging_lightning_9x10);
canvas_set_bitmap_mode(canvas, 0);
}
} else {
canvas_draw_box(canvas, 8, 3, 8, 2);
}
}
static ViewPort* power_battery_view_port_alloc(Power* power) {
ViewPort* battery_view_port = view_port_alloc();
view_port_set_width(battery_view_port, icon_get_width(&I_Battery_26x8));
view_port_draw_callback_set(battery_view_port, power_draw_battery_callback, power);
return battery_view_port;
}
static bool power_update_info(Power* power) {
const PowerInfo info = {
.is_charging = furi_hal_power_is_charging(),
.gauge_is_ok = furi_hal_power_gauge_is_ok(),
.is_shutdown_requested = furi_hal_power_is_shutdown_requested(),
.charge = furi_hal_power_get_pct(),
.health = furi_hal_power_get_bat_health_pct(),
.capacity_remaining = furi_hal_power_get_battery_remaining_capacity(),
.capacity_full = furi_hal_power_get_battery_full_capacity(),
.current_charger = furi_hal_power_get_battery_current(FuriHalPowerICCharger),
.current_gauge = furi_hal_power_get_battery_current(FuriHalPowerICFuelGauge),
.voltage_battery_charge_limit = furi_hal_power_get_battery_charge_voltage_limit(),
.voltage_charger = furi_hal_power_get_battery_voltage(FuriHalPowerICCharger),
.voltage_gauge = furi_hal_power_get_battery_voltage(FuriHalPowerICFuelGauge),
.voltage_vbus = furi_hal_power_get_usb_voltage(),
.temperature_charger = furi_hal_power_get_battery_temperature(FuriHalPowerICCharger),
.temperature_gauge = furi_hal_power_get_battery_temperature(FuriHalPowerICFuelGauge),
};
const bool need_refresh = (power->info.charge != info.charge) ||
(power->info.is_charging != info.is_charging);
power->info = info;
return need_refresh;
}
static void power_check_charging_state(Power* power) {
NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
if(furi_hal_power_is_charging()) {
if((power->info.charge == 100) || (furi_hal_power_is_charging_done())) {
if(power->state != PowerStateCharged) {
notification_internal_message(notification, &sequence_charged);
power->state = PowerStateCharged;
power->event.type = PowerEventTypeFullyCharged;
furi_pubsub_publish(power->event_pubsub, &power->event);
}
} else if(power->state != PowerStateCharging) {
notification_internal_message(notification, &sequence_charging);
power->state = PowerStateCharging;
power->event.type = PowerEventTypeStartCharging;
furi_pubsub_publish(power->event_pubsub, &power->event);
}
} else if(power->state != PowerStateNotCharging) {
notification_internal_message(notification, &sequence_not_charging);
power->state = PowerStateNotCharging;
power->event.type = PowerEventTypeStopCharging;
furi_pubsub_publish(power->event_pubsub, &power->event);
}
furi_record_close(RECORD_NOTIFICATION);
}
static void power_check_low_battery(Power* power) {
if(!power->info.gauge_is_ok) {
return;
}
// Check battery charge and vbus voltage
if((power->info.is_shutdown_requested) &&
(power->info.voltage_vbus < POWER_VBUS_LOW_THRESHOLD) && power->show_battery_low_warning) {
if(!power->battery_low) {
view_holder_send_to_front(power->view_holder);
view_holder_set_view(power->view_holder, power_off_get_view(power->view_power_off));
}
power->battery_low = true;
} else {
if(power->battery_low) {
// view_dispatcher_switch_to_view(power->view_dispatcher, VIEW_NONE);
view_holder_set_view(power->view_holder, NULL);
power->power_off_timeout = POWER_OFF_TIMEOUT_S;
}
power->battery_low = false;
}
// If battery low, update view and switch off power after timeout
if(power->battery_low) {
PowerOffResponse response = power_off_get_response(power->view_power_off);
if(response == PowerOffResponseDefault) {
if(power->power_off_timeout) {
power_off_set_time_left(power->view_power_off, power->power_off_timeout--);
} else {
power_off(power);
}
} else if(response == PowerOffResponseOk) {
power_off(power);
} else if(response == PowerOffResponseHide) {
view_holder_set_view(power->view_holder, NULL);
if(power->power_off_timeout) {
power_off_set_time_left(power->view_power_off, power->power_off_timeout--);
} else {
power_off(power);
}
} else if(response == PowerOffResponseCancel) {
view_holder_set_view(power->view_holder, NULL);
}
}
}
static void power_check_battery_level_change(Power* power) {
if(power->battery_level != power->info.charge) {
power->battery_level = power->info.charge;
power->event.type = PowerEventTypeBatteryLevelChanged;
power->event.data.battery_level = power->battery_level;
furi_pubsub_publish(power->event_pubsub, &power->event);
}
}
static void power_handle_shutdown(Power* power) {
furi_hal_power_off();
// Notify user if USB is plugged
view_holder_send_to_front(power->view_holder);
view_holder_set_view(
power->view_holder, power_unplug_usb_get_view(power->view_power_unplug_usb));
furi_delay_ms(100);
furi_halt("Disconnect USB for safe shutdown");
}
static void power_handle_reboot(PowerBootMode mode) {
if(mode == PowerBootModeNormal) {
update_operation_disarm();
} else if(mode == PowerBootModeDfu) {
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeDfu);
} else if(mode == PowerBootModeUpdateStart) {
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePreUpdate);
} else {
furi_crash();
}
furi_hal_power_reset();
}
static bool power_message_callback(FuriEventLoopObject* object, void* context) {
furi_assert(context);
Power* power = context;
furi_assert(object == power->message_queue);
PowerMessage msg;
furi_check(furi_message_queue_get(power->message_queue, &msg, 0) == FuriStatusOk);
switch(msg.type) {
case PowerMessageTypeShutdown:
power_handle_shutdown(power);
break;
case PowerMessageTypeReboot:
power_handle_reboot(msg.boot_mode);
break;
case PowerMessageTypeGetInfo:
*msg.power_info = power->info;
break;
case PowerMessageTypeIsBatteryHealthy:
*msg.bool_param = power->info.health > POWER_HEALTH_LOW_THRESHOLD;
break;
case PowerMessageTypeShowBatteryLowWarning:
power->show_battery_low_warning = *msg.bool_param;
break;
default:
furi_crash();
}
if(msg.lock) {
api_lock_unlock(msg.lock);
}
return true;
}
static void power_tick_callback(void* context) {
furi_assert(context);
Power* power = context;
// Update data from gauge and charger
const bool need_refresh = power_update_info(power);
// Check low battery level
power_check_low_battery(power);
// Check and notify about charging state
power_check_charging_state(power);
// Check and notify about battery level change
power_check_battery_level_change(power);
// Update battery view port
if(need_refresh) {
view_port_update(power->battery_view_port);
}
// Check OTG status and disable it in case of fault
if(furi_hal_power_is_otg_enabled()) {
furi_hal_power_check_otg_status();
}
}
static Power* power_alloc(void) {
Power* power = malloc(sizeof(Power));
// Pubsub
power->event_pubsub = furi_pubsub_alloc();
// State initialization
power->power_off_timeout = POWER_OFF_TIMEOUT_S;
power->show_battery_low_warning = true;
// Gui
Gui* gui = furi_record_open(RECORD_GUI);
power->view_holder = view_holder_alloc();
power->view_power_off = power_off_alloc();
power->view_power_unplug_usb = power_unplug_usb_alloc();
view_holder_attach_to_gui(power->view_holder, gui);
// Battery view port
power->battery_view_port = power_battery_view_port_alloc(power);
gui_add_view_port(gui, power->battery_view_port, GuiLayerStatusBarRight);
// Event loop
power->event_loop = furi_event_loop_alloc();
power->message_queue = furi_message_queue_alloc(4, sizeof(PowerMessage));
furi_event_loop_subscribe_message_queue(
power->event_loop,
power->message_queue,
FuriEventLoopEventIn,
power_message_callback,
power);
furi_event_loop_tick_set(power->event_loop, 1000, power_tick_callback, power);
return power;
}
int32_t power_srv(void* p) {
UNUSED(p);
if(furi_hal_rtc_get_boot_mode() != FuriHalRtcBootModeNormal) {
FURI_LOG_W(TAG, "Skipping start in special boot mode");
furi_thread_suspend(furi_thread_get_current_id());
return 0;
}
Power* power = power_alloc();
power_update_info(power);
furi_record_create(RECORD_POWER, power);
furi_event_loop_run(power->event_loop);
return 0;
}