57 lines
1.5 KiB
C
57 lines
1.5 KiB
C
#ifndef BATTERY_H
|
|
#define BATTERY_H
|
|
|
|
#include <stdint.h>
|
|
#include "esp_err.h"
|
|
|
|
// Battery monitoring configuration
|
|
#define BATTERY_ADC_CHANNEL ADC_CHANNEL_6 // GPIO34 (ADC1_CH6)
|
|
#define BATTERY_ADC_ATTEN ADC_ATTEN_DB_12 // Full range ~3.9V (0-3300mV with attenuation)
|
|
#define BATTERY_ADC_WIDTH ADC_WIDTH_BIT_12 // 12-bit resolution (0-4095)
|
|
|
|
// Battery voltage calculation constants
|
|
// Adjust these based on your voltage divider circuit
|
|
#define BATTERY_VOLTAGE_DIVIDER_RATIO 2.0f // Example: R1=R2, adjust for your circuit
|
|
#define BATTERY_SAMPLES 16 // Number of samples to average
|
|
|
|
// Battery percentage thresholds (in millivolts at battery)
|
|
#define BATTERY_VOLTAGE_MAX 4200 // Fully charged Li-Ion
|
|
#define BATTERY_VOLTAGE_MIN 3000 // Empty Li-Ion (cutoff)
|
|
|
|
/**
|
|
* @brief Initialize battery monitoring ADC
|
|
*
|
|
* @return esp_err_t ESP_OK on success
|
|
*/
|
|
esp_err_t battery_init(void);
|
|
|
|
/**
|
|
* @brief Read raw ADC value from battery pin
|
|
*
|
|
* @return int Raw ADC value (0-4095)
|
|
*/
|
|
int battery_read_raw(void);
|
|
|
|
/**
|
|
* @brief Read battery voltage in millivolts (averaged)
|
|
*
|
|
* @return int Battery voltage in mV
|
|
*/
|
|
int battery_read_voltage_mv(void);
|
|
|
|
/**
|
|
* @brief Get battery percentage (0-100)
|
|
*
|
|
* @return int Battery percentage
|
|
*/
|
|
int battery_get_percentage(void);
|
|
|
|
/**
|
|
* @brief Start battery monitoring task
|
|
*
|
|
* This task periodically reads battery voltage and updates system state
|
|
*/
|
|
void battery_start_monitoring_task(void);
|
|
|
|
#endif // BATTERY_H
|