|
| 1 | +#include "wifi_board.h" |
| 2 | +#include "audio_codecs/box_audio_codec.h" |
| 3 | +#include "application.h" |
| 4 | +#include "display/lcd_display.h" |
| 5 | +// #include "display/no_display.h" |
| 6 | +#include "button.h" |
| 7 | +#include "config.h" |
| 8 | +#include "iot/thing_manager.h" |
| 9 | + |
| 10 | +#include "esp_lcd_panel_ops.h" |
| 11 | +#include "esp_lcd_mipi_dsi.h" |
| 12 | +#include "esp_ldo_regulator.h" |
| 13 | + |
| 14 | +#include "esp_lcd_st7703.h" |
| 15 | + |
| 16 | +#include <wifi_station.h> |
| 17 | +#include <esp_log.h> |
| 18 | +#include <driver/i2c_master.h> |
| 19 | +#include <esp_lvgl_port.h> |
| 20 | +#include "esp_lcd_touch_gt911.h" |
| 21 | +#define TAG "WaveshareEsp32p44b" |
| 22 | + |
| 23 | +LV_FONT_DECLARE(font_puhui_30_4); |
| 24 | +LV_FONT_DECLARE(font_awesome_30_4); |
| 25 | + |
| 26 | +class WaveshareEsp32p44b : public WifiBoard { |
| 27 | +private: |
| 28 | + i2c_master_bus_handle_t i2c_bus_; |
| 29 | + Button boot_button_; |
| 30 | + LcdDisplay *display_; |
| 31 | + |
| 32 | + void InitializeCodecI2c() { |
| 33 | + // Initialize I2C peripheral |
| 34 | + i2c_master_bus_config_t i2c_bus_cfg = { |
| 35 | + .i2c_port = I2C_NUM_1, |
| 36 | + .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN, |
| 37 | + .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN, |
| 38 | + .clk_source = I2C_CLK_SRC_DEFAULT, |
| 39 | + .glitch_ignore_cnt = 7, |
| 40 | + .intr_priority = 0, |
| 41 | + .trans_queue_depth = 0, |
| 42 | + .flags = { |
| 43 | + .enable_internal_pullup = 1, |
| 44 | + }, |
| 45 | + }; |
| 46 | + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_)); |
| 47 | + } |
| 48 | + |
| 49 | + static esp_err_t bsp_enable_dsi_phy_power(void) { |
| 50 | +#if MIPI_DSI_PHY_PWR_LDO_CHAN > 0 |
| 51 | + // Turn on the power for MIPI DSI PHY, so it can go from "No Power" state to "Shutdown" state |
| 52 | + static esp_ldo_channel_handle_t phy_pwr_chan = NULL; |
| 53 | + esp_ldo_channel_config_t ldo_cfg = { |
| 54 | + .chan_id = MIPI_DSI_PHY_PWR_LDO_CHAN, |
| 55 | + .voltage_mv = MIPI_DSI_PHY_PWR_LDO_VOLTAGE_MV, |
| 56 | + }; |
| 57 | + esp_ldo_acquire_channel(&ldo_cfg, &phy_pwr_chan); |
| 58 | + ESP_LOGI(TAG, "MIPI DSI PHY Powered on"); |
| 59 | +#endif // BSP_MIPI_DSI_PHY_PWR_LDO_CHAN > 0 |
| 60 | + |
| 61 | + return ESP_OK; |
| 62 | + } |
| 63 | + |
| 64 | + void InitializeLCD() { |
| 65 | + bsp_enable_dsi_phy_power(); |
| 66 | + esp_lcd_panel_io_handle_t io = NULL; |
| 67 | + esp_lcd_panel_handle_t disp_panel = NULL; |
| 68 | + |
| 69 | + esp_lcd_dsi_bus_handle_t mipi_dsi_bus = NULL; |
| 70 | + esp_lcd_dsi_bus_config_t bus_config = ST7703_PANEL_BUS_DSI_2CH_CONFIG(); |
| 71 | + esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus); |
| 72 | + |
| 73 | + ESP_LOGI(TAG, "Install MIPI DSI LCD control panel"); |
| 74 | + // we use DBI interface to send LCD commands and parameters |
| 75 | + esp_lcd_dbi_io_config_t dbi_config = ST7703_PANEL_IO_DBI_CONFIG(); |
| 76 | + esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_config, &io); |
| 77 | + |
| 78 | + esp_lcd_dpi_panel_config_t dpi_config = { |
| 79 | + .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT, |
| 80 | + .dpi_clock_freq_mhz = 46, |
| 81 | + .pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565, |
| 82 | + .num_fbs = 1, |
| 83 | + .video_timing = { |
| 84 | + .h_size = 720, |
| 85 | + .v_size = 720, |
| 86 | + .hsync_pulse_width = 20, |
| 87 | + .hsync_back_porch = 80, |
| 88 | + .hsync_front_porch = 80, |
| 89 | + .vsync_pulse_width = 4, |
| 90 | + .vsync_back_porch = 12, |
| 91 | + .vsync_front_porch = 30, |
| 92 | + }, |
| 93 | + .flags = { |
| 94 | + .use_dma2d = true, |
| 95 | + }, |
| 96 | + }; |
| 97 | + st7703_vendor_config_t vendor_config = { |
| 98 | + |
| 99 | + .mipi_config = { |
| 100 | + .dsi_bus = mipi_dsi_bus, |
| 101 | + .dpi_config = &dpi_config, |
| 102 | + }, |
| 103 | + .flags = { |
| 104 | + .use_mipi_interface = 1, |
| 105 | + }, |
| 106 | + }; |
| 107 | + |
| 108 | + const esp_lcd_panel_dev_config_t lcd_dev_config = { |
| 109 | + .reset_gpio_num = PIN_NUM_LCD_RST, |
| 110 | + .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB, |
| 111 | + .bits_per_pixel = 16, |
| 112 | + .vendor_config = &vendor_config, |
| 113 | + }; |
| 114 | + esp_lcd_new_panel_st7703(io, &lcd_dev_config, &disp_panel); |
| 115 | + esp_lcd_panel_reset(disp_panel); |
| 116 | + esp_lcd_panel_init(disp_panel); |
| 117 | + |
| 118 | + display_ = new MipiLcdDisplay(io, disp_panel, DISPLAY_WIDTH, DISPLAY_HEIGHT, |
| 119 | + DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY, |
| 120 | + { |
| 121 | + .text_font = &font_puhui_30_4, |
| 122 | + .icon_font = &font_awesome_30_4, |
| 123 | + .emoji_font = font_emoji_64_init(), |
| 124 | + }); |
| 125 | + } |
| 126 | + void InitializeTouch() |
| 127 | + { |
| 128 | + esp_lcd_touch_handle_t tp; |
| 129 | + esp_lcd_touch_config_t tp_cfg = { |
| 130 | + .x_max = DISPLAY_WIDTH, |
| 131 | + .y_max = DISPLAY_HEIGHT, |
| 132 | + .rst_gpio_num = GPIO_NUM_23, |
| 133 | + .int_gpio_num = GPIO_NUM_NC, |
| 134 | + .levels = { |
| 135 | + .reset = 0, |
| 136 | + .interrupt = 0, |
| 137 | + }, |
| 138 | + .flags = { |
| 139 | + .swap_xy = 0, |
| 140 | + .mirror_x = 0, |
| 141 | + .mirror_y = 0, |
| 142 | + }, |
| 143 | + }; |
| 144 | + esp_lcd_panel_io_handle_t tp_io_handle = NULL; |
| 145 | + esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG(); |
| 146 | + tp_io_config.scl_speed_hz = 400 * 1000; |
| 147 | + ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle)); |
| 148 | + ESP_LOGI(TAG, "Initialize touch controller"); |
| 149 | + ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp)); |
| 150 | + const lvgl_port_touch_cfg_t touch_cfg = { |
| 151 | + .disp = lv_display_get_default(), |
| 152 | + .handle = tp, |
| 153 | + }; |
| 154 | + lvgl_port_add_touch(&touch_cfg); |
| 155 | + ESP_LOGI(TAG, "Touch panel initialized successfully"); |
| 156 | + } |
| 157 | + void InitializeButtons() { |
| 158 | + boot_button_.OnClick([this]() { |
| 159 | + auto& app = Application::GetInstance(); |
| 160 | + if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { |
| 161 | + ResetWifiConfiguration(); |
| 162 | + } |
| 163 | + app.ToggleChatState(); }); |
| 164 | + } |
| 165 | + |
| 166 | + // 物联网初始化,添加对 AI 可见设备 |
| 167 | + void InitializeIot() { |
| 168 | + auto &thing_manager = iot::ThingManager::GetInstance(); |
| 169 | + thing_manager.AddThing(iot::CreateThing("Speaker")); |
| 170 | + thing_manager.AddThing(iot::CreateThing("Screen")); |
| 171 | + } |
| 172 | + |
| 173 | +public: |
| 174 | + WaveshareEsp32p44b() : |
| 175 | + boot_button_(BOOT_BUTTON_GPIO) { |
| 176 | + InitializeCodecI2c(); |
| 177 | + InitializeIot(); |
| 178 | + InitializeLCD(); |
| 179 | + InitializeTouch(); |
| 180 | + InitializeButtons(); |
| 181 | + GetBacklight()->RestoreBrightness(); |
| 182 | + } |
| 183 | + |
| 184 | + virtual AudioCodec* GetAudioCodec() override { |
| 185 | + static BoxAudioCodec audio_codec( |
| 186 | + i2c_bus_, |
| 187 | + AUDIO_INPUT_SAMPLE_RATE, |
| 188 | + AUDIO_OUTPUT_SAMPLE_RATE, |
| 189 | + AUDIO_I2S_GPIO_MCLK, |
| 190 | + AUDIO_I2S_GPIO_BCLK, |
| 191 | + AUDIO_I2S_GPIO_WS, |
| 192 | + AUDIO_I2S_GPIO_DOUT, |
| 193 | + AUDIO_I2S_GPIO_DIN, |
| 194 | + AUDIO_CODEC_PA_PIN, |
| 195 | + AUDIO_CODEC_ES8311_ADDR, |
| 196 | + AUDIO_CODEC_ES7210_ADDR, |
| 197 | + AUDIO_INPUT_REFERENCE); |
| 198 | + return &audio_codec; |
| 199 | + } |
| 200 | + |
| 201 | + virtual Display *GetDisplay() override { |
| 202 | + return display_; |
| 203 | + } |
| 204 | + |
| 205 | + virtual Backlight* GetBacklight() override { |
| 206 | + static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT); |
| 207 | + return &backlight; |
| 208 | + } |
| 209 | +}; |
| 210 | + |
| 211 | +DECLARE_BOARD(WaveshareEsp32p44b); |
0 commit comments