|
| 1 | +#include "wifi_board.h" |
| 2 | +#include "audio_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 "iot/thing_manager.h" |
| 8 | +#include "led/single_led.h" |
| 9 | + |
| 10 | +#include <wifi_station.h> |
| 11 | +#include <esp_log.h> |
| 12 | +#include <esp_efuse_table.h> |
| 13 | +#include <driver/i2c_master.h> |
| 14 | + |
| 15 | +#include <esp_lcd_panel_io.h> |
| 16 | +#include <esp_lcd_panel_ops.h> |
| 17 | +#include <esp_lcd_gc9a01.h> |
| 18 | + |
| 19 | +#include "driver/gpio.h" |
| 20 | +#include "driver/spi_master.h" |
| 21 | + |
| 22 | +#define TAG "MovecallCuicanESP32S3" |
| 23 | + |
| 24 | +LV_FONT_DECLARE(font_puhui_16_4); |
| 25 | +LV_FONT_DECLARE(font_awesome_16_4); |
| 26 | + |
| 27 | +class MovecallCuicanESP32S3 : public WifiBoard { |
| 28 | +private: |
| 29 | + i2c_master_bus_handle_t codec_i2c_bus_; |
| 30 | + Button boot_button_; |
| 31 | + Display* display_; |
| 32 | + |
| 33 | + void InitializeCodecI2c() { |
| 34 | + // Initialize I2C peripheral |
| 35 | + i2c_master_bus_config_t i2c_bus_cfg = { |
| 36 | + .i2c_port = I2C_NUM_0, |
| 37 | + .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN, |
| 38 | + .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN, |
| 39 | + .clk_source = I2C_CLK_SRC_DEFAULT, |
| 40 | + .glitch_ignore_cnt = 7, |
| 41 | + .intr_priority = 0, |
| 42 | + .trans_queue_depth = 0, |
| 43 | + .flags = { |
| 44 | + .enable_internal_pullup = 1, |
| 45 | + }, |
| 46 | + }; |
| 47 | + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_)); |
| 48 | + } |
| 49 | + |
| 50 | + // SPI初始化 |
| 51 | + void InitializeSpi() { |
| 52 | + ESP_LOGI(TAG, "Initialize SPI bus"); |
| 53 | + spi_bus_config_t buscfg = GC9A01_PANEL_BUS_SPI_CONFIG(DISPLAY_SPI_SCLK_PIN, DISPLAY_SPI_MOSI_PIN, |
| 54 | + DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t)); |
| 55 | + ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO)); |
| 56 | + } |
| 57 | + |
| 58 | + // GC9A01初始化 |
| 59 | + void InitializeGc9a01Display() { |
| 60 | + ESP_LOGI(TAG, "Init GC9A01 display"); |
| 61 | + |
| 62 | + ESP_LOGI(TAG, "Install panel IO"); |
| 63 | + esp_lcd_panel_io_handle_t io_handle = NULL; |
| 64 | + esp_lcd_panel_io_spi_config_t io_config = GC9A01_PANEL_IO_SPI_CONFIG(DISPLAY_SPI_CS_PIN, DISPLAY_SPI_DC_PIN, NULL, NULL); |
| 65 | + io_config.pclk_hz = DISPLAY_SPI_SCLK_HZ; |
| 66 | + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &io_handle)); |
| 67 | + |
| 68 | + ESP_LOGI(TAG, "Install GC9A01 panel driver"); |
| 69 | + esp_lcd_panel_handle_t panel_handle = NULL; |
| 70 | + esp_lcd_panel_dev_config_t panel_config = {}; |
| 71 | + panel_config.reset_gpio_num = DISPLAY_SPI_RESET_PIN; // Set to -1 if not use |
| 72 | + panel_config.rgb_endian = LCD_RGB_ENDIAN_BGR; //LCD_RGB_ENDIAN_RGB; |
| 73 | + panel_config.bits_per_pixel = 16; // Implemented by LCD command `3Ah` (16/18) |
| 74 | + |
| 75 | + ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle)); |
| 76 | + ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle)); |
| 77 | + ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle)); |
| 78 | + ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true)); |
| 79 | + ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false)); |
| 80 | + ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true)); |
| 81 | + |
| 82 | + display_ = new SpiLcdDisplay(io_handle, panel_handle, |
| 83 | + DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY, |
| 84 | + { |
| 85 | + .text_font = &font_puhui_16_4, |
| 86 | + .icon_font = &font_awesome_16_4, |
| 87 | + .emoji_font = font_emoji_64_init(), |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + void InitializeButtons() { |
| 92 | + boot_button_.OnClick([this]() { |
| 93 | + auto& app = Application::GetInstance(); |
| 94 | + if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { |
| 95 | + ResetWifiConfiguration(); |
| 96 | + } |
| 97 | + app.ToggleChatState(); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + // 物联网初始化,添加对 AI 可见设备 |
| 102 | + void InitializeIot() { |
| 103 | + auto& thing_manager = iot::ThingManager::GetInstance(); |
| 104 | + thing_manager.AddThing(iot::CreateThing("Speaker")); |
| 105 | + thing_manager.AddThing(iot::CreateThing("Screen")); |
| 106 | + } |
| 107 | + |
| 108 | +public: |
| 109 | + MovecallCuicanESP32S3() : boot_button_(BOOT_BUTTON_GPIO) { |
| 110 | + InitializeCodecI2c(); |
| 111 | + InitializeSpi(); |
| 112 | + InitializeGc9a01Display(); |
| 113 | + InitializeButtons(); |
| 114 | + InitializeIot(); |
| 115 | + GetBacklight()->RestoreBrightness(); |
| 116 | + } |
| 117 | + |
| 118 | + virtual Led* GetLed() override { |
| 119 | + static SingleLed led_strip(BUILTIN_LED_GPIO); |
| 120 | + return &led_strip; |
| 121 | + } |
| 122 | + |
| 123 | + virtual Display* GetDisplay() override { |
| 124 | + return display_; |
| 125 | + } |
| 126 | + |
| 127 | + virtual Backlight* GetBacklight() override { |
| 128 | + static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT); |
| 129 | + return &backlight; |
| 130 | + } |
| 131 | + |
| 132 | + virtual AudioCodec* GetAudioCodec() override { |
| 133 | + static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, |
| 134 | + AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN, |
| 135 | + AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR); |
| 136 | + return &audio_codec; |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +DECLARE_BOARD(MovecallCuicanESP32S3); |
0 commit comments