forked from xiaozhi/xiaozhi-esp32
v1.6.6: Set MCP as default IoT Protocol (#690)
This commit is contained in:
@@ -5,13 +5,15 @@
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "iot/thing_manager.h"
|
||||
#include "mcp_server.h"
|
||||
|
||||
#include <wifi_station.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_lcd_panel_vendor.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/spi_common.h>
|
||||
#include <driver/uart.h>
|
||||
#include <cstring>
|
||||
|
||||
#include "esp32_camera.h"
|
||||
|
||||
@@ -48,6 +50,7 @@ private:
|
||||
Button boot_button_;
|
||||
Display* display_;
|
||||
Esp32Camera* camera_;
|
||||
light_mode_t light_mode_ = LIGHT_MODE_ALWAYS_ON;
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
@@ -132,8 +135,8 @@ private:
|
||||
camera_config.pin_reset = SPARKBOT_CAMERA_RESET;
|
||||
camera_config.pin_xclk = SPARKBOT_CAMERA_XCLK;
|
||||
camera_config.pin_pclk = SPARKBOT_CAMERA_PCLK;
|
||||
camera_config.pin_sscb_sda = SPARKBOT_CAMERA_SIOD;
|
||||
camera_config.pin_sscb_scl = SPARKBOT_CAMERA_SIOC;
|
||||
camera_config.pin_sccb_sda = SPARKBOT_CAMERA_SIOD;
|
||||
camera_config.pin_sccb_scl = SPARKBOT_CAMERA_SIOC;
|
||||
|
||||
camera_config.pin_d0 = SPARKBOT_CAMERA_D0;
|
||||
camera_config.pin_d1 = SPARKBOT_CAMERA_D1;
|
||||
@@ -163,12 +166,86 @@ private:
|
||||
camera_ = new Esp32Camera(camera_config);
|
||||
}
|
||||
|
||||
// 物联网初始化,添加对 AI 可见设备
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Screen"));
|
||||
thing_manager.AddThing(iot::CreateThing("Chassis"));
|
||||
/*
|
||||
ESP-SparkBot 的底座
|
||||
https://gitee.com/esp-friends/esp_sparkbot/tree/master/example/tank/c2_tracked_chassis
|
||||
*/
|
||||
void InitializeEchoUart() {
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = ECHO_UART_BAUD_RATE,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
};
|
||||
int intr_alloc_flags = 0;
|
||||
|
||||
ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
|
||||
ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
|
||||
ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, UART_ECHO_TXD, UART_ECHO_RXD, UART_ECHO_RTS, UART_ECHO_CTS));
|
||||
|
||||
SendUartMessage("w2");
|
||||
}
|
||||
|
||||
void SendUartMessage(const char * command_str) {
|
||||
uint8_t len = strlen(command_str);
|
||||
uart_write_bytes(ECHO_UART_PORT_NUM, command_str, len);
|
||||
ESP_LOGI(TAG, "Sent command: %s", command_str);
|
||||
}
|
||||
|
||||
void InitializeTools() {
|
||||
auto& mcp_server = McpServer::GetInstance();
|
||||
// 定义设备的属性
|
||||
mcp_server.AddTool("self.chassis.get_light_mode", "获取灯光效果编号", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
if (light_mode_ < 2) {
|
||||
return 1;
|
||||
} else {
|
||||
return light_mode_ - 2;
|
||||
}
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.chassis.go_forward", "前进", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
SendUartMessage("x0.0 y1.0");
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.chassis.go_back", "后退", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
SendUartMessage("x0.0 y-1.0");
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.chassis.turn_left", "向左转", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
SendUartMessage("x-1.0 y0.0");
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.chassis.turn_right", "向右转", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
SendUartMessage("x1.0 y0.0");
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.chassis.dance", "跳舞", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
SendUartMessage("d1");
|
||||
light_mode_ = LIGHT_MODE_MAX;
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.chassis.switch_light_mode", "打开灯光效果", PropertyList({
|
||||
Property("light_mode", kPropertyTypeInteger, 1, 6)
|
||||
}), [this](const PropertyList& properties) -> ReturnValue {
|
||||
char command_str[5] = {'w', 0, 0};
|
||||
char mode = static_cast<light_mode_t>(properties["light_mode"].value<int>());
|
||||
|
||||
ESP_LOGI(TAG, "Switch Light Mode: %c", (mode + '0'));
|
||||
|
||||
if (mode >= 3 && mode <= 8) {
|
||||
command_str[1] = mode + '0';
|
||||
SendUartMessage(command_str);
|
||||
return true;
|
||||
}
|
||||
throw std::runtime_error("Invalid light mode");
|
||||
});
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -177,8 +254,9 @@ public:
|
||||
InitializeSpi();
|
||||
InitializeDisplay();
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
InitializeCamera();
|
||||
InitializeEchoUart();
|
||||
InitializeTools();
|
||||
GetBacklight()->RestoreBrightness();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user