Add initial board, HAL, and executor modules
This commit is contained in:
1 parent
3b9e129c88
commit
0b2eebe76d
82 files changed
+20611
No files matched your search
@@ -0,0 +1,167 @@
|
||||
#include "adc_battery_monitor.h"
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "AdcBatteryMonitor" ///< 日志标签,用于ESP-IDF日志系统
|
||||
|
||||
AdcBatteryMonitor::AdcBatteryMonitor(BoardSweekar& board, adc_unit_t adc_unit, adc_channel_t adc_channel, float upper_resistor, float lower_resistor)
|
||||
: board_(board) {
|
||||
|
||||
// Initialize ADC battery estimation
|
||||
adc_battery_estimation_t adc_cfg = {
|
||||
.internal = {
|
||||
.adc_unit = adc_unit,
|
||||
.adc_bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
.adc_atten = ADC_ATTEN_DB_12,
|
||||
},
|
||||
.adc_channel = adc_channel,
|
||||
.upper_resistor = upper_resistor,
|
||||
.lower_resistor = lower_resistor
|
||||
};
|
||||
adc_cfg.charging_detect_cb = [](void *user_data) -> bool {
|
||||
AdcBatteryMonitor *self = (AdcBatteryMonitor *)user_data;
|
||||
return self->IsCharging();
|
||||
};
|
||||
adc_cfg.charging_detect_user_data = this;
|
||||
adc_battery_estimation_handle_ = adc_battery_estimation_create(&adc_cfg);
|
||||
|
||||
// Initialize timer
|
||||
esp_timer_create_args_t timer_cfg = {
|
||||
.callback = [](void *arg) {
|
||||
AdcBatteryMonitor *self = (AdcBatteryMonitor *)arg;
|
||||
self->CheckBatteryStatus();
|
||||
},
|
||||
.arg = this,
|
||||
.name = "adc_battery_monitor",
|
||||
.skip_unhandled_events = false,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_cfg, &timer_handle_));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 1000000));
|
||||
}
|
||||
|
||||
// 注册ADC电池回调函数
|
||||
void AdcBatteryMonitor::AdcBatteryMonitor_Init(void) {
|
||||
ESP_LOGI(TAG, "注册ADC电池回调函数");
|
||||
// 设置默认的电池状态回调函数
|
||||
OnNotCharging([]() {
|
||||
ESP_LOGI(TAG, "电池未充电状态回调 - 默认处理");
|
||||
});
|
||||
|
||||
OnFull([]() {
|
||||
ESP_LOGI(TAG, "电池充满电状态回调 - 默认处理");
|
||||
});
|
||||
|
||||
OnCharging([]() {
|
||||
ESP_LOGI(TAG, "电池充电中状态回调 - 默认处理");
|
||||
});
|
||||
}
|
||||
|
||||
AdcBatteryMonitor::~AdcBatteryMonitor() {
|
||||
if (adc_battery_estimation_handle_) {
|
||||
ESP_ERROR_CHECK(adc_battery_estimation_destroy(adc_battery_estimation_handle_));
|
||||
}
|
||||
|
||||
if (timer_handle_) {
|
||||
esp_timer_stop(timer_handle_);
|
||||
esp_timer_delete(timer_handle_);
|
||||
}
|
||||
}
|
||||
|
||||
bool AdcBatteryMonitor::IsCharging() {
|
||||
// 通过IO扩展板读取充电检测引脚状态
|
||||
// BATTERY_CHRG_PIN连接到P0_5,对应INPUT_PORT0
|
||||
uint8_t port0_data = 0;
|
||||
esp_err_t err = board_.io_expansion_.ReadInputPort0(&port0_data);
|
||||
|
||||
// 如果读取失败,返回false
|
||||
if (err != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查BATTERY_CHRG_PIN位状态
|
||||
// 当引脚为低电平时表示正在充电
|
||||
return (port0_data & (1 << BATTERY_CHRG_PIN)) == 0;
|
||||
}
|
||||
|
||||
bool AdcBatteryMonitor::IsDischarging() {
|
||||
return !IsCharging();
|
||||
}
|
||||
|
||||
uint8_t AdcBatteryMonitor::GetBatteryLevel() {
|
||||
// 如果句柄无效,返回默认值
|
||||
if (adc_battery_estimation_handle_ == nullptr) {
|
||||
printf("adc_battery_estimation_handle_ 为空!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
float capacity = 0;
|
||||
esp_err_t err = adc_battery_estimation_get_capacity(adc_battery_estimation_handle_, &capacity);
|
||||
if (err != ESP_OK) {
|
||||
printf("返回值出错!\n");
|
||||
return 0; // 出错时返回默认值
|
||||
}
|
||||
return (uint8_t)capacity;
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::OnChargingStatusChanged(std::function<void(bool)> callback) {
|
||||
on_charging_status_changed_ = callback;
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::CheckBatteryStatus() {
|
||||
bool new_charging_status = IsCharging();
|
||||
if (new_charging_status != is_charging_) {
|
||||
is_charging_ = new_charging_status;
|
||||
if (on_charging_status_changed_) {
|
||||
on_charging_status_changed_(is_charging_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 三个电池状态回调函数的实现
|
||||
void AdcBatteryMonitor::TriggerNotChrgCallback() {
|
||||
if (on_not_charging_callback_) {
|
||||
on_not_charging_callback_();
|
||||
}
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::TriggerFullCallback() {
|
||||
if (on_full_callback_) {
|
||||
on_full_callback_();
|
||||
}
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::TriggerChrgCallback() {
|
||||
if (on_charging_callback_) {
|
||||
on_charging_callback_();
|
||||
}
|
||||
}
|
||||
|
||||
// 注册电池状态回调函数的实现
|
||||
void AdcBatteryMonitor::OnNotCharging(std::function<void()> callback) {
|
||||
on_not_charging_callback_ = callback;
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::OnFull(std::function<void()> callback) {
|
||||
on_full_callback_ = callback;
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::OnCharging(std::function<void()> callback) {
|
||||
on_charging_callback_ = callback;
|
||||
}
|
||||
|
||||
//CHRG引脚充电前为高电平 充电中为低电平 充满电为高电平
|
||||
//FULL未充满为高电平 充满电为低电平
|
||||
bool AdcBatteryMonitor::IsFull() {
|
||||
// 通过IO扩展板读取电池充满检测引脚状态
|
||||
// BATTERY_FULL_PIN连接到P1_3,对应INPUT_PORT1
|
||||
uint8_t port1_data = 0;
|
||||
esp_err_t err = board_.io_expansion_.ReadInputPort1(&port1_data);
|
||||
|
||||
// 如果读取失败,返回false
|
||||
if (err != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查BATTERY_FULL_PIN位状态
|
||||
// 当引脚为低电平时表示电池已充满
|
||||
return (port1_data & (1 << BATTERY_FULL_PIN)) == 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef ADC_BATTERY_MONITOR_H
|
||||
#define ADC_BATTERY_MONITOR_H
|
||||
|
||||
#include <functional>
|
||||
#include <driver/gpio.h>
|
||||
#include <adc_battery_estimation.h>
|
||||
#include <esp_timer.h>
|
||||
#include "02_physical/boards/board_sweekar.h"
|
||||
|
||||
class AdcBatteryMonitor {
|
||||
public:
|
||||
AdcBatteryMonitor(BoardSweekar& board, adc_unit_t adc_unit, adc_channel_t adc_channel, float upper_resistor, float lower_resistor);
|
||||
~AdcBatteryMonitor();
|
||||
|
||||
bool IsCharging();
|
||||
bool IsDischarging();
|
||||
bool IsFull(); // 电池充满检测函数
|
||||
uint8_t GetBatteryLevel();
|
||||
|
||||
// 注册电池状态回调函数
|
||||
void AdcBatteryMonitor_Init(void);
|
||||
// 三个电池状态回调函数
|
||||
void TriggerNotChrgCallback(); // 触发未充电回调
|
||||
void TriggerFullCallback(); // 触发充满电回调
|
||||
void TriggerChrgCallback(); // 触发充电中回调
|
||||
|
||||
// 注册电池状态回调函数
|
||||
void OnNotCharging(std::function<void()> callback); // 注册未充电回调
|
||||
void OnFull(std::function<void()> callback); // 注册充满电回调
|
||||
void OnCharging(std::function<void()> callback); // 注册充电中回调
|
||||
|
||||
void OnChargingStatusChanged(std::function<void(bool)> callback);
|
||||
|
||||
private:
|
||||
BoardSweekar& board_; // 添加对BoardSweekar的引用
|
||||
adc_battery_estimation_handle_t adc_battery_estimation_handle_ = nullptr;
|
||||
esp_timer_handle_t timer_handle_ = nullptr;
|
||||
bool is_charging_ = false;
|
||||
std::function<void(bool)> on_charging_status_changed_;
|
||||
std::function<void()> on_not_charging_callback_; // 未充电回调函数
|
||||
std::function<void()> on_full_callback_; // 充满电回调函数
|
||||
std::function<void()> on_charging_callback_; // 充电中回调函数
|
||||
|
||||
void CheckBatteryStatus();
|
||||
};
|
||||
|
||||
#endif // ADC_BATTERY_MONITOR_H
|
||||
@@ -0,0 +1,16 @@
|
||||
# main/05_executors/adc_battery_monitor/module.cmake
|
||||
|
||||
|
||||
# 判断 menuconfig 中的开关
|
||||
if(NOT CONFIG_ENABLE_EXEC_ADC_BATTERY_MONITOR AND NOT EARLY_EXPANSION)
|
||||
message(STATUS " [Skip] adc_battery_monitor module disabled by menuconfig.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
list(APPEND MODULE_SRCS
|
||||
"05_executors/adc_battery_monitor/adc_battery_monitor.cc"
|
||||
)
|
||||
|
||||
list(APPEND MODULE_INCLUDE_DIRS
|
||||
"05_executors/adc_battery_monitor"
|
||||
)
|
||||
Reference in new issue
Block a user