magiclick_2p4_board.cc 8.2 KB

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