forked from xiaozhi/xiaozhi-esp32
reconstruct led control
This commit is contained in:
@@ -20,6 +20,10 @@ Display* Board::GetDisplay() {
|
||||
return &display;
|
||||
}
|
||||
|
||||
Led* Board::GetLed() {
|
||||
static NoLed led;
|
||||
return &led;
|
||||
}
|
||||
|
||||
std::string Board::GetJson() {
|
||||
/*
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <udp.h>
|
||||
#include <string>
|
||||
|
||||
#include "led_strip/led_strip_wrapper.h"
|
||||
#include "led/led.h"
|
||||
|
||||
void* create_board();
|
||||
class AudioCodec;
|
||||
@@ -32,7 +32,7 @@ public:
|
||||
|
||||
virtual void StartNetwork() = 0;
|
||||
virtual ~Board() = default;
|
||||
virtual LedStripWrapper* GetLedStrip() = 0;
|
||||
virtual Led* GetLed() = 0;
|
||||
virtual AudioCodec* GetAudioCodec() = 0;
|
||||
virtual Display* GetDisplay();
|
||||
virtual Http* CreateHttp() = 0;
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
#include "led.h"
|
||||
#include "board.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "Led"
|
||||
|
||||
Led::Led(gpio_num_t gpio, uint8_t max_leds) {
|
||||
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 = max_leds;
|
||||
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
|
||||
|
||||
max_leds_ = max_leds;
|
||||
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip_));
|
||||
|
||||
led_strip_clear(led_strip_);
|
||||
SetGrey();
|
||||
}
|
||||
|
||||
Led::~Led() {
|
||||
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_);
|
||||
for (int i = 0; i < max_leds_; i++) {
|
||||
led_strip_set_pixel(led_strip_, i, r_, g_, b_);
|
||||
}
|
||||
led_strip_refresh(led_strip_);
|
||||
}
|
||||
|
||||
void Led::TurnOff() {
|
||||
if (led_strip_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
led_strip_clear(led_strip_);
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#ifndef _LED_H_
|
||||
#define _LED_H_
|
||||
|
||||
#include <led_strip.h>
|
||||
#include <esp_timer.h>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
#define DEFAULT_BRIGHTNESS 4
|
||||
#define HIGH_BRIGHTNESS 16
|
||||
#define LOW_BRIGHTNESS 2
|
||||
|
||||
class Led {
|
||||
public:
|
||||
Led(gpio_num_t gpio, uint8_t max_leds);
|
||||
~Led();
|
||||
|
||||
led_strip_handle_t led_strip() { return led_strip_; }
|
||||
uint8_t max_leds() { return max_leds_; }
|
||||
|
||||
void TurnOn();
|
||||
void TurnOff();
|
||||
void SetColor(uint8_t r, uint8_t g, uint8_t b);
|
||||
void SetWhite(uint8_t brightness = DEFAULT_BRIGHTNESS) { SetColor(brightness, brightness, brightness); }
|
||||
void SetGrey(uint8_t brightness = DEFAULT_BRIGHTNESS) { SetColor(brightness, brightness, brightness); }
|
||||
void SetRed(uint8_t brightness = DEFAULT_BRIGHTNESS) { SetColor(brightness, 0, 0); }
|
||||
void SetGreen(uint8_t brightness = DEFAULT_BRIGHTNESS) { SetColor(0, brightness, 0); }
|
||||
void SetBlue(uint8_t brightness = DEFAULT_BRIGHTNESS) { SetColor(0, 0, brightness); }
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
uint8_t max_leds_ = -1;
|
||||
led_strip_handle_t led_strip_ = nullptr;
|
||||
uint8_t r_ = 0, g_ = 0, b_ = 0;
|
||||
};
|
||||
|
||||
#endif // _LED_H_
|
||||
@@ -46,7 +46,7 @@ void Ml307Board::StartNetwork() {
|
||||
modem_.OnMaterialReady([this, &application]() {
|
||||
ESP_LOGI(TAG, "ML307 material ready");
|
||||
application.Schedule([this, &application]() {
|
||||
application.SetChatState(kChatStateIdle);
|
||||
application.SetDeviceState(kDeviceStateIdle);
|
||||
WaitForNetworkReady();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -38,14 +38,14 @@ static std::string rssi_to_string(int rssi) {
|
||||
void WifiBoard::StartNetwork() {
|
||||
auto& application = Application::GetInstance();
|
||||
auto display = Board::GetInstance().GetDisplay();
|
||||
auto led_strip = Board::GetInstance().GetLedStrip();
|
||||
|
||||
// Try to connect to WiFi, if failed, launch the WiFi configuration AP
|
||||
auto& wifi_station = WifiStation::GetInstance();
|
||||
display->SetStatus(std::string("正在连接 ") + wifi_station.GetSsid());
|
||||
wifi_station.Start();
|
||||
if (!wifi_station.IsConnected()) {
|
||||
led_strip->LightOn(kConnecting);
|
||||
application.SetDeviceState(kDeviceStateWifiConfiguring);
|
||||
|
||||
auto& wifi_ap = WifiConfigurationAp::GetInstance();
|
||||
wifi_ap.SetSsidPrefix("Xiaozhi");
|
||||
wifi_ap.Start();
|
||||
|
||||
Reference in New Issue
Block a user