xmini_c3_board.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "wifi_board.h"
  2. #include "codecs/es8311_audio_codec.h"
  3. #include "display/oled_display.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "led/single_led.h"
  7. #include "mcp_server.h"
  8. #include "settings.h"
  9. #include "config.h"
  10. #include "power_save_timer.h"
  11. #include "font_awesome_symbols.h"
  12. #include <wifi_station.h>
  13. #include <esp_log.h>
  14. #include <esp_efuse_table.h>
  15. #include <driver/i2c_master.h>
  16. #include <esp_lcd_panel_ops.h>
  17. #include <esp_lcd_panel_vendor.h>
  18. #define TAG "XminiC3Board"
  19. LV_FONT_DECLARE(font_puhui_14_1);
  20. LV_FONT_DECLARE(font_awesome_14_1);
  21. class XminiC3Board : public WifiBoard {
  22. private:
  23. i2c_master_bus_handle_t codec_i2c_bus_;
  24. esp_lcd_panel_io_handle_t panel_io_ = nullptr;
  25. esp_lcd_panel_handle_t panel_ = nullptr;
  26. Display* display_ = nullptr;
  27. Button boot_button_;
  28. bool press_to_talk_enabled_ = false;
  29. PowerSaveTimer* power_save_timer_ = nullptr;
  30. void InitializePowerSaveTimer() {
  31. #if CONFIG_USE_ESP_WAKE_WORD
  32. power_save_timer_ = new PowerSaveTimer(160, 600);
  33. #else
  34. power_save_timer_ = new PowerSaveTimer(160, 60);
  35. #endif
  36. power_save_timer_->OnEnterSleepMode([this]() {
  37. ESP_LOGI(TAG, "Enabling sleep mode");
  38. auto display = GetDisplay();
  39. display->SetChatMessage("system", "");
  40. display->SetEmotion("sleepy");
  41. auto codec = GetAudioCodec();
  42. codec->EnableInput(false);
  43. });
  44. power_save_timer_->OnExitSleepMode([this]() {
  45. auto codec = GetAudioCodec();
  46. codec->EnableInput(true);
  47. auto display = GetDisplay();
  48. display->SetChatMessage("system", "");
  49. display->SetEmotion("neutral");
  50. });
  51. power_save_timer_->SetEnabled(true);
  52. }
  53. void InitializeCodecI2c() {
  54. // Initialize I2C peripheral
  55. i2c_master_bus_config_t i2c_bus_cfg = {
  56. .i2c_port = I2C_NUM_0,
  57. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  58. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  59. .clk_source = I2C_CLK_SRC_DEFAULT,
  60. .glitch_ignore_cnt = 7,
  61. .intr_priority = 0,
  62. .trans_queue_depth = 0,
  63. .flags = {
  64. .enable_internal_pullup = 1,
  65. },
  66. };
  67. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  68. // Print I2C bus info
  69. if (i2c_master_probe(codec_i2c_bus_, 0x18, 1000) != ESP_OK) {
  70. while (true) {
  71. ESP_LOGE(TAG, "Failed to probe I2C bus, please check if you have installed the correct firmware");
  72. vTaskDelay(1000 / portTICK_PERIOD_MS);
  73. }
  74. }
  75. }
  76. void InitializeSsd1306Display() {
  77. // SSD1306 config
  78. esp_lcd_panel_io_i2c_config_t io_config = {
  79. .dev_addr = 0x3C,
  80. .on_color_trans_done = nullptr,
  81. .user_ctx = nullptr,
  82. .control_phase_bytes = 1,
  83. .dc_bit_offset = 6,
  84. .lcd_cmd_bits = 8,
  85. .lcd_param_bits = 8,
  86. .flags = {
  87. .dc_low_on_data = 0,
  88. .disable_control_phase = 0,
  89. },
  90. .scl_speed_hz = 400 * 1000,
  91. };
  92. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c_v2(codec_i2c_bus_, &io_config, &panel_io_));
  93. ESP_LOGI(TAG, "Install SSD1306 driver");
  94. esp_lcd_panel_dev_config_t panel_config = {};
  95. panel_config.reset_gpio_num = -1;
  96. panel_config.bits_per_pixel = 1;
  97. esp_lcd_panel_ssd1306_config_t ssd1306_config = {
  98. .height = static_cast<uint8_t>(DISPLAY_HEIGHT),
  99. };
  100. panel_config.vendor_config = &ssd1306_config;
  101. ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(panel_io_, &panel_config, &panel_));
  102. ESP_LOGI(TAG, "SSD1306 driver installed");
  103. // Reset the display
  104. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
  105. if (esp_lcd_panel_init(panel_) != ESP_OK) {
  106. ESP_LOGE(TAG, "Failed to initialize display");
  107. display_ = new NoDisplay();
  108. return;
  109. }
  110. // Set the display to on
  111. ESP_LOGI(TAG, "Turning display on");
  112. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  113. display_ = new OledDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y,
  114. {&font_puhui_14_1, &font_awesome_14_1});
  115. }
  116. void InitializeButtons() {
  117. boot_button_.OnClick([this]() {
  118. auto& app = Application::GetInstance();
  119. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  120. ResetWifiConfiguration();
  121. }
  122. if (!press_to_talk_enabled_) {
  123. app.ToggleChatState();
  124. }
  125. });
  126. boot_button_.OnPressDown([this]() {
  127. if (power_save_timer_) {
  128. power_save_timer_->WakeUp();
  129. }
  130. if (press_to_talk_enabled_) {
  131. Application::GetInstance().StartListening();
  132. }
  133. });
  134. boot_button_.OnPressUp([this]() {
  135. if (press_to_talk_enabled_) {
  136. Application::GetInstance().StopListening();
  137. }
  138. });
  139. }
  140. void InitializeTools() {
  141. Settings settings("vendor");
  142. press_to_talk_enabled_ = settings.GetInt("press_to_talk", 0) != 0;
  143. auto& mcp_server = McpServer::GetInstance();
  144. mcp_server.AddTool("self.set_press_to_talk",
  145. "Switch between press to talk mode (长按说话) and click to talk mode (单击说话).\n"
  146. "The mode can be `press_to_talk` or `click_to_talk`.",
  147. PropertyList({
  148. Property("mode", kPropertyTypeString)
  149. }),
  150. [this](const PropertyList& properties) -> ReturnValue {
  151. auto mode = properties["mode"].value<std::string>();
  152. if (mode == "press_to_talk") {
  153. SetPressToTalkEnabled(true);
  154. return true;
  155. } else if (mode == "click_to_talk") {
  156. SetPressToTalkEnabled(false);
  157. return true;
  158. }
  159. throw std::runtime_error("Invalid mode: " + mode);
  160. });
  161. }
  162. public:
  163. XminiC3Board() : boot_button_(BOOT_BUTTON_GPIO) {
  164. InitializeCodecI2c();
  165. InitializeSsd1306Display();
  166. InitializeButtons();
  167. InitializePowerSaveTimer();
  168. InitializeTools();
  169. // 避免使用错误的固件,把 EFUSE 操作放在最后
  170. // 把 ESP32C3 的 VDD SPI 引脚作为普通 GPIO 口使用
  171. esp_efuse_write_field_bit(ESP_EFUSE_VDD_SPI_AS_GPIO);
  172. }
  173. virtual Led* GetLed() override {
  174. static SingleLed led(BUILTIN_LED_GPIO);
  175. return &led;
  176. }
  177. virtual Display* GetDisplay() override {
  178. return display_;
  179. }
  180. virtual AudioCodec* GetAudioCodec() override {
  181. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  182. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  183. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  184. return &audio_codec;
  185. }
  186. void SetPressToTalkEnabled(bool enabled) {
  187. press_to_talk_enabled_ = enabled;
  188. Settings settings("vendor", true);
  189. settings.SetInt("press_to_talk", enabled ? 1 : 0);
  190. ESP_LOGI(TAG, "Press to talk enabled: %d", enabled);
  191. }
  192. bool IsPressToTalkEnabled() {
  193. return press_to_talk_enabled_;
  194. }
  195. };
  196. DECLARE_BOARD(XminiC3Board);