kevin_box_board.cc 9.3 KB

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