|
| 1 | +#include "application.h" |
| 2 | +#include "button.h" |
| 3 | +#include "codecs/es8311_audio_codec.h" |
| 4 | +#include "config.h" |
| 5 | +#include "wifi_board.h" |
| 6 | + |
| 7 | +#include "display/lcd_display.h" |
| 8 | +#include "esp_lcd_sh8601.h" |
| 9 | +#include "lvgl.h" |
| 10 | +#include "mcp_server.h" |
| 11 | +#include <driver/i2c_master.h> |
| 12 | +#include <driver/spi_common.h> |
| 13 | +#include <esp_lcd_panel_vendor.h> |
| 14 | +#include <esp_log.h> |
| 15 | +#include <wifi_station.h> |
| 16 | + |
| 17 | +#define TAG "waveshare_s3_amoled_1_32" |
| 18 | + |
| 19 | +static const sh8601_lcd_init_cmd_t lcd_init_cmds[] = { |
| 20 | + {0xFE, (uint8_t[]) {0x00}, 1, 0}, |
| 21 | + {0xC4, (uint8_t[]) {0x80}, 1, 0}, |
| 22 | + {0x3A, (uint8_t[]) {0x55}, 1, 0}, // 0x55 for RGB565, 0x77 for RGB888 |
| 23 | + {0x35, (uint8_t[]) {0x00}, 1, 0}, |
| 24 | + {0x53, (uint8_t[]) {0x20}, 1, 0}, |
| 25 | + {0x51, (uint8_t[]) {0xFF}, 1, 0}, // Brightness |
| 26 | + {0x63, (uint8_t[]) {0xFF}, 1, 0}, |
| 27 | + {0x2A, (uint8_t[]) {0x00, 0x06, 0x01, 0xD7}, 4, 0}, |
| 28 | + {0x2B, (uint8_t[]) {0x00, 0x00, 0x01, 0xD1}, 4, 0}, |
| 29 | + {0x11, (uint8_t[]) {0x00}, 0, 100}, |
| 30 | + {0x29, (uint8_t[]) {0x00}, 0, 0}, |
| 31 | +}; |
| 32 | + |
| 33 | +class CustomLcdDisplay : public SpiLcdDisplay { |
| 34 | + private: |
| 35 | + esp_lcd_panel_io_handle_t io_handle_; |
| 36 | + |
| 37 | + public: |
| 38 | + static void my_draw_event_cb(lv_event_t *e) { |
| 39 | + lv_area_t *area = (lv_area_t *) lv_event_get_param(e); |
| 40 | + |
| 41 | + uint16_t x1 = area->x1; |
| 42 | + uint16_t x2 = area->x2; |
| 43 | + uint16_t y1 = area->y1; |
| 44 | + uint16_t y2 = area->y2; |
| 45 | + |
| 46 | + // Round area for better performance |
| 47 | + area->x1 = (x1 >> 1) << 1; |
| 48 | + area->y1 = (y1 >> 1) << 1; |
| 49 | + area->x2 = ((x2 >> 1) << 1) + 1; |
| 50 | + area->y2 = ((y2 >> 1) << 1) + 1; |
| 51 | + } |
| 52 | + |
| 53 | + void SetMIRROR_XY(uint8_t mirror) { |
| 54 | + uint32_t lcd_cmd = 0x36; |
| 55 | + lcd_cmd &= 0xff; |
| 56 | + lcd_cmd <<= 8; |
| 57 | + lcd_cmd |= 0x02 << 24; |
| 58 | + uint8_t param = mirror; |
| 59 | + esp_lcd_panel_io_tx_param(io_handle_, lcd_cmd, ¶m, 1); |
| 60 | + } |
| 61 | + |
| 62 | + CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle, esp_lcd_panel_handle_t panel_handle, int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy) : |
| 63 | + SpiLcdDisplay(io_handle, panel_handle, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy), |
| 64 | + io_handle_(io_handle) { |
| 65 | + DisplayLockGuard lock(this); |
| 66 | + lv_display_add_event_cb(display_, my_draw_event_cb, LV_EVENT_INVALIDATE_AREA, NULL); |
| 67 | + SetMIRROR_XY(0xC0); // Rotate 180 degrees |
| 68 | + lv_obj_invalidate(lv_screen_active()); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +class CustomBoard : public WifiBoard { |
| 73 | + private: |
| 74 | + i2c_master_bus_handle_t i2c_bus_; |
| 75 | + Button boot_button_; |
| 76 | + Button pwr_button_; |
| 77 | + esp_lcd_panel_handle_t panel_handle = NULL; |
| 78 | + esp_lcd_panel_io_handle_t io_handle = NULL; |
| 79 | + CustomLcdDisplay *display_; |
| 80 | + lv_indev_t *touch_indev = NULL; |
| 81 | + i2c_master_dev_handle_t disp_touch_dev_handle = NULL; |
| 82 | + |
| 83 | + void InitializeI2c() { |
| 84 | + i2c_master_bus_config_t i2c_bus_cfg = {}; |
| 85 | + i2c_bus_cfg.i2c_port = I2C_NUM_0; |
| 86 | + i2c_bus_cfg.sda_io_num = AUDIO_CODEC_I2C_SDA_PIN; |
| 87 | + i2c_bus_cfg.scl_io_num = AUDIO_CODEC_I2C_SCL_PIN; |
| 88 | + i2c_bus_cfg.clk_source = I2C_CLK_SRC_DEFAULT; |
| 89 | + i2c_bus_cfg.glitch_ignore_cnt = 7; |
| 90 | + i2c_bus_cfg.intr_priority = 0; |
| 91 | + i2c_bus_cfg.trans_queue_depth = 0; |
| 92 | + i2c_bus_cfg.flags.enable_internal_pullup = 1; |
| 93 | + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_)); |
| 94 | + } |
| 95 | + |
| 96 | + void SetDispbacklight(uint8_t backlight) { |
| 97 | + uint32_t lcd_cmd = 0x51; |
| 98 | + lcd_cmd &= 0xff; |
| 99 | + lcd_cmd <<= 8; |
| 100 | + lcd_cmd |= 0x02 << 24; |
| 101 | + uint8_t param = backlight; |
| 102 | + esp_lcd_panel_io_tx_param(io_handle, lcd_cmd, ¶m, 1); |
| 103 | + } |
| 104 | + |
| 105 | + void InitializeButtons() { |
| 106 | + boot_button_.OnClick([this]() { |
| 107 | + auto &app = Application::GetInstance(); |
| 108 | + if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) { |
| 109 | + ResetWifiConfiguration(); |
| 110 | + } |
| 111 | + app.ToggleChatState(); |
| 112 | + }); |
| 113 | + |
| 114 | + pwr_button_.OnLongPress([this]() { |
| 115 | + GetDisplay()->SetChatMessage("system", "OFF"); |
| 116 | + vTaskDelay(pdMS_TO_TICKS(1000)); |
| 117 | + gpio_set_level(PWR_EN_GPIO, 0); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + void InitializeSpi() { |
| 122 | + spi_bus_config_t buscfg = {}; |
| 123 | + buscfg.data0_io_num = LCD_D0; |
| 124 | + buscfg.data1_io_num = LCD_D1; |
| 125 | + buscfg.sclk_io_num = LCD_PCLK; |
| 126 | + buscfg.data2_io_num = LCD_D2; |
| 127 | + buscfg.data3_io_num = LCD_D3; |
| 128 | + buscfg.max_transfer_sz = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * sizeof(uint16_t); |
| 129 | + ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO)); |
| 130 | + } |
| 131 | + |
| 132 | + void InitializeLcdDisplay() { |
| 133 | + esp_lcd_panel_io_spi_config_t io_config = {}; |
| 134 | + io_config.cs_gpio_num = LCD_CS; |
| 135 | + io_config.dc_gpio_num = -1; |
| 136 | + io_config.spi_mode = 0; |
| 137 | + io_config.pclk_hz = 40 * 1000 * 1000; |
| 138 | + io_config.trans_queue_depth = 8; |
| 139 | + io_config.on_color_trans_done = NULL; |
| 140 | + io_config.user_ctx = NULL; |
| 141 | + io_config.lcd_cmd_bits = 32; |
| 142 | + io_config.lcd_param_bits = 8; |
| 143 | + io_config.flags.quad_mode = true; |
| 144 | + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &io_handle)); |
| 145 | + |
| 146 | + sh8601_vendor_config_t vendor_config = {}; |
| 147 | + vendor_config.init_cmds = lcd_init_cmds; |
| 148 | + vendor_config.init_cmds_size = sizeof(lcd_init_cmds) / sizeof(lcd_init_cmds[0]); |
| 149 | + vendor_config.flags.use_qspi_interface = 1; |
| 150 | + |
| 151 | + esp_lcd_panel_dev_config_t panel_config = {}; |
| 152 | + panel_config.reset_gpio_num = LCD_RST; |
| 153 | + panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB; |
| 154 | + panel_config.bits_per_pixel = 16; |
| 155 | + panel_config.vendor_config = &vendor_config; |
| 156 | + |
| 157 | + ESP_ERROR_CHECK(esp_lcd_new_panel_sh8601(io_handle, &panel_config, &panel_handle)); |
| 158 | + esp_lcd_panel_set_gap(panel_handle, 0x06, 0x00); |
| 159 | + ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle)); |
| 160 | + ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle)); |
| 161 | + |
| 162 | + display_ = new CustomLcdDisplay(io_handle, panel_handle, EXAMPLE_LCD_H_RES, EXAMPLE_LCD_V_RES, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY); |
| 163 | + } |
| 164 | + |
| 165 | + void InitializeTools() { |
| 166 | + auto &mcp_server = McpServer::GetInstance(); |
| 167 | + mcp_server.AddTool("self.disp.setbacklight", "设置屏幕亮度", PropertyList({Property("level", kPropertyTypeInteger, 0, 255)}), [this](const PropertyList &properties) -> ReturnValue { |
| 168 | + int level = properties["level"].value<int>(); |
| 169 | + ESP_LOGI("setbacklight", "%d", level); |
| 170 | + SetDispbacklight(level); |
| 171 | + return true; |
| 172 | + }); |
| 173 | + |
| 174 | + mcp_server.AddTool("self.disp.network", "重新配网", PropertyList(), [this](const PropertyList &) -> ReturnValue { |
| 175 | + ResetWifiConfiguration(); |
| 176 | + return true; |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + void CheckPowerKeyState() { |
| 181 | + gpio_config_t gpio_conf = {}; |
| 182 | + gpio_conf.intr_type = GPIO_INTR_DISABLE; |
| 183 | + gpio_conf.mode = GPIO_MODE_OUTPUT; |
| 184 | + gpio_conf.pin_bit_mask = (0x1ULL << PWR_EN_GPIO); |
| 185 | + gpio_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; |
| 186 | + gpio_conf.pull_up_en = GPIO_PULLUP_ENABLE; |
| 187 | + ESP_ERROR_CHECK_WITHOUT_ABORT(gpio_config(&gpio_conf)); |
| 188 | + gpio_set_level(PWR_EN_GPIO, 1); |
| 189 | + do { |
| 190 | + vTaskDelay(pdMS_TO_TICKS(10)); |
| 191 | + } while (!gpio_get_level(PWR_BUTTON_GPIO)); |
| 192 | + } |
| 193 | + |
| 194 | + public: |
| 195 | + CustomBoard() : boot_button_(BOOT_BUTTON_GPIO), pwr_button_(PWR_BUTTON_GPIO) { |
| 196 | + CheckPowerKeyState(); |
| 197 | + InitializeI2c(); |
| 198 | + InitializeSpi(); |
| 199 | + InitializeLcdDisplay(); |
| 200 | + InitializeButtons(); |
| 201 | + InitializeTools(); |
| 202 | + } |
| 203 | + |
| 204 | + virtual AudioCodec *GetAudioCodec() override { |
| 205 | + 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); |
| 206 | + return &audio_codec; |
| 207 | + } |
| 208 | + |
| 209 | + virtual Display *GetDisplay() override { |
| 210 | + return display_; |
| 211 | + } |
| 212 | +}; |
| 213 | + |
| 214 | +DECLARE_BOARD(CustomBoard); |
0 commit comments