esp32-s3-touch-amoled-2.06.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/box_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 "settings.h"
  20. #include <esp_lcd_touch_ft5x06.h>
  21. #include <esp_lvgl_port.h>
  22. #include <lvgl.h>
  23. #define TAG "WaveshareEsp32s3TouchAMOLED2inch06"
  24. LV_FONT_DECLARE(font_puhui_30_4);
  25. LV_FONT_DECLARE(font_awesome_30_4);
  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. // Disable All DCs but DC1
  32. WriteReg(0x80, 0x01);
  33. // Disable All LDOs
  34. WriteReg(0x90, 0x00);
  35. WriteReg(0x91, 0x00);
  36. // Set DC1 to 3.3V
  37. WriteReg(0x82, (3300 - 1500) / 100);
  38. // Set ALDO1 to 3.3V
  39. WriteReg(0x92, (3300 - 500) / 100);
  40. WriteReg(0x93, (3300 - 500) / 100);
  41. // Enable ALDO1(MIC)
  42. WriteReg(0x90, 0x03);
  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, 0x0A); // 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. // set display to qspi mode
  54. {0x11, (uint8_t []){0x00}, 0, 120},
  55. {0xC4, (uint8_t []){0x80}, 1, 0},
  56. {0x44, (uint8_t []){0x01, 0xD1}, 2, 0},
  57. {0x35, (uint8_t []){0x00}, 1, 0},
  58. {0x53, (uint8_t []){0x20}, 1, 10},
  59. {0x63, (uint8_t []){0xFF}, 1, 10},
  60. {0x51, (uint8_t []){0x00}, 1, 10},
  61. {0x2A, (uint8_t []){0x00,0x16,0x01,0xAF}, 4, 0},
  62. {0x2B, (uint8_t []){0x00,0x00,0x01,0xF5}, 4, 0},
  63. {0x29, (uint8_t []){0x00}, 0, 10},
  64. {0x51, (uint8_t []){0xFF}, 1, 0},
  65. };
  66. // 在waveshare_amoled_2_06类之前添加新的显示类
  67. class CustomLcdDisplay : public SpiLcdDisplay {
  68. public:
  69. static void rounder_event_cb(lv_event_t* e) {
  70. lv_area_t* area = (lv_area_t* )lv_event_get_param(e);
  71. uint16_t x1 = area->x1;
  72. uint16_t x2 = area->x2;
  73. uint16_t y1 = area->y1;
  74. uint16_t y2 = area->y2;
  75. // round the start of coordinate down to the nearest 2M number
  76. area->x1 = (x1 >> 1) << 1;
  77. area->y1 = (y1 >> 1) << 1;
  78. // round the end of coordinate up to the nearest 2N+1 number
  79. area->x2 = ((x2 >> 1) << 1) + 1;
  80. area->y2 = ((y2 >> 1) << 1) + 1;
  81. }
  82. CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
  83. esp_lcd_panel_handle_t panel_handle,
  84. int width,
  85. int height,
  86. int offset_x,
  87. int offset_y,
  88. bool mirror_x,
  89. bool mirror_y,
  90. bool swap_xy)
  91. : SpiLcdDisplay(io_handle, panel_handle,
  92. width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  93. {
  94. .text_font = &font_puhui_30_4,
  95. .icon_font = &font_awesome_30_4,
  96. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  97. .emoji_font = font_emoji_32_init(),
  98. #else
  99. .emoji_font = font_emoji_64_init(),
  100. #endif
  101. })
  102. {
  103. DisplayLockGuard lock(this);
  104. lv_obj_set_style_pad_left(status_bar_, LV_HOR_RES* 0.1, 0);
  105. lv_obj_set_style_pad_right(status_bar_, LV_HOR_RES* 0.1, 0);
  106. lv_display_add_event_cb(display_, rounder_event_cb, LV_EVENT_INVALIDATE_AREA, NULL);
  107. }
  108. };
  109. class CustomBacklight : public Backlight {
  110. public:
  111. CustomBacklight(esp_lcd_panel_io_handle_t panel_io) : Backlight(), panel_io_(panel_io) {}
  112. protected:
  113. esp_lcd_panel_io_handle_t panel_io_;
  114. virtual void SetBrightnessImpl(uint8_t brightness) override {
  115. auto display = Board::GetInstance().GetDisplay();
  116. DisplayLockGuard lock(display);
  117. uint8_t data[1] = {((uint8_t)((255* brightness) / 100))};
  118. int lcd_cmd = 0x51;
  119. lcd_cmd &= 0xff;
  120. lcd_cmd <<= 8;
  121. lcd_cmd |= LCD_OPCODE_WRITE_CMD << 24;
  122. esp_lcd_panel_io_tx_param(panel_io_, lcd_cmd, &data, sizeof(data));
  123. }
  124. };
  125. class WaveshareEsp32s3TouchAMOLED2inch06 : public WifiBoard {
  126. private:
  127. i2c_master_bus_handle_t i2c_bus_;
  128. Pmic* pmic_ = nullptr;
  129. Button boot_button_;
  130. CustomLcdDisplay* display_;
  131. CustomBacklight* backlight_;
  132. PowerSaveTimer* power_save_timer_;
  133. void InitializePowerSaveTimer() {
  134. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  135. power_save_timer_->OnEnterSleepMode([this]() {
  136. ESP_LOGI(TAG, "Enabling sleep mode");
  137. auto display = GetDisplay();
  138. display->SetChatMessage("system", "");
  139. display->SetEmotion("sleepy");
  140. GetBacklight()->SetBrightness(20); });
  141. power_save_timer_->OnExitSleepMode([this]() {
  142. auto display = GetDisplay();
  143. display->SetChatMessage("system", "");
  144. display->SetEmotion("neutral");
  145. GetBacklight()->RestoreBrightness(); });
  146. power_save_timer_->OnShutdownRequest([this](){
  147. pmic_->PowerOff(); });
  148. power_save_timer_->SetEnabled(true);
  149. }
  150. void InitializeCodecI2c() {
  151. // Initialize I2C peripheral
  152. i2c_master_bus_config_t i2c_bus_cfg = {
  153. .i2c_port = I2C_NUM_0,
  154. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  155. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  156. .clk_source = I2C_CLK_SRC_DEFAULT,
  157. .flags = {
  158. .enable_internal_pullup = 1,
  159. },
  160. };
  161. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  162. }
  163. void InitializeAxp2101() {
  164. ESP_LOGI(TAG, "Init AXP2101");
  165. pmic_ = new Pmic(i2c_bus_, 0x34);
  166. }
  167. void InitializeSpi() {
  168. spi_bus_config_t buscfg = {};
  169. buscfg.sclk_io_num = EXAMPLE_PIN_NUM_LCD_PCLK;
  170. buscfg.data0_io_num = EXAMPLE_PIN_NUM_LCD_DATA0;
  171. buscfg.data1_io_num = EXAMPLE_PIN_NUM_LCD_DATA1;
  172. buscfg.data2_io_num = EXAMPLE_PIN_NUM_LCD_DATA2;
  173. buscfg.data3_io_num = EXAMPLE_PIN_NUM_LCD_DATA3;
  174. buscfg.max_transfer_sz = DISPLAY_WIDTH* DISPLAY_HEIGHT* sizeof(uint16_t);
  175. buscfg.flags = SPICOMMON_BUSFLAG_QUAD;
  176. ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
  177. }
  178. void InitializeButtons() {
  179. boot_button_.OnClick([this]() {
  180. auto& app = Application::GetInstance();
  181. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  182. ResetWifiConfiguration();
  183. }
  184. app.ToggleChatState();
  185. });
  186. #if CONFIG_USE_DEVICE_AEC
  187. boot_button_.OnDoubleClick([this]() {
  188. auto& app = Application::GetInstance();
  189. if (app.GetDeviceState() == kDeviceStateIdle) {
  190. app.SetAecMode(app.GetAecMode() == kAecOff ? kAecOnDeviceSide : kAecOff);
  191. }
  192. });
  193. #endif
  194. }
  195. void InitializeSH8601Display() {
  196. esp_lcd_panel_io_handle_t panel_io = nullptr;
  197. esp_lcd_panel_handle_t panel = nullptr;
  198. // 液晶屏控制IO初始化
  199. ESP_LOGD(TAG, "Install panel IO");
  200. esp_lcd_panel_io_spi_config_t io_config = SH8601_PANEL_IO_QSPI_CONFIG(
  201. EXAMPLE_PIN_NUM_LCD_CS,
  202. nullptr,
  203. nullptr);
  204. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io));
  205. // 初始化液晶屏驱动芯片
  206. ESP_LOGD(TAG, "Install LCD driver");
  207. const sh8601_vendor_config_t vendor_config = {
  208. .init_cmds = &vendor_specific_init[0],
  209. .init_cmds_size = sizeof(vendor_specific_init) / sizeof(sh8601_lcd_init_cmd_t),
  210. .flags = {
  211. .use_qspi_interface = 1,
  212. }};
  213. esp_lcd_panel_dev_config_t panel_config = {};
  214. panel_config.reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST;
  215. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  216. panel_config.bits_per_pixel = 16;
  217. panel_config.vendor_config = (void* )&vendor_config;
  218. ESP_ERROR_CHECK(esp_lcd_new_panel_sh8601(panel_io, &panel_config, &panel));
  219. esp_lcd_panel_set_gap(panel, 0x16, 0);
  220. esp_lcd_panel_reset(panel);
  221. esp_lcd_panel_init(panel);
  222. esp_lcd_panel_invert_color(panel, false);
  223. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  224. esp_lcd_panel_disp_on_off(panel, true);
  225. display_ = new CustomLcdDisplay(panel_io, panel,
  226. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  227. backlight_ = new CustomBacklight(panel_io);
  228. backlight_->RestoreBrightness();
  229. }
  230. void InitializeTouch() {
  231. esp_lcd_touch_handle_t tp;
  232. esp_lcd_touch_config_t tp_cfg = {
  233. .x_max = DISPLAY_WIDTH - 1,
  234. .y_max = DISPLAY_HEIGHT - 1,
  235. .rst_gpio_num = GPIO_NUM_9,
  236. .int_gpio_num = GPIO_NUM_38,
  237. .levels = {
  238. .reset = 0,
  239. .interrupt = 0,
  240. },
  241. .flags = {
  242. .swap_xy = 0,
  243. .mirror_x = 0,
  244. .mirror_y = 0,
  245. },
  246. };
  247. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  248. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
  249. tp_io_config.scl_speed_hz = 400* 1000;
  250. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle));
  251. ESP_LOGI(TAG, "Initialize touch controller");
  252. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp));
  253. const lvgl_port_touch_cfg_t touch_cfg = {
  254. .disp = lv_display_get_default(),
  255. .handle = tp,
  256. };
  257. lvgl_port_add_touch(&touch_cfg);
  258. ESP_LOGI(TAG, "Touch panel initialized successfully");
  259. }
  260. // 初始化工具
  261. void InitializeTools() {
  262. auto &mcp_server = McpServer::GetInstance();
  263. mcp_server.AddTool("self.system.reconfigure_wifi",
  264. "Reboot the device and enter WiFi configuration mode.\n"
  265. "**CAUTION** You must ask the user to confirm this action.",
  266. PropertyList(), [this](const PropertyList& properties) {
  267. ResetWifiConfiguration();
  268. return true;
  269. });
  270. }
  271. public:
  272. WaveshareEsp32s3TouchAMOLED2inch06() : boot_button_(BOOT_BUTTON_GPIO) {
  273. InitializePowerSaveTimer();
  274. InitializeCodecI2c();
  275. InitializeAxp2101();
  276. InitializeSpi();
  277. InitializeSH8601Display();
  278. InitializeTouch();
  279. InitializeButtons();
  280. InitializeTools();
  281. }
  282. virtual AudioCodec* GetAudioCodec() override {
  283. static BoxAudioCodec audio_codec(
  284. i2c_bus_,
  285. AUDIO_INPUT_SAMPLE_RATE,
  286. AUDIO_OUTPUT_SAMPLE_RATE,
  287. AUDIO_I2S_GPIO_MCLK,
  288. AUDIO_I2S_GPIO_BCLK,
  289. AUDIO_I2S_GPIO_WS,
  290. AUDIO_I2S_GPIO_DOUT,
  291. AUDIO_I2S_GPIO_DIN,
  292. AUDIO_CODEC_PA_PIN,
  293. AUDIO_CODEC_ES8311_ADDR,
  294. AUDIO_CODEC_ES7210_ADDR,
  295. AUDIO_INPUT_REFERENCE);
  296. return &audio_codec;
  297. }
  298. virtual Display* GetDisplay() override {
  299. return display_;
  300. }
  301. virtual Backlight* GetBacklight() override {
  302. return backlight_;
  303. }
  304. virtual bool GetBatteryLevel(int &level, bool &charging, bool &discharging) override {
  305. static bool last_discharging = false;
  306. charging = pmic_->IsCharging();
  307. discharging = pmic_->IsDischarging();
  308. if (discharging != last_discharging)
  309. {
  310. power_save_timer_->SetEnabled(discharging);
  311. last_discharging = discharging;
  312. }
  313. level = pmic_->GetBatteryLevel();
  314. return true;
  315. }
  316. virtual void SetPowerSaveMode(bool enabled) override {
  317. if (!enabled)
  318. {
  319. power_save_timer_->WakeUp();
  320. }
  321. WifiBoard::SetPowerSaveMode(enabled);
  322. }
  323. };
  324. DECLARE_BOARD(WaveshareEsp32s3TouchAMOLED2inch06);