esp_sparkbot_board.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "wifi_board.h"
  2. #include "codecs/es8311_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "font_awesome_symbols.h"
  5. #include "application.h"
  6. #include "button.h"
  7. #include "config.h"
  8. #include "mcp_server.h"
  9. #include "settings.h"
  10. #include <wifi_station.h>
  11. #include <esp_log.h>
  12. #include <esp_lcd_panel_vendor.h>
  13. #include <driver/i2c_master.h>
  14. #include <driver/spi_common.h>
  15. #include <driver/uart.h>
  16. #include <cstring>
  17. #include "esp32_camera.h"
  18. #define TAG "esp_sparkbot"
  19. LV_FONT_DECLARE(font_puhui_20_4);
  20. LV_FONT_DECLARE(font_awesome_20_4);
  21. class SparkBotEs8311AudioCodec : public Es8311AudioCodec {
  22. private:
  23. public:
  24. SparkBotEs8311AudioCodec(void* i2c_master_handle, i2c_port_t i2c_port, int input_sample_rate, int output_sample_rate,
  25. gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
  26. gpio_num_t pa_pin, uint8_t es8311_addr, bool use_mclk = true)
  27. : Es8311AudioCodec(i2c_master_handle, i2c_port, input_sample_rate, output_sample_rate,
  28. mclk, bclk, ws, dout, din,pa_pin, es8311_addr, use_mclk = true) {}
  29. void EnableOutput(bool enable) override {
  30. if (enable == output_enabled_) {
  31. return;
  32. }
  33. if (enable) {
  34. Es8311AudioCodec::EnableOutput(enable);
  35. } else {
  36. // Nothing todo because the display io and PA io conflict
  37. }
  38. }
  39. };
  40. class EspSparkBot : public WifiBoard {
  41. private:
  42. i2c_master_bus_handle_t i2c_bus_;
  43. Button boot_button_;
  44. Display* display_;
  45. Esp32Camera* camera_;
  46. light_mode_t light_mode_ = LIGHT_MODE_ALWAYS_ON;
  47. void InitializeI2c() {
  48. // Initialize I2C peripheral
  49. i2c_master_bus_config_t i2c_bus_cfg = {
  50. .i2c_port = I2C_NUM_0,
  51. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  52. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  53. .clk_source = I2C_CLK_SRC_DEFAULT,
  54. .glitch_ignore_cnt = 7,
  55. .intr_priority = 0,
  56. .trans_queue_depth = 0,
  57. .flags = {
  58. .enable_internal_pullup = 1,
  59. },
  60. };
  61. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  62. }
  63. void InitializeSpi() {
  64. spi_bus_config_t buscfg = {};
  65. buscfg.mosi_io_num = DISPLAY_MOSI_GPIO;
  66. buscfg.miso_io_num = GPIO_NUM_NC;
  67. buscfg.sclk_io_num = DISPLAY_CLK_GPIO;
  68. buscfg.quadwp_io_num = GPIO_NUM_NC;
  69. buscfg.quadhd_io_num = GPIO_NUM_NC;
  70. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  71. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  72. }
  73. void InitializeButtons() {
  74. boot_button_.OnClick([this]() {
  75. auto& app = Application::GetInstance();
  76. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  77. ResetWifiConfiguration();
  78. }
  79. app.ToggleChatState();
  80. });
  81. }
  82. void InitializeDisplay() {
  83. esp_lcd_panel_io_handle_t panel_io = nullptr;
  84. esp_lcd_panel_handle_t panel = nullptr;
  85. // 液晶屏控制IO初始化
  86. ESP_LOGD(TAG, "Install panel IO");
  87. esp_lcd_panel_io_spi_config_t io_config = {};
  88. io_config.cs_gpio_num = DISPLAY_CS_GPIO;
  89. io_config.dc_gpio_num = DISPLAY_DC_GPIO;
  90. io_config.spi_mode = 0;
  91. io_config.pclk_hz = 40 * 1000 * 1000;
  92. io_config.trans_queue_depth = 10;
  93. io_config.lcd_cmd_bits = 8;
  94. io_config.lcd_param_bits = 8;
  95. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  96. // 初始化液晶屏驱动芯片
  97. ESP_LOGD(TAG, "Install LCD driver");
  98. esp_lcd_panel_dev_config_t panel_config = {};
  99. panel_config.reset_gpio_num = GPIO_NUM_NC;
  100. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  101. panel_config.bits_per_pixel = 16;
  102. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  103. esp_lcd_panel_reset(panel);
  104. esp_lcd_panel_init(panel);
  105. esp_lcd_panel_invert_color(panel, true);
  106. esp_lcd_panel_disp_on_off(panel, true);
  107. display_ = new SpiLcdDisplay(panel_io, panel,
  108. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  109. {
  110. .text_font = &font_puhui_20_4,
  111. .icon_font = &font_awesome_20_4,
  112. .emoji_font = font_emoji_64_init(),
  113. });
  114. }
  115. void InitializeCamera() {
  116. camera_config_t camera_config = {};
  117. camera_config.pin_pwdn = SPARKBOT_CAMERA_PWDN;
  118. camera_config.pin_reset = SPARKBOT_CAMERA_RESET;
  119. camera_config.pin_xclk = SPARKBOT_CAMERA_XCLK;
  120. camera_config.pin_pclk = SPARKBOT_CAMERA_PCLK;
  121. camera_config.pin_sccb_sda = SPARKBOT_CAMERA_SIOD;
  122. camera_config.pin_sccb_scl = SPARKBOT_CAMERA_SIOC;
  123. camera_config.pin_d0 = SPARKBOT_CAMERA_D0;
  124. camera_config.pin_d1 = SPARKBOT_CAMERA_D1;
  125. camera_config.pin_d2 = SPARKBOT_CAMERA_D2;
  126. camera_config.pin_d3 = SPARKBOT_CAMERA_D3;
  127. camera_config.pin_d4 = SPARKBOT_CAMERA_D4;
  128. camera_config.pin_d5 = SPARKBOT_CAMERA_D5;
  129. camera_config.pin_d6 = SPARKBOT_CAMERA_D6;
  130. camera_config.pin_d7 = SPARKBOT_CAMERA_D7;
  131. camera_config.pin_vsync = SPARKBOT_CAMERA_VSYNC;
  132. camera_config.pin_href = SPARKBOT_CAMERA_HSYNC;
  133. camera_config.pin_pclk = SPARKBOT_CAMERA_PCLK;
  134. camera_config.xclk_freq_hz = SPARKBOT_CAMERA_XCLK_FREQ;
  135. camera_config.ledc_timer = SPARKBOT_LEDC_TIMER;
  136. camera_config.ledc_channel = SPARKBOT_LEDC_CHANNEL;
  137. camera_config.fb_location = CAMERA_FB_IN_PSRAM;
  138. camera_config.sccb_i2c_port = I2C_NUM_0;
  139. camera_config.pixel_format = PIXFORMAT_RGB565;
  140. camera_config.frame_size = FRAMESIZE_240X240;
  141. camera_config.jpeg_quality = 12;
  142. camera_config.fb_count = 1;
  143. camera_config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  144. camera_ = new Esp32Camera(camera_config);
  145. Settings settings("sparkbot", false);
  146. // 考虑到部分复刻使用了不可动摄像头的设计,默认启用翻转
  147. bool camera_flipped = static_cast<bool>(settings.GetInt("camera-flipped", 1));
  148. camera_->SetHMirror(camera_flipped);
  149. camera_->SetVFlip(camera_flipped);
  150. }
  151. /*
  152. ESP-SparkBot 的底座
  153. https://gitee.com/esp-friends/esp_sparkbot/tree/master/example/tank/c2_tracked_chassis
  154. */
  155. void InitializeEchoUart() {
  156. uart_config_t uart_config = {
  157. .baud_rate = ECHO_UART_BAUD_RATE,
  158. .data_bits = UART_DATA_8_BITS,
  159. .parity = UART_PARITY_DISABLE,
  160. .stop_bits = UART_STOP_BITS_1,
  161. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  162. .source_clk = UART_SCLK_DEFAULT,
  163. };
  164. int intr_alloc_flags = 0;
  165. ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
  166. ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
  167. ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, UART_ECHO_TXD, UART_ECHO_RXD, UART_ECHO_RTS, UART_ECHO_CTS));
  168. SendUartMessage("w2");
  169. }
  170. void SendUartMessage(const char * command_str) {
  171. uint8_t len = strlen(command_str);
  172. uart_write_bytes(ECHO_UART_PORT_NUM, command_str, len);
  173. ESP_LOGI(TAG, "Sent command: %s", command_str);
  174. }
  175. void InitializeTools() {
  176. auto& mcp_server = McpServer::GetInstance();
  177. // 定义设备的属性
  178. mcp_server.AddTool("self.chassis.get_light_mode", "获取灯光效果编号", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
  179. if (light_mode_ < 2) {
  180. return 1;
  181. } else {
  182. return light_mode_ - 2;
  183. }
  184. });
  185. mcp_server.AddTool("self.chassis.go_forward", "前进", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
  186. SendUartMessage("x0.0 y1.0");
  187. return true;
  188. });
  189. mcp_server.AddTool("self.chassis.go_back", "后退", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
  190. SendUartMessage("x0.0 y-1.0");
  191. return true;
  192. });
  193. mcp_server.AddTool("self.chassis.turn_left", "向左转", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
  194. SendUartMessage("x-1.0 y0.0");
  195. return true;
  196. });
  197. mcp_server.AddTool("self.chassis.turn_right", "向右转", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
  198. SendUartMessage("x1.0 y0.0");
  199. return true;
  200. });
  201. mcp_server.AddTool("self.chassis.dance", "跳舞", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
  202. SendUartMessage("d1");
  203. light_mode_ = LIGHT_MODE_MAX;
  204. return true;
  205. });
  206. mcp_server.AddTool("self.chassis.switch_light_mode", "打开灯光效果", PropertyList({
  207. Property("light_mode", kPropertyTypeInteger, 1, 6)
  208. }), [this](const PropertyList& properties) -> ReturnValue {
  209. char command_str[5] = {'w', 0, 0};
  210. char mode = static_cast<light_mode_t>(properties["light_mode"].value<int>());
  211. ESP_LOGI(TAG, "Switch Light Mode: %c", (mode + '0'));
  212. if (mode >= 3 && mode <= 8) {
  213. command_str[1] = mode + '0';
  214. SendUartMessage(command_str);
  215. return true;
  216. }
  217. throw std::runtime_error("Invalid light mode");
  218. });
  219. mcp_server.AddTool("self.camera.set_camera_flipped", "翻转摄像头图像方向", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
  220. Settings settings("sparkbot", true);
  221. // 考虑到部分复刻使用了不可动摄像头的设计,默认启用翻转
  222. bool flipped = !static_cast<bool>(settings.GetInt("camera-flipped", 1));
  223. camera_->SetHMirror(flipped);
  224. camera_->SetVFlip(flipped);
  225. settings.SetInt("camera-flipped", flipped ? 1 : 0);
  226. return true;
  227. });
  228. }
  229. public:
  230. EspSparkBot() : boot_button_(BOOT_BUTTON_GPIO) {
  231. InitializeI2c();
  232. InitializeSpi();
  233. InitializeDisplay();
  234. InitializeButtons();
  235. InitializeCamera();
  236. InitializeEchoUart();
  237. InitializeTools();
  238. GetBacklight()->RestoreBrightness();
  239. }
  240. virtual AudioCodec* GetAudioCodec() override {
  241. static SparkBotEs8311AudioCodec audio_codec(i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  242. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  243. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  244. return &audio_codec;
  245. }
  246. virtual Display* GetDisplay() override {
  247. return display_;
  248. }
  249. virtual Backlight* GetBacklight() override {
  250. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  251. return &backlight;
  252. }
  253. virtual Camera* GetCamera() override {
  254. return camera_;
  255. }
  256. };
  257. DECLARE_BOARD(EspSparkBot);