29 lines
1.2 KiB
C++
29 lines
1.2 KiB
C++
#pragma once
|
|||
|
|
// Cell voltage of the XIAO ESP32-C5's battery, read through the board's own divider: GPIO6 (ADC1)
|
||
|
|
// behind two 100 k resistors, switched on by GPIO26 (a load switch that keeps the divider from
|
||
|
|
// draining the cell between readings). The policy (warning, shutdown) is in battery_monitor.hpp.
|
||
|
|
#include <cstdint>
|
||
|
|
|
||
|
|
namespace microbu {
|
||
|
|
|
||
|
|
class Battery {
|
||
|
|
public:
|
||
|
|
/// @brief Configures the enable GPIO and the ADC channel. On failure measure() returns 0.
|
||
|
|
Battery();
|
||
|
|
/// @brief Takes one measurement: enables the divider, samples, disables it again. Blocks for a few ms.
|
||
|
|
/// @return Cell voltage in millivolts, or 0 if the ADC could not be set up or read.
|
||
|
|
std::uint16_t measure();
|
||
|
|
/// @brief Puts the board into deep sleep and wakes it after CONFIG_MICROBU_BATTERY_SLEEP_S seconds.
|
||
|
|
/// Does not return.
|
||
|
|
[[noreturn]] static void deep_sleep();
|
||
|
|
/// @return true if this boot is the end of such a sleep (the only timer wake-up the firmware arms).
|
||
|
|
static bool woke_from_battery_sleep();
|
||
|
|
|
||
|
|
private:
|
||
|
|
void* adc_ = nullptr; // adc_oneshot_unit_handle_t
|
||
|
|
void* cali_ = nullptr; // adc_cali_handle_t, null when the chip has no calibration
|
||
|
|
int channel_ = -1;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace microbu
|