xmini_c3_board.cc 8.2 KB

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