kevin_box_board.cc 6.7 KB

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