xmini_c3_board.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "wifi_board.h"
  2. #include "audio_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 "iot/thing_manager.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_;
  30. void InitializePowerSaveTimer() {
  31. power_save_timer_ = new PowerSaveTimer(160, 60);
  32. power_save_timer_->OnEnterSleepMode([this]() {
  33. ESP_LOGI(TAG, "Enabling sleep mode");
  34. auto display = GetDisplay();
  35. display->SetChatMessage("system", "");
  36. display->SetEmotion("sleepy");
  37. auto codec = GetAudioCodec();
  38. codec->EnableInput(false);
  39. });
  40. power_save_timer_->OnExitSleepMode([this]() {
  41. auto codec = GetAudioCodec();
  42. codec->EnableInput(true);
  43. auto display = GetDisplay();
  44. display->SetChatMessage("system", "");
  45. display->SetEmotion("neutral");
  46. });
  47. power_save_timer_->SetEnabled(true);
  48. }
  49. void InitializeCodecI2c() {
  50. // Initialize I2C peripheral
  51. i2c_master_bus_config_t i2c_bus_cfg = {
  52. .i2c_port = I2C_NUM_0,
  53. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  54. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  55. .clk_source = I2C_CLK_SRC_DEFAULT,
  56. .glitch_ignore_cnt = 7,
  57. .intr_priority = 0,
  58. .trans_queue_depth = 0,
  59. .flags = {
  60. .enable_internal_pullup = 1,
  61. },
  62. };
  63. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  64. }
  65. void InitializeSsd1306Display() {
  66. // SSD1306 config
  67. esp_lcd_panel_io_i2c_config_t io_config = {
  68. .dev_addr = 0x3C,
  69. .on_color_trans_done = nullptr,
  70. .user_ctx = nullptr,
  71. .control_phase_bytes = 1,
  72. .dc_bit_offset = 6,
  73. .lcd_cmd_bits = 8,
  74. .lcd_param_bits = 8,
  75. .flags = {
  76. .dc_low_on_data = 0,
  77. .disable_control_phase = 0,
  78. },
  79. .scl_speed_hz = 400 * 1000,
  80. };
  81. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c_v2(codec_i2c_bus_, &io_config, &panel_io_));
  82. ESP_LOGI(TAG, "Install SSD1306 driver");
  83. esp_lcd_panel_dev_config_t panel_config = {};
  84. panel_config.reset_gpio_num = -1;
  85. panel_config.bits_per_pixel = 1;
  86. esp_lcd_panel_ssd1306_config_t ssd1306_config = {
  87. .height = static_cast<uint8_t>(DISPLAY_HEIGHT),
  88. };
  89. panel_config.vendor_config = &ssd1306_config;
  90. ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(panel_io_, &panel_config, &panel_));
  91. ESP_LOGI(TAG, "SSD1306 driver installed");
  92. // Reset the display
  93. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
  94. if (esp_lcd_panel_init(panel_) != ESP_OK) {
  95. ESP_LOGE(TAG, "Failed to initialize display");
  96. display_ = new NoDisplay();
  97. return;
  98. }
  99. // Set the display to on
  100. ESP_LOGI(TAG, "Turning display on");
  101. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  102. display_ = new OledDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y,
  103. {&font_puhui_14_1, &font_awesome_14_1});
  104. }
  105. void InitializeButtons() {
  106. boot_button_.OnClick([this]() {
  107. auto& app = Application::GetInstance();
  108. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  109. ResetWifiConfiguration();
  110. }
  111. if (!press_to_talk_enabled_) {
  112. app.ToggleChatState();
  113. }
  114. });
  115. boot_button_.OnPressDown([this]() {
  116. power_save_timer_->WakeUp();
  117. if (press_to_talk_enabled_) {
  118. Application::GetInstance().StartListening();
  119. }
  120. });
  121. boot_button_.OnPressUp([this]() {
  122. if (press_to_talk_enabled_) {
  123. Application::GetInstance().StopListening();
  124. }
  125. });
  126. }
  127. // 物联网初始化,添加对 AI 可见设备
  128. void InitializeIot() {
  129. Settings settings("vendor");
  130. press_to_talk_enabled_ = settings.GetInt("press_to_talk", 0) != 0;
  131. auto& thing_manager = iot::ThingManager::GetInstance();
  132. thing_manager.AddThing(iot::CreateThing("Speaker"));
  133. thing_manager.AddThing(iot::CreateThing("PressToTalk"));
  134. }
  135. public:
  136. XminiC3Board() : boot_button_(BOOT_BUTTON_GPIO) {
  137. // 把 ESP32C3 的 VDD SPI 引脚作为普通 GPIO 口使用
  138. esp_efuse_write_field_bit(ESP_EFUSE_VDD_SPI_AS_GPIO);
  139. InitializeCodecI2c();
  140. InitializeSsd1306Display();
  141. InitializeButtons();
  142. InitializePowerSaveTimer();
  143. InitializeIot();
  144. }
  145. virtual Led* GetLed() override {
  146. static SingleLed led(BUILTIN_LED_GPIO);
  147. return &led;
  148. }
  149. virtual Display* GetDisplay() override {
  150. return display_;
  151. }
  152. virtual AudioCodec* GetAudioCodec() override {
  153. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  154. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  155. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  156. return &audio_codec;
  157. }
  158. void SetPressToTalkEnabled(bool enabled) {
  159. press_to_talk_enabled_ = enabled;
  160. Settings settings("vendor", true);
  161. settings.SetInt("press_to_talk", enabled ? 1 : 0);
  162. ESP_LOGI(TAG, "Press to talk enabled: %d", enabled);
  163. }
  164. bool IsPressToTalkEnabled() {
  165. return press_to_talk_enabled_;
  166. }
  167. };
  168. DECLARE_BOARD(XminiC3Board);
  169. namespace iot {
  170. class PressToTalk : public Thing {
  171. public:
  172. PressToTalk() : Thing("PressToTalk", "控制对话模式,一种是长按对话,一种是单击后连续对话。") {
  173. // 定义设备的属性
  174. properties_.AddBooleanProperty("enabled", "true 表示长按说话模式,false 表示单击说话模式", []() -> bool {
  175. auto board = static_cast<XminiC3Board*>(&Board::GetInstance());
  176. return board->IsPressToTalkEnabled();
  177. });
  178. // 定义设备可以被远程执行的指令
  179. methods_.AddMethod("SetEnabled", "启用或禁用长按说话模式,调用前需要经过用户确认", ParameterList({
  180. Parameter("enabled", "true 表示长按说话模式,false 表示单击说话模式", kValueTypeBoolean, true)
  181. }), [](const ParameterList& parameters) {
  182. bool enabled = parameters["enabled"].boolean();
  183. auto board = static_cast<XminiC3Board*>(&Board::GetInstance());
  184. board->SetPressToTalkEnabled(enabled);
  185. });
  186. }
  187. };
  188. } // namespace iot
  189. DECLARE_THING(PressToTalk);