add some code

This commit is contained in:
2025-09-05 13:25:11 +08:00
parent 9ff0a99e7a
commit 3cf1229a85
8911 changed files with 2535396 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
#ifndef _BOARD_CONFIG_H_
#define _BOARD_CONFIG_H_
#include <driver/gpio.h>
#define AUDIO_INPUT_SAMPLE_RATE 24000
#define AUDIO_OUTPUT_SAMPLE_RATE 24000
#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_10
#define AUDIO_I2S_GPIO_WS GPIO_NUM_12
#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_8
#define AUDIO_I2S_GPIO_DIN GPIO_NUM_7
#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_11
#define AUDIO_CODEC_PA_PIN GPIO_NUM_13
#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_0
#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_1
#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR
#define BUILTIN_LED_GPIO GPIO_NUM_5
#define BOOT_BUTTON_GPIO GPIO_NUM_6
#endif // _BOARD_CONFIG_H_

View File

@@ -0,0 +1,13 @@
{
"target": "esp32c3",
"builds": [
{
"name": "kevin-c3",
"sdkconfig_append": [
"CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT=n",
"CONFIG_LWIP_IPV6=n",
"CONFIG_USE_ESP_WAKE_WORD=y"
]
}
]
}

View File

@@ -0,0 +1,90 @@
#include "wifi_board.h"
#include "codecs/es8311_audio_codec.h"
#include "application.h"
#include "button.h"
#include "config.h"
#include "led/circular_strip.h"
#include "led_strip_control.h"
#include <wifi_station.h>
#include <esp_log.h>
#include <esp_efuse_table.h>
#include <driver/i2c_master.h>
#define TAG "KevinBoxBoard"
class KevinBoxBoard : public WifiBoard {
private:
i2c_master_bus_handle_t codec_i2c_bus_;
Button boot_button_;
CircularStrip* led_strip_;
void InitializeCodecI2c() {
// Initialize I2C peripheral
i2c_master_bus_config_t i2c_bus_cfg = {
.i2c_port = I2C_NUM_0,
.sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
.scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.intr_priority = 0,
.trans_queue_depth = 0,
.flags = {
.enable_internal_pullup = 1,
},
};
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
// Print I2C bus info
if (i2c_master_probe(codec_i2c_bus_, 0x18, 1000) != ESP_OK) {
while (true) {
ESP_LOGE(TAG, "Failed to probe I2C bus, please check if you have installed the correct firmware");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
}
void InitializeButtons() {
boot_button_.OnClick([this]() {
auto& app = Application::GetInstance();
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
ResetWifiConfiguration();
}
});
boot_button_.OnPressDown([this]() {
Application::GetInstance().StartListening();
});
boot_button_.OnPressUp([this]() {
Application::GetInstance().StopListening();
});
}
// 物联网初始化,添加对 AI 可见设备
void InitializeTools() {
led_strip_ = new CircularStrip(BUILTIN_LED_GPIO, 8);
new LedStripControl(led_strip_);
}
public:
KevinBoxBoard() : boot_button_(BOOT_BUTTON_GPIO) {
InitializeCodecI2c();
InitializeButtons();
InitializeTools();
// 把 ESP32C3 的 VDD SPI 引脚作为普通 GPIO 口使用
esp_efuse_write_field_bit(ESP_EFUSE_VDD_SPI_AS_GPIO);
}
virtual Led* GetLed() override {
return led_strip_;
}
virtual AudioCodec* GetAudioCodec() override {
static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
return &audio_codec;
}
};
DECLARE_BOARD(KevinBoxBoard);

View File

@@ -0,0 +1,130 @@
#include "led_strip_control.h"
#include "settings.h"
#include "mcp_server.h"
#include <esp_log.h>
#define TAG "LedStripControl"
int LedStripControl::LevelToBrightness(int level) const {
if (level < 0) level = 0;
if (level > 8) level = 8;
return (1 << level) - 1; // 2^n - 1
}
StripColor LedStripControl::RGBToColor(int red, int green, int blue) {
if (red < 0) red = 0;
if (red > 255) red = 255;
if (green < 0) green = 0;
if (green > 255) green = 255;
if (blue < 0) blue = 0;
if (blue > 255) blue = 255;
return {static_cast<uint8_t>(red), static_cast<uint8_t>(green), static_cast<uint8_t>(blue)};
}
LedStripControl::LedStripControl(CircularStrip* led_strip)
: led_strip_(led_strip) {
// 从设置中读取亮度等级
Settings settings("led_strip");
brightness_level_ = settings.GetInt("brightness", 4); // 默认等级4
led_strip_->SetBrightness(LevelToBrightness(brightness_level_), 4);
auto& mcp_server = McpServer::GetInstance();
mcp_server.AddTool("self.led_strip.get_brightness",
"Get the brightness of the led strip (0-8)",
PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
return brightness_level_;
});
mcp_server.AddTool("self.led_strip.set_brightness",
"Set the brightness of the led strip (0-8)",
PropertyList({
Property("level", kPropertyTypeInteger, 0, 8)
}), [this](const PropertyList& properties) -> ReturnValue {
int level = properties["level"].value<int>();
ESP_LOGI(TAG, "Set LedStrip brightness level to %d", level);
brightness_level_ = level;
led_strip_->SetBrightness(LevelToBrightness(brightness_level_), 4);
// 保存设置
Settings settings("led_strip", true);
settings.SetInt("brightness", brightness_level_);
return true;
});
mcp_server.AddTool("self.led_strip.set_single_color",
"Set the color of a single led.",
PropertyList({
Property("index", kPropertyTypeInteger, 0, 7),
Property("red", kPropertyTypeInteger, 0, 255),
Property("green", kPropertyTypeInteger, 0, 255),
Property("blue", kPropertyTypeInteger, 0, 255)
}), [this](const PropertyList& properties) -> ReturnValue {
int index = properties["index"].value<int>();
int red = properties["red"].value<int>();
int green = properties["green"].value<int>();
int blue = properties["blue"].value<int>();
ESP_LOGI(TAG, "Set led strip single color %d to %d, %d, %d",
index, red, green, blue);
led_strip_->SetSingleColor(index, RGBToColor(red, green, blue));
return true;
});
mcp_server.AddTool("self.led_strip.set_all_color",
"Set the color of all leds.",
PropertyList({
Property("red", kPropertyTypeInteger, 0, 255),
Property("green", kPropertyTypeInteger, 0, 255),
Property("blue", kPropertyTypeInteger, 0, 255)
}), [this](const PropertyList& properties) -> ReturnValue {
int red = properties["red"].value<int>();
int green = properties["green"].value<int>();
int blue = properties["blue"].value<int>();
ESP_LOGI(TAG, "Set led strip all color to %d, %d, %d",
red, green, blue);
led_strip_->SetAllColor(RGBToColor(red, green, blue));
return true;
});
mcp_server.AddTool("self.led_strip.blink",
"Blink the led strip. (闪烁)",
PropertyList({
Property("red", kPropertyTypeInteger, 0, 255),
Property("green", kPropertyTypeInteger, 0, 255),
Property("blue", kPropertyTypeInteger, 0, 255),
Property("interval", kPropertyTypeInteger, 0, 1000)
}), [this](const PropertyList& properties) -> ReturnValue {
int red = properties["red"].value<int>();
int green = properties["green"].value<int>();
int blue = properties["blue"].value<int>();
int interval = properties["interval"].value<int>();
ESP_LOGI(TAG, "Blink led strip with color %d, %d, %d, interval %dms",
red, green, blue, interval);
led_strip_->Blink(RGBToColor(red, green, blue), interval);
return true;
});
mcp_server.AddTool("self.led_strip.scroll",
"Scroll the led strip. (跑马灯)",
PropertyList({
Property("red", kPropertyTypeInteger, 0, 255),
Property("green", kPropertyTypeInteger, 0, 255),
Property("blue", kPropertyTypeInteger, 0, 255),
Property("length", kPropertyTypeInteger, 1, 7),
Property("interval", kPropertyTypeInteger, 0, 1000)
}), [this](const PropertyList& properties) -> ReturnValue {
int red = properties["red"].value<int>();
int green = properties["green"].value<int>();
int blue = properties["blue"].value<int>();
int interval = properties["interval"].value<int>();
int length = properties["length"].value<int>();
ESP_LOGI(TAG, "Scroll led strip with color %d, %d, %d, length %d, interval %dms",
red, green, blue, length, interval);
StripColor low = RGBToColor(4, 4, 4);
StripColor high = RGBToColor(red, green, blue);
led_strip_->Scroll(low, high, length, interval);
return true;
});
}

View File

@@ -0,0 +1,18 @@
#ifndef LED_STRIP_CONTROL_H
#define LED_STRIP_CONTROL_H
#include "led/circular_strip.h"
class LedStripControl {
private:
CircularStrip* led_strip_;
int brightness_level_; // 亮度等级 (0-8)
int LevelToBrightness(int level) const; // 将等级转换为实际亮度值
StripColor RGBToColor(int red, int green, int blue);
public:
explicit LedStripControl(CircularStrip* led_strip);
};
#endif // LED_STRIP_CONTROL_H