Skip to content

Commit f1e5611

Browse files
committed
esp-now codec example
1 parent 609389b commit f1e5611

File tree

8 files changed

+11
-16
lines changed

8 files changed

+11
-16
lines changed

examples/sandbox/esp-now-codec/communication-codec-espnow-send/communication-codec-espnow-send.ino renamed to examples/examples-communication/esp-now/codec/communication-codec-espnow-send/communication-codec-espnow-send.ino

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#include "AudioLibs/Communication.h"
1313
#include "AudioCodecs/CodecSBC.h"
1414

15-
uint16_t sample_rate = 32000;
16-
uint8_t channels = 2; // The stream will have 2 channels
15+
AudioInfo info(32000,1,16);
1716
SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
1817
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
1918
ESPNowStream now;
20-
EncodedAudioStream encoder(&now, new SBCEncoder()); // encode and write to ESP-now
19+
SBCEncoder sbc;
20+
EncodedAudioStream encoder(now, sbc); // encode and write to ESP-now
2121
StreamCopy copier(encoder, sound); // copies sound into i2s
2222
const char *peers[] = {"A8:48:FA:0B:93:01"};
2323

@@ -31,14 +31,10 @@ void setup() {
3131
now.addPeers(peers);
3232

3333
// Setup sine wave
34-
auto cfgs = sineWave.defaultConfig();
35-
cfgs.sample_rate = sample_rate;
36-
cfgs.channels = channels;
37-
cfgs.bits_per_sample = 16;
38-
sineWave.begin(cfgs, N_B4);
34+
sineWave.begin(info, N_B4);
3935

4036
// start encoder
41-
encoder.begin(cfgs);
37+
encoder.begin(info);
4238

4339
Serial.println("Sender started...");
4440
}

src/AudioLibs/Communication.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ struct ESPNowStreamConfig {
5757
int write_retry_count = -1; // -1 endless
5858
void (*recveive_cb)(const uint8_t *mac_addr, const uint8_t *data,
5959
int data_len) = nullptr;
60-
// to encrypt set primary_master_key and local_master_key to 16 byte strings
60+
/// to encrypt set primary_master_key and local_master_key to 16 byte strings
6161
const char *primary_master_key = nullptr;
6262
const char *local_master_key = nullptr;
63-
64-
#ifdef FAST_ESP_NOW_HACK
63+
/// esp-now bit rate
6564
wifi_phy_rate_t rate = WIFI_PHY_RATE_2M_S;
66-
#endif
6765
};
6866

6967
/**
@@ -86,7 +84,10 @@ class ESPNowStream : public AudioStream {
8684
}
8785

8886
/// Returns the mac address of the current ESP32
89-
const char *macAddress() { return WiFi.macAddress().c_str(); }
87+
const char *macAddress() {
88+
static const char* result = WiFi.macAddress().c_str();
89+
return result;
90+
}
9091

9192
/// Defines an alternative send callback
9293
void setSendCallback(esp_now_send_cb_t cb) { send = cb; }
@@ -128,13 +129,11 @@ class ESPNowStream : public AudioStream {
128129
}
129130
}
130131

131-
#ifdef FAST_ESP_NOW_HACK
132132
LOGI("Setting ESP-NEW rate");
133133
if (esp_wifi_config_espnow_rate(getInterface(), cfg.rate) !=
134134
ESP_OK) {
135135
LOGW("Could not set rate");
136136
}
137-
#endif
138137

139138
Serial.println();
140139
Serial.print("mac: ");

0 commit comments

Comments
 (0)