Files
xiaozhi-esp32/main/boards/common/led.cc

116 lines
2.7 KiB
C++
Raw Normal View History

2024-11-06 06:18:56 +08:00
#include "led.h"
2024-11-05 20:15:00 +08:00
#include "board.h"
2024-10-29 00:22:29 +08:00
#include <cstring>
#include <esp_log.h>
2024-11-06 06:18:56 +08:00
#define TAG "Led"
2024-10-29 00:22:29 +08:00
2024-11-06 06:18:56 +08:00
Led::Led(gpio_num_t gpio) {
if (gpio == GPIO_NUM_NC) {
2024-10-29 00:22:29 +08:00
ESP_LOGI(TAG, "Builtin LED not connected");
return;
}
2024-11-06 06:18:56 +08:00
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_);
2024-10-29 00:22:29 +08:00
SetGrey();
2024-11-29 11:06:05 +08:00
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_));
2024-10-29 00:22:29 +08:00
}
2024-11-06 06:18:56 +08:00
Led::~Led() {
2024-11-29 11:06:05 +08:00
esp_timer_stop(blink_timer_);
2024-10-29 00:22:29 +08:00
if (led_strip_ != nullptr) {
led_strip_del(led_strip_);
}
}
2024-11-06 06:18:56 +08:00
void Led::SetColor(uint8_t r, uint8_t g, uint8_t b) {
2024-10-29 00:22:29 +08:00
r_ = r;
g_ = g;
b_ = b;
}
2024-11-06 06:18:56 +08:00
void Led::TurnOn() {
2024-10-29 00:22:29 +08:00
if (led_strip_ == nullptr) {
return;
}
2024-11-29 11:06:05 +08:00
std::lock_guard<std::mutex> lock(mutex_);
esp_timer_stop(blink_timer_);
2024-10-29 00:22:29 +08:00
led_strip_set_pixel(led_strip_, 0, r_, g_, b_);
led_strip_refresh(led_strip_);
}
2024-11-06 06:18:56 +08:00
void Led::TurnOff() {
2024-10-29 00:22:29 +08:00
if (led_strip_ == nullptr) {
return;
}
2024-11-29 11:06:05 +08:00
std::lock_guard<std::mutex> lock(mutex_);
esp_timer_stop(blink_timer_);
2024-10-29 00:22:29 +08:00
led_strip_clear(led_strip_);
}
2024-11-06 06:18:56 +08:00
void Led::BlinkOnce() {
2024-10-29 00:22:29 +08:00
Blink(1, 100);
}
2024-11-06 06:18:56 +08:00
void Led::Blink(int times, int interval_ms) {
2024-10-29 00:22:29 +08:00
StartBlinkTask(times, interval_ms);
}
2024-11-06 06:18:56 +08:00
void Led::StartContinuousBlink(int interval_ms) {
2024-10-29 00:22:29 +08:00
StartBlinkTask(BLINK_INFINITE, interval_ms);
}
2024-11-06 06:18:56 +08:00
void Led::StartBlinkTask(int times, int interval_ms) {
2024-10-29 00:22:29 +08:00
if (led_strip_ == nullptr) {
return;
}
2024-11-29 11:06:05 +08:00
std::lock_guard<std::mutex> lock(mutex_);
esp_timer_stop(blink_timer_);
2024-10-29 00:22:29 +08:00
2024-11-29 11:06:05 +08:00
led_strip_clear(led_strip_);
blink_counter_ = times * 2;
2024-10-29 00:22:29 +08:00
blink_interval_ms_ = interval_ms;
2024-11-29 11:06:05 +08:00
esp_timer_start_periodic(blink_timer_, interval_ms * 1000);
2024-10-29 00:22:29 +08:00
}
2024-11-29 11:06:05 +08:00
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_);
}
}
2024-10-29 00:22:29 +08:00
}