kevin_box_board.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "ml307_board.h"
  2. #include "audio_codecs/box_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 "config.h"
  9. #include "power_save_timer.h"
  10. #include "axp2101.h"
  11. #include "assets/lang_config.h"
  12. #include "font_awesome_symbols.h"
  13. #include <esp_log.h>
  14. #include <driver/gpio.h>
  15. #include <driver/i2c_master.h>
  16. #include <esp_lcd_panel_ops.h>
  17. #include <esp_lcd_panel_vendor.h>
  18. #define TAG "KevinBoxBoard"
  19. LV_FONT_DECLARE(font_puhui_14_1);
  20. LV_FONT_DECLARE(font_awesome_14_1);
  21. class Pmic : public Axp2101 {
  22. public:
  23. Pmic(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : Axp2101(i2c_bus, addr) {
  24. // ** EFUSE defaults **
  25. WriteReg(0x22, 0b110); // PWRON > OFFLEVEL as POWEROFF Source enable
  26. WriteReg(0x27, 0x10); // hold 4s to power off
  27. WriteReg(0x93, 0x1C); // 配置 aldo2 输出为 3.3V
  28. uint8_t value = ReadReg(0x90); // XPOWERS_AXP2101_LDO_ONOFF_CTRL0
  29. value = value | 0x02; // set bit 1 (ALDO2)
  30. WriteReg(0x90, value); // and power channels now enabled
  31. WriteReg(0x64, 0x03); // CV charger voltage setting to 4.2V
  32. WriteReg(0x61, 0x05); // set Main battery precharge current to 125mA
  33. WriteReg(0x62, 0x0A); // set Main battery charger current to 400mA ( 0x08-200mA, 0x09-300mA, 0x0A-400mA )
  34. WriteReg(0x63, 0x15); // set Main battery term charge current to 125mA
  35. WriteReg(0x14, 0x00); // set minimum system voltage to 4.1V (default 4.7V), for poor USB cables
  36. WriteReg(0x15, 0x00); // set input voltage limit to 3.88v, for poor USB cables
  37. WriteReg(0x16, 0x05); // set input current limit to 2000mA
  38. WriteReg(0x24, 0x01); // set Vsys for PWROFF threshold to 3.2V (default - 2.6V and kill battery)
  39. WriteReg(0x50, 0x14); // set TS pin to EXTERNAL input (not temperature)
  40. }
  41. };
  42. class KevinBoxBoard : public Ml307Board {
  43. private:
  44. i2c_master_bus_handle_t display_i2c_bus_;
  45. i2c_master_bus_handle_t codec_i2c_bus_;
  46. esp_lcd_panel_io_handle_t panel_io_ = nullptr;
  47. esp_lcd_panel_handle_t panel_ = nullptr;
  48. Display* display_ = nullptr;
  49. Pmic* pmic_ = nullptr;
  50. Button boot_button_;
  51. Button volume_up_button_;
  52. Button volume_down_button_;
  53. PowerSaveTimer* power_save_timer_;
  54. void InitializePowerSaveTimer() {
  55. power_save_timer_ = new PowerSaveTimer(240, 60, -1);
  56. power_save_timer_->OnEnterSleepMode([this]() {
  57. ESP_LOGI(TAG, "Enabling sleep mode");
  58. if (!modem_.Command("AT+MLPMCFG=\"sleepmode\",2,0")) {
  59. ESP_LOGE(TAG, "Failed to enable module sleep mode");
  60. }
  61. auto display = GetDisplay();
  62. display->SetChatMessage("system", "");
  63. display->SetEmotion("sleepy");
  64. auto codec = GetAudioCodec();
  65. codec->EnableInput(false);
  66. });
  67. power_save_timer_->OnExitSleepMode([this]() {
  68. auto codec = GetAudioCodec();
  69. codec->EnableInput(true);
  70. auto display = GetDisplay();
  71. display->SetChatMessage("system", "");
  72. display->SetEmotion("neutral");
  73. });
  74. power_save_timer_->SetEnabled(true);
  75. }
  76. void Enable4GModule() {
  77. // Make GPIO HIGH to enable the 4G module
  78. gpio_config_t ml307_enable_config = {
  79. .pin_bit_mask = (1ULL << 4),
  80. .mode = GPIO_MODE_OUTPUT,
  81. .pull_up_en = GPIO_PULLUP_DISABLE,
  82. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  83. .intr_type = GPIO_INTR_DISABLE,
  84. };
  85. gpio_config(&ml307_enable_config);
  86. gpio_set_level(GPIO_NUM_4, 1);
  87. }
  88. void InitializeDisplayI2c() {
  89. i2c_master_bus_config_t bus_config = {
  90. .i2c_port = (i2c_port_t)0,
  91. .sda_io_num = DISPLAY_SDA_PIN,
  92. .scl_io_num = DISPLAY_SCL_PIN,
  93. .clk_source = I2C_CLK_SRC_DEFAULT,
  94. .glitch_ignore_cnt = 7,
  95. .intr_priority = 0,
  96. .trans_queue_depth = 0,
  97. .flags = {
  98. .enable_internal_pullup = 1,
  99. },
  100. };
  101. ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &display_i2c_bus_));
  102. }
  103. void InitializeSsd1306Display() {
  104. // SSD1306 config
  105. esp_lcd_panel_io_i2c_config_t io_config = {
  106. .dev_addr = 0x3C,
  107. .on_color_trans_done = nullptr,
  108. .user_ctx = nullptr,
  109. .control_phase_bytes = 1,
  110. .dc_bit_offset = 6,
  111. .lcd_cmd_bits = 8,
  112. .lcd_param_bits = 8,
  113. .flags = {
  114. .dc_low_on_data = 0,
  115. .disable_control_phase = 0,
  116. },
  117. .scl_speed_hz = 400 * 1000,
  118. };
  119. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c_v2(display_i2c_bus_, &io_config, &panel_io_));
  120. ESP_LOGI(TAG, "Install SSD1306 driver");
  121. esp_lcd_panel_dev_config_t panel_config = {};
  122. panel_config.reset_gpio_num = -1;
  123. panel_config.bits_per_pixel = 1;
  124. esp_lcd_panel_ssd1306_config_t ssd1306_config = {
  125. .height = static_cast<uint8_t>(DISPLAY_HEIGHT),
  126. };
  127. panel_config.vendor_config = &ssd1306_config;
  128. ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(panel_io_, &panel_config, &panel_));
  129. ESP_LOGI(TAG, "SSD1306 driver installed");
  130. // Reset the display
  131. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
  132. if (esp_lcd_panel_init(panel_) != ESP_OK) {
  133. ESP_LOGE(TAG, "Failed to initialize display");
  134. display_ = new NoDisplay();
  135. return;
  136. }
  137. // Set the display to on
  138. ESP_LOGI(TAG, "Turning display on");
  139. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  140. display_ = new OledDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y,
  141. {&font_puhui_14_1, &font_awesome_14_1});
  142. }
  143. void InitializeCodecI2c() {
  144. // Initialize I2C peripheral
  145. i2c_master_bus_config_t i2c_bus_cfg = {
  146. .i2c_port = (i2c_port_t)1,
  147. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  148. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  149. .clk_source = I2C_CLK_SRC_DEFAULT,
  150. .glitch_ignore_cnt = 7,
  151. .intr_priority = 0,
  152. .trans_queue_depth = 0,
  153. .flags = {
  154. .enable_internal_pullup = 1,
  155. },
  156. };
  157. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  158. }
  159. void InitializeButtons() {
  160. boot_button_.OnPressDown([this]() {
  161. power_save_timer_->WakeUp();
  162. Application::GetInstance().StartListening();
  163. });
  164. boot_button_.OnPressUp([this]() {
  165. Application::GetInstance().StopListening();
  166. });
  167. volume_up_button_.OnClick([this]() {
  168. power_save_timer_->WakeUp();
  169. auto codec = GetAudioCodec();
  170. auto volume = codec->output_volume() + 10;
  171. if (volume > 100) {
  172. volume = 100;
  173. }
  174. codec->SetOutputVolume(volume);
  175. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  176. });
  177. volume_up_button_.OnLongPress([this]() {
  178. power_save_timer_->WakeUp();
  179. GetAudioCodec()->SetOutputVolume(100);
  180. GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
  181. });
  182. volume_down_button_.OnClick([this]() {
  183. power_save_timer_->WakeUp();
  184. auto codec = GetAudioCodec();
  185. auto volume = codec->output_volume() - 10;
  186. if (volume < 0) {
  187. volume = 0;
  188. }
  189. codec->SetOutputVolume(volume);
  190. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  191. });
  192. volume_down_button_.OnLongPress([this]() {
  193. power_save_timer_->WakeUp();
  194. GetAudioCodec()->SetOutputVolume(0);
  195. GetDisplay()->ShowNotification(Lang::Strings::MUTED);
  196. });
  197. }
  198. // 物联网初始化,添加对 AI 可见设备
  199. void InitializeIot() {
  200. auto& thing_manager = iot::ThingManager::GetInstance();
  201. thing_manager.AddThing(iot::CreateThing("Speaker"));
  202. thing_manager.AddThing(iot::CreateThing("Battery"));
  203. }
  204. public:
  205. KevinBoxBoard() : Ml307Board(ML307_TX_PIN, ML307_RX_PIN, 4096),
  206. boot_button_(BOOT_BUTTON_GPIO),
  207. volume_up_button_(VOLUME_UP_BUTTON_GPIO),
  208. volume_down_button_(VOLUME_DOWN_BUTTON_GPIO) {
  209. InitializeDisplayI2c();
  210. InitializeSsd1306Display();
  211. InitializeCodecI2c();
  212. pmic_ = new Pmic(codec_i2c_bus_, AXP2101_I2C_ADDR);
  213. Enable4GModule();
  214. InitializeButtons();
  215. InitializePowerSaveTimer();
  216. InitializeIot();
  217. }
  218. virtual Led* GetLed() override {
  219. static SingleLed led(BUILTIN_LED_GPIO);
  220. return &led;
  221. }
  222. virtual AudioCodec* GetAudioCodec() override {
  223. static BoxAudioCodec audio_codec(codec_i2c_bus_, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  224. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  225. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR, AUDIO_CODEC_ES7210_ADDR, AUDIO_INPUT_REFERENCE);
  226. return &audio_codec;
  227. }
  228. virtual Display* GetDisplay() override {
  229. return display_;
  230. }
  231. virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging) override {
  232. static bool last_discharging = false;
  233. charging = pmic_->IsCharging();
  234. discharging = pmic_->IsDischarging();
  235. if (discharging != last_discharging) {
  236. power_save_timer_->SetEnabled(discharging);
  237. last_discharging = discharging;
  238. }
  239. level = pmic_->GetBatteryLevel();
  240. return true;
  241. }
  242. };
  243. DECLARE_BOARD(KevinBoxBoard);