atommatrix_echo_base.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "wifi_board.h"
  2. #include "audio_codecs/es8311_audio_codec.h"
  3. #include "application.h"
  4. #include "button.h"
  5. #include "config.h"
  6. #include "i2c_device.h"
  7. #include "iot/thing_manager.h"
  8. #include <esp_log.h>
  9. #include <driver/i2c_master.h>
  10. #include <wifi_station.h>
  11. #include "led/circular_strip.h"
  12. #define TAG "XX+EchoBase"
  13. #define PI4IOE_ADDR 0x43
  14. #define PI4IOE_REG_CTRL 0x00
  15. #define PI4IOE_REG_IO_PP 0x07
  16. #define PI4IOE_REG_IO_DIR 0x03
  17. #define PI4IOE_REG_IO_OUT 0x05
  18. #define PI4IOE_REG_IO_PULLUP 0x0D
  19. class Pi4ioe : public I2cDevice {
  20. public:
  21. Pi4ioe(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  22. WriteReg(PI4IOE_REG_IO_PP, 0x00); // Set to high-impedance
  23. WriteReg(PI4IOE_REG_IO_PULLUP, 0xFF); // Enable pull-up
  24. WriteReg(PI4IOE_REG_IO_DIR, 0x6E); // Set input=0, output=1
  25. WriteReg(PI4IOE_REG_IO_OUT, 0xFF); // Set outputs to 1
  26. }
  27. void SetSpeakerMute(bool mute) {
  28. WriteReg(PI4IOE_REG_IO_OUT, mute ? 0x00 : 0xFF);
  29. }
  30. };
  31. class AtomMatrixEchoBaseBoard : public WifiBoard {
  32. private:
  33. i2c_master_bus_handle_t i2c_bus_;
  34. Pi4ioe* pi4ioe_;
  35. Button face_button_;
  36. void InitializeI2c() {
  37. // Initialize I2C peripheral
  38. i2c_master_bus_config_t i2c_bus_cfg = {
  39. .i2c_port = I2C_NUM_1,
  40. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  41. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  42. .clk_source = I2C_CLK_SRC_DEFAULT,
  43. .glitch_ignore_cnt = 7,
  44. .intr_priority = 0,
  45. .trans_queue_depth = 0,
  46. .flags = {
  47. .enable_internal_pullup = 1,
  48. },
  49. };
  50. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  51. }
  52. void I2cDetect() {
  53. uint8_t address;
  54. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  55. for (int i = 0; i < 128; i += 16) {
  56. printf("%02x: ", i);
  57. for (int j = 0; j < 16; j++) {
  58. fflush(stdout);
  59. address = i + j;
  60. esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
  61. if (ret == ESP_OK) {
  62. printf("%02x ", address);
  63. } else if (ret == ESP_ERR_TIMEOUT) {
  64. printf("UU ");
  65. } else {
  66. printf("-- ");
  67. }
  68. }
  69. printf("\r\n");
  70. }
  71. }
  72. void InitializePi4ioe() {
  73. ESP_LOGI(TAG, "Init PI4IOE");
  74. pi4ioe_ = new Pi4ioe(i2c_bus_, PI4IOE_ADDR);
  75. pi4ioe_->SetSpeakerMute(false);
  76. }
  77. void InitializeButtons() {
  78. face_button_.OnClick([this]() {
  79. ESP_LOGI(TAG, " ===>>> face_button_.OnClick ");
  80. auto& app = Application::GetInstance();
  81. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  82. ResetWifiConfiguration();
  83. }
  84. app.ToggleChatState();
  85. });
  86. }
  87. // 物联网初始化,添加对 AI 可见设备
  88. void InitializeIot() {
  89. auto& thing_manager = iot::ThingManager::GetInstance();
  90. thing_manager.AddThing(iot::CreateThing("Speaker"));
  91. }
  92. public:
  93. AtomMatrixEchoBaseBoard() : face_button_(BOOT_BUTTON_GPIO) {
  94. InitializeI2c();
  95. I2cDetect();
  96. InitializePi4ioe();
  97. InitializeButtons();
  98. InitializeIot();
  99. }
  100. virtual Led* GetLed() override {
  101. static CircularStrip led(BUILTIN_LED_GPIO, 25);
  102. return &led;
  103. }
  104. virtual AudioCodec* GetAudioCodec() override {
  105. static Es8311AudioCodec audio_codec(
  106. i2c_bus_,
  107. I2C_NUM_1,
  108. AUDIO_INPUT_SAMPLE_RATE,
  109. AUDIO_OUTPUT_SAMPLE_RATE,
  110. AUDIO_I2S_GPIO_MCLK,
  111. AUDIO_I2S_GPIO_BCLK,
  112. AUDIO_I2S_GPIO_WS,
  113. AUDIO_I2S_GPIO_DOUT,
  114. AUDIO_I2S_GPIO_DIN,
  115. AUDIO_CODEC_GPIO_PA,
  116. AUDIO_CODEC_ES8311_ADDR,
  117. false);
  118. return &audio_codec;
  119. }
  120. };
  121. DECLARE_BOARD(AtomMatrixEchoBaseBoard);