bolar_1 date 2026-7-1 d:/proc

This commit is contained in:
2026-11-27 17:44:38 +08:00
commit 0dbdfa57bc
890 changed files with 325428 additions and 0 deletions

223
main/led/single_led.cc Normal file
View File

@@ -0,0 +1,223 @@
#include "single_led.h"
#include <esp_log.h>
#define TAG "SingleLed"
// Brightness levels (aligned with xiaozhi SingleLed)
static constexpr uint8_t DEFAULT_BRIGHTNESS = 4;
static constexpr uint8_t MEDIUM_BRIGHTNESS = 8;
static constexpr uint8_t HIGH_BRIGHTNESS = 16;
static constexpr uint8_t LOW_BRIGHTNESS = 2;
static constexpr uint8_t TINT_BRIGHTNESS = 12; // for compound colors (purple/yellow)
static constexpr int BLINK_INFINITE = -1;
// Breathing animation
static constexpr int BREATH_TICK_MS = 30; // refresh interval
static constexpr int BREATH_SLOW_MS = 4000; // idle (calm)
static constexpr int BREATH_NORMAL_MS = 1500; // speaking
static constexpr int BREATH_FAST_MS = 1000; // connecting
static constexpr int BREATH_SHUTDOWN_MS = 2000; // shutting down
SingleLed::SingleLed(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; // 10MHz
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip_));
led_strip_clear(led_strip_);
esp_timer_create_args_t blink_timer_args = {
.callback = [](void* arg) {
static_cast<SingleLed*>(arg)->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_));
esp_timer_create_args_t breath_timer_args = {
.callback = [](void* arg) {
static_cast<SingleLed*>(arg)->OnBreathingTimer();
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "breath_timer",
.skip_unhandled_events = false,
};
ESP_ERROR_CHECK(esp_timer_create(&breath_timer_args, &breath_timer_));
ESP_LOGI(TAG, "WS2812 LED initialized on GPIO%d", gpio);
}
SingleLed::~SingleLed()
{
esp_timer_stop(blink_timer_);
esp_timer_delete(blink_timer_);
esp_timer_stop(breath_timer_);
esp_timer_delete(breath_timer_);
if (led_strip_) {
led_strip_del(led_strip_);
}
}
void SingleLed::SetColor(uint8_t r, uint8_t g, uint8_t b)
{
r_ = r;
g_ = g;
b_ = b;
}
void SingleLed::TurnOn()
{
if (!led_strip_) return;
std::lock_guard<std::mutex> lock(mutex_);
esp_timer_stop(blink_timer_);
esp_timer_stop(breath_timer_);
led_strip_set_pixel(led_strip_, 0, r_, g_, b_);
led_strip_refresh(led_strip_);
}
void SingleLed::TurnOff()
{
if (!led_strip_) return;
std::lock_guard<std::mutex> lock(mutex_);
esp_timer_stop(blink_timer_);
esp_timer_stop(breath_timer_);
led_strip_clear(led_strip_);
}
void SingleLed::StartContinuousBlink(int interval_ms)
{
if (!led_strip_) return;
std::lock_guard<std::mutex> lock(mutex_);
esp_timer_stop(blink_timer_);
esp_timer_stop(breath_timer_);
blink_counter_ = BLINK_INFINITE;
esp_timer_start_periodic(blink_timer_, interval_ms * 1000);
}
void SingleLed::StartBreathing(int period_ms)
{
if (!led_strip_) return;
std::lock_guard<std::mutex> lock(mutex_);
esp_timer_stop(blink_timer_);
esp_timer_stop(breath_timer_);
breath_steps_ = period_ms / BREATH_TICK_MS;
if (breath_steps_ < 2) breath_steps_ = 2;
breath_phase_ = 0;
esp_timer_start_periodic(breath_timer_, BREATH_TICK_MS * 1000);
}
void SingleLed::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_);
}
}
}
void SingleLed::OnBreathingTimer()
{
std::lock_guard<std::mutex> lock(mutex_);
if (breath_steps_ < 2) return;
int half = breath_steps_ / 2;
// Triangle wave: phase 0 → half (peak) → steps (back to 0)
int dist = (breath_phase_ <= half) ? breath_phase_ : (breath_steps_ - breath_phase_);
uint8_t r = static_cast<uint8_t>((static_cast<int>(r_) * dist) / half);
uint8_t g = static_cast<uint8_t>((static_cast<int>(g_) * dist) / half);
uint8_t b = static_cast<uint8_t>((static_cast<int>(b_) * dist) / half);
led_strip_set_pixel(led_strip_, 0, r, g, b);
led_strip_refresh(led_strip_);
breath_phase_ = (breath_phase_ + 1) % breath_steps_;
}
void SingleLed::SetState(LedState state)
{
if (state == current_state_) return;
current_state_ = state;
// State → unique (color + behavior). One state, one light signature.
switch (state) {
case LedState::kOff:
TurnOff();
break;
case LedState::kIdle:
// Warm white slow breathing — RGB ratio (8,6,2) compensates for
// WS2812 low-brightness color cast (B over-bright, R weak) so it
// looks like a calm warm-white heartbeat instead of a bluish glow.
SetColor(MEDIUM_BRIGHTNESS, MEDIUM_BRIGHTNESS - 2, LOW_BRIGHTNESS);
StartBreathing(BREATH_SLOW_MS);
break;
case LedState::kConnecting:
// Blue fast breathing
SetColor(0, 0, MEDIUM_BRIGHTNESS);
StartBreathing(BREATH_FAST_MS);
break;
case LedState::kWifiConfig:
// Cyan blink 500ms
SetColor(0, MEDIUM_BRIGHTNESS, MEDIUM_BRIGHTNESS);
StartContinuousBlink(500);
break;
case LedState::kListening:
// Red steady (high brightness)
SetColor(HIGH_BRIGHTNESS, 0, 0);
TurnOn();
break;
case LedState::kSpeaking:
// Green breathing — gentle pulse while TTS plays
SetColor(0, MEDIUM_BRIGHTNESS, 0);
StartBreathing(BREATH_NORMAL_MS);
break;
case LedState::kRinging:
// Purple blink 500ms (incoming call) — distinct from speaking green
SetColor(TINT_BRIGHTNESS, 0, TINT_BRIGHTNESS);
StartContinuousBlink(500);
break;
case LedState::kCalling:
// Green steady high (active call)
SetColor(0, HIGH_BRIGHTNESS, 0);
TurnOn();
break;
case LedState::kUpgrading:
// Yellow blink 200ms — distinct from green family
SetColor(TINT_BRIGHTNESS, MEDIUM_BRIGHTNESS, 0);
StartContinuousBlink(200);
break;
case LedState::kError:
// Red fast blink 100ms
SetColor(HIGH_BRIGHTNESS, 0, 0);
StartContinuousBlink(100);
break;
case LedState::kShuttingDown:
// Orange breathing (slow fade) — warm tone, distinct from red listening
SetColor(MEDIUM_BRIGHTNESS, LOW_BRIGHTNESS, 0);
StartBreathing(BREATH_SHUTDOWN_MS);
break;
}
}