atk_dnesp32s3m.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "ml307_board.h"
  2. #include "codecs/es8388_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "system_reset.h"
  5. #include "application.h"
  6. #include "button.h"
  7. #include "config.h"
  8. #include "i2c_device.h"
  9. #include "led/single_led.h"
  10. #include "driver/gpio.h"
  11. #include "assets/lang_config.h"
  12. #include <esp_log.h>
  13. #include <esp_lcd_panel_vendor.h>
  14. #include <driver/i2c_master.h>
  15. #include <esp_lcd_panel_vendor.h>
  16. #include <esp_lcd_panel_io.h>
  17. #include <esp_lcd_panel_ops.h>
  18. #include <driver/spi_common.h>
  19. #define TAG "atk_dnesp32s3m_4g"
  20. LV_FONT_DECLARE(font_puhui_16_4);
  21. LV_FONT_DECLARE(font_awesome_16_4);
  22. class atk_dnesp32s3m_4g : public Ml307Board {
  23. private:
  24. i2c_master_bus_handle_t i2c_bus_;
  25. Button boot_button_;
  26. Button volume_up_button_;
  27. Button volume_down_button_;
  28. Button phone_button_;
  29. LcdDisplay* display_;
  30. void InitializeI2c() {
  31. // Initialize I2C peripheral
  32. i2c_master_bus_config_t i2c_bus_cfg = {
  33. .i2c_port = (i2c_port_t)I2C_NUM_0,
  34. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  35. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  36. .clk_source = I2C_CLK_SRC_DEFAULT,
  37. .glitch_ignore_cnt = 7,
  38. .intr_priority = 0,
  39. .trans_queue_depth = 0,
  40. .flags = {
  41. .enable_internal_pullup = 1,
  42. },
  43. };
  44. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  45. }
  46. // Initialize spi peripheral
  47. void InitializeSpi() {
  48. spi_bus_config_t buscfg = {};
  49. buscfg.mosi_io_num = LCD_MOSI_PIN;
  50. buscfg.miso_io_num = GPIO_NUM_NC;
  51. buscfg.sclk_io_num = LCD_SCLK_PIN;
  52. buscfg.quadwp_io_num = GPIO_NUM_NC;
  53. buscfg.quadhd_io_num = GPIO_NUM_NC;
  54. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  55. ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
  56. }
  57. void InitializeButtons() {
  58. boot_button_.OnClick([this]() {
  59. Application::GetInstance().ToggleChatState();
  60. });
  61. volume_up_button_.OnClick([this]() {
  62. auto codec = GetAudioCodec();
  63. auto volume = codec->output_volume() + 10;
  64. if (volume > 100) {
  65. volume = 100;
  66. }
  67. codec->SetOutputVolume(volume);
  68. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  69. });
  70. volume_up_button_.OnLongPress([this]() {
  71. GetAudioCodec()->SetOutputVolume(100);
  72. GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
  73. });
  74. volume_down_button_.OnClick([this]() {
  75. auto codec = GetAudioCodec();
  76. auto volume = codec->output_volume() - 10;
  77. if (volume < 0) {
  78. volume = 0;
  79. }
  80. codec->SetOutputVolume(volume);
  81. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  82. });
  83. volume_down_button_.OnLongPress([this]() {
  84. GetAudioCodec()->SetOutputVolume(0);
  85. GetDisplay()->ShowNotification(Lang::Strings::MUTED);
  86. });
  87. //不插耳机
  88. phone_button_.OnPressDown([this]() {
  89. gpio_set_level(SPK_EN_PIN, 1);
  90. });
  91. //插入耳机
  92. phone_button_.OnPressUp([this]() {
  93. gpio_set_level(SPK_EN_PIN, 0);
  94. });
  95. }
  96. void InitializeSt7735Display() {
  97. esp_lcd_panel_io_handle_t panel_io = nullptr;
  98. esp_lcd_panel_handle_t panel = nullptr;
  99. // 液晶屏控制IO初始化
  100. ESP_LOGD(TAG, "Install panel IO");
  101. esp_lcd_panel_io_spi_config_t io_config = {};
  102. io_config.cs_gpio_num = LCD_CS_PIN;
  103. io_config.dc_gpio_num = LCD_DC_PIN;
  104. io_config.spi_mode = 0;
  105. io_config.pclk_hz = 60 * 1000 * 1000;
  106. io_config.trans_queue_depth = 7;
  107. io_config.lcd_cmd_bits = 8;
  108. io_config.lcd_param_bits = 8;
  109. esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io);
  110. // 初始化液晶屏驱动芯片ST7735
  111. ESP_LOGD(TAG, "Install LCD driver");
  112. esp_lcd_panel_dev_config_t panel_config = {};
  113. panel_config.reset_gpio_num = LCD_RST_PIN;
  114. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR;
  115. panel_config.bits_per_pixel = 16;
  116. panel_config.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
  117. esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel);
  118. //使能功放引脚
  119. gpio_config_t io_conf;
  120. io_conf.intr_type = GPIO_INTR_DISABLE;
  121. io_conf.mode = GPIO_MODE_OUTPUT;
  122. io_conf.pin_bit_mask = (1ULL << SPK_EN_PIN);
  123. io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
  124. io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
  125. gpio_config(&io_conf);
  126. gpio_set_level(SPK_EN_PIN, 0);
  127. //检测耳机是否插入,插入时为高电平
  128. io_conf.intr_type = GPIO_INTR_DISABLE;
  129. io_conf.mode = GPIO_MODE_INPUT;
  130. io_conf.pin_bit_mask = (1ULL << PHONE_CK_PIN);
  131. io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
  132. io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
  133. gpio_config(&io_conf);
  134. //耳机插入
  135. if (gpio_get_level(PHONE_CK_PIN)) {
  136. gpio_set_level(SPK_EN_PIN, 1);
  137. }
  138. esp_lcd_panel_reset(panel);
  139. esp_lcd_panel_init(panel);
  140. uint8_t data0[] = {0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10};
  141. uint8_t data1[] = {0x03, 0x1d, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D, 0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x10};
  142. esp_lcd_panel_io_tx_param(panel_io, 0xe0, data0, 16);
  143. esp_lcd_panel_io_tx_param(panel_io, 0xe1, data1, 16);
  144. esp_lcd_panel_invert_color(panel, true);
  145. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  146. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  147. display_ = new SpiLcdDisplay(panel_io, panel,
  148. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  149. {
  150. .text_font = &font_puhui_16_4,
  151. .icon_font = &font_awesome_16_4,
  152. .emoji_font = font_emoji_32_init(),
  153. });
  154. }
  155. public:
  156. atk_dnesp32s3m_4g() : Ml307Board(Module_4G_TX_PIN, Module_4G_RX_PIN),
  157. boot_button_(BOOT_BUTTON_GPIO),
  158. volume_up_button_(VOLUME_UP_BUTTON_GPIO),
  159. volume_down_button_(VOLUME_DOWN_BUTTON_GPIO),
  160. phone_button_(PHONE_CK_PIN, true) {
  161. InitializeI2c();
  162. InitializeSpi();
  163. InitializeSt7735Display();
  164. InitializeButtons();
  165. if (DISPLAY_BACKLIGHT_PIN != GPIO_NUM_NC) {
  166. GetBacklight()->RestoreBrightness();
  167. }
  168. }
  169. virtual Led* GetLed() override {
  170. static SingleLed led(BUILTIN_LED_GPIO);
  171. return &led;
  172. }
  173. virtual AudioCodec* GetAudioCodec() override {
  174. static Es8388AudioCodec audio_codec(
  175. i2c_bus_,
  176. I2C_NUM_0,
  177. AUDIO_INPUT_SAMPLE_RATE,
  178. AUDIO_OUTPUT_SAMPLE_RATE,
  179. AUDIO_I2S_GPIO_MCLK,
  180. AUDIO_I2S_GPIO_BCLK,
  181. AUDIO_I2S_GPIO_WS,
  182. AUDIO_I2S_GPIO_DOUT,
  183. AUDIO_I2S_GPIO_DIN,
  184. GPIO_NUM_NC,
  185. AUDIO_CODEC_ES8388_ADDR
  186. );
  187. return &audio_codec;
  188. }
  189. virtual Display* GetDisplay() override {
  190. return display_;
  191. }
  192. virtual Backlight* GetBacklight() override {
  193. if (DISPLAY_BACKLIGHT_PIN != GPIO_NUM_NC) {
  194. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  195. return &backlight;
  196. }
  197. return nullptr;
  198. }
  199. };
  200. DECLARE_BOARD(atk_dnesp32s3m_4g);