|
| 1 | +#include "wifi_board.h" |
| 2 | +#include "audio_codecs/no_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 "mcp_server.h" |
| 9 | +#include "lamp_controller.h" |
| 10 | +#include "iot/thing_manager.h" |
| 11 | +#include "led/single_led.h" |
| 12 | +#include "esp32_camera.h" |
| 13 | + |
| 14 | +#include <wifi_station.h> |
| 15 | +#include <esp_log.h> |
| 16 | +#include <driver/i2c_master.h> |
| 17 | +#include <esp_lcd_panel_vendor.h> |
| 18 | +#include <esp_lcd_panel_io.h> |
| 19 | +#include <esp_lcd_panel_ops.h> |
| 20 | +#include <driver/spi_common.h> |
| 21 | + |
| 22 | +#if defined(LCD_TYPE_ILI9341_SERIAL) |
| 23 | +#include "esp_lcd_ili9341.h" |
| 24 | +#endif |
| 25 | + |
| 26 | +#if defined(LCD_TYPE_GC9A01_SERIAL) |
| 27 | +#include "esp_lcd_gc9a01.h" |
| 28 | +static const gc9a01_lcd_init_cmd_t gc9107_lcd_init_cmds[] = { |
| 29 | + // {cmd, { data }, data_size, delay_ms} |
| 30 | + {0xfe, (uint8_t[]){0x00}, 0, 0}, |
| 31 | + {0xef, (uint8_t[]){0x00}, 0, 0}, |
| 32 | + {0xb0, (uint8_t[]){0xc0}, 1, 0}, |
| 33 | + {0xb1, (uint8_t[]){0x80}, 1, 0}, |
| 34 | + {0xb2, (uint8_t[]){0x27}, 1, 0}, |
| 35 | + {0xb3, (uint8_t[]){0x13}, 1, 0}, |
| 36 | + {0xb6, (uint8_t[]){0x19}, 1, 0}, |
| 37 | + {0xb7, (uint8_t[]){0x05}, 1, 0}, |
| 38 | + {0xac, (uint8_t[]){0xc8}, 1, 0}, |
| 39 | + {0xab, (uint8_t[]){0x0f}, 1, 0}, |
| 40 | + {0x3a, (uint8_t[]){0x05}, 1, 0}, |
| 41 | + {0xb4, (uint8_t[]){0x04}, 1, 0}, |
| 42 | + {0xa8, (uint8_t[]){0x08}, 1, 0}, |
| 43 | + {0xb8, (uint8_t[]){0x08}, 1, 0}, |
| 44 | + {0xea, (uint8_t[]){0x02}, 1, 0}, |
| 45 | + {0xe8, (uint8_t[]){0x2A}, 1, 0}, |
| 46 | + {0xe9, (uint8_t[]){0x47}, 1, 0}, |
| 47 | + {0xe7, (uint8_t[]){0x5f}, 1, 0}, |
| 48 | + {0xc6, (uint8_t[]){0x21}, 1, 0}, |
| 49 | + {0xc7, (uint8_t[]){0x15}, 1, 0}, |
| 50 | + {0xf0, |
| 51 | + (uint8_t[]){0x1D, 0x38, 0x09, 0x4D, 0x92, 0x2F, 0x35, 0x52, 0x1E, 0x0C, |
| 52 | + 0x04, 0x12, 0x14, 0x1f}, |
| 53 | + 14, 0}, |
| 54 | + {0xf1, |
| 55 | + (uint8_t[]){0x16, 0x40, 0x1C, 0x54, 0xA9, 0x2D, 0x2E, 0x56, 0x10, 0x0D, |
| 56 | + 0x0C, 0x1A, 0x14, 0x1E}, |
| 57 | + 14, 0}, |
| 58 | + {0xf4, (uint8_t[]){0x00, 0x00, 0xFF}, 3, 0}, |
| 59 | + {0xba, (uint8_t[]){0xFF, 0xFF}, 2, 0}, |
| 60 | +}; |
| 61 | +#endif |
| 62 | + |
| 63 | +#define TAG "CompactWifiBoardS3Cam" |
| 64 | + |
| 65 | +LV_FONT_DECLARE(font_puhui_16_4); |
| 66 | +LV_FONT_DECLARE(font_awesome_16_4); |
| 67 | + |
| 68 | +class CompactWifiBoardS3Cam : public WifiBoard { |
| 69 | +private: |
| 70 | + |
| 71 | + Button boot_button_; |
| 72 | + LcdDisplay* display_; |
| 73 | + Esp32Camera* camera_; |
| 74 | + |
| 75 | + void InitializeSpi() { |
| 76 | + spi_bus_config_t buscfg = {}; |
| 77 | + buscfg.mosi_io_num = DISPLAY_MOSI_PIN; |
| 78 | + buscfg.miso_io_num = GPIO_NUM_NC; |
| 79 | + buscfg.sclk_io_num = DISPLAY_CLK_PIN; |
| 80 | + buscfg.quadwp_io_num = GPIO_NUM_NC; |
| 81 | + buscfg.quadhd_io_num = GPIO_NUM_NC; |
| 82 | + buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t); |
| 83 | + ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO)); |
| 84 | + } |
| 85 | + |
| 86 | + void InitializeLcdDisplay() { |
| 87 | + esp_lcd_panel_io_handle_t panel_io = nullptr; |
| 88 | + esp_lcd_panel_handle_t panel = nullptr; |
| 89 | + // 液晶屏控制IO初始化 |
| 90 | + ESP_LOGD(TAG, "Install panel IO"); |
| 91 | + esp_lcd_panel_io_spi_config_t io_config = {}; |
| 92 | + io_config.cs_gpio_num = DISPLAY_CS_PIN; |
| 93 | + io_config.dc_gpio_num = DISPLAY_DC_PIN; |
| 94 | + io_config.spi_mode = DISPLAY_SPI_MODE; |
| 95 | + io_config.pclk_hz = 40 * 1000 * 1000; |
| 96 | + io_config.trans_queue_depth = 10; |
| 97 | + io_config.lcd_cmd_bits = 8; |
| 98 | + io_config.lcd_param_bits = 8; |
| 99 | + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io)); |
| 100 | + |
| 101 | + // 初始化液晶屏驱动芯片 |
| 102 | + ESP_LOGD(TAG, "Install LCD driver"); |
| 103 | + esp_lcd_panel_dev_config_t panel_config = {}; |
| 104 | + panel_config.reset_gpio_num = DISPLAY_RST_PIN; |
| 105 | + panel_config.rgb_ele_order = DISPLAY_RGB_ORDER; |
| 106 | + panel_config.bits_per_pixel = 16; |
| 107 | +#if defined(LCD_TYPE_ILI9341_SERIAL) |
| 108 | + ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(panel_io, &panel_config, &panel)); |
| 109 | +#elif defined(LCD_TYPE_GC9A01_SERIAL) |
| 110 | + ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(panel_io, &panel_config, &panel)); |
| 111 | + gc9a01_vendor_config_t gc9107_vendor_config = { |
| 112 | + .init_cmds = gc9107_lcd_init_cmds, |
| 113 | + .init_cmds_size = sizeof(gc9107_lcd_init_cmds) / sizeof(gc9a01_lcd_init_cmd_t), |
| 114 | + }; |
| 115 | +#else |
| 116 | + ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel)); |
| 117 | +#endif |
| 118 | + |
| 119 | + esp_lcd_panel_reset(panel); |
| 120 | + |
| 121 | + |
| 122 | + esp_lcd_panel_init(panel); |
| 123 | + esp_lcd_panel_invert_color(panel, DISPLAY_INVERT_COLOR); |
| 124 | + esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY); |
| 125 | + esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); |
| 126 | +#ifdef LCD_TYPE_GC9A01_SERIAL |
| 127 | + panel_config.vendor_config = &gc9107_vendor_config; |
| 128 | +#endif |
| 129 | + display_ = new SpiLcdDisplay(panel_io, panel, |
| 130 | + DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY, |
| 131 | + { |
| 132 | + .text_font = &font_puhui_16_4, |
| 133 | + .icon_font = &font_awesome_16_4, |
| 134 | +#if CONFIG_USE_WECHAT_MESSAGE_STYLE |
| 135 | + .emoji_font = font_emoji_32_init(), |
| 136 | +#else |
| 137 | + .emoji_font = DISPLAY_HEIGHT >= 240 ? font_emoji_64_init() : font_emoji_32_init(), |
| 138 | +#endif |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + void InitializeCamera() { |
| 143 | + camera_config_t config = {}; |
| 144 | + config.pin_d0 = CAMERA_PIN_D0; |
| 145 | + config.pin_d1 = CAMERA_PIN_D1; |
| 146 | + config.pin_d2 = CAMERA_PIN_D2; |
| 147 | + config.pin_d3 = CAMERA_PIN_D3; |
| 148 | + config.pin_d4 = CAMERA_PIN_D4; |
| 149 | + config.pin_d5 = CAMERA_PIN_D5; |
| 150 | + config.pin_d6 = CAMERA_PIN_D6; |
| 151 | + config.pin_d7 = CAMERA_PIN_D7; |
| 152 | + config.pin_xclk = CAMERA_PIN_XCLK; |
| 153 | + config.pin_pclk = CAMERA_PIN_PCLK; |
| 154 | + config.pin_vsync = CAMERA_PIN_VSYNC; |
| 155 | + config.pin_href = CAMERA_PIN_HREF; |
| 156 | + config.pin_sccb_sda = CAMERA_PIN_SIOD; |
| 157 | + config.pin_sccb_scl = CAMERA_PIN_SIOC; |
| 158 | + config.sccb_i2c_port = 0; |
| 159 | + config.pin_pwdn = CAMERA_PIN_PWDN; |
| 160 | + config.pin_reset = CAMERA_PIN_RESET; |
| 161 | + config.xclk_freq_hz = XCLK_FREQ_HZ; |
| 162 | + config.pixel_format = PIXFORMAT_RGB565; |
| 163 | + config.frame_size = FRAMESIZE_QVGA; |
| 164 | + config.jpeg_quality = 12; |
| 165 | + config.fb_count = 1; |
| 166 | + config.fb_location = CAMERA_FB_IN_PSRAM; |
| 167 | + config.grab_mode = CAMERA_GRAB_WHEN_EMPTY; |
| 168 | + camera_ = new Esp32Camera(config); |
| 169 | + camera_->SetHMirror(false); |
| 170 | + } |
| 171 | + |
| 172 | + void InitializeButtons() { |
| 173 | + boot_button_.OnClick([this]() { |
| 174 | + auto& app = Application::GetInstance(); |
| 175 | + if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { |
| 176 | + ResetWifiConfiguration(); |
| 177 | + } |
| 178 | + app.ToggleChatState(); |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + // 物联网初始化,添加对 AI 可见设备 |
| 183 | + void InitializeIot() { |
| 184 | +#if CONFIG_IOT_PROTOCOL_XIAOZHI |
| 185 | + auto& thing_manager = iot::ThingManager::GetInstance(); |
| 186 | + thing_manager.AddThing(iot::CreateThing("Speaker")); |
| 187 | + thing_manager.AddThing(iot::CreateThing("Screen")); |
| 188 | +#elif CONFIG_IOT_PROTOCOL_MCP |
| 189 | + |
| 190 | +#endif |
| 191 | + } |
| 192 | + |
| 193 | +public: |
| 194 | + CompactWifiBoardS3Cam() : |
| 195 | + boot_button_(BOOT_BUTTON_GPIO) { |
| 196 | + InitializeSpi(); |
| 197 | + InitializeLcdDisplay(); |
| 198 | + InitializeButtons(); |
| 199 | + InitializeIot(); |
| 200 | + InitializeCamera(); |
| 201 | + if (DISPLAY_BACKLIGHT_PIN != GPIO_NUM_NC) { |
| 202 | + GetBacklight()->RestoreBrightness(); |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + virtual Led* GetLed() override { |
| 208 | + static SingleLed led(BUILTIN_LED_GPIO); |
| 209 | + return &led; |
| 210 | + } |
| 211 | + |
| 212 | + virtual AudioCodec* GetAudioCodec() override { |
| 213 | +#ifdef AUDIO_I2S_METHOD_SIMPLEX |
| 214 | + static NoAudioCodecSimplex audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, |
| 215 | + AUDIO_I2S_SPK_GPIO_BCLK, AUDIO_I2S_SPK_GPIO_LRCK, AUDIO_I2S_SPK_GPIO_DOUT, AUDIO_I2S_MIC_GPIO_SCK, AUDIO_I2S_MIC_GPIO_WS, AUDIO_I2S_MIC_GPIO_DIN); |
| 216 | +#else |
| 217 | + static NoAudioCodecDuplex audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, |
| 218 | + AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN); |
| 219 | +#endif |
| 220 | + return &audio_codec; |
| 221 | + } |
| 222 | + |
| 223 | + virtual Display* GetDisplay() override { |
| 224 | + return display_; |
| 225 | + } |
| 226 | + |
| 227 | + virtual Backlight* GetBacklight() override { |
| 228 | + if (DISPLAY_BACKLIGHT_PIN != GPIO_NUM_NC) { |
| 229 | + static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT); |
| 230 | + return &backlight; |
| 231 | + } |
| 232 | + return nullptr; |
| 233 | + } |
| 234 | + |
| 235 | + virtual Camera* GetCamera() override { |
| 236 | + return camera_; |
| 237 | + } |
| 238 | +}; |
| 239 | + |
| 240 | +DECLARE_BOARD(CompactWifiBoardS3Cam); |
0 commit comments