magiclick_2p4_board.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "wifi_board.h"
  2. #include "display/lcd_display.h"
  3. #include "codecs/es8311_audio_codec.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "led/circular_strip.h"
  7. #include "config.h"
  8. #include "font_awesome_symbols.h"
  9. #include "assets/lang_config.h"
  10. #include <esp_lcd_panel_vendor.h>
  11. #include <wifi_station.h>
  12. #include <esp_log.h>
  13. #include <driver/i2c_master.h>
  14. #include <driver/spi_common.h>
  15. #include <esp_lcd_nv3023.h>
  16. #include "../magiclick-2p5/power_manager.h"
  17. #include "power_save_timer.h"
  18. #define TAG "magiclick_2p4"
  19. LV_FONT_DECLARE(font_puhui_16_4);
  20. LV_FONT_DECLARE(font_awesome_16_4);
  21. class NV3023Display : public SpiLcdDisplay {
  22. public:
  23. NV3023Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  24. int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy)
  25. : SpiLcdDisplay(panel_io, panel, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  26. {
  27. .text_font = &font_puhui_16_4,
  28. .icon_font = &font_awesome_16_4,
  29. .emoji_font = font_emoji_32_init(),
  30. }) {
  31. DisplayLockGuard lock(this);
  32. // 只需要覆盖颜色相关的样式
  33. auto screen = lv_disp_get_scr_act(lv_disp_get_default());
  34. lv_obj_set_style_text_color(screen, lv_color_black(), 0);
  35. // 设置容器背景色
  36. lv_obj_set_style_bg_color(container_, lv_color_black(), 0);
  37. // 设置状态栏背景色和文本颜色
  38. lv_obj_set_style_bg_color(status_bar_, lv_color_white(), 0);
  39. lv_obj_set_style_text_color(network_label_, lv_color_black(), 0);
  40. lv_obj_set_style_text_color(notification_label_, lv_color_black(), 0);
  41. lv_obj_set_style_text_color(status_label_, lv_color_black(), 0);
  42. lv_obj_set_style_text_color(mute_label_, lv_color_black(), 0);
  43. lv_obj_set_style_text_color(battery_label_, lv_color_black(), 0);
  44. // 设置内容区背景色和文本颜色
  45. lv_obj_set_style_bg_color(content_, lv_color_black(), 0);
  46. lv_obj_set_style_border_width(content_, 0, 0);
  47. lv_obj_set_style_text_color(emotion_label_, lv_color_white(), 0);
  48. lv_obj_set_style_text_color(chat_message_label_, lv_color_white(), 0);
  49. }
  50. };
  51. class magiclick_2p4 : public WifiBoard {
  52. private:
  53. i2c_master_bus_handle_t codec_i2c_bus_;
  54. Button main_button_;
  55. Button left_button_;
  56. Button right_button_;
  57. NV3023Display* display_;
  58. PowerSaveTimer* power_save_timer_;
  59. PowerManager* power_manager_;
  60. esp_lcd_panel_io_handle_t panel_io = nullptr;
  61. esp_lcd_panel_handle_t panel = nullptr;
  62. void InitializePowerManager() {
  63. power_manager_ = new PowerManager(GPIO_NUM_48);
  64. power_manager_->OnChargingStatusChanged([this](bool is_charging) {
  65. if (is_charging) {
  66. power_save_timer_->SetEnabled(false);
  67. } else {
  68. power_save_timer_->SetEnabled(true);
  69. }
  70. });
  71. }
  72. void InitializePowerSaveTimer() {
  73. power_save_timer_ = new PowerSaveTimer(240, 60, -1);
  74. power_save_timer_->OnEnterSleepMode([this]() {
  75. ESP_LOGI(TAG, "Enabling sleep mode");
  76. display_->SetChatMessage("system", "");
  77. display_->SetEmotion("sleepy");
  78. GetBacklight()->SetBrightness(1);
  79. });
  80. power_save_timer_->OnExitSleepMode([this]() {
  81. display_->SetChatMessage("system", "");
  82. display_->SetEmotion("neutral");
  83. GetBacklight()->RestoreBrightness();
  84. });
  85. power_save_timer_->SetEnabled(true);
  86. }
  87. void InitializeCodecI2c() {
  88. // Initialize I2C peripheral
  89. i2c_master_bus_config_t i2c_bus_cfg = {
  90. .i2c_port = I2C_NUM_0,
  91. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  92. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  93. .clk_source = I2C_CLK_SRC_DEFAULT,
  94. .glitch_ignore_cnt = 7,
  95. .intr_priority = 0,
  96. .trans_queue_depth = 0,
  97. .flags = {
  98. .enable_internal_pullup = 1,
  99. },
  100. };
  101. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  102. }
  103. void InitializeButtons() {
  104. main_button_.OnClick([this]() {
  105. auto& app = Application::GetInstance();
  106. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  107. ResetWifiConfiguration();
  108. }
  109. });
  110. main_button_.OnPressDown([this]() {
  111. power_save_timer_->WakeUp();
  112. Application::GetInstance().StartListening();
  113. });
  114. main_button_.OnPressUp([this]() {
  115. Application::GetInstance().StopListening();
  116. });
  117. left_button_.OnClick([this]() {
  118. power_save_timer_->WakeUp();
  119. auto& app = Application::GetInstance();
  120. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  121. ResetWifiConfiguration();
  122. }
  123. auto codec = GetAudioCodec();
  124. auto volume = codec->output_volume() - 10;
  125. if (volume < 0) {
  126. volume = 0;
  127. }
  128. codec->SetOutputVolume(volume);
  129. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  130. });
  131. left_button_.OnLongPress([this]() {
  132. power_save_timer_->WakeUp();
  133. GetAudioCodec()->SetOutputVolume(0);
  134. GetDisplay()->ShowNotification(Lang::Strings::MUTED);
  135. });
  136. right_button_.OnClick([this]() {
  137. power_save_timer_->WakeUp();
  138. auto codec = GetAudioCodec();
  139. auto volume = codec->output_volume() + 10;
  140. if (volume > 100) {
  141. volume = 100;
  142. }
  143. codec->SetOutputVolume(volume);
  144. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  145. });
  146. right_button_.OnLongPress([this]() {
  147. power_save_timer_->WakeUp();
  148. GetAudioCodec()->SetOutputVolume(100);
  149. GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
  150. });
  151. }
  152. void InitializeLedPower() {
  153. // 设置GPIO模式
  154. gpio_reset_pin(BUILTIN_LED_POWER);
  155. gpio_set_direction(BUILTIN_LED_POWER, GPIO_MODE_OUTPUT);
  156. gpio_set_level(BUILTIN_LED_POWER, BUILTIN_LED_POWER_OUTPUT_INVERT ? 0 : 1);
  157. }
  158. void InitializeSpi() {
  159. spi_bus_config_t buscfg = {};
  160. buscfg.mosi_io_num = DISPLAY_SDA_PIN;
  161. buscfg.miso_io_num = GPIO_NUM_NC;
  162. buscfg.sclk_io_num = DISPLAY_SCL_PIN;
  163. buscfg.quadwp_io_num = GPIO_NUM_NC;
  164. buscfg.quadhd_io_num = GPIO_NUM_NC;
  165. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  166. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  167. }
  168. void InitializeNv3023Display(){
  169. // esp_lcd_panel_io_handle_t panel_io = nullptr;
  170. // esp_lcd_panel_handle_t panel = nullptr;
  171. // 液晶屏控制IO初始化
  172. ESP_LOGD(TAG, "Install panel IO");
  173. esp_lcd_panel_io_spi_config_t io_config = {};
  174. io_config.cs_gpio_num = DISPLAY_CS_PIN;
  175. io_config.dc_gpio_num = DISPLAY_DC_PIN;
  176. io_config.spi_mode = 0;
  177. io_config.pclk_hz = 40 * 1000 * 1000;
  178. io_config.trans_queue_depth = 10;
  179. io_config.lcd_cmd_bits = 8;
  180. io_config.lcd_param_bits = 8;
  181. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  182. // 初始化液晶屏驱动芯片NV3023
  183. ESP_LOGD(TAG, "Install LCD driver");
  184. esp_lcd_panel_dev_config_t panel_config = {};
  185. panel_config.reset_gpio_num = DISPLAY_RST_PIN;
  186. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR;
  187. panel_config.bits_per_pixel = 16;
  188. ESP_ERROR_CHECK(esp_lcd_new_panel_nv3023(panel_io, &panel_config, &panel));
  189. esp_lcd_panel_reset(panel);
  190. esp_lcd_panel_init(panel);
  191. esp_lcd_panel_invert_color(panel, false);
  192. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  193. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  194. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
  195. display_ = new NV3023Display(panel_io, panel,
  196. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  197. }
  198. public:
  199. magiclick_2p4() :
  200. main_button_(MAIN_BUTTON_GPIO),
  201. left_button_(LEFT_BUTTON_GPIO),
  202. right_button_(RIGHT_BUTTON_GPIO) {
  203. InitializeLedPower();
  204. InitializePowerManager();
  205. InitializePowerSaveTimer();
  206. InitializeCodecI2c();
  207. InitializeButtons();
  208. InitializeSpi();
  209. InitializeNv3023Display();
  210. GetBacklight()->RestoreBrightness();
  211. }
  212. virtual Led* GetLed() override {
  213. static CircularStrip led(BUILTIN_LED_GPIO, BUILTIN_LED_NUM);
  214. return &led;
  215. }
  216. virtual AudioCodec* GetAudioCodec() override {
  217. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  218. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  219. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  220. return &audio_codec;
  221. }
  222. virtual Display* GetDisplay() override {
  223. return display_;
  224. }
  225. virtual Backlight* GetBacklight() override {
  226. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  227. return &backlight;
  228. }
  229. virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
  230. static bool last_discharging = false;
  231. charging = power_manager_->IsCharging();
  232. discharging = power_manager_->IsDischarging();
  233. if (discharging != last_discharging) {
  234. power_save_timer_->SetEnabled(discharging);
  235. last_discharging = discharging;
  236. }
  237. level = power_manager_->GetBatteryLevel();
  238. return true;
  239. }
  240. virtual void SetPowerSaveMode(bool enabled) override {
  241. if (!enabled) {
  242. power_save_timer_->WakeUp();
  243. }
  244. WifiBoard::SetPowerSaveMode(enabled);
  245. }
  246. };
  247. DECLARE_BOARD(magiclick_2p4);