|
| 1 | +#include "wifi_board.h" |
| 2 | +#include "display/lcd_display.h" |
| 3 | +#include "codecs/box_audio_codec.h" |
| 4 | +#include "application.h" |
| 5 | +#include "button.h" |
| 6 | +#include "led/single_led.h" |
| 7 | +#include "mcp_server.h" |
| 8 | +#include "config.h" |
| 9 | +#include "power_save_timer.h" |
| 10 | +#include "axp2101.h" |
| 11 | +#include "i2c_device.h" |
| 12 | +#include <wifi_station.h> |
| 13 | + |
| 14 | +#include <esp_log.h> |
| 15 | +#include <esp_lcd_panel_vendor.h> |
| 16 | +#include <driver/i2c_master.h> |
| 17 | +#include <driver/spi_master.h> |
| 18 | +#include "settings.h" |
| 19 | + |
| 20 | +#include <esp_lcd_touch_cst816s.h> |
| 21 | +#include <esp_lvgl_port.h> |
| 22 | +#include <lvgl.h> |
| 23 | + |
| 24 | +#define TAG "WaveshareEsp32s3TouchLCD1inch83" |
| 25 | + |
| 26 | +class Pmic : public Axp2101 { |
| 27 | +public: |
| 28 | + Pmic(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : Axp2101(i2c_bus, addr) { |
| 29 | + WriteReg(0x22, 0b110); // PWRON > OFFLEVEL as POWEROFF Source enable |
| 30 | + WriteReg(0x27, 0x10); // hold 4s to power off |
| 31 | + |
| 32 | + // Disable All DCs but DC1 |
| 33 | + WriteReg(0x80, 0x01); |
| 34 | + // Disable All LDOs |
| 35 | + WriteReg(0x90, 0x00); |
| 36 | + WriteReg(0x91, 0x00); |
| 37 | + |
| 38 | + // Set DC1 to 3.3V |
| 39 | + WriteReg(0x82, (3300 - 1500) / 100); |
| 40 | + |
| 41 | + // Set ALDO1 to 3.3V |
| 42 | + WriteReg(0x92, (3300 - 500) / 100); |
| 43 | + |
| 44 | + // Enable ALDO1(MIC) |
| 45 | + WriteReg(0x90, 0x01); |
| 46 | + |
| 47 | + WriteReg(0x64, 0x02); // CV charger voltage setting to 4.1V |
| 48 | + |
| 49 | + WriteReg(0x61, 0x02); // set Main battery precharge current to 50mA |
| 50 | + WriteReg(0x62, 0x08); // set Main battery charger current to 400mA ( 0x08-200mA, 0x09-300mA, 0x0A-400mA ) |
| 51 | + WriteReg(0x63, 0x01); // set Main battery term charge current to 25mA |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +class WaveshareEsp32s3TouchLCD1inch83 : public WifiBoard { |
| 56 | +private: |
| 57 | + i2c_master_bus_handle_t i2c_bus_; |
| 58 | + Pmic* pmic_ = nullptr; |
| 59 | + Button boot_button_; |
| 60 | + Display* display_; |
| 61 | + PowerSaveTimer* power_save_timer_; |
| 62 | + |
| 63 | + void InitializePowerSaveTimer() { |
| 64 | + power_save_timer_ = new PowerSaveTimer(-1, 60, 300); |
| 65 | + power_save_timer_->OnEnterSleepMode([this]() { |
| 66 | + GetDisplay()->SetPowerSaveMode(true); |
| 67 | + GetBacklight()->SetBrightness(20); }); |
| 68 | + power_save_timer_->OnExitSleepMode([this]() { |
| 69 | + GetDisplay()->SetPowerSaveMode(false); |
| 70 | + GetBacklight()->RestoreBrightness(); }); |
| 71 | + power_save_timer_->OnShutdownRequest([this](){ |
| 72 | + pmic_->PowerOff(); }); |
| 73 | + power_save_timer_->SetEnabled(true); |
| 74 | + } |
| 75 | + |
| 76 | + void InitializeCodecI2c() { |
| 77 | + // Initialize I2C peripheral |
| 78 | + i2c_master_bus_config_t i2c_bus_cfg = { |
| 79 | + .i2c_port = I2C_NUM_0, |
| 80 | + .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN, |
| 81 | + .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN, |
| 82 | + .clk_source = I2C_CLK_SRC_DEFAULT, |
| 83 | + .flags = { |
| 84 | + .enable_internal_pullup = 1, |
| 85 | + }, |
| 86 | + }; |
| 87 | + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_)); |
| 88 | + } |
| 89 | + |
| 90 | + void InitializeAxp2101() { |
| 91 | + ESP_LOGI(TAG, "Init AXP2101"); |
| 92 | + pmic_ = new Pmic(i2c_bus_, 0x34); |
| 93 | + } |
| 94 | + |
| 95 | + void InitializeSpi() { |
| 96 | + spi_bus_config_t buscfg = {}; |
| 97 | + buscfg.mosi_io_num = DISPLAY_MOSI_PIN; |
| 98 | + buscfg.miso_io_num = GPIO_NUM_NC; |
| 99 | + buscfg.sclk_io_num = DISPLAY_CLK_PIN; |
| 100 | + buscfg.quadwp_io_num = GPIO_NUM_NC; |
| 101 | + buscfg.quadhd_io_num = GPIO_NUM_NC; |
| 102 | + buscfg.max_transfer_sz = DISPLAY_WIDTH* DISPLAY_HEIGHT* sizeof(uint16_t); |
| 103 | + ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO)); |
| 104 | + } |
| 105 | + |
| 106 | + void InitializeButtons() { |
| 107 | + boot_button_.OnClick([this]() { |
| 108 | + auto& app = Application::GetInstance(); |
| 109 | + if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { |
| 110 | + ResetWifiConfiguration(); |
| 111 | + } |
| 112 | + app.ToggleChatState(); |
| 113 | + }); |
| 114 | + |
| 115 | +#if CONFIG_USE_DEVICE_AEC |
| 116 | + boot_button_.OnDoubleClick([this]() { |
| 117 | + auto& app = Application::GetInstance(); |
| 118 | + if (app.GetDeviceState() == kDeviceStateIdle) { |
| 119 | + app.SetAecMode(app.GetAecMode() == kAecOff ? kAecOnDeviceSide : kAecOff); |
| 120 | + } |
| 121 | + }); |
| 122 | +#endif |
| 123 | + } |
| 124 | + |
| 125 | + void InitializeDisplay() { |
| 126 | + esp_lcd_panel_io_handle_t panel_io = nullptr; |
| 127 | + esp_lcd_panel_handle_t panel = nullptr; |
| 128 | + |
| 129 | + // 液晶屏控制IO初始化 |
| 130 | + ESP_LOGD(TAG, "Install panel IO"); |
| 131 | + esp_lcd_panel_io_spi_config_t io_config = {}; |
| 132 | + io_config.cs_gpio_num = DISPLAY_CS_PIN; |
| 133 | + io_config.dc_gpio_num = DISPLAY_DC_PIN; |
| 134 | + io_config.spi_mode = 0; |
| 135 | + io_config.pclk_hz = 24 * 1000 * 1000; |
| 136 | + io_config.trans_queue_depth = 10; |
| 137 | + io_config.lcd_cmd_bits = 8; |
| 138 | + io_config.lcd_param_bits = 8; |
| 139 | + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io)); |
| 140 | + |
| 141 | + // 初始化液晶屏驱动芯片 |
| 142 | + ESP_LOGD(TAG, "Install LCD driver"); |
| 143 | + esp_lcd_panel_dev_config_t panel_config = {}; |
| 144 | + panel_config.reset_gpio_num = DISPLAY_RST_PIN; |
| 145 | + panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB; |
| 146 | + panel_config.bits_per_pixel = 16; |
| 147 | + ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel)); |
| 148 | + esp_lcd_panel_reset(panel); |
| 149 | + esp_lcd_panel_init(panel); |
| 150 | + esp_lcd_panel_invert_color(panel, true); |
| 151 | + // esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); |
| 152 | + esp_lcd_panel_disp_on_off(panel, true); |
| 153 | + display_ = new SpiLcdDisplay(panel_io, panel, |
| 154 | + DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY); |
| 155 | + } |
| 156 | + |
| 157 | + void InitializeTouch() { |
| 158 | + esp_lcd_touch_handle_t tp; |
| 159 | + esp_lcd_touch_config_t tp_cfg = { |
| 160 | + .x_max = DISPLAY_WIDTH - 1, |
| 161 | + .y_max = DISPLAY_HEIGHT - 1, |
| 162 | + .rst_gpio_num = GPIO_NUM_39, |
| 163 | + .int_gpio_num = GPIO_NUM_13, |
| 164 | + .levels = { |
| 165 | + .reset = 0, |
| 166 | + .interrupt = 0, |
| 167 | + }, |
| 168 | + .flags = { |
| 169 | + .swap_xy = 0, |
| 170 | + .mirror_x = 0, |
| 171 | + .mirror_y = 0, |
| 172 | + }, |
| 173 | + }; |
| 174 | + esp_lcd_panel_io_handle_t tp_io_handle = NULL; |
| 175 | + esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_CST816S_CONFIG(); |
| 176 | + tp_io_config.scl_speed_hz = 400* 1000; |
| 177 | + ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle)); |
| 178 | + ESP_LOGI(TAG, "Initialize touch controller"); |
| 179 | + ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_cst816s(tp_io_handle, &tp_cfg, &tp)); |
| 180 | + const lvgl_port_touch_cfg_t touch_cfg = { |
| 181 | + .disp = lv_display_get_default(), |
| 182 | + .handle = tp, |
| 183 | + }; |
| 184 | + lvgl_port_add_touch(&touch_cfg); |
| 185 | + ESP_LOGI(TAG, "Touch panel initialized successfully"); |
| 186 | + } |
| 187 | + |
| 188 | + // 初始化工具 |
| 189 | + void InitializeTools() { |
| 190 | + auto &mcp_server = McpServer::GetInstance(); |
| 191 | + mcp_server.AddTool("self.system.reconfigure_wifi", |
| 192 | + "Reboot the device and enter WiFi configuration mode.\n" |
| 193 | + "**CAUTION** You must ask the user to confirm this action.", |
| 194 | + PropertyList(), [this](const PropertyList& properties) { |
| 195 | + ResetWifiConfiguration(); |
| 196 | + return true; |
| 197 | + }); |
| 198 | + } |
| 199 | + |
| 200 | +public: |
| 201 | + WaveshareEsp32s3TouchLCD1inch83() : boot_button_(BOOT_BUTTON_GPIO) { |
| 202 | + InitializePowerSaveTimer(); |
| 203 | + InitializeCodecI2c(); |
| 204 | + InitializeAxp2101(); |
| 205 | + InitializeSpi(); |
| 206 | + InitializeDisplay(); |
| 207 | + InitializeTouch(); |
| 208 | + InitializeButtons(); |
| 209 | + InitializeTools(); |
| 210 | + GetBacklight()->RestoreBrightness(); |
| 211 | + } |
| 212 | + |
| 213 | + virtual AudioCodec* GetAudioCodec() override { |
| 214 | + static BoxAudioCodec audio_codec( |
| 215 | + i2c_bus_, |
| 216 | + AUDIO_INPUT_SAMPLE_RATE, |
| 217 | + AUDIO_OUTPUT_SAMPLE_RATE, |
| 218 | + AUDIO_I2S_GPIO_MCLK, |
| 219 | + AUDIO_I2S_GPIO_BCLK, |
| 220 | + AUDIO_I2S_GPIO_WS, |
| 221 | + AUDIO_I2S_GPIO_DOUT, |
| 222 | + AUDIO_I2S_GPIO_DIN, |
| 223 | + AUDIO_CODEC_PA_PIN, |
| 224 | + AUDIO_CODEC_ES8311_ADDR, |
| 225 | + AUDIO_CODEC_ES7210_ADDR, |
| 226 | + AUDIO_INPUT_REFERENCE); |
| 227 | + return &audio_codec; |
| 228 | + } |
| 229 | + |
| 230 | + virtual Display* GetDisplay() override { |
| 231 | + return display_; |
| 232 | + } |
| 233 | + |
| 234 | + virtual Backlight* GetBacklight() override { |
| 235 | + static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT); |
| 236 | + return &backlight; |
| 237 | + } |
| 238 | + |
| 239 | + virtual bool GetBatteryLevel(int &level, bool &charging, bool &discharging) override { |
| 240 | + static bool last_discharging = false; |
| 241 | + charging = pmic_->IsCharging(); |
| 242 | + discharging = pmic_->IsDischarging(); |
| 243 | + if (discharging != last_discharging) |
| 244 | + { |
| 245 | + power_save_timer_->SetEnabled(discharging); |
| 246 | + last_discharging = discharging; |
| 247 | + } |
| 248 | + |
| 249 | + level = pmic_->GetBatteryLevel(); |
| 250 | + return true; |
| 251 | + } |
| 252 | + |
| 253 | + virtual void SetPowerSaveMode(bool enabled) override { |
| 254 | + if (!enabled) |
| 255 | + { |
| 256 | + power_save_timer_->WakeUp(); |
| 257 | + } |
| 258 | + WifiBoard::SetPowerSaveMode(enabled); |
| 259 | + } |
| 260 | +}; |
| 261 | + |
| 262 | +DECLARE_BOARD(WaveshareEsp32s3TouchLCD1inch83); |
0 commit comments