Advertisement
safwan092

TX_IMNP441_SA

Apr 8th, 2024
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <driver/i2s.h>
  2. #define I2S_WS 15
  3. #define I2S_SD 13
  4. #define I2S_SCK 2
  5. #define I2S_PORT I2S_NUM_0
  6.  
  7.  
  8. #define bufferLen 64
  9.  
  10.  
  11. int16_t sBuffer[bufferLen];
  12.  
  13. void setup() {
  14. Serial.begin(115200);
  15. Serial2.begin(115200);
  16. Serial.println("Setup I2S ...");
  17.  
  18. delay(1000);
  19. i2s_install();
  20. i2s_setpin();
  21. i2s_start(I2S_PORT);
  22.  
  23. delay(500);
  24. }
  25.  
  26. void loop() {
  27.  
  28. size_t bytesIn = 0;
  29.  
  30. esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen, &bytesIn, portMAX_DELAY);
  31.  
  32. if (result == ESP_OK) {
  33. int samples_read = bytesIn / 8;
  34. if (samples_read > 0) {
  35. float mean = 0;
  36. for (int i = 0; i < samples_read; ++i) {
  37. mean += (sBuffer[i]);
  38. }
  39. mean /= samples_read;
  40. if (mean < 500) {
  41.  
  42. }
  43. else {
  44. Serial.print("S1:");
  45. Serial.println(mean);
  46. Serial2.println("R");//String(mean));
  47. //Serial.print(",");
  48. }
  49.  
  50. }
  51. }
  52. }
  53.  
  54.  
  55. void i2s_install() {
  56. const i2s_config_t i2s_config = {
  57. .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
  58. .sample_rate = 44100,
  59. .bits_per_sample = i2s_bits_per_sample_t(16),
  60. .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  61. .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
  62. .intr_alloc_flags = 0, // default interrupt priority
  63. .dma_buf_count = 8,
  64. .dma_buf_len = bufferLen,
  65. .use_apll = false
  66. };
  67.  
  68. i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  69. }
  70.  
  71. void i2s_setpin() {
  72. const i2s_pin_config_t pin_config = {
  73. .bck_io_num = I2S_SCK,
  74. .ws_io_num = I2S_WS,
  75. .data_out_num = -1,
  76. .data_in_num = I2S_SD
  77. };
  78.  
  79. i2s_set_pin(I2S_PORT, &pin_config);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement