v1.6.6: Set MCP as default IoT Protocol (#690)

This commit is contained in:
Xiaoxia
2025-05-27 14:58:49 +08:00
committed by GitHub
parent 0c83263762
commit d80f94387a
46 changed files with 524 additions and 428 deletions

View File

@@ -1,98 +0,0 @@
/*
ESP-SparkBot 的底座
https://gitee.com/esp-friends/esp_sparkbot/tree/master/example/tank/c2_tracked_chassis
*/
#include "sdkconfig.h"
#include "iot/thing.h"
#include "board.h"
#include <driver/gpio.h>
#include <driver/uart.h>
#include <esp_log.h>
#include <cstring>
#include "boards/esp-sparkbot/config.h"
#define TAG "Chassis"
namespace iot {
class Chassis : public Thing {
private:
light_mode_t light_mode_ = LIGHT_MODE_ALWAYS_ON;
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 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");
}
public:
Chassis() : Thing("Chassis", "小机器人的底座:有履带可以移动;可以调整灯光效果"), light_mode_(LIGHT_MODE_ALWAYS_ON) {
InitializeEchoUart();
// 定义设备的属性
properties_.AddNumberProperty("light_mode", "灯光效果编号", [this]() -> int {
return (light_mode_ - 2 <= 0) ? 1 : light_mode_ - 2;
});
// 定义设备可以被远程执行的指令
methods_.AddMethod("GoForward", "向前走", ParameterList(), [this](const ParameterList& parameters) {
SendUartMessage("x0.0 y1.0");
});
methods_.AddMethod("GoBack", "向后退", ParameterList(), [this](const ParameterList& parameters) {
SendUartMessage("x0.0 y-1.0");
});
methods_.AddMethod("TurnLeft", "向左转", ParameterList(), [this](const ParameterList& parameters) {
SendUartMessage("x-1.0 y0.0");
});
methods_.AddMethod("TurnRight", "向右转", ParameterList(), [this](const ParameterList& parameters) {
SendUartMessage("x1.0 y0.0");
});
methods_.AddMethod("Dance", "跳舞", ParameterList(), [this](const ParameterList& parameters) {
SendUartMessage("d1");
light_mode_ = LIGHT_MODE_MAX;
});
methods_.AddMethod("SwitchLightMode", "打开灯", ParameterList({
Parameter("lightmode", "1到6之间的整数", kValueTypeNumber, true)
}), [this](const ParameterList& parameters) {
char command_str[5] = {'w', 0, 0};
char mode = static_cast<char>(parameters["lightmode"].number()) + 2;
ESP_LOGI(TAG, "Input Light Mode: %c", (mode + '0'));
if (mode >= 3 && mode <= 8) {
command_str[1] = mode + '0';
SendUartMessage(command_str);
}
});
}
};
} // namespace iot
DECLARE_THING(Chassis);

View File

@@ -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();
}