Skip to content

Commit ee77c2c

Browse files
committed
DRAFT Serail Synch
1 parent 08e3341 commit ee77c2c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* @file receive-mp3.ino
4+
* @brief Example of receiving an mp3 stream over serial and playing it over I2S
5+
* using the AudioTools library.
6+
* The processing implements a flow control using a custom digical pin. We process
7+
* the data receving in a separate task and the playback in the main loop.
8+
*/
9+
10+
#include "AudioTools.h"
11+
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
12+
#include "AudioTools/AudioLibs/AudioBoardStream.h"
13+
#include "AudioTools/Concurrency/RTOS.h"
14+
15+
const int flowControlPin = 17; // flow control pin acitive low
16+
const int min_percent = 10;
17+
const int max_percent = 90;
18+
19+
AudioBoardStream i2s(AudioKitEs8388V1); // final output of decoded stream
20+
EncodedAudioStream dec(&i2s, new MP3DecoderHelix()); // Decoding stream
21+
// queue
22+
BufferRTOS<uint8_t> buffer(0);
23+
QueueStream<uint8_t> queue(buffer);
24+
// copy
25+
StreamCopy copierFill(queue, Serial1);
26+
StreamCopy copierPlay(dec, queue);
27+
28+
Task task("mp3-copy", 10000, 1, 0);
29+
30+
void setup() {
31+
Serial.begin(115200);
32+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
33+
34+
Serial1.begin(115200);
35+
pinMode(flowControlPin, OUTPUT); // flow control pin
36+
37+
// set up buffer here to allow PSRAM usage
38+
buffer.resize(1024 * 10); // 10kB buffer
39+
queue.begin(50); // start when half full
40+
41+
// setup i2s
42+
auto config = i2s.defaultConfig(TX_MODE);
43+
i2s.begin(config);
44+
45+
// setup decoder
46+
dec.begin();
47+
48+
// start fill buffer copy task
49+
task.begin([]() {
50+
copierFill.copy();
51+
// data synchronization to prevent buffer overflow
52+
if (buffer.levelPercent() > max_percent) {
53+
digitalWrite(flowControlPin, HIGH); // stop receiving
54+
} else if (buffer.levelPercent() < min_percent) {
55+
digitalWrite(flowControlPin, LOW); // start receiving
56+
}
57+
});
58+
}
59+
60+
void loop() { copierPlay.copy(); }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
/**
3+
* @file send-mp3.ino
4+
* @brief Example of sending an mp3 stream over Serial the AudioTools library.
5+
* We use a custom pin to control the flow of the data. If we are ready to
6+
* receive data, we set the pin to LOW.
7+
*/
8+
9+
#include "AudioTools.h"
10+
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
11+
#include "AudioTools/AudioLibs/AudioBoardStream.h"
12+
13+
URLStream url("ssid", "password"); // or replace with ICYStream to get metadata
14+
AudioBoardStream i2s(AudioKitEs8388V1); // final output of decoded stream
15+
StreamCopy copier(Serial1, url); // copy url to decoder
16+
// pins
17+
const int flowControlPin = 17;
18+
19+
void setup() {
20+
Serial.begin(115200);
21+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Error);
22+
23+
// setup flow control pin
24+
pinMode(flowControlPin, INPUT_PULLUP); // flow control pin
25+
26+
// setup serial data sink
27+
Serial1.begin(115200);
28+
29+
// mp3 radio
30+
url.begin("http://stream.srg-ssr.ch/m/rsj/mp3_128", "audio/mp3");
31+
}
32+
33+
void loop() {
34+
if (digitalRead(flowControlPin) == LOW) {
35+
copier.copy();
36+
}
37+
}

0 commit comments

Comments
 (0)