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

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