esp_ble_headphones/headphones/headphone_slave/headphone_slave.ino
2025-05-24 13:01:43 +03:00

40 lines
1.1 KiB
C++

#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
#include "driver/i2s.h"
void on_audio_receive(const uint8_t *data, int len) {
// Воспроизводим аудио через I2S
size_t bytes_written;
i2s_write(I2S_NUM_0, data, len, &bytes_written, portMAX_DELAY);
}
void setup() {
Serial.begin(115200);
// Настройка I2S
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, // Только правый канал
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = 0,
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = false
};
i2s_pin_config_t pin_config = {26, 25, 22, I2S_PIN_NO_CHANGE};
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &pin_config);
// Настройка ESP-NOW
WiFi.mode(WIFI_STA);
esp_now_init();
esp_now_register_recv_cb([](const uint8_t *mac, const uint8_t *data, int len) {
on_audio_receive(data, len);
});
}
void loop() {}