bolar_2 backup
This commit is contained in:
241
main/platform/borla_s3/ws2812_led.cc
Normal file
241
main/platform/borla_s3/ws2812_led.cc
Normal file
@@ -0,0 +1,241 @@
|
||||
#include "platform/borla_s3/ws2812_led.h"
|
||||
|
||||
#include <esp_err.h>
|
||||
#include <esp_log.h>
|
||||
#include <led_strip_rmt.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "Ws2812Led2";
|
||||
constexpr uint32_t kAnimationTickMs = 30;
|
||||
|
||||
} // namespace
|
||||
|
||||
Ws2812Led::~Ws2812Led()
|
||||
{
|
||||
if (timer_ != nullptr) {
|
||||
esp_timer_stop(timer_);
|
||||
esp_timer_delete(timer_);
|
||||
}
|
||||
if (strip_ != nullptr) {
|
||||
led_strip_clear(strip_);
|
||||
led_strip_del(strip_);
|
||||
}
|
||||
}
|
||||
|
||||
bool Ws2812Led::Initialize(gpio_num_t gpio)
|
||||
{
|
||||
led_strip_config_t strip_config = {};
|
||||
strip_config.strip_gpio_num = gpio;
|
||||
strip_config.max_leds = 1;
|
||||
strip_config.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB;
|
||||
strip_config.led_model = LED_MODEL_WS2812;
|
||||
|
||||
led_strip_rmt_config_t rmt_config = {};
|
||||
rmt_config.resolution_hz = 10 * 1000 * 1000;
|
||||
|
||||
esp_err_t result =
|
||||
led_strip_new_rmt_device(&strip_config, &rmt_config, &strip_);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to create LED strip: %s",
|
||||
esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
const esp_timer_create_args_t timer_args = {
|
||||
.callback = &Ws2812Led::TimerCallback,
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "borla_led",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
result = esp_timer_create(&timer_args, &timer_);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to create animation timer: %s",
|
||||
esp_err_to_name(result));
|
||||
led_strip_del(strip_);
|
||||
strip_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = led_strip_clear(strip_);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to clear LED: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(kTag, "WS2812 initialized on GPIO%d", static_cast<int>(gpio));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Ws2812Led::SetPattern(LedPattern pattern)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (strip_ == nullptr || timer_ == nullptr) {
|
||||
ESP_LOGW(kTag, "ignored LED pattern before initialization");
|
||||
return;
|
||||
}
|
||||
if (pattern == current_pattern_) {
|
||||
return;
|
||||
}
|
||||
|
||||
StopTimer();
|
||||
current_pattern_ = pattern;
|
||||
phase_ms_ = 0;
|
||||
Configure(pattern);
|
||||
|
||||
if (animation_ == Animation::kOff) {
|
||||
led_strip_clear(strip_);
|
||||
} else if (animation_ == Animation::kSteady) {
|
||||
Render(255);
|
||||
} else {
|
||||
Render(animation_ == Animation::kBlink ? 255 : 0);
|
||||
const esp_err_t result =
|
||||
esp_timer_start_periodic(timer_, kAnimationTickMs * 1000);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to start animation: %s",
|
||||
esp_err_to_name(result));
|
||||
led_strip_clear(strip_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Ws2812Led::TimerCallback(void* context)
|
||||
{
|
||||
static_cast<Ws2812Led*>(context)->OnTimer();
|
||||
}
|
||||
|
||||
void Ws2812Led::OnTimer()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (strip_ == nullptr || period_ms_ == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
phase_ms_ = (phase_ms_ + kAnimationTickMs) % period_ms_;
|
||||
if (animation_ == Animation::kBlink) {
|
||||
Render(phase_ms_ < period_ms_ / 2 ? 255 : 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (animation_ == Animation::kBreathe) {
|
||||
const uint32_t half = std::max<uint32_t>(period_ms_ / 2, 1);
|
||||
const uint32_t distance =
|
||||
phase_ms_ <= half ? phase_ms_ : period_ms_ - phase_ms_;
|
||||
Render(static_cast<uint8_t>((distance * 255) / half));
|
||||
}
|
||||
}
|
||||
|
||||
void Ws2812Led::StopTimer()
|
||||
{
|
||||
const esp_err_t result = esp_timer_stop(timer_);
|
||||
if (result != ESP_OK && result != ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGW(kTag, "failed to stop animation: %s",
|
||||
esp_err_to_name(result));
|
||||
}
|
||||
}
|
||||
|
||||
void Ws2812Led::Render(uint8_t scale)
|
||||
{
|
||||
const uint8_t red =
|
||||
static_cast<uint8_t>((static_cast<uint16_t>(red_) * scale) / 255);
|
||||
const uint8_t green =
|
||||
static_cast<uint8_t>((static_cast<uint16_t>(green_) * scale) / 255);
|
||||
const uint8_t blue =
|
||||
static_cast<uint8_t>((static_cast<uint16_t>(blue_) * scale) / 255);
|
||||
led_strip_set_pixel(strip_, 0, red, green, blue);
|
||||
led_strip_refresh(strip_);
|
||||
}
|
||||
|
||||
void Ws2812Led::Configure(LedPattern pattern)
|
||||
{
|
||||
switch (pattern) {
|
||||
case LedPattern::kOff:
|
||||
animation_ = Animation::kOff;
|
||||
red_ = green_ = blue_ = 0;
|
||||
period_ms_ = 0;
|
||||
break;
|
||||
case LedPattern::kStarting:
|
||||
animation_ = Animation::kBreathe;
|
||||
red_ = 8;
|
||||
green_ = 2;
|
||||
blue_ = 0;
|
||||
period_ms_ = 1500;
|
||||
break;
|
||||
case LedPattern::kConnecting:
|
||||
animation_ = Animation::kBreathe;
|
||||
red_ = 0;
|
||||
green_ = 0;
|
||||
blue_ = 8;
|
||||
period_ms_ = 1000;
|
||||
break;
|
||||
case LedPattern::kIdle:
|
||||
animation_ = Animation::kBreathe;
|
||||
red_ = 8;
|
||||
green_ = 6;
|
||||
blue_ = 2;
|
||||
period_ms_ = 4000;
|
||||
break;
|
||||
case LedPattern::kListening:
|
||||
animation_ = Animation::kSteady;
|
||||
red_ = 16;
|
||||
green_ = 0;
|
||||
blue_ = 0;
|
||||
period_ms_ = 0;
|
||||
break;
|
||||
case LedPattern::kSpeaking:
|
||||
animation_ = Animation::kBreathe;
|
||||
red_ = 0;
|
||||
green_ = 8;
|
||||
blue_ = 0;
|
||||
period_ms_ = 1500;
|
||||
break;
|
||||
case LedPattern::kRinging:
|
||||
animation_ = Animation::kBlink;
|
||||
red_ = 12;
|
||||
green_ = 0;
|
||||
blue_ = 12;
|
||||
period_ms_ = 500;
|
||||
break;
|
||||
case LedPattern::kCalling:
|
||||
animation_ = Animation::kSteady;
|
||||
red_ = 0;
|
||||
green_ = 16;
|
||||
blue_ = 0;
|
||||
period_ms_ = 0;
|
||||
break;
|
||||
case LedPattern::kFriendMode:
|
||||
animation_ = Animation::kBreathe;
|
||||
red_ = 8;
|
||||
green_ = 0;
|
||||
blue_ = 12;
|
||||
period_ms_ = 800;
|
||||
break;
|
||||
case LedPattern::kProvisioning:
|
||||
animation_ = Animation::kBlink;
|
||||
red_ = 0;
|
||||
green_ = 8;
|
||||
blue_ = 8;
|
||||
period_ms_ = 500;
|
||||
break;
|
||||
case LedPattern::kUpgrading:
|
||||
animation_ = Animation::kBlink;
|
||||
red_ = 12;
|
||||
green_ = 8;
|
||||
blue_ = 0;
|
||||
period_ms_ = 200;
|
||||
break;
|
||||
case LedPattern::kError:
|
||||
animation_ = Animation::kBlink;
|
||||
red_ = 16;
|
||||
green_ = 0;
|
||||
blue_ = 0;
|
||||
period_ms_ = 100;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user