magiclick_c3_v2_board.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/single_led.h"
  7. #include "config.h"
  8. #include "power_save_timer.h"
  9. #include "font_awesome_symbols.h"
  10. #include <wifi_station.h>
  11. #include <esp_log.h>
  12. #include <driver/i2c_master.h>
  13. #include <driver/spi_common.h>
  14. #include <esp_lcd_panel_vendor.h>
  15. #include <esp_lcd_panel_io.h>
  16. #include <esp_lcd_panel_ops.h>
  17. #include <esp_lcd_gc9a01.h>
  18. #include <esp_efuse_table.h>
  19. #define TAG "magiclick_c3_v2"
  20. LV_FONT_DECLARE(font_puhui_16_4);
  21. LV_FONT_DECLARE(font_awesome_16_4);
  22. class GC9107Display : public SpiLcdDisplay {
  23. public:
  24. GC9107Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  25. int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy)
  26. : SpiLcdDisplay(panel_io, panel, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  27. {
  28. .text_font = &font_puhui_16_4,
  29. .icon_font = &font_awesome_16_4,
  30. .emoji_font = font_emoji_32_init(),
  31. }) {
  32. DisplayLockGuard lock(this);
  33. // 只需要覆盖颜色相关的样式
  34. auto screen = lv_disp_get_scr_act(lv_disp_get_default());
  35. lv_obj_set_style_text_color(screen, lv_color_black(), 0);
  36. // 设置容器背景色
  37. lv_obj_set_style_bg_color(container_, lv_color_black(), 0);
  38. // 设置状态栏背景色和文本颜色
  39. lv_obj_set_style_bg_color(status_bar_, lv_color_make(0x1e, 0x90, 0xff), 0);
  40. lv_obj_set_style_text_color(network_label_, lv_color_black(), 0);
  41. lv_obj_set_style_text_color(notification_label_, lv_color_black(), 0);
  42. lv_obj_set_style_text_color(status_label_, lv_color_black(), 0);
  43. lv_obj_set_style_text_color(mute_label_, lv_color_black(), 0);
  44. lv_obj_set_style_text_color(battery_label_, lv_color_black(), 0);
  45. // 设置内容区背景色和文本颜色
  46. lv_obj_set_style_bg_color(content_, lv_color_black(), 0);
  47. lv_obj_set_style_border_width(content_, 0, 0);
  48. lv_obj_set_style_text_color(emotion_label_, lv_color_white(), 0);
  49. lv_obj_set_style_text_color(chat_message_label_, lv_color_white(), 0);
  50. }
  51. };
  52. static const gc9a01_lcd_init_cmd_t gc9107_lcd_init_cmds[] = {
  53. // {cmd, { data }, data_size, delay_ms}
  54. {0xfe, (uint8_t[]){0x00}, 0, 0},
  55. {0xef, (uint8_t[]){0x00}, 0, 0},
  56. {0xb0, (uint8_t[]){0xc0}, 1, 0},
  57. {0xb1, (uint8_t[]){0x80}, 1, 0},
  58. {0xb2, (uint8_t[]){0x27}, 1, 0},
  59. {0xb3, (uint8_t[]){0x13}, 1, 0},
  60. {0xb6, (uint8_t[]){0x19}, 1, 0},
  61. {0xb7, (uint8_t[]){0x05}, 1, 0},
  62. {0xac, (uint8_t[]){0xc8}, 1, 0},
  63. {0xab, (uint8_t[]){0x0f}, 1, 0},
  64. {0x3a, (uint8_t[]){0x05}, 1, 0},
  65. {0xb4, (uint8_t[]){0x04}, 1, 0},
  66. {0xa8, (uint8_t[]){0x08}, 1, 0},
  67. {0xb8, (uint8_t[]){0x08}, 1, 0},
  68. {0xea, (uint8_t[]){0x02}, 1, 0},
  69. {0xe8, (uint8_t[]){0x2A}, 1, 0},
  70. {0xe9, (uint8_t[]){0x47}, 1, 0},
  71. {0xe7, (uint8_t[]){0x5f}, 1, 0},
  72. {0xc6, (uint8_t[]){0x21}, 1, 0},
  73. {0xc7, (uint8_t[]){0x15}, 1, 0},
  74. {0xf0,
  75. (uint8_t[]){0x1D, 0x38, 0x09, 0x4D, 0x92, 0x2F, 0x35, 0x52, 0x1E, 0x0C,
  76. 0x04, 0x12, 0x14, 0x1f},
  77. 14, 0},
  78. {0xf1,
  79. (uint8_t[]){0x16, 0x40, 0x1C, 0x54, 0xA9, 0x2D, 0x2E, 0x56, 0x10, 0x0D,
  80. 0x0C, 0x1A, 0x14, 0x1E},
  81. 14, 0},
  82. {0xf4, (uint8_t[]){0x00, 0x00, 0xFF}, 3, 0},
  83. {0xba, (uint8_t[]){0xFF, 0xFF}, 2, 0},
  84. };
  85. class magiclick_c3_v2 : public WifiBoard {
  86. private:
  87. i2c_master_bus_handle_t codec_i2c_bus_;
  88. Button boot_button_;
  89. GC9107Display* display_;
  90. PowerSaveTimer* power_save_timer_;
  91. void InitializePowerSaveTimer() {
  92. power_save_timer_ = new PowerSaveTimer(160);
  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. auto codec = GetAudioCodec();
  100. codec->EnableInput(false);
  101. });
  102. power_save_timer_->OnExitSleepMode([this]() {
  103. auto codec = GetAudioCodec();
  104. codec->EnableInput(true);
  105. auto display = GetDisplay();
  106. display->SetChatMessage("system", "");
  107. display->SetEmotion("neutral");
  108. GetBacklight()->RestoreBrightness();
  109. });
  110. power_save_timer_->SetEnabled(true);
  111. }
  112. void InitializeCodecI2c() {
  113. // Initialize I2C peripheral
  114. i2c_master_bus_config_t i2c_bus_cfg = {
  115. .i2c_port = I2C_NUM_0,
  116. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  117. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  118. .clk_source = I2C_CLK_SRC_DEFAULT,
  119. .glitch_ignore_cnt = 7,
  120. .intr_priority = 0,
  121. .trans_queue_depth = 0,
  122. .flags = {
  123. .enable_internal_pullup = 1,
  124. },
  125. };
  126. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  127. // Print I2C bus info
  128. if (i2c_master_probe(codec_i2c_bus_, 0x18, 1000) != ESP_OK) {
  129. while (true) {
  130. ESP_LOGE(TAG, "Failed to probe I2C bus, please check if you have installed the correct firmware");
  131. vTaskDelay(1000 / portTICK_PERIOD_MS);
  132. }
  133. }
  134. }
  135. void InitializeButtons() {
  136. boot_button_.OnClick([this]() {
  137. auto& app = Application::GetInstance();
  138. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  139. ResetWifiConfiguration();
  140. }
  141. });
  142. boot_button_.OnPressDown([this]() {
  143. power_save_timer_->WakeUp();
  144. Application::GetInstance().StartListening();
  145. });
  146. boot_button_.OnPressUp([this]() {
  147. Application::GetInstance().StopListening();
  148. });
  149. }
  150. void InitializeSpi() {
  151. spi_bus_config_t buscfg = {};
  152. buscfg.mosi_io_num = DISPLAY_SDA_PIN;
  153. buscfg.miso_io_num = GPIO_NUM_NC;
  154. buscfg.sclk_io_num = DISPLAY_SCL_PIN;
  155. buscfg.quadwp_io_num = GPIO_NUM_NC;
  156. buscfg.quadhd_io_num = GPIO_NUM_NC;
  157. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  158. ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
  159. }
  160. void InitializeGc9107Display(){
  161. esp_lcd_panel_io_handle_t panel_io = nullptr;
  162. esp_lcd_panel_handle_t panel = nullptr;
  163. // 液晶屏控制IO初始化
  164. ESP_LOGD(TAG, "Install panel IO");
  165. esp_lcd_panel_io_spi_config_t io_config = {};
  166. io_config.cs_gpio_num = DISPLAY_CS_PIN;
  167. io_config.dc_gpio_num = DISPLAY_DC_PIN;
  168. io_config.spi_mode = 0;
  169. io_config.pclk_hz = 40 * 1000 * 1000;
  170. io_config.trans_queue_depth = 10;
  171. io_config.lcd_cmd_bits = 8;
  172. io_config.lcd_param_bits = 8;
  173. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io));
  174. // 初始化液晶屏驱动芯片GC9107
  175. ESP_LOGD(TAG, "Install LCD driver");
  176. gc9a01_vendor_config_t gc9107_vendor_config = {
  177. .init_cmds = gc9107_lcd_init_cmds,
  178. .init_cmds_size = sizeof(gc9107_lcd_init_cmds) / sizeof(gc9a01_lcd_init_cmd_t),
  179. };
  180. esp_lcd_panel_dev_config_t panel_config = {};
  181. panel_config.reset_gpio_num = DISPLAY_RST_PIN;
  182. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  183. panel_config.bits_per_pixel = 16;
  184. panel_config.vendor_config = &gc9107_vendor_config;
  185. esp_lcd_new_panel_gc9a01(panel_io, &panel_config, &panel);
  186. esp_lcd_panel_reset(panel);
  187. esp_lcd_panel_init(panel);
  188. esp_lcd_panel_invert_color(panel, false);
  189. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  190. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  191. esp_lcd_panel_disp_on_off(panel, true);
  192. display_ = new GC9107Display(panel_io, panel,
  193. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  194. }
  195. public:
  196. magiclick_c3_v2() : boot_button_(BOOT_BUTTON_GPIO) {
  197. InitializeCodecI2c();
  198. InitializeButtons();
  199. InitializePowerSaveTimer();
  200. InitializeSpi();
  201. InitializeGc9107Display();
  202. GetBacklight()->RestoreBrightness();
  203. // 把 ESP32C3 的 VDD SPI 引脚作为普通 GPIO 口使用
  204. esp_efuse_write_field_bit(ESP_EFUSE_VDD_SPI_AS_GPIO);
  205. }
  206. virtual Led* GetLed() override {
  207. static SingleLed led(BUILTIN_LED_GPIO);
  208. return &led;
  209. }
  210. virtual AudioCodec* GetAudioCodec() override {
  211. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  212. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  213. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  214. return &audio_codec;
  215. }
  216. virtual Display* GetDisplay() override {
  217. return display_;
  218. }
  219. virtual Backlight* GetBacklight() override {
  220. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  221. return &backlight;
  222. }
  223. };
  224. DECLARE_BOARD(magiclick_c3_v2);