esp32-s3-touch-amoled-1.8.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "wifi_board.h"
  2. #include "display/lcd_display.h"
  3. #include "esp_lcd_sh8601.h"
  4. #include "font_awesome_symbols.h"
  5. #include "codecs/es8311_audio_codec.h"
  6. #include "application.h"
  7. #include "button.h"
  8. #include "led/single_led.h"
  9. #include "mcp_server.h"
  10. #include "config.h"
  11. #include "power_save_timer.h"
  12. #include "axp2101.h"
  13. #include "i2c_device.h"
  14. #include <wifi_station.h>
  15. #include <esp_log.h>
  16. #include <esp_lcd_panel_vendor.h>
  17. #include <driver/i2c_master.h>
  18. #include <driver/spi_master.h>
  19. #include "esp_io_expander_tca9554.h"
  20. #include "settings.h"
  21. #include <esp_lcd_touch_ft5x06.h>
  22. #include <esp_lvgl_port.h>
  23. #include <lvgl.h>
  24. #define TAG "WaveshareEsp32s3TouchAMOLED1inch8"
  25. LV_FONT_DECLARE(font_puhui_30_4);
  26. LV_FONT_DECLARE(font_awesome_30_4);
  27. class Pmic : public Axp2101 {
  28. public:
  29. Pmic(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : Axp2101(i2c_bus, addr) {
  30. WriteReg(0x22, 0b110); // PWRON > OFFLEVEL as POWEROFF Source enable
  31. WriteReg(0x27, 0x10); // hold 4s to power off
  32. // Disable All DCs but DC1
  33. WriteReg(0x80, 0x01);
  34. // Disable All LDOs
  35. WriteReg(0x90, 0x00);
  36. WriteReg(0x91, 0x00);
  37. // Set DC1 to 3.3V
  38. WriteReg(0x82, (3300 - 1500) / 100);
  39. // Set ALDO1 to 3.3V
  40. WriteReg(0x92, (3300 - 500) / 100);
  41. // Enable ALDO1(MIC)
  42. WriteReg(0x90, 0x01);
  43. WriteReg(0x64, 0x02); // CV charger voltage setting to 4.1V
  44. WriteReg(0x61, 0x02); // set Main battery precharge current to 50mA
  45. WriteReg(0x62, 0x08); // set Main battery charger current to 400mA ( 0x08-200mA, 0x09-300mA, 0x0A-400mA )
  46. WriteReg(0x63, 0x01); // set Main battery term charge current to 25mA
  47. }
  48. };
  49. #define LCD_OPCODE_WRITE_CMD (0x02ULL)
  50. #define LCD_OPCODE_READ_CMD (0x03ULL)
  51. #define LCD_OPCODE_WRITE_COLOR (0x32ULL)
  52. static const sh8601_lcd_init_cmd_t vendor_specific_init[] = {
  53. {0x11, (uint8_t[]){0x00}, 0, 120},
  54. {0x44, (uint8_t[]){0x01, 0xD1}, 2, 0},
  55. {0x35, (uint8_t[]){0x00}, 1, 0},
  56. {0x53, (uint8_t[]){0x20}, 1, 10},
  57. {0x2A, (uint8_t[]){0x00, 0x00, 0x01, 0x6F}, 4, 0},
  58. {0x2B, (uint8_t[]){0x00, 0x00, 0x01, 0xBF}, 4, 0},
  59. {0x51, (uint8_t[]){0x00}, 1, 10},
  60. {0x29, (uint8_t[]){0x00}, 0, 10}
  61. };
  62. // 在waveshare_amoled_1_8类之前添加新的显示类
  63. class CustomLcdDisplay : public SpiLcdDisplay {
  64. public:
  65. CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
  66. esp_lcd_panel_handle_t panel_handle,
  67. int width,
  68. int height,
  69. int offset_x,
  70. int offset_y,
  71. bool mirror_x,
  72. bool mirror_y,
  73. bool swap_xy)
  74. : SpiLcdDisplay(io_handle, panel_handle,
  75. width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  76. {
  77. .text_font = &font_puhui_30_4,
  78. .icon_font = &font_awesome_30_4,
  79. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  80. .emoji_font = font_emoji_32_init(),
  81. #else
  82. .emoji_font = font_emoji_64_init(),
  83. #endif
  84. }) {
  85. DisplayLockGuard lock(this);
  86. lv_obj_set_style_pad_left(status_bar_, LV_HOR_RES * 0.1, 0);
  87. lv_obj_set_style_pad_right(status_bar_, LV_HOR_RES * 0.1, 0);
  88. }
  89. };
  90. class CustomBacklight : public Backlight {
  91. public:
  92. CustomBacklight(esp_lcd_panel_io_handle_t panel_io) : Backlight(), panel_io_(panel_io) {}
  93. protected:
  94. esp_lcd_panel_io_handle_t panel_io_;
  95. virtual void SetBrightnessImpl(uint8_t brightness) override {
  96. auto display = Board::GetInstance().GetDisplay();
  97. DisplayLockGuard lock(display);
  98. uint8_t data[1] = {((uint8_t)((255 * brightness) / 100))};
  99. int lcd_cmd = 0x51;
  100. lcd_cmd &= 0xff;
  101. lcd_cmd <<= 8;
  102. lcd_cmd |= LCD_OPCODE_WRITE_CMD << 24;
  103. esp_lcd_panel_io_tx_param(panel_io_, lcd_cmd, &data, sizeof(data));
  104. }
  105. };
  106. class WaveshareEsp32s3TouchAMOLED1inch8 : public WifiBoard {
  107. private:
  108. i2c_master_bus_handle_t codec_i2c_bus_;
  109. Pmic* pmic_ = nullptr;
  110. Button boot_button_;
  111. CustomLcdDisplay* display_;
  112. CustomBacklight* backlight_;
  113. esp_io_expander_handle_t io_expander = NULL;
  114. PowerSaveTimer* power_save_timer_;
  115. void InitializePowerSaveTimer() {
  116. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  117. power_save_timer_->OnEnterSleepMode([this]() {
  118. ESP_LOGI(TAG, "Enabling sleep mode");
  119. auto display = GetDisplay();
  120. display->SetChatMessage("system", "");
  121. display->SetEmotion("sleepy");
  122. GetBacklight()->SetBrightness(20);
  123. });
  124. power_save_timer_->OnExitSleepMode([this]() {
  125. auto display = GetDisplay();
  126. display->SetChatMessage("system", "");
  127. display->SetEmotion("neutral");
  128. GetBacklight()->RestoreBrightness();
  129. });
  130. power_save_timer_->OnShutdownRequest([this]() {
  131. pmic_->PowerOff();
  132. });
  133. power_save_timer_->SetEnabled(true);
  134. }
  135. void InitializeCodecI2c() {
  136. // Initialize I2C peripheral
  137. i2c_master_bus_config_t i2c_bus_cfg = {
  138. .i2c_port = I2C_NUM_0,
  139. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  140. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  141. .clk_source = I2C_CLK_SRC_DEFAULT,
  142. .glitch_ignore_cnt = 7,
  143. .intr_priority = 0,
  144. .trans_queue_depth = 0,
  145. .flags = {
  146. .enable_internal_pullup = 1,
  147. },
  148. };
  149. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  150. }
  151. void InitializeTca9554(void) {
  152. esp_err_t ret = esp_io_expander_new_i2c_tca9554(codec_i2c_bus_, I2C_ADDRESS, &io_expander);
  153. if(ret != ESP_OK)
  154. ESP_LOGE(TAG, "TCA9554 create returned error");
  155. ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 |IO_EXPANDER_PIN_NUM_2, IO_EXPANDER_OUTPUT);
  156. ret |= esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_4, IO_EXPANDER_INPUT);
  157. ESP_ERROR_CHECK(ret);
  158. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1|IO_EXPANDER_PIN_NUM_2, 1);
  159. ESP_ERROR_CHECK(ret);
  160. vTaskDelay(pdMS_TO_TICKS(100));
  161. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1|IO_EXPANDER_PIN_NUM_2, 0);
  162. ESP_ERROR_CHECK(ret);
  163. vTaskDelay(pdMS_TO_TICKS(300));
  164. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1|IO_EXPANDER_PIN_NUM_2, 1);
  165. ESP_ERROR_CHECK(ret);
  166. }
  167. void InitializeAxp2101() {
  168. ESP_LOGI(TAG, "Init AXP2101");
  169. pmic_ = new Pmic(codec_i2c_bus_, 0x34);
  170. }
  171. void InitializeSpi() {
  172. spi_bus_config_t buscfg = {};
  173. buscfg.sclk_io_num = GPIO_NUM_11;
  174. buscfg.data0_io_num = GPIO_NUM_4;
  175. buscfg.data1_io_num = GPIO_NUM_5;
  176. buscfg.data2_io_num = GPIO_NUM_6;
  177. buscfg.data3_io_num = GPIO_NUM_7;
  178. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  179. buscfg.flags = SPICOMMON_BUSFLAG_QUAD;
  180. ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
  181. }
  182. void InitializeButtons() {
  183. boot_button_.OnClick([this]() {
  184. auto& app = Application::GetInstance();
  185. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  186. ResetWifiConfiguration();
  187. }
  188. app.ToggleChatState();
  189. });
  190. }
  191. void InitializeSH8601Display() {
  192. esp_lcd_panel_io_handle_t panel_io = nullptr;
  193. esp_lcd_panel_handle_t panel = nullptr;
  194. // 液晶屏控制IO初始化
  195. ESP_LOGD(TAG, "Install panel IO");
  196. esp_lcd_panel_io_spi_config_t io_config = SH8601_PANEL_IO_QSPI_CONFIG(
  197. EXAMPLE_PIN_NUM_LCD_CS,
  198. nullptr,
  199. nullptr
  200. );
  201. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io));
  202. // 初始化液晶屏驱动芯片
  203. ESP_LOGD(TAG, "Install LCD driver");
  204. const sh8601_vendor_config_t vendor_config = {
  205. .init_cmds = &vendor_specific_init[0],
  206. .init_cmds_size = sizeof(vendor_specific_init) / sizeof(sh8601_lcd_init_cmd_t),
  207. .flags ={
  208. .use_qspi_interface = 1,
  209. }
  210. };
  211. esp_lcd_panel_dev_config_t panel_config = {};
  212. panel_config.reset_gpio_num = GPIO_NUM_NC;
  213. panel_config.flags.reset_active_high = 1,
  214. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  215. panel_config.bits_per_pixel = 16;
  216. panel_config.vendor_config = (void *)&vendor_config;
  217. ESP_ERROR_CHECK(esp_lcd_new_panel_sh8601(panel_io, &panel_config, &panel));
  218. esp_lcd_panel_reset(panel);
  219. esp_lcd_panel_init(panel);
  220. esp_lcd_panel_invert_color(panel, false);
  221. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  222. esp_lcd_panel_disp_on_off(panel, true);
  223. display_ = new CustomLcdDisplay(panel_io, panel,
  224. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  225. backlight_ = new CustomBacklight(panel_io);
  226. backlight_->RestoreBrightness();
  227. }
  228. void InitializeTouch()
  229. {
  230. esp_lcd_touch_handle_t tp;
  231. esp_lcd_touch_config_t tp_cfg = {
  232. .x_max = DISPLAY_WIDTH,
  233. .y_max = DISPLAY_HEIGHT,
  234. .rst_gpio_num = GPIO_NUM_NC,
  235. .int_gpio_num = GPIO_NUM_21,
  236. .levels = {
  237. .reset = 0,
  238. .interrupt = 0,
  239. },
  240. .flags = {
  241. .swap_xy = 0,
  242. .mirror_x = 0,
  243. .mirror_y = 0,
  244. },
  245. };
  246. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  247. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
  248. tp_io_config.scl_speed_hz = 400 * 1000;
  249. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(codec_i2c_bus_, &tp_io_config, &tp_io_handle));
  250. ESP_LOGI(TAG, "Initialize touch controller");
  251. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp));
  252. const lvgl_port_touch_cfg_t touch_cfg = {
  253. .disp = lv_display_get_default(),
  254. .handle = tp,
  255. };
  256. lvgl_port_add_touch(&touch_cfg);
  257. ESP_LOGI(TAG, "Touch panel initialized successfully");
  258. }
  259. // 初始化工具
  260. void InitializeTools() {
  261. auto &mcp_server = McpServer::GetInstance();
  262. mcp_server.AddTool("self.system.reconfigure_wifi",
  263. "Reboot the device and enter WiFi configuration mode.\n"
  264. "**CAUTION** You must ask the user to confirm this action.",
  265. PropertyList(), [this](const PropertyList& properties) {
  266. ResetWifiConfiguration();
  267. return true;
  268. });
  269. }
  270. public:
  271. WaveshareEsp32s3TouchAMOLED1inch8() :
  272. boot_button_(BOOT_BUTTON_GPIO) {
  273. InitializePowerSaveTimer();
  274. InitializeCodecI2c();
  275. InitializeTca9554();
  276. InitializeAxp2101();
  277. InitializeSpi();
  278. InitializeSH8601Display();
  279. InitializeTouch();
  280. InitializeButtons();
  281. InitializeTools();
  282. }
  283. virtual AudioCodec* GetAudioCodec() override {
  284. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  285. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  286. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  287. return &audio_codec;
  288. }
  289. virtual Display* GetDisplay() override {
  290. return display_;
  291. }
  292. virtual Backlight* GetBacklight() override {
  293. return backlight_;
  294. }
  295. virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging) override {
  296. static bool last_discharging = false;
  297. charging = pmic_->IsCharging();
  298. discharging = pmic_->IsDischarging();
  299. if (discharging != last_discharging) {
  300. power_save_timer_->SetEnabled(discharging);
  301. last_discharging = discharging;
  302. }
  303. level = pmic_->GetBatteryLevel();
  304. return true;
  305. }
  306. virtual void SetPowerSaveMode(bool enabled) override {
  307. if (!enabled) {
  308. power_save_timer_->WakeUp();
  309. }
  310. WifiBoard::SetPowerSaveMode(enabled);
  311. }
  312. };
  313. DECLARE_BOARD(WaveshareEsp32s3TouchAMOLED1inch8);