genjutech-s3-1.54tft.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "wifi_board.h"
  2. #include "codecs/es8311_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "system_reset.h"
  5. #include "application.h"
  6. #include "button.h"
  7. #include "config.h"
  8. #include "led/single_led.h"
  9. #include <esp_log.h>
  10. #include <esp_lcd_panel_vendor.h>
  11. #include <driver/i2c_master.h>
  12. #include <wifi_station.h>
  13. #include <esp_efuse_table.h>
  14. #include <freertos/FreeRTOS.h>
  15. #include <freertos/task.h>
  16. #include "power_save_timer.h"
  17. #include "assets/lang_config.h"
  18. #include "power_manager.h"
  19. #define TAG "GenJuTech_s3_1_54TFT"
  20. LV_FONT_DECLARE(font_puhui_20_4);
  21. LV_FONT_DECLARE(font_awesome_20_4);
  22. class SparkBotEs8311AudioCodec : public Es8311AudioCodec {
  23. private:
  24. public:
  25. SparkBotEs8311AudioCodec(void* i2c_master_handle, i2c_port_t i2c_port, int input_sample_rate, int output_sample_rate,
  26. gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
  27. gpio_num_t pa_pin, uint8_t es8311_addr, bool use_mclk = true)
  28. : Es8311AudioCodec(i2c_master_handle, i2c_port, input_sample_rate, output_sample_rate,
  29. mclk, bclk, ws, dout, din,pa_pin, es8311_addr, use_mclk = true) {}
  30. void EnableOutput(bool enable) override {
  31. if (enable == output_enabled_) {
  32. return;
  33. }
  34. if (enable) {
  35. Es8311AudioCodec::EnableOutput(enable);
  36. } else {
  37. // Nothing todo because the display io and PA io conflict
  38. }
  39. }
  40. };
  41. class GenJuTech_s3_1_54TFT : public WifiBoard {
  42. private:
  43. // i2c_master_bus_handle_t display_i2c_bus_;
  44. Button boot_button_;
  45. Button volume_up_button_;
  46. Button volume_down_button_;
  47. LcdDisplay* display_;
  48. i2c_master_bus_handle_t codec_i2c_bus_;
  49. PowerSaveTimer* power_save_timer_;
  50. PowerManager* power_manager_;
  51. void InitializePowerManager() {
  52. power_manager_ = new PowerManager(GPIO_NUM_16);
  53. power_manager_->OnChargingStatusChanged([this](bool is_charging) {
  54. if (is_charging) {
  55. power_save_timer_->SetEnabled(false);
  56. } else {
  57. power_save_timer_->SetEnabled(true);
  58. }
  59. });
  60. }
  61. void InitializePowerSaveTimer() {
  62. power_save_timer_ = new PowerSaveTimer(240, 60);
  63. power_save_timer_->OnEnterSleepMode([this]() {
  64. ESP_LOGI(TAG, "Enabling sleep mode");
  65. auto display = GetDisplay();
  66. display->SetChatMessage("system", "");
  67. display->SetEmotion("sleepy");
  68. auto codec = GetAudioCodec();
  69. codec->EnableInput(false);
  70. });
  71. power_save_timer_->OnExitSleepMode([this]() {
  72. auto codec = GetAudioCodec();
  73. codec->EnableInput(true);
  74. auto display = GetDisplay();
  75. display->SetChatMessage("system", "");
  76. display->SetEmotion("neutral");
  77. });
  78. power_save_timer_->SetEnabled(true);
  79. }
  80. void InitializeCodecI2c() {
  81. // Initialize I2C peripheral
  82. i2c_master_bus_config_t i2c_bus_cfg = {
  83. .i2c_port = I2C_NUM_0,
  84. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  85. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  86. .clk_source = I2C_CLK_SRC_DEFAULT,
  87. .glitch_ignore_cnt = 7,
  88. .intr_priority = 0,
  89. .trans_queue_depth = 0,
  90. .flags = {
  91. .enable_internal_pullup = 1,
  92. },
  93. };
  94. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  95. }
  96. void InitializeSpi() {
  97. spi_bus_config_t buscfg = {};
  98. buscfg.mosi_io_num = DISPLAY_SDA;
  99. buscfg.miso_io_num = GPIO_NUM_NC;
  100. buscfg.sclk_io_num = DISPLAY_SCL;
  101. buscfg.quadwp_io_num = GPIO_NUM_NC;
  102. buscfg.quadhd_io_num = GPIO_NUM_NC;
  103. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  104. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  105. }
  106. void InitializeButtons() {
  107. boot_button_.OnClick([this]() {
  108. power_save_timer_->WakeUp();
  109. auto& app = Application::GetInstance();
  110. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  111. ResetWifiConfiguration();
  112. }
  113. app.ToggleChatState();
  114. });
  115. // boot_button_.OnPressDown([this]() {
  116. // Application::GetInstance().StartListening();
  117. // });
  118. // boot_button_.OnPressUp([this]() {
  119. // Application::GetInstance().StopListening();
  120. // });
  121. volume_up_button_.OnClick([this]() {
  122. power_save_timer_->WakeUp();
  123. auto codec = GetAudioCodec();
  124. auto volume = codec->output_volume() + 10;
  125. if (volume > 100) {
  126. volume = 100;
  127. }
  128. codec->SetOutputVolume(volume);
  129. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  130. });
  131. volume_up_button_.OnLongPress([this]() {
  132. power_save_timer_->WakeUp();
  133. GetAudioCodec()->SetOutputVolume(100);
  134. GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
  135. });
  136. volume_down_button_.OnClick([this]() {
  137. power_save_timer_->WakeUp();
  138. auto codec = GetAudioCodec();
  139. auto volume = codec->output_volume() - 10;
  140. if (volume < 0) {
  141. volume = 0;
  142. }
  143. codec->SetOutputVolume(volume);
  144. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  145. });
  146. volume_down_button_.OnLongPress([this]() {
  147. power_save_timer_->WakeUp();
  148. GetAudioCodec()->SetOutputVolume(0);
  149. GetDisplay()->ShowNotification(Lang::Strings::MUTED);
  150. });
  151. }
  152. void InitializeSt7789Display() {
  153. gpio_config_t config = {
  154. .pin_bit_mask = (1ULL << DISPLAY_RES),
  155. .mode = GPIO_MODE_OUTPUT,
  156. .pull_up_en = GPIO_PULLUP_DISABLE,
  157. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  158. .intr_type = GPIO_INTR_DISABLE,
  159. };
  160. ESP_ERROR_CHECK(gpio_config(&config));
  161. gpio_set_level(DISPLAY_RES, 0);
  162. vTaskDelay(20);
  163. gpio_set_level(DISPLAY_RES, 1);
  164. esp_lcd_panel_io_handle_t panel_io = nullptr;
  165. esp_lcd_panel_handle_t panel = nullptr;
  166. // 液晶屏控制IO初始化
  167. ESP_LOGD(TAG, "Install panel IO");
  168. esp_lcd_panel_io_spi_config_t io_config = {};
  169. io_config.cs_gpio_num = DISPLAY_CS;
  170. io_config.dc_gpio_num = DISPLAY_DC;
  171. io_config.spi_mode = 3;
  172. io_config.pclk_hz = 80 * 1000 * 1000;
  173. io_config.trans_queue_depth = 10;
  174. io_config.lcd_cmd_bits = 8;
  175. io_config.lcd_param_bits = 8;
  176. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  177. // 初始化液晶屏驱动芯片ST7789
  178. ESP_LOGD(TAG, "Install LCD driver");
  179. esp_lcd_panel_dev_config_t panel_config = {};
  180. panel_config.reset_gpio_num = DISPLAY_RES;
  181. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  182. panel_config.bits_per_pixel = 16;
  183. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  184. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  185. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  186. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
  187. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  188. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, true));
  189. display_ = new SpiLcdDisplay(panel_io, panel,
  190. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  191. {
  192. .text_font = &font_puhui_20_4,
  193. .icon_font = &font_awesome_20_4,
  194. .emoji_font = font_emoji_64_init(),
  195. });
  196. }
  197. public:
  198. GenJuTech_s3_1_54TFT() :
  199. boot_button_(BOOT_BUTTON_GPIO),
  200. volume_up_button_(VOLUME_UP_BUTTON_GPIO),
  201. volume_down_button_(VOLUME_DOWN_BUTTON_GPIO) {
  202. ESP_LOGI(TAG, "Initializing GenJuTech S3 1.54 Board");
  203. InitializePowerManager();
  204. InitializePowerSaveTimer();
  205. InitializeCodecI2c();
  206. InitializeSpi();
  207. InitializeButtons();
  208. InitializeSt7789Display();
  209. GetBacklight()->RestoreBrightness();
  210. }
  211. virtual Led* GetLed() override {
  212. static SingleLed led(BUILTIN_LED_GPIO);
  213. return &led;
  214. }
  215. virtual AudioCodec* GetAudioCodec() override {
  216. static SparkBotEs8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  217. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  218. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  219. return &audio_codec;
  220. }
  221. virtual Display *GetDisplay() override {
  222. return display_;
  223. }
  224. virtual Backlight* GetBacklight() override {
  225. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  226. return &backlight;
  227. }
  228. virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
  229. static bool last_discharging = false;
  230. charging = power_manager_->IsCharging();
  231. discharging = power_manager_->IsDischarging();
  232. if (discharging != last_discharging) {
  233. power_save_timer_->SetEnabled(discharging);
  234. last_discharging = discharging;
  235. }
  236. level = power_manager_->GetBatteryLevel();
  237. return true;
  238. }
  239. virtual void SetPowerSaveMode(bool enabled) override {
  240. if (!enabled) {
  241. power_save_timer_->WakeUp();
  242. }
  243. WifiBoard::SetPowerSaveMode(enabled);
  244. }
  245. };
  246. DECLARE_BOARD(GenJuTech_s3_1_54TFT);