Rdzleo c63b35a0e7
Some checks failed
Build Boards / Determine variants to build (push) Successful in 6m26s
Build Boards / Build ${{ matrix.name }} (push) Failing after 6m43s
1、第一次提交项目,项目初始化;
2、修改了RP2040的代码,使其在没有安装摄像头的情况下也可以左右转动眼球和左右转动身体;
3、增加了一些中文注释的说明;
2026-04-09 14:22:24 +08:00

58 lines
1.4 KiB
C++

#pragma once
#include <mutex>
#include <functional>
#include <driver/gpio.h>
#include <driver/rtc_io.h>
#include <esp_log.h>
#include "config.h"
enum class PowerState {
ACTIVE,
LIGHT_SLEEP,
DEEP_SLEEP,
SHUTDOWN
};
class PowerController {
public:
static PowerController& Instance() {
static PowerController instance;
return instance;
}
void SetState(PowerState newState) {
std::lock_guard<std::mutex> lock(mutex_);
if (currentState_ != newState) {
ESP_LOGI("PowerCtrl", "State change: %d -> %d",
static_cast<int>(currentState_),
static_cast<int>(newState));
currentState_ = newState;
if (stateChangeCallback_) {
stateChangeCallback_(newState);
}
}
}
PowerState GetState() const {
std::lock_guard<std::mutex> lock(mutex_);
return currentState_;
}
void OnStateChange(std::function<void(PowerState)> callback) {
stateChangeCallback_ = callback;
}
private:
PowerController(){
rtc_gpio_init(PWR_EN_GPIO);
rtc_gpio_set_direction(PWR_EN_GPIO, RTC_GPIO_MODE_OUTPUT_ONLY);
rtc_gpio_set_level(PWR_EN_GPIO, 1);
}
~PowerController() = default;
PowerState currentState_ = PowerState::ACTIVE;
std::function<void(PowerState)> stateChangeCallback_;
mutable std::mutex mutex_;
};