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

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