You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
11 KiB
296 lines
11 KiB
3 months ago
|
#include "wifi_board.h"
|
||
|
#include "audio_codecs/es8311_audio_codec.h"
|
||
|
#include "display/lcd_display.h"
|
||
|
#include "system_reset.h"
|
||
|
#include "application.h"
|
||
|
#include "button.h"
|
||
|
#include "config.h"
|
||
|
#include "iot/thing_manager.h"
|
||
|
|
||
|
|
||
|
#include <esp_log.h>
|
||
|
#include "i2c_device.h"
|
||
|
#include <driver/i2c_master.h>
|
||
|
#include <driver/ledc.h>
|
||
|
#include <wifi_station.h>
|
||
|
#include <esp_lcd_panel_vendor.h>
|
||
|
#include <esp_lcd_panel_io.h>
|
||
|
#include <esp_lcd_panel_ops.h>
|
||
|
|
||
|
#include <esp_timer.h>
|
||
|
#include "esp_io_expander_tca9554.h"
|
||
|
|
||
|
#include "axp2101.h"
|
||
|
#include "power_save_timer.h"
|
||
|
|
||
|
|
||
|
#define TAG "waveshare_lcd_3_5"
|
||
|
|
||
|
|
||
|
LV_FONT_DECLARE(font_puhui_16_4);
|
||
|
LV_FONT_DECLARE(font_awesome_16_4);
|
||
|
|
||
|
|
||
|
class Pmic : public Axp2101 {
|
||
|
public:
|
||
|
Pmic(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : Axp2101(i2c_bus, addr) {
|
||
|
WriteReg(0x22, 0b110); // PWRON > OFFLEVEL as POWEROFF Source enable
|
||
|
WriteReg(0x27, 0x10); // hold 4s to power off
|
||
|
|
||
|
// Disable All DCs but DC1
|
||
|
WriteReg(0x80, 0x01);
|
||
|
// Disable All LDOs
|
||
|
WriteReg(0x90, 0x00);
|
||
|
WriteReg(0x91, 0x00);
|
||
|
|
||
|
// Set DC1 to 3.3V
|
||
|
WriteReg(0x82, (3300 - 1500) / 100);
|
||
|
|
||
|
// Set ALDO1 to 3.3V
|
||
|
WriteReg(0x92, (3300 - 500) / 100);
|
||
|
|
||
|
// Enable ALDO1(MIC)
|
||
|
WriteReg(0x90, 0x01);
|
||
|
|
||
|
WriteReg(0x64, 0x02); // CV charger voltage setting to 4.1V
|
||
|
|
||
|
WriteReg(0x61, 0x02); // set Main battery precharge current to 50mA
|
||
|
WriteReg(0x62, 0x08); // set Main battery charger current to 400mA ( 0x08-200mA, 0x09-300mA, 0x0A-400mA )
|
||
|
WriteReg(0x63, 0x01); // set Main battery term charge current to 25mA
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
typedef struct {
|
||
|
int cmd; /*<! The specific LCD command */
|
||
|
const void *data; /*<! Buffer that holds the command specific data */
|
||
|
size_t data_bytes; /*<! Size of `data` in memory, in bytes */
|
||
|
unsigned int delay_ms; /*<! Delay in milliseconds after this command */
|
||
|
} st7796_lcd_init_cmd_t;
|
||
|
|
||
|
typedef struct {
|
||
|
const st7796_lcd_init_cmd_t *init_cmds; /*!< Pointer to initialization commands array. Set to NULL if using default commands.
|
||
|
* The array should be declared as `static const` and positioned outside the function.
|
||
|
* Please refer to `vendor_specific_init_default` in source file.
|
||
|
*/
|
||
|
uint16_t init_cmds_size; /*<! Number of commands in above array */
|
||
|
} st7796_vendor_config_t;
|
||
|
|
||
|
|
||
|
st7796_lcd_init_cmd_t st7796_lcd_init_cmds[] = {
|
||
|
{0x11, (uint8_t []){ 0x00 }, 0, 120},
|
||
|
|
||
|
// {0x36, (uint8_t []){ 0x08 }, 1, 0},
|
||
|
|
||
|
{0x3A, (uint8_t []){ 0x05 }, 1, 0},
|
||
|
{0xF0, (uint8_t []){ 0xC3 }, 1, 0},
|
||
|
{0xF0, (uint8_t []){ 0x96 }, 1, 0},
|
||
|
{0xB4, (uint8_t []){ 0x01 }, 1, 0},
|
||
|
{0xB7, (uint8_t []){ 0xC6 }, 1, 0},
|
||
|
{0xC0, (uint8_t []){ 0x80, 0x45 }, 2, 0},
|
||
|
{0xC1, (uint8_t []){ 0x13 }, 1, 0},
|
||
|
{0xC2, (uint8_t []){ 0xA7 }, 1, 0},
|
||
|
{0xC5, (uint8_t []){ 0x0A }, 1, 0},
|
||
|
{0xE8, (uint8_t []){ 0x40, 0x8A, 0x00, 0x00, 0x29, 0x19, 0xA5, 0x33}, 8, 0},
|
||
|
{0xE0, (uint8_t []){ 0xD0, 0x08, 0x0F, 0x06, 0x06, 0x33, 0x30, 0x33, 0x47, 0x17, 0x13, 0x13, 0x2B, 0x31}, 14, 0},
|
||
|
{0xE1, (uint8_t []){ 0xD0, 0x0A, 0x11, 0x0B, 0x09, 0x07, 0x2F, 0x33, 0x47, 0x38, 0x15, 0x16, 0x2C, 0x32},14, 0},
|
||
|
{0xF0, (uint8_t []){ 0x3C }, 1, 0},
|
||
|
{0xF0, (uint8_t []){ 0x69 }, 1, 120},
|
||
|
{0x21, (uint8_t []){ 0x00 }, 0, 0},
|
||
|
{0x29, (uint8_t []){ 0x00 }, 0, 0},
|
||
|
};
|
||
|
|
||
|
|
||
|
class CustomBoard : public WifiBoard {
|
||
|
private:
|
||
|
Button boot_button_;
|
||
|
Pmic* pmic_ = nullptr;
|
||
|
i2c_master_bus_handle_t i2c_bus_;
|
||
|
esp_io_expander_handle_t io_expander = NULL;
|
||
|
LcdDisplay* display_;
|
||
|
PowerSaveTimer* power_save_timer_;
|
||
|
|
||
|
void InitializePowerSaveTimer() {
|
||
|
power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
|
||
|
power_save_timer_->OnEnterSleepMode([this]() {
|
||
|
ESP_LOGI(TAG, "Enabling sleep mode");
|
||
|
auto display = GetDisplay();
|
||
|
display->SetChatMessage("system", "");
|
||
|
display->SetEmotion("sleepy");
|
||
|
GetBacklight()->SetBrightness(20);
|
||
|
});
|
||
|
power_save_timer_->OnExitSleepMode([this]() {
|
||
|
auto display = GetDisplay();
|
||
|
display->SetChatMessage("system", "");
|
||
|
display->SetEmotion("neutral");
|
||
|
GetBacklight()->RestoreBrightness();
|
||
|
});
|
||
|
power_save_timer_->OnShutdownRequest([this]() {
|
||
|
pmic_->PowerOff();
|
||
|
});
|
||
|
power_save_timer_->SetEnabled(true);
|
||
|
}
|
||
|
|
||
|
void InitializeI2c() {
|
||
|
// Initialize I2C peripheral
|
||
|
i2c_master_bus_config_t i2c_bus_cfg = {
|
||
|
.i2c_port = (i2c_port_t)0,
|
||
|
.sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
|
||
|
.scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
|
||
|
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||
|
};
|
||
|
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
|
||
|
}
|
||
|
|
||
|
void InitializeTca9554(void)
|
||
|
{
|
||
|
esp_err_t ret = esp_io_expander_new_i2c_tca9554(i2c_bus_, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &io_expander);
|
||
|
if(ret != ESP_OK)
|
||
|
ESP_LOGE(TAG, "TCA9554 create returned error");
|
||
|
ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, IO_EXPANDER_OUTPUT);
|
||
|
ESP_ERROR_CHECK(ret);
|
||
|
vTaskDelay(pdMS_TO_TICKS(100));
|
||
|
ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_1, 0);
|
||
|
ESP_ERROR_CHECK(ret);
|
||
|
vTaskDelay(pdMS_TO_TICKS(100));
|
||
|
ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, 1);
|
||
|
ESP_ERROR_CHECK(ret);
|
||
|
}
|
||
|
|
||
|
void InitializeAxp2101() {
|
||
|
ESP_LOGI(TAG, "Init AXP2101");
|
||
|
pmic_ = new Pmic(i2c_bus_, 0x34);
|
||
|
}
|
||
|
|
||
|
void InitializeSpi() {
|
||
|
ESP_LOGI(TAG, "Initialize QSPI bus");
|
||
|
spi_bus_config_t buscfg = {};
|
||
|
buscfg.mosi_io_num = DISPLAY_MOSI_PIN;
|
||
|
buscfg.miso_io_num = DISPLAY_MISO_PIN;
|
||
|
buscfg.sclk_io_num = DISPLAY_CLK_PIN;
|
||
|
buscfg.quadwp_io_num = GPIO_NUM_NC;
|
||
|
buscfg.quadhd_io_num = GPIO_NUM_NC;
|
||
|
buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
|
||
|
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
||
|
}
|
||
|
|
||
|
void InitializeLcdDisplay() {
|
||
|
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||
|
esp_lcd_panel_handle_t panel = nullptr;
|
||
|
// 液晶屏控制IO初始化
|
||
|
ESP_LOGI(TAG, "Install panel IO");
|
||
|
esp_lcd_panel_io_spi_config_t io_config = {};
|
||
|
io_config.cs_gpio_num = DISPLAY_CS_PIN;
|
||
|
io_config.dc_gpio_num = DISPLAY_DC_PIN;
|
||
|
io_config.spi_mode = DISPLAY_SPI_MODE;
|
||
|
io_config.pclk_hz = 40 * 1000 * 1000;
|
||
|
io_config.trans_queue_depth = 10;
|
||
|
io_config.lcd_cmd_bits = 8;
|
||
|
io_config.lcd_param_bits = 8;
|
||
|
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
|
||
|
|
||
|
st7796_vendor_config_t st7796_vendor_config = {
|
||
|
.init_cmds = st7796_lcd_init_cmds,
|
||
|
.init_cmds_size = sizeof(st7796_lcd_init_cmds) / sizeof(st7796_lcd_init_cmd_t),
|
||
|
};
|
||
|
|
||
|
// 初始化液晶屏驱动芯片
|
||
|
ESP_LOGI(TAG, "Install LCD driver");
|
||
|
esp_lcd_panel_dev_config_t panel_config = {};
|
||
|
panel_config.reset_gpio_num = DISPLAY_RST_PIN;
|
||
|
panel_config.rgb_ele_order = DISPLAY_RGB_ORDER;
|
||
|
panel_config.bits_per_pixel = 16;
|
||
|
panel_config.vendor_config = &st7796_vendor_config;
|
||
|
|
||
|
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
|
||
|
|
||
|
|
||
|
esp_lcd_panel_reset(panel);
|
||
|
|
||
|
esp_lcd_panel_init(panel);
|
||
|
esp_lcd_panel_invert_color(panel, DISPLAY_INVERT_COLOR);
|
||
|
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
|
||
|
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
|
||
|
|
||
|
|
||
|
|
||
|
display_ = new SpiLcdDisplay(panel_io, panel,
|
||
|
DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
|
||
|
{
|
||
|
.text_font = &font_puhui_16_4,
|
||
|
.icon_font = &font_awesome_16_4,
|
||
|
.emoji_font = font_emoji_32_init(),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void InitializeButtons() {
|
||
|
boot_button_.OnClick([this]() {
|
||
|
auto& app = Application::GetInstance();
|
||
|
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||
|
ResetWifiConfiguration();
|
||
|
}
|
||
|
app.ToggleChatState();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 物联网初始化,添加对 AI 可见设备
|
||
|
void InitializeIot() {
|
||
|
auto& thing_manager = iot::ThingManager::GetInstance();
|
||
|
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||
|
thing_manager.AddThing(iot::CreateThing("Screen"));
|
||
|
thing_manager.AddThing(iot::CreateThing("Battery"));
|
||
|
thing_manager.AddThing(iot::CreateThing("BoardControl"));
|
||
|
}
|
||
|
|
||
|
public:
|
||
|
CustomBoard() :
|
||
|
boot_button_(BOOT_BUTTON_GPIO) {
|
||
|
InitializePowerSaveTimer();
|
||
|
InitializeI2c();
|
||
|
InitializeTca9554();
|
||
|
InitializeAxp2101();
|
||
|
InitializeSpi();
|
||
|
InitializeLcdDisplay();
|
||
|
InitializeButtons();
|
||
|
InitializeIot();
|
||
|
GetBacklight()->RestoreBrightness();
|
||
|
}
|
||
|
|
||
|
virtual AudioCodec* GetAudioCodec() override {
|
||
|
static Es8311AudioCodec audio_codec(i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
|
||
|
AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
|
||
|
AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
|
||
|
return &audio_codec;
|
||
|
}
|
||
|
|
||
|
virtual Display* GetDisplay() override {
|
||
|
return display_;
|
||
|
}
|
||
|
|
||
|
virtual Backlight* GetBacklight() override {
|
||
|
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||
|
return &backlight;
|
||
|
}
|
||
|
virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging) override {
|
||
|
static bool last_discharging = false;
|
||
|
charging = pmic_->IsCharging();
|
||
|
discharging = pmic_->IsDischarging();
|
||
|
if (discharging != last_discharging) {
|
||
|
power_save_timer_->SetEnabled(discharging);
|
||
|
last_discharging = discharging;
|
||
|
}
|
||
|
|
||
|
level = pmic_->GetBatteryLevel();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
virtual void SetPowerSaveMode(bool enabled) override {
|
||
|
if (!enabled) {
|
||
|
power_save_timer_->WakeUp();
|
||
|
}
|
||
|
WifiBoard::SetPowerSaveMode(enabled);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
DECLARE_BOARD(CustomBoard);
|