forked from xiaozhi/xiaozhi-esp32
IoT电池状态读取
This commit is contained in:
@@ -54,7 +54,13 @@ private:
|
|||||||
seconds = 0;
|
seconds = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!axp2101_->IsDischarging()) {
|
if (axp2101_->IsDischarging()) {
|
||||||
|
// 电量低于 10% 时,显示低电量警告
|
||||||
|
if (!show_low_power_warning_ && axp2101_->GetBatteryLevel() <= 10) {
|
||||||
|
app.Alert(Lang::Strings::WARNING, Lang::Strings::BATTERY_LOW, "sad", Lang::Sounds::P3_VIBRATION);
|
||||||
|
show_low_power_warning_ = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
seconds = 0;
|
seconds = 0;
|
||||||
if (show_low_power_warning_) {
|
if (show_low_power_warning_) {
|
||||||
app.DismissAlert();
|
app.DismissAlert();
|
||||||
@@ -62,11 +68,6 @@ private:
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 电量低于 10% 时,显示低电量警告
|
|
||||||
if (axp2101_->GetBatteryLevel() <= 10 && !show_low_power_warning_) {
|
|
||||||
app.Alert(Lang::Strings::WARNING, Lang::Strings::BATTERY_LOW, "sad", Lang::Sounds::P3_VIBRATION);
|
|
||||||
show_low_power_warning_ = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
seconds++;
|
seconds++;
|
||||||
if (seconds >= seconds_to_shutdown) {
|
if (seconds >= seconds_to_shutdown) {
|
||||||
@@ -163,6 +164,7 @@ private:
|
|||||||
void InitializeIot() {
|
void InitializeIot() {
|
||||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||||
|
thing_manager.AddThing(iot::CreateThing("Battery"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -207,6 +207,7 @@ private:
|
|||||||
void InitializeIot() {
|
void InitializeIot() {
|
||||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||||
|
thing_manager.AddThing(iot::CreateThing("Battery"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
34
main/iot/things/battery.cc
Normal file
34
main/iot/things/battery.cc
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include "iot/thing.h"
|
||||||
|
#include "board.h"
|
||||||
|
|
||||||
|
#include <esp_log.h>
|
||||||
|
|
||||||
|
#define TAG "Battery"
|
||||||
|
|
||||||
|
namespace iot {
|
||||||
|
|
||||||
|
// 这里仅定义 Battery 的属性和方法,不包含具体的实现
|
||||||
|
class Battery : public Thing {
|
||||||
|
private:
|
||||||
|
int level_ = 0;
|
||||||
|
bool charging_ = false;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Battery() : Thing("Battery", "电池管理") {
|
||||||
|
// 定义设备的属性
|
||||||
|
properties_.AddNumberProperty("level", "当前电量百分比(0-100)", [this]() -> int {
|
||||||
|
auto& board = Board::GetInstance();
|
||||||
|
if (board.GetBatteryLevel(level_, charging_)) {
|
||||||
|
return level_;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
properties_.AddBooleanProperty("charging", "是否充电中", [this]() -> int {
|
||||||
|
return charging_;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace iot
|
||||||
|
|
||||||
|
DECLARE_THING(Battery);
|
||||||
Reference in New Issue
Block a user