kevin_box_board.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "config.h"
  7. #include "iot/thing_manager.h"
  8. #include "led/single_led.h"
  9. #include "assets/lang_config.h"
  10. #include <esp_log.h>
  11. #include <esp_spiffs.h>
  12. #include <driver/gpio.h>
  13. #include <driver/i2c_master.h>
  14. #include <esp_lcd_panel_ops.h>
  15. #include <esp_lcd_panel_vendor.h>
  16. #define TAG "KevinBoxBoard"
  17. LV_FONT_DECLARE(font_puhui_14_1);
  18. LV_FONT_DECLARE(font_awesome_14_1);
  19. class KevinBoxBoard : public Ml307Board {
  20. private:
  21. i2c_master_bus_handle_t display_i2c_bus_;
  22. i2c_master_bus_handle_t codec_i2c_bus_;
  23. esp_lcd_panel_io_handle_t panel_io_ = nullptr;
  24. esp_lcd_panel_handle_t panel_ = nullptr;
  25. Display* display_ = nullptr;
  26. Button boot_button_;
  27. Button volume_up_button_;
  28. Button volume_down_button_;
  29. void MountStorage() {
  30. // Mount the storage partition
  31. esp_vfs_spiffs_conf_t conf = {
  32. .base_path = "/storage",
  33. .partition_label = "storage",
  34. .max_files = 5,
  35. .format_if_mount_failed = true,
  36. };
  37. esp_vfs_spiffs_register(&conf);
  38. }
  39. void Enable4GModule() {
  40. // Make GPIO15 HIGH to enable the 4G module
  41. gpio_config_t ml307_enable_config = {
  42. .pin_bit_mask = (1ULL << 15) | (1ULL << 18),
  43. .mode = GPIO_MODE_OUTPUT,
  44. .pull_up_en = GPIO_PULLUP_DISABLE,
  45. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  46. .intr_type = GPIO_INTR_DISABLE,
  47. };
  48. gpio_config(&ml307_enable_config);
  49. gpio_set_level(GPIO_NUM_15, 1);
  50. gpio_set_level(GPIO_NUM_18, 1);
  51. }
  52. void InitializeDisplayI2c() {
  53. i2c_master_bus_config_t bus_config = {
  54. .i2c_port = (i2c_port_t)0,
  55. .sda_io_num = DISPLAY_SDA_PIN,
  56. .scl_io_num = DISPLAY_SCL_PIN,
  57. .clk_source = I2C_CLK_SRC_DEFAULT,
  58. .glitch_ignore_cnt = 7,
  59. .intr_priority = 0,
  60. .trans_queue_depth = 0,
  61. .flags = {
  62. .enable_internal_pullup = 1,
  63. },
  64. };
  65. ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &display_i2c_bus_));
  66. }
  67. void InitializeSsd1306Display() {
  68. // SSD1306 config
  69. esp_lcd_panel_io_i2c_config_t io_config = {
  70. .dev_addr = 0x3C,
  71. .on_color_trans_done = nullptr,
  72. .user_ctx = nullptr,
  73. .control_phase_bytes = 1,
  74. .dc_bit_offset = 6,
  75. .lcd_cmd_bits = 8,
  76. .lcd_param_bits = 8,
  77. .flags = {
  78. .dc_low_on_data = 0,
  79. .disable_control_phase = 0,
  80. },
  81. .scl_speed_hz = 400 * 1000,
  82. };
  83. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c_v2(display_i2c_bus_, &io_config, &panel_io_));
  84. ESP_LOGI(TAG, "Install SSD1306 driver");
  85. esp_lcd_panel_dev_config_t panel_config = {};
  86. panel_config.reset_gpio_num = -1;
  87. panel_config.bits_per_pixel = 1;
  88. esp_lcd_panel_ssd1306_config_t ssd1306_config = {
  89. .height = static_cast<uint8_t>(DISPLAY_HEIGHT),
  90. };
  91. panel_config.vendor_config = &ssd1306_config;
  92. ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(panel_io_, &panel_config, &panel_));
  93. ESP_LOGI(TAG, "SSD1306 driver installed");
  94. // Reset the display
  95. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
  96. if (esp_lcd_panel_init(panel_) != ESP_OK) {
  97. ESP_LOGE(TAG, "Failed to initialize display");
  98. display_ = new NoDisplay();
  99. return;
  100. }
  101. // Set the display to on
  102. ESP_LOGI(TAG, "Turning display on");
  103. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  104. display_ = new OledDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y,
  105. {&font_puhui_14_1, &font_awesome_14_1});
  106. }
  107. void InitializeCodecI2c() {
  108. // Initialize I2C peripheral
  109. i2c_master_bus_config_t i2c_bus_cfg = {
  110. .i2c_port = (i2c_port_t)1,
  111. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  112. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  113. .clk_source = I2C_CLK_SRC_DEFAULT,
  114. .glitch_ignore_cnt = 7,
  115. .intr_priority = 0,
  116. .trans_queue_depth = 0,
  117. .flags = {
  118. .enable_internal_pullup = 1,
  119. },
  120. };
  121. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  122. }
  123. void InitializeButtons() {
  124. boot_button_.OnPressDown([this]() {
  125. Application::GetInstance().StartListening();
  126. });
  127. boot_button_.OnPressUp([this]() {
  128. Application::GetInstance().StopListening();
  129. });
  130. volume_up_button_.OnClick([this]() {
  131. auto codec = GetAudioCodec();
  132. auto volume = codec->output_volume() + 10;
  133. if (volume > 100) {
  134. volume = 100;
  135. }
  136. codec->SetOutputVolume(volume);
  137. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  138. });
  139. volume_up_button_.OnLongPress([this]() {
  140. GetAudioCodec()->SetOutputVolume(100);
  141. GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
  142. });
  143. volume_down_button_.OnClick([this]() {
  144. auto codec = GetAudioCodec();
  145. auto volume = codec->output_volume() - 10;
  146. if (volume < 0) {
  147. volume = 0;
  148. }
  149. codec->SetOutputVolume(volume);
  150. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  151. });
  152. volume_down_button_.OnLongPress([this]() {
  153. GetAudioCodec()->SetOutputVolume(0);
  154. GetDisplay()->ShowNotification(Lang::Strings::MUTED);
  155. });
  156. }
  157. // 物联网初始化,添加对 AI 可见设备
  158. void InitializeIot() {
  159. auto& thing_manager = iot::ThingManager::GetInstance();
  160. thing_manager.AddThing(iot::CreateThing("Speaker"));
  161. }
  162. public:
  163. KevinBoxBoard() : Ml307Board(ML307_TX_PIN, ML307_RX_PIN, 4096),
  164. boot_button_(BOOT_BUTTON_GPIO),
  165. volume_up_button_(VOLUME_UP_BUTTON_GPIO),
  166. volume_down_button_(VOLUME_DOWN_BUTTON_GPIO) {
  167. InitializeDisplayI2c();
  168. InitializeSsd1306Display();
  169. InitializeCodecI2c();
  170. MountStorage();
  171. Enable4GModule();
  172. InitializeButtons();
  173. InitializeIot();
  174. }
  175. virtual Led* GetLed() override {
  176. static SingleLed led(BUILTIN_LED_GPIO);
  177. return &led;
  178. }
  179. virtual AudioCodec* GetAudioCodec() override {
  180. static BoxAudioCodec audio_codec(codec_i2c_bus_, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  181. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  182. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR, AUDIO_CODEC_ES7210_ADDR, AUDIO_INPUT_REFERENCE);
  183. return &audio_codec;
  184. }
  185. virtual Display* GetDisplay() override {
  186. return display_;
  187. }
  188. };
  189. DECLARE_BOARD(KevinBoxBoard);