forked from xiaozhi/xiaozhi-esp32
116 lines
2.7 KiB
C++
116 lines
2.7 KiB
C++
#include "led.h"
|
|
#include "board.h"
|
|
|
|
#include <cstring>
|
|
#include <esp_log.h>
|
|
|
|
#define TAG "Led"
|
|
|
|
Led::Led(gpio_num_t gpio) {
|
|
if (gpio == GPIO_NUM_NC) {
|
|
ESP_LOGI(TAG, "Builtin LED not connected");
|
|
return;
|
|
}
|
|
|
|
led_strip_config_t strip_config = {};
|
|
strip_config.strip_gpio_num = gpio;
|
|
strip_config.max_leds = 1;
|
|
strip_config.led_pixel_format = LED_PIXEL_FORMAT_GRB;
|
|
strip_config.led_model = LED_MODEL_WS2812;
|
|
|
|
led_strip_rmt_config_t rmt_config = {};
|
|
rmt_config.resolution_hz = 10 * 1000 * 1000; // 10MHz
|
|
|
|
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip_));
|
|
led_strip_clear(led_strip_);
|
|
|
|
SetGrey();
|
|
|
|
esp_timer_create_args_t blink_timer_args = {
|
|
.callback = [](void *arg) {
|
|
auto led = static_cast<Led*>(arg);
|
|
led->OnBlinkTimer();
|
|
},
|
|
.arg = this,
|
|
.dispatch_method = ESP_TIMER_TASK,
|
|
.name = "Blink Timer",
|
|
.skip_unhandled_events = false,
|
|
};
|
|
ESP_ERROR_CHECK(esp_timer_create(&blink_timer_args, &blink_timer_));
|
|
}
|
|
|
|
Led::~Led() {
|
|
esp_timer_stop(blink_timer_);
|
|
if (led_strip_ != nullptr) {
|
|
led_strip_del(led_strip_);
|
|
}
|
|
}
|
|
|
|
void Led::SetColor(uint8_t r, uint8_t g, uint8_t b) {
|
|
r_ = r;
|
|
g_ = g;
|
|
b_ = b;
|
|
}
|
|
|
|
void Led::TurnOn() {
|
|
if (led_strip_ == nullptr) {
|
|
return;
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
esp_timer_stop(blink_timer_);
|
|
led_strip_set_pixel(led_strip_, 0, r_, g_, b_);
|
|
led_strip_refresh(led_strip_);
|
|
}
|
|
|
|
void Led::TurnOff() {
|
|
if (led_strip_ == nullptr) {
|
|
return;
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
esp_timer_stop(blink_timer_);
|
|
led_strip_clear(led_strip_);
|
|
}
|
|
|
|
void Led::BlinkOnce() {
|
|
Blink(1, 100);
|
|
}
|
|
|
|
void Led::Blink(int times, int interval_ms) {
|
|
StartBlinkTask(times, interval_ms);
|
|
}
|
|
|
|
void Led::StartContinuousBlink(int interval_ms) {
|
|
StartBlinkTask(BLINK_INFINITE, interval_ms);
|
|
}
|
|
|
|
void Led::StartBlinkTask(int times, int interval_ms) {
|
|
if (led_strip_ == nullptr) {
|
|
return;
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
esp_timer_stop(blink_timer_);
|
|
|
|
led_strip_clear(led_strip_);
|
|
blink_counter_ = times * 2;
|
|
blink_interval_ms_ = interval_ms;
|
|
esp_timer_start_periodic(blink_timer_, interval_ms * 1000);
|
|
}
|
|
|
|
void Led::OnBlinkTimer() {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
blink_counter_--;
|
|
if (blink_counter_ & 1) {
|
|
led_strip_set_pixel(led_strip_, 0, r_, g_, b_);
|
|
led_strip_refresh(led_strip_);
|
|
} else {
|
|
led_strip_clear(led_strip_);
|
|
|
|
if (blink_counter_ == 0) {
|
|
esp_timer_stop(blink_timer_);
|
|
}
|
|
}
|
|
}
|