movecall_moji_esp32s3.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "wifi_board.h"
  2. #include "codecs/es8311_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "config.h"
  7. #include "led/single_led.h"
  8. #include <wifi_station.h>
  9. #include <esp_log.h>
  10. #include <esp_efuse_table.h>
  11. #include <driver/i2c_master.h>
  12. #include <esp_lcd_panel_io.h>
  13. #include <esp_lcd_panel_ops.h>
  14. #include <esp_lcd_gc9a01.h>
  15. #include "driver/gpio.h"
  16. #include "driver/spi_master.h"
  17. #define TAG "MovecallMojiESP32S3"
  18. LV_FONT_DECLARE(font_puhui_20_4);
  19. LV_FONT_DECLARE(font_awesome_20_4);
  20. class CustomLcdDisplay : public SpiLcdDisplay {
  21. public:
  22. CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
  23. esp_lcd_panel_handle_t panel_handle,
  24. int width,
  25. int height,
  26. int offset_x,
  27. int offset_y,
  28. bool mirror_x,
  29. bool mirror_y,
  30. bool swap_xy)
  31. : SpiLcdDisplay(io_handle, panel_handle, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  32. {
  33. .text_font = &font_puhui_20_4,
  34. .icon_font = &font_awesome_20_4,
  35. .emoji_font = font_emoji_64_init(),
  36. }) {
  37. DisplayLockGuard lock(this);
  38. // 由于屏幕是圆的,所以状态栏需要增加左右内边距
  39. lv_obj_set_style_pad_left(status_bar_, LV_HOR_RES * 0.33, 0);
  40. lv_obj_set_style_pad_right(status_bar_, LV_HOR_RES * 0.33, 0);
  41. }
  42. };
  43. class MovecallMojiESP32S3 : public WifiBoard {
  44. private:
  45. i2c_master_bus_handle_t codec_i2c_bus_;
  46. Button boot_button_;
  47. Display* display_;
  48. void InitializeCodecI2c() {
  49. // Initialize I2C peripheral
  50. i2c_master_bus_config_t i2c_bus_cfg = {
  51. .i2c_port = I2C_NUM_0,
  52. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  53. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  54. .clk_source = I2C_CLK_SRC_DEFAULT,
  55. .glitch_ignore_cnt = 7,
  56. .intr_priority = 0,
  57. .trans_queue_depth = 0,
  58. .flags = {
  59. .enable_internal_pullup = 1,
  60. },
  61. };
  62. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  63. }
  64. // SPI初始化
  65. void InitializeSpi() {
  66. ESP_LOGI(TAG, "Initialize SPI bus");
  67. spi_bus_config_t buscfg = GC9A01_PANEL_BUS_SPI_CONFIG(DISPLAY_SPI_SCLK_PIN, DISPLAY_SPI_MOSI_PIN,
  68. DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t));
  69. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  70. }
  71. // GC9A01初始化
  72. void InitializeGc9a01Display() {
  73. ESP_LOGI(TAG, "Init GC9A01 display");
  74. ESP_LOGI(TAG, "Install panel IO");
  75. esp_lcd_panel_io_handle_t io_handle = NULL;
  76. esp_lcd_panel_io_spi_config_t io_config = GC9A01_PANEL_IO_SPI_CONFIG(DISPLAY_SPI_CS_PIN, DISPLAY_SPI_DC_PIN, NULL, NULL);
  77. io_config.pclk_hz = DISPLAY_SPI_SCLK_HZ;
  78. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &io_handle));
  79. ESP_LOGI(TAG, "Install GC9A01 panel driver");
  80. esp_lcd_panel_handle_t panel_handle = NULL;
  81. esp_lcd_panel_dev_config_t panel_config = {};
  82. panel_config.reset_gpio_num = DISPLAY_SPI_RESET_PIN; // Set to -1 if not use
  83. panel_config.rgb_endian = LCD_RGB_ENDIAN_BGR; //LCD_RGB_ENDIAN_RGB;
  84. panel_config.bits_per_pixel = 16; // Implemented by LCD command `3Ah` (16/18)
  85. ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
  86. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  87. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  88. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
  89. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false));
  90. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  91. display_ = new SpiLcdDisplay(io_handle, panel_handle,
  92. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  93. {
  94. .text_font = &font_puhui_20_4,
  95. .icon_font = &font_awesome_20_4,
  96. .emoji_font = font_emoji_64_init(),
  97. });
  98. }
  99. void InitializeButtons() {
  100. boot_button_.OnClick([this]() {
  101. auto& app = Application::GetInstance();
  102. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  103. ResetWifiConfiguration();
  104. }
  105. app.ToggleChatState();
  106. });
  107. }
  108. public:
  109. MovecallMojiESP32S3() : boot_button_(BOOT_BUTTON_GPIO) {
  110. InitializeCodecI2c();
  111. InitializeSpi();
  112. InitializeGc9a01Display();
  113. InitializeButtons();
  114. GetBacklight()->RestoreBrightness();
  115. }
  116. virtual Led* GetLed() override {
  117. static SingleLed led_strip(BUILTIN_LED_GPIO);
  118. return &led_strip;
  119. }
  120. virtual Display* GetDisplay() override {
  121. return display_;
  122. }
  123. virtual Backlight* GetBacklight() override {
  124. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  125. return &backlight;
  126. }
  127. virtual AudioCodec* GetAudioCodec() override {
  128. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  129. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  130. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  131. return &audio_codec;
  132. }
  133. };
  134. DECLARE_BOARD(MovecallMojiESP32S3);