atoms3_echo_base.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "wifi_board.h"
  2. #include "codecs/es8311_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "config.h"
  7. #include "i2c_device.h"
  8. #include "assets/lang_config.h"
  9. #include <esp_log.h>
  10. #include <driver/i2c_master.h>
  11. #include <wifi_station.h>
  12. #include <esp_lcd_panel_io.h>
  13. #include <esp_lcd_panel_ops.h>
  14. #include <esp_lcd_gc9a01.h>
  15. #define TAG "AtomS3+EchoBase"
  16. LV_FONT_DECLARE(font_puhui_16_4);
  17. LV_FONT_DECLARE(font_awesome_16_4);
  18. static const gc9a01_lcd_init_cmd_t gc9107_lcd_init_cmds[] = {
  19. // {cmd, { data }, data_size, delay_ms}
  20. {0xfe, (uint8_t[]){0x00}, 0, 0},
  21. {0xef, (uint8_t[]){0x00}, 0, 0},
  22. {0xb0, (uint8_t[]){0xc0}, 1, 0},
  23. {0xb2, (uint8_t[]){0x2f}, 1, 0},
  24. {0xb3, (uint8_t[]){0x03}, 1, 0},
  25. {0xb6, (uint8_t[]){0x19}, 1, 0},
  26. {0xb7, (uint8_t[]){0x01}, 1, 0},
  27. {0xac, (uint8_t[]){0xcb}, 1, 0},
  28. {0xab, (uint8_t[]){0x0e}, 1, 0},
  29. {0xb4, (uint8_t[]){0x04}, 1, 0},
  30. {0xa8, (uint8_t[]){0x19}, 1, 0},
  31. {0xb8, (uint8_t[]){0x08}, 1, 0},
  32. {0xe8, (uint8_t[]){0x24}, 1, 0},
  33. {0xe9, (uint8_t[]){0x48}, 1, 0},
  34. {0xea, (uint8_t[]){0x22}, 1, 0},
  35. {0xc6, (uint8_t[]){0x30}, 1, 0},
  36. {0xc7, (uint8_t[]){0x18}, 1, 0},
  37. {0xf0,
  38. (uint8_t[]){0x1f, 0x28, 0x04, 0x3e, 0x2a, 0x2e, 0x20, 0x00, 0x0c, 0x06,
  39. 0x00, 0x1c, 0x1f, 0x0f},
  40. 14, 0},
  41. {0xf1,
  42. (uint8_t[]){0x00, 0x2d, 0x2f, 0x3c, 0x6f, 0x1c, 0x0b, 0x00, 0x00, 0x00,
  43. 0x07, 0x0d, 0x11, 0x0f},
  44. 14, 0},
  45. };
  46. class AtomS3EchoBaseBoard : public WifiBoard {
  47. private:
  48. i2c_master_bus_handle_t i2c_bus_;
  49. Display* display_;
  50. Button boot_button_;
  51. bool is_echo_base_connected_ = false;
  52. void InitializeI2c() {
  53. // Initialize I2C peripheral
  54. i2c_master_bus_config_t i2c_bus_cfg = {
  55. .i2c_port = I2C_NUM_1,
  56. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  57. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  58. .clk_source = I2C_CLK_SRC_DEFAULT,
  59. .glitch_ignore_cnt = 7,
  60. .intr_priority = 0,
  61. .trans_queue_depth = 0,
  62. .flags = {
  63. .enable_internal_pullup = 1,
  64. },
  65. };
  66. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  67. }
  68. void I2cDetect() {
  69. is_echo_base_connected_ = false;
  70. uint8_t echo_base_connected_flag = 0x00;
  71. uint8_t address;
  72. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  73. for (int i = 0; i < 128; i += 16) {
  74. printf("%02x: ", i);
  75. for (int j = 0; j < 16; j++) {
  76. fflush(stdout);
  77. address = i + j;
  78. esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
  79. if (ret == ESP_OK) {
  80. printf("%02x ", address);
  81. if (address == 0x18) {
  82. echo_base_connected_flag |= 0xF0;
  83. } else if (address == 0x43) {
  84. echo_base_connected_flag |= 0x0F;
  85. }
  86. } else if (ret == ESP_ERR_TIMEOUT) {
  87. printf("UU ");
  88. } else {
  89. printf("-- ");
  90. }
  91. }
  92. printf("\r\n");
  93. }
  94. is_echo_base_connected_ = (echo_base_connected_flag == 0xFF);
  95. }
  96. void CheckEchoBaseConnection() {
  97. if (is_echo_base_connected_) {
  98. return;
  99. }
  100. // Pop error page
  101. InitializeSpi();
  102. InitializeGc9107Display();
  103. InitializeButtons();
  104. GetBacklight()->SetBrightness(100);
  105. display_->SetStatus(Lang::Strings::ERROR);
  106. display_->SetEmotion("sad");
  107. display_->SetChatMessage("system", "Echo Base\nnot connected");
  108. while (1) {
  109. ESP_LOGE(TAG, "Atomic Echo Base is disconnected");
  110. vTaskDelay(pdMS_TO_TICKS(1000));
  111. // Rerun detection
  112. I2cDetect();
  113. if (is_echo_base_connected_) {
  114. vTaskDelay(pdMS_TO_TICKS(500));
  115. I2cDetect();
  116. if (is_echo_base_connected_) {
  117. ESP_LOGI(TAG, "Atomic Echo Base is reconnected");
  118. vTaskDelay(pdMS_TO_TICKS(200));
  119. esp_restart();
  120. }
  121. }
  122. }
  123. }
  124. void InitializeSpi() {
  125. ESP_LOGI(TAG, "Initialize SPI bus");
  126. spi_bus_config_t buscfg = {};
  127. buscfg.mosi_io_num = GPIO_NUM_21;
  128. buscfg.miso_io_num = GPIO_NUM_NC;
  129. buscfg.sclk_io_num = GPIO_NUM_17;
  130. buscfg.quadwp_io_num = GPIO_NUM_NC;
  131. buscfg.quadhd_io_num = GPIO_NUM_NC;
  132. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  133. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  134. }
  135. void InitializeGc9107Display() {
  136. ESP_LOGI(TAG, "Init GC9107 display");
  137. ESP_LOGI(TAG, "Install panel IO");
  138. esp_lcd_panel_io_handle_t io_handle = NULL;
  139. esp_lcd_panel_io_spi_config_t io_config = {};
  140. io_config.cs_gpio_num = GPIO_NUM_15;
  141. io_config.dc_gpio_num = GPIO_NUM_33;
  142. io_config.spi_mode = 0;
  143. io_config.pclk_hz = 40 * 1000 * 1000;
  144. io_config.trans_queue_depth = 10;
  145. io_config.lcd_cmd_bits = 8;
  146. io_config.lcd_param_bits = 8;
  147. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &io_handle));
  148. ESP_LOGI(TAG, "Install GC9A01 panel driver");
  149. esp_lcd_panel_handle_t panel_handle = NULL;
  150. gc9a01_vendor_config_t gc9107_vendor_config = {
  151. .init_cmds = gc9107_lcd_init_cmds,
  152. .init_cmds_size = sizeof(gc9107_lcd_init_cmds) / sizeof(gc9a01_lcd_init_cmd_t),
  153. };
  154. esp_lcd_panel_dev_config_t panel_config = {};
  155. panel_config.reset_gpio_num = GPIO_NUM_34; // Set to -1 if not use
  156. panel_config.rgb_endian = LCD_RGB_ENDIAN_BGR;
  157. panel_config.bits_per_pixel = 16; // Implemented by LCD command `3Ah` (16/18)
  158. panel_config.vendor_config = &gc9107_vendor_config;
  159. ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
  160. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  161. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  162. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  163. display_ = new SpiLcdDisplay(io_handle, panel_handle,
  164. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  165. {
  166. .text_font = &font_puhui_16_4,
  167. .icon_font = &font_awesome_16_4,
  168. .emoji_font = font_emoji_32_init(),
  169. });
  170. }
  171. void InitializeButtons() {
  172. boot_button_.OnClick([this]() {
  173. auto& app = Application::GetInstance();
  174. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  175. ResetWifiConfiguration();
  176. }
  177. app.ToggleChatState();
  178. });
  179. }
  180. public:
  181. AtomS3EchoBaseBoard() : boot_button_(BOOT_BUTTON_GPIO) {
  182. InitializeI2c();
  183. I2cDetect();
  184. CheckEchoBaseConnection();
  185. InitializeSpi();
  186. InitializeGc9107Display();
  187. InitializeButtons();
  188. GetBacklight()->RestoreBrightness();
  189. }
  190. virtual AudioCodec* GetAudioCodec() override {
  191. static Es8311AudioCodec audio_codec(
  192. i2c_bus_,
  193. I2C_NUM_1,
  194. AUDIO_INPUT_SAMPLE_RATE,
  195. AUDIO_OUTPUT_SAMPLE_RATE,
  196. AUDIO_I2S_GPIO_MCLK,
  197. AUDIO_I2S_GPIO_BCLK,
  198. AUDIO_I2S_GPIO_WS,
  199. AUDIO_I2S_GPIO_DOUT,
  200. AUDIO_I2S_GPIO_DIN,
  201. AUDIO_CODEC_GPIO_PA,
  202. AUDIO_CODEC_ES8311_ADDR,
  203. false);
  204. return &audio_codec;
  205. }
  206. virtual Display* GetDisplay() override {
  207. return display_;
  208. }
  209. virtual Backlight* GetBacklight() override {
  210. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT, 256);
  211. return &backlight;
  212. }
  213. };
  214. DECLARE_BOARD(AtomS3EchoBaseBoard);