movecall_cuican_esp32s3.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "MovecallCuicanESP32S3"
  18. LV_FONT_DECLARE(font_puhui_16_4);
  19. LV_FONT_DECLARE(font_awesome_16_4);
  20. class MovecallCuicanESP32S3 : public WifiBoard {
  21. private:
  22. i2c_master_bus_handle_t codec_i2c_bus_;
  23. Button boot_button_;
  24. Display* display_;
  25. void InitializeCodecI2c() {
  26. // Initialize I2C peripheral
  27. i2c_master_bus_config_t i2c_bus_cfg = {
  28. .i2c_port = I2C_NUM_0,
  29. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  30. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  31. .clk_source = I2C_CLK_SRC_DEFAULT,
  32. .glitch_ignore_cnt = 7,
  33. .intr_priority = 0,
  34. .trans_queue_depth = 0,
  35. .flags = {
  36. .enable_internal_pullup = 1,
  37. },
  38. };
  39. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  40. }
  41. // SPI初始化
  42. void InitializeSpi() {
  43. ESP_LOGI(TAG, "Initialize SPI bus");
  44. spi_bus_config_t buscfg = GC9A01_PANEL_BUS_SPI_CONFIG(DISPLAY_SPI_SCLK_PIN, DISPLAY_SPI_MOSI_PIN,
  45. DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t));
  46. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  47. }
  48. // GC9A01初始化
  49. void InitializeGc9a01Display() {
  50. ESP_LOGI(TAG, "Init GC9A01 display");
  51. ESP_LOGI(TAG, "Install panel IO");
  52. esp_lcd_panel_io_handle_t io_handle = NULL;
  53. esp_lcd_panel_io_spi_config_t io_config = GC9A01_PANEL_IO_SPI_CONFIG(DISPLAY_SPI_CS_PIN, DISPLAY_SPI_DC_PIN, NULL, NULL);
  54. io_config.pclk_hz = DISPLAY_SPI_SCLK_HZ;
  55. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &io_handle));
  56. ESP_LOGI(TAG, "Install GC9A01 panel driver");
  57. esp_lcd_panel_handle_t panel_handle = NULL;
  58. esp_lcd_panel_dev_config_t panel_config = {};
  59. panel_config.reset_gpio_num = DISPLAY_SPI_RESET_PIN; // Set to -1 if not use
  60. panel_config.rgb_endian = LCD_RGB_ENDIAN_BGR; //LCD_RGB_ENDIAN_RGB;
  61. panel_config.bits_per_pixel = 16; // Implemented by LCD command `3Ah` (16/18)
  62. ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
  63. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  64. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  65. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
  66. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false));
  67. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  68. display_ = new SpiLcdDisplay(io_handle, panel_handle,
  69. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  70. {
  71. .text_font = &font_puhui_16_4,
  72. .icon_font = &font_awesome_16_4,
  73. .emoji_font = font_emoji_64_init(),
  74. });
  75. }
  76. void InitializeButtons() {
  77. boot_button_.OnClick([this]() {
  78. auto& app = Application::GetInstance();
  79. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  80. ResetWifiConfiguration();
  81. }
  82. app.ToggleChatState();
  83. });
  84. }
  85. public:
  86. MovecallCuicanESP32S3() : boot_button_(BOOT_BUTTON_GPIO) {
  87. InitializeCodecI2c();
  88. InitializeSpi();
  89. InitializeGc9a01Display();
  90. InitializeButtons();
  91. GetBacklight()->RestoreBrightness();
  92. }
  93. virtual Led* GetLed() override {
  94. static SingleLed led_strip(BUILTIN_LED_GPIO);
  95. return &led_strip;
  96. }
  97. virtual Display* GetDisplay() override {
  98. return display_;
  99. }
  100. virtual Backlight* GetBacklight() override {
  101. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  102. return &backlight;
  103. }
  104. virtual AudioCodec* GetAudioCodec() override {
  105. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  106. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  107. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  108. return &audio_codec;
  109. }
  110. };
  111. DECLARE_BOARD(MovecallCuicanESP32S3);