#pragma once12#if AP_PERIPH_BATTERY_BMS_ENABLED34class BatteryBMS {5public:6friend class AP_Periph_FW;78// main update function9void update(void);1011private:1213// configure gpio pins. returns true once configured14bool configured();1516// check and handle button press events17void handle_button_press();1819// request display of battery SOC percentage using LEDs20void request_display_percentage();2122// display battery SOC percentage using LEDs23void display_percentage();2425// get battery SOC percentage (0-100). returns true on success26bool get_percentage(uint8_t &percentage);2728// set LED pattern based on 8-bit bitmask29void set_led_pattern(uint8_t pattern);3031// update the LEDs. called regularly from update()32void update_led_state(void);3334// BMS state machine variables35enum class BmsState : uint8_t {36IDLE = 0,37POWERING_ON,38POWERED_ON,39POWERING_OFF,40POWERED_OFF41};4243// request change in bms state. the only valid inputs are POWERED_ON and POWERING_OFF44// return true on success45bool request_bms_state(BmsState new_state);4647// update bms state. transitions bms_state to req_bms_state48void update_bms_state();4950// configuration variables51bool config_complete; // true once configuration has been completed5253// button handling variables54struct {55bool startup_complete; // true once startup delay has completed56bool pressed_prev; // true if button was pressed during previous iteration57uint32_t pressed_start_ms; // system time that button was first detected as pressed58bool short_press_handled; // true once a short press has been detected and handled59bool long_press_handled; // true once a long pressed has been detected and handled60} button;61static const uint32_t BUTTON_SHORT_PRESS_THRESHOLD_MS = 10; // 10 ms for short press62static const uint32_t BUTTON_LONG_PRESS_THRESHOLD_MS = 1000; // 1 second for long press63static const uint32_t BUTTON_STARTUP_DELAY_MS = 2000; // ignore button presses for first 2 seconds after startup6465// BMS state machine variables66BmsState bms_state, req_bms_state; // current and requested BMS states67uint32_t bms_last_update_ms; // system time of last state machine update68uint8_t bms_transition_counter; // transition counter also used for animation6970// LED display variables71uint32_t led_last_update_ms; // system time of last LED update. used to rate limit LED updates to 20hz72uint32_t led_display_soc_start_ms; // system time that SOC display started. 0 if not displaying SOC73uint8_t led_charging_animation_step; // LED charging animation step74static const uint32_t LED_UPDATE_INTERVAL_MS = 50; // update LEDs at 20hz75static const uint32_t LED_DISPLAY_SOC_DURATION_MS = 1000; // Display SOC percentage for 1 second76static const uint8_t led_gpios[]; // GPIO pins used for BMS LEDs77};7879#endif // AP_PERIPH_BATTERY_BMS_ENABLED80818283