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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "audio_codecs/es8311_audio_codec.h"
  6. #include "application.h"
  7. #include "button.h"
  8. #include "led/single_led.h"
  9. #include "iot/thing_manager.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. #define TAG "waveshare_amoled_1_8"
  22. LV_FONT_DECLARE(font_puhui_30_4);
  23. LV_FONT_DECLARE(font_awesome_30_4);
  24. class Pmic : public Axp2101 {
  25. public:
  26. Pmic(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : Axp2101(i2c_bus, addr) {
  27. WriteReg(0x22, 0b110); // PWRON > OFFLEVEL as POWEROFF Source enable
  28. WriteReg(0x27, 0x10); // hold 4s to power off
  29. // Disable All DCs but DC1
  30. WriteReg(0x80, 0x01);
  31. // Disable All LDOs
  32. WriteReg(0x90, 0x00);
  33. WriteReg(0x91, 0x00);
  34. // Set DC1 to 3.3V
  35. WriteReg(0x82, (3300 - 1500) / 100);
  36. // Set ALDO1 to 3.3V
  37. WriteReg(0x92, (3300 - 500) / 100);
  38. // Enable ALDO1(MIC)
  39. WriteReg(0x90, 0x01);
  40. WriteReg(0x64, 0x02); // CV charger voltage setting to 4.1V
  41. WriteReg(0x61, 0x02); // set Main battery precharge current to 50mA
  42. WriteReg(0x62, 0x08); // set Main battery charger current to 400mA ( 0x08-200mA, 0x09-300mA, 0x0A-400mA )
  43. WriteReg(0x63, 0x01); // set Main battery term charge current to 25mA
  44. }
  45. };
  46. #define LCD_OPCODE_WRITE_CMD (0x02ULL)
  47. #define LCD_OPCODE_READ_CMD (0x03ULL)
  48. #define LCD_OPCODE_WRITE_COLOR (0x32ULL)
  49. static const sh8601_lcd_init_cmd_t vendor_specific_init[] = {
  50. {0x11, (uint8_t[]){0x00}, 0, 120},
  51. {0x44, (uint8_t[]){0x01, 0xD1}, 2, 0},
  52. {0x35, (uint8_t[]){0x00}, 1, 0},
  53. {0x53, (uint8_t[]){0x20}, 1, 10},
  54. {0x2A, (uint8_t[]){0x00, 0x00, 0x01, 0x6F}, 4, 0},
  55. {0x2B, (uint8_t[]){0x00, 0x00, 0x01, 0xBF}, 4, 0},
  56. {0x51, (uint8_t[]){0x00}, 1, 10},
  57. {0x29, (uint8_t[]){0x00}, 0, 10}
  58. };
  59. // 在waveshare_amoled_1_8类之前添加新的显示类
  60. class CustomLcdDisplay : public SpiLcdDisplay {
  61. public:
  62. CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
  63. esp_lcd_panel_handle_t panel_handle,
  64. int width,
  65. int height,
  66. int offset_x,
  67. int offset_y,
  68. bool mirror_x,
  69. bool mirror_y,
  70. bool swap_xy)
  71. : SpiLcdDisplay(io_handle, panel_handle,
  72. width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  73. {
  74. .text_font = &font_puhui_30_4,
  75. .icon_font = &font_awesome_30_4,
  76. .emoji_font = font_emoji_64_init(),
  77. }) {
  78. DisplayLockGuard lock(this);
  79. lv_obj_set_style_pad_left(status_bar_, LV_HOR_RES * 0.1, 0);
  80. lv_obj_set_style_pad_right(status_bar_, LV_HOR_RES * 0.1, 0);
  81. }
  82. };
  83. class CustomBacklight : public Backlight {
  84. public:
  85. CustomBacklight(esp_lcd_panel_io_handle_t panel_io) : Backlight(), panel_io_(panel_io) {}
  86. protected:
  87. esp_lcd_panel_io_handle_t panel_io_;
  88. virtual void SetBrightnessImpl(uint8_t brightness) override {
  89. auto display = Board::GetInstance().GetDisplay();
  90. DisplayLockGuard lock(display);
  91. uint8_t data[1] = {((uint8_t)((255 * brightness) / 100))};
  92. int lcd_cmd = 0x51;
  93. lcd_cmd &= 0xff;
  94. lcd_cmd <<= 8;
  95. lcd_cmd |= LCD_OPCODE_WRITE_CMD << 24;
  96. esp_lcd_panel_io_tx_param(panel_io_, lcd_cmd, &data, sizeof(data));
  97. }
  98. };
  99. class waveshare_amoled_1_8 : public WifiBoard {
  100. private:
  101. i2c_master_bus_handle_t codec_i2c_bus_;
  102. Pmic* pmic_ = nullptr;
  103. Button boot_button_;
  104. CustomLcdDisplay* display_;
  105. CustomBacklight* backlight_;
  106. esp_io_expander_handle_t io_expander = NULL;
  107. PowerSaveTimer* power_save_timer_;
  108. void InitializePowerSaveTimer() {
  109. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  110. power_save_timer_->OnEnterSleepMode([this]() {
  111. ESP_LOGI(TAG, "Enabling sleep mode");
  112. auto display = GetDisplay();
  113. display->SetChatMessage("system", "");
  114. display->SetEmotion("sleepy");
  115. GetBacklight()->SetBrightness(20);
  116. });
  117. power_save_timer_->OnExitSleepMode([this]() {
  118. auto display = GetDisplay();
  119. display->SetChatMessage("system", "");
  120. display->SetEmotion("neutral");
  121. GetBacklight()->RestoreBrightness();
  122. });
  123. power_save_timer_->OnShutdownRequest([this]() {
  124. pmic_->PowerOff();
  125. });
  126. power_save_timer_->SetEnabled(true);
  127. }
  128. void InitializeCodecI2c() {
  129. // Initialize I2C peripheral
  130. i2c_master_bus_config_t i2c_bus_cfg = {
  131. .i2c_port = I2C_NUM_0,
  132. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  133. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  134. .clk_source = I2C_CLK_SRC_DEFAULT,
  135. .glitch_ignore_cnt = 7,
  136. .intr_priority = 0,
  137. .trans_queue_depth = 0,
  138. .flags = {
  139. .enable_internal_pullup = 1,
  140. },
  141. };
  142. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  143. }
  144. void InitializeTca9554(void) {
  145. esp_err_t ret = esp_io_expander_new_i2c_tca9554(codec_i2c_bus_, I2C_ADDRESS, &io_expander);
  146. if(ret != ESP_OK)
  147. ESP_LOGE(TAG, "TCA9554 create returned error");
  148. 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);
  149. ret |= esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_4, IO_EXPANDER_INPUT);
  150. ESP_ERROR_CHECK(ret);
  151. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1|IO_EXPANDER_PIN_NUM_2, 1);
  152. ESP_ERROR_CHECK(ret);
  153. vTaskDelay(pdMS_TO_TICKS(100));
  154. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1|IO_EXPANDER_PIN_NUM_2, 0);
  155. ESP_ERROR_CHECK(ret);
  156. vTaskDelay(pdMS_TO_TICKS(300));
  157. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1|IO_EXPANDER_PIN_NUM_2, 1);
  158. ESP_ERROR_CHECK(ret);
  159. }
  160. void InitializeAxp2101() {
  161. ESP_LOGI(TAG, "Init AXP2101");
  162. pmic_ = new Pmic(codec_i2c_bus_, 0x34);
  163. }
  164. void InitializeSpi() {
  165. spi_bus_config_t buscfg = {};
  166. buscfg.sclk_io_num = GPIO_NUM_11;
  167. buscfg.data0_io_num = GPIO_NUM_4;
  168. buscfg.data1_io_num = GPIO_NUM_5;
  169. buscfg.data2_io_num = GPIO_NUM_6;
  170. buscfg.data3_io_num = GPIO_NUM_7;
  171. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  172. buscfg.flags = SPICOMMON_BUSFLAG_QUAD;
  173. ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
  174. }
  175. void InitializeButtons() {
  176. boot_button_.OnClick([this]() {
  177. auto& app = Application::GetInstance();
  178. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  179. ResetWifiConfiguration();
  180. }
  181. app.ToggleChatState();
  182. });
  183. }
  184. void InitializeSH8601Display() {
  185. esp_lcd_panel_io_handle_t panel_io = nullptr;
  186. esp_lcd_panel_handle_t panel = nullptr;
  187. // 液晶屏控制IO初始化
  188. ESP_LOGD(TAG, "Install panel IO");
  189. esp_lcd_panel_io_spi_config_t io_config = SH8601_PANEL_IO_QSPI_CONFIG(
  190. EXAMPLE_PIN_NUM_LCD_CS,
  191. nullptr,
  192. nullptr
  193. );
  194. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io));
  195. // 初始化液晶屏驱动芯片
  196. ESP_LOGD(TAG, "Install LCD driver");
  197. const sh8601_vendor_config_t vendor_config = {
  198. .init_cmds = &vendor_specific_init[0],
  199. .init_cmds_size = sizeof(vendor_specific_init) / sizeof(sh8601_lcd_init_cmd_t),
  200. .flags ={
  201. .use_qspi_interface = 1,
  202. }
  203. };
  204. esp_lcd_panel_dev_config_t panel_config = {};
  205. panel_config.reset_gpio_num = GPIO_NUM_NC;
  206. panel_config.flags.reset_active_high = 1,
  207. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  208. panel_config.bits_per_pixel = 16;
  209. panel_config.vendor_config = (void *)&vendor_config;
  210. ESP_ERROR_CHECK(esp_lcd_new_panel_sh8601(panel_io, &panel_config, &panel));
  211. esp_lcd_panel_reset(panel);
  212. esp_lcd_panel_init(panel);
  213. esp_lcd_panel_invert_color(panel, false);
  214. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  215. esp_lcd_panel_disp_on_off(panel, true);
  216. display_ = new CustomLcdDisplay(panel_io, panel,
  217. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  218. backlight_ = new CustomBacklight(panel_io);
  219. backlight_->RestoreBrightness();
  220. }
  221. // 物联网初始化,添加对 AI 可见设备
  222. void InitializeIot() {
  223. auto& thing_manager = iot::ThingManager::GetInstance();
  224. thing_manager.AddThing(iot::CreateThing("Speaker"));
  225. thing_manager.AddThing(iot::CreateThing("Screen"));
  226. thing_manager.AddThing(iot::CreateThing("Battery"));
  227. thing_manager.AddThing(iot::CreateThing("BoardControl"));
  228. }
  229. public:
  230. waveshare_amoled_1_8() :
  231. boot_button_(BOOT_BUTTON_GPIO) {
  232. InitializePowerSaveTimer();
  233. InitializeCodecI2c();
  234. InitializeTca9554();
  235. InitializeAxp2101();
  236. InitializeSpi();
  237. InitializeSH8601Display();
  238. InitializeButtons();
  239. InitializeIot();
  240. }
  241. virtual AudioCodec* GetAudioCodec() override {
  242. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  243. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  244. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  245. return &audio_codec;
  246. }
  247. virtual Display* GetDisplay() override {
  248. return display_;
  249. }
  250. virtual Backlight* GetBacklight() override {
  251. return backlight_;
  252. }
  253. virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging) override {
  254. static bool last_discharging = false;
  255. charging = pmic_->IsCharging();
  256. discharging = pmic_->IsDischarging();
  257. if (discharging != last_discharging) {
  258. power_save_timer_->SetEnabled(discharging);
  259. last_discharging = discharging;
  260. }
  261. level = pmic_->GetBatteryLevel();
  262. return true;
  263. }
  264. virtual void SetPowerSaveMode(bool enabled) override {
  265. if (!enabled) {
  266. power_save_timer_->WakeUp();
  267. }
  268. WifiBoard::SetPowerSaveMode(enabled);
  269. }
  270. };
  271. DECLARE_BOARD(waveshare_amoled_1_8);