Power management detection of active power source, and approximate battery state. More...
![]() |
Typedefs | |
typedef void(* | hw_power_callback_t) (hw_power_t *power, hw_power_flag_t flags, uint32_t value, void *user_data) |
Power status callback prototype. More... | |
Enumerations | |
enum | hw_power_flag_t { HW_POWER_UNKNOWN = 0, HW_POWER_USB = (1 << 0), HW_POWER_BATTERY = (1 << 1), HW_POWER_RESET = (1 << 2) } |
Power source capability / status flags. More... | |
Functions | |
hw_power_t * | hw_power_init (uint8_t gpio_vsys, uint8_t gpio_vbus, hw_power_callback_t callback, void *user_data) |
Initialize power management. More... | |
bool | hw_power_valid (hw_power_t *power) |
Determine if the power handle is initialized and usable. More... | |
void | hw_power_finalize (hw_power_t *power) |
Finalize and release any resources. More... | |
uint8_t | hw_power_battery_percent (hw_power_t *power) |
Approximate battery state of charge. More... | |
hw_power_flag_t | hw_power_source (hw_power_t *power) |
Current detected power source(s). More... | |
Power management detection of active power source, and approximate battery state.
The power management interface provides battery state of charge reporting (0–100%) when supported, and optional asynchronous callback notification on power changes.
In order to be notified of power changes, the user must provide a callback function when initializing the power management interface, and in the main event loop, call hw_poll().
In order to be notified of reset events, the watchdog must be initialized and the user must call hw_watchdog_reset() to initiate a reset event.
typedef void(* hw_power_callback_t) (hw_power_t *power, hw_power_flag_t flags, uint32_t value, void *user_data) |
Power status callback prototype.
power | Pointer to the associated power handle |
flags | Bitmask of power flags that changed since the last callback. If HW_POWER_BATTERY flag is set, the callback may be invoked on battery level changes. If HW_POWER_RESET is set, the callback was invoked on a reset event. |
value | If flags includes HW_POWER_BATTERY then the value is a battery percentage (1-100) or zero if unknown. If HW_POWER_RESET is set, the value is the reset delay in milliseconds. |
user_data | Optional user data pointer passed to the callback. |
enum hw_power_flag_t |
Power source capability / status flags.
Bitmask describing which power sources are currently present / can be detected. Implementations may return a combination (e.g. USB + BATTERY) if simultaneous presence is detectable; otherwise a single dominant source may be reported. When no information is available, HW_POWER_UNKNOWN is returned.
Enumerator | |
---|---|
HW_POWER_UNKNOWN | Unknown power source. |
HW_POWER_USB | USB (VBUS) power. |
HW_POWER_BATTERY | Battery power. |
HW_POWER_RESET | Hardware will reset. |
Definition at line 44 of file power.h.
uint8_t hw_power_battery_percent | ( | hw_power_t * | power | ) |
Approximate battery state of charge.
power | Power handle |
If unsupported or indeterminable, returns 100
void hw_power_finalize | ( | hw_power_t * | power | ) |
Finalize and release any resources.
power | Power handle |
Safe to call on an already finalized or never‑initialized handle; in that case it is a no‑op. After finalization, hw_power_valid() returns false.
hw_power_t* hw_power_init | ( | uint8_t | gpio_vsys, |
uint8_t | gpio_vbus, | ||
hw_power_callback_t | callback, | ||
void * | user_data | ||
) |
Initialize power management.
gpio_vsys | GPIO (or 0xFF for default) used for VSYS / battery sense (platform specific) |
gpio_vbus | GPIO (or 0xFF for default) used for VBUS / USB present detect |
callback | Optional callback for asynchronous updates (may be NULL) |
GPIO arguments are platform dependent; pass 0xFF to use default signals, or if unavailable. The returned handle is valid if hw_power_valid() subsequently reports true. Supplying a callback enables event driven notification when supported.
hw_power_flag_t hw_power_source | ( | hw_power_t * | power | ) |
Current detected power source(s).
power | Power handle |
bool hw_power_valid | ( | hw_power_t * | power | ) |
Determine if the power handle is initialized and usable.
power | Power handle |