xmini_c3_4g_board.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "ml307_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 Ml307Board {
  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 InitializeBatteryMonitor() {
  33. adc_battery_monitor_ = new AdcBatteryMonitor(ADC_UNIT_1, ADC_CHANNEL_4, 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. // Show the standby screen
  51. GetDisplay()->ShowStandbyScreen(true);
  52. // Enable sleep mode, and sleep in 1 second after DTR is set to high
  53. modem_->SetSleepMode(true, 1);
  54. // Set the DTR pin to high to make the modem enter sleep mode
  55. modem_->GetAtUart()->SetDtrPin(true);
  56. });
  57. sleep_timer_->OnExitLightSleepMode([this]() {
  58. // Set the DTR pin to low to make the modem wake up
  59. modem_->GetAtUart()->SetDtrPin(false);
  60. // Hide the standby screen
  61. GetDisplay()->ShowStandbyScreen(false);
  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. if (i2c_master_probe(codec_i2c_bus_, 0x18, 1000) != ESP_OK) {
  81. while (true) {
  82. ESP_LOGE(TAG, "Failed to probe I2C bus, please check if you have installed the correct firmware");
  83. vTaskDelay(1000 / portTICK_PERIOD_MS);
  84. }
  85. }
  86. }
  87. void InitializeSsd1306Display() {
  88. // SSD1306 config
  89. esp_lcd_panel_io_i2c_config_t io_config = {
  90. .dev_addr = 0x3C,
  91. .on_color_trans_done = nullptr,
  92. .user_ctx = nullptr,
  93. .control_phase_bytes = 1,
  94. .dc_bit_offset = 6,
  95. .lcd_cmd_bits = 8,
  96. .lcd_param_bits = 8,
  97. .flags = {
  98. .dc_low_on_data = 0,
  99. .disable_control_phase = 0,
  100. },
  101. .scl_speed_hz = 400 * 1000,
  102. };
  103. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c_v2(codec_i2c_bus_, &io_config, &panel_io_));
  104. ESP_LOGI(TAG, "Install SSD1306 driver");
  105. esp_lcd_panel_dev_config_t panel_config = {};
  106. panel_config.reset_gpio_num = -1;
  107. panel_config.bits_per_pixel = 1;
  108. esp_lcd_panel_ssd1306_config_t ssd1306_config = {
  109. .height = static_cast<uint8_t>(DISPLAY_HEIGHT),
  110. };
  111. panel_config.vendor_config = &ssd1306_config;
  112. ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(panel_io_, &panel_config, &panel_));
  113. ESP_LOGI(TAG, "SSD1306 driver installed");
  114. // Reset the display
  115. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
  116. if (esp_lcd_panel_init(panel_) != ESP_OK) {
  117. ESP_LOGE(TAG, "Failed to initialize display");
  118. display_ = new NoDisplay();
  119. return;
  120. }
  121. // Set the display to on
  122. ESP_LOGI(TAG, "Turning display on");
  123. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  124. display_ = new OledDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y,
  125. {&font_puhui_14_1, &font_awesome_14_1});
  126. }
  127. void InitializeButtons() {
  128. boot_button_.OnClick([this]() {
  129. auto& app = Application::GetInstance();
  130. if (!press_to_talk_enabled_) {
  131. app.ToggleChatState();
  132. }
  133. });
  134. boot_button_.OnPressDown([this]() {
  135. if (press_to_talk_enabled_) {
  136. Application::GetInstance().StartListening();
  137. }
  138. });
  139. boot_button_.OnPressUp([this]() {
  140. if (press_to_talk_enabled_) {
  141. Application::GetInstance().StopListening();
  142. }
  143. });
  144. }
  145. void InitializeTools() {
  146. Settings settings("vendor");
  147. press_to_talk_enabled_ = settings.GetInt("press_to_talk", 0) != 0;
  148. auto& mcp_server = McpServer::GetInstance();
  149. mcp_server.AddTool("self.set_press_to_talk",
  150. "Switch between press to talk mode (长按说话) and click to talk mode (单击说话).\n"
  151. "The mode can be `press_to_talk` or `click_to_talk`.",
  152. PropertyList({
  153. Property("mode", kPropertyTypeString)
  154. }),
  155. [this](const PropertyList& properties) -> ReturnValue {
  156. auto mode = properties["mode"].value<std::string>();
  157. if (mode == "press_to_talk") {
  158. SetPressToTalkEnabled(true);
  159. return true;
  160. } else if (mode == "click_to_talk") {
  161. SetPressToTalkEnabled(false);
  162. return true;
  163. }
  164. throw std::runtime_error("Invalid mode: " + mode);
  165. });
  166. }
  167. public:
  168. XminiC3Board() : Ml307Board(ML307_TX_PIN, ML307_RX_PIN, ML307_DTR_PIN),
  169. boot_button_(BOOT_BUTTON_GPIO, false, 0, 0, true) {
  170. InitializeBatteryMonitor();
  171. InitializePowerSaveTimer();
  172. InitializeCodecI2c();
  173. InitializeSsd1306Display();
  174. InitializeButtons();
  175. InitializeTools();
  176. }
  177. virtual Led* GetLed() override {
  178. static SingleLed led(BUILTIN_LED_GPIO);
  179. return &led;
  180. }
  181. virtual Display* GetDisplay() override {
  182. return display_;
  183. }
  184. virtual AudioCodec* GetAudioCodec() override {
  185. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  186. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  187. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  188. return &audio_codec;
  189. }
  190. virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
  191. charging = adc_battery_monitor_->IsCharging();
  192. discharging = adc_battery_monitor_->IsDischarging();
  193. level = adc_battery_monitor_->GetBatteryLevel();
  194. return true;
  195. }
  196. void SetPressToTalkEnabled(bool enabled) {
  197. press_to_talk_enabled_ = enabled;
  198. Settings settings("vendor", true);
  199. settings.SetInt("press_to_talk", enabled ? 1 : 0);
  200. ESP_LOGI(TAG, "Press to talk enabled: %d", enabled);
  201. }
  202. bool IsPressToTalkEnabled() {
  203. return press_to_talk_enabled_;
  204. }
  205. virtual void SetPowerSaveMode(bool enabled) override {
  206. if (!enabled) {
  207. sleep_timer_->WakeUp();
  208. }
  209. Ml307Board::SetPowerSaveMode(enabled);
  210. }
  211. };
  212. DECLARE_BOARD(XminiC3Board);