Skip to content

Commit 13ad52b

Browse files
Refactor callbacks so they can be set and cleared at runtime (#70)
* Refactor callbacks so they can be set and cleared at runtime * Update the examples in README.md * Make all codec names < 5 chars * Shorten and camelCase member function names * Update README.md
1 parent 78309b3 commit 13ad52b

3 files changed

Lines changed: 154 additions & 89 deletions

File tree

README.md

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Also plays mp3 and ogg files from sdcard or any mounted filesystem.
1414

1515
While this [PR](https://github.com/baldram/ESP_VS1053_Library/pull/119) is waiting to be merged in the `baldram/ESP_VS1053_Library` repo, you have to use the this [fork](baldram/ESP_VS1053_Library) to be able to compile the `master` branch.
1616

17-
Use the [latest Arduino ESP32 core version](https://github.com/espressif/arduino-esp32/releases/latest).
17+
Use the [latest Arduino ESP32 core version](https://github.com/espressif/arduino-esp32/releases/latest) for Arduino IDE or the corresponding [PIOArduino release](https://github.com/pioarduino/platform-espressif32/releases/latest) if yuou use PlatformIO in VSCode.
1818

1919
## Example: play a stream
2020
```c++
@@ -37,24 +37,38 @@ ESP32_VS1053_Stream stream;
3737
const char* SSID = "xxx";
3838
const char* PSK = "xxx";
3939

40-
// called when codec is detected
40+
// Called when codec is detected
4141
void codecCallBack(const char *codec)
4242
{
4343
Serial.printf("codec: %s\n", codec);
4444
}
4545

46-
// called when bitrate is detected (cbr) and changes (vbr)
46+
// Called when bitrate is detected (cbr) and changes (vbr)
4747
void bitrateCallback(uint32_t bitrate)
4848
{
4949
Serial.printf("bitrate: %lu kbps\n", bitrate);
5050
}
5151

52-
void setup() {
53-
Serial.begin(115200);
52+
// Called when a stream has an ICY name header set
53+
void stationCallback(const char *name)
54+
{
55+
Serial.printf("station: %s\n", name);
56+
}
5457

55-
while (!Serial)
56-
delay(10);
58+
// Called when stream metadata is available
59+
void infoCallback(const char *info)
60+
{
61+
Serial.printf("info: %s\n", info);
62+
}
5763

64+
// Called on end-of-file
65+
void eofCallback(const char *url)
66+
{
67+
Serial.printf("eof: %s\n", url);
68+
}
69+
70+
void setup() {
71+
Serial.begin(115200);
5872
Serial.println("\n\nVS1053 Radio Streaming Example\n");
5973

6074
// Connect to Wi-Fi
@@ -76,16 +90,26 @@ void setup() {
7690
Serial.println("Decoder not running - system halted");
7791
while (1) delay(100);
7892
}
79-
Serial.println("VS1053 running - starting radio stream");
8093

81-
// Setup the codec callback
82-
stream.setCodecCallback(codecCallBack);
94+
// Set the codec callback
95+
stream.setCodecCB(codecCallBack);
8396

84-
// Setup the bitrate callback
85-
stream.setBitrateCallback(bitrateCallback);
97+
// Set the bitrate callback
98+
stream.setBitrateCB(bitrateCallback);
99+
100+
// Set the station name callback
101+
stream.setStationCB(stationCallback);
102+
103+
// Set the stream metadata callback
104+
stream.setInfoCB(infoCallback);
105+
106+
// Set the EOF callback
107+
stream.setEofCB(eofCallback);
108+
109+
Serial.println("VS1053 running - starting radio stream");
86110

87111
// Connect to the radio stream
88-
stream.connecttohost("http://icecast.omroep.nl/radio6-bb-mp3");
112+
stream.connectToHost("http://icecast.omroep.nl/radio6-bb-mp3");
89113

90114
if (!stream.isRunning()) {
91115
Serial.println("Stream not running - system halted");
@@ -98,17 +122,6 @@ void loop() {
98122
delay(5);
99123
}
100124

101-
void audio_showstation(const char* info) {
102-
Serial.printf("Station: %s\n", info);
103-
}
104-
105-
void audio_showstreamtitle(const char* info) {
106-
Serial.printf("Stream title: %s\n", info);
107-
}
108-
109-
void audio_eof_stream(const char* info) {
110-
Serial.printf("End of stream: %s\n", info);
111-
}
112125
```
113126
114127
## Example: play from SD card
@@ -129,18 +142,24 @@ void audio_eof_stream(const char* info) {
129142
130143
ESP32_VS1053_Stream stream;
131144
132-
// called when codec is detected
145+
// Called when codec is detected
133146
void codecCallBack(const char *codec)
134147
{
135148
Serial.printf("codec: %s\n", codec);
136149
}
137150
138-
// called when bitrate is detected (cbr) and changes (vbr)
151+
// Called when bitrate is detected (cbr) and changes (vbr)
139152
void bitrateCallback(uint32_t bitrate)
140153
{
141154
Serial.printf("bitrate: %lu kbps\n", bitrate);
142155
}
143156
157+
// Called on end-of-file
158+
void eofCallback(const char *url)
159+
{
160+
Serial.printf("eof: %s\n", url);
161+
}
162+
144163
bool mountSDcard() {
145164
if (!SD.begin(SDREADER_CS)) {
146165
Serial.println("Card mount failed");
@@ -160,10 +179,6 @@ bool mountSDcard() {
160179
161180
void setup() {
162181
Serial.begin(115200);
163-
164-
while (!Serial)
165-
delay(10);
166-
167182
Serial.println("\n\nVS1053 SD Card Playback Example\n");
168183
169184
// Start SPI bus
@@ -184,16 +199,19 @@ void setup() {
184199
while (1) delay(100);
185200
}
186201
187-
Serial.println("VS1053 running - starting SD playback");
202+
// Set the codec callback
203+
stream.setCodecCB(codecCallBack);
204+
205+
// Set the bitrate callback
206+
stream.setBitrateCB(bitrateCallback);
188207
189-
// Setup the codec callback
190-
stream.setCodecCallback(codecCallBack);
208+
// Set the EOF callback
209+
stream.setEofCB(eofCallback);
191210
192-
// Setup the bitrate callback
193-
stream.setBitrateCallback(bitrateCallback);
211+
Serial.println("VS1053 running - starting SD playback");
194212
195213
// Start playback from an SD file
196-
stream.connecttofile(SD, "/test.mp3");
214+
stream.connectToFile(SD, "/test.mp3");
197215
198216
if (!stream.isRunning()) {
199217
Serial.println("No file running - system halted");
@@ -206,9 +224,6 @@ void loop() {
206224
delay(5);
207225
}
208226
209-
void audio_eof_stream(const char* info) {
210-
Serial.printf("End of file: %s\n", info);
211-
}
212227
```
213228

214229
## Known issues
@@ -269,16 +284,16 @@ bool isChipConnected()
269284
```
270285
### Start or resume a stream
271286
```c++
272-
bool connecttohost(url)
287+
bool connectToHost(url)
273288
```
274289
```c++
275-
bool connecttohost(url, offset)
290+
bool connectToHost(url, offset)
276291
```
277292
```c++
278-
bool connecttohost(url, user, pwd)
293+
bool connectToHost(url, user, pwd)
279294
```
280295
```c++
281-
bool connecttohost(url, user, pwd, offset)
296+
bool connectToHost(url, user, pwd, offset)
282297
```
283298
Note: When a stream does not start in this library but it does play on your desktop or laptop you can try increasing the connection timeout.
284299
You can do this in `ESP32_VS1053_Stream.h` by increasing these values:
@@ -288,10 +303,10 @@ You can do this in `ESP32_VS1053_Stream.h` by increasing these values:
288303
```
289304
### Start or resume a local file
290305
```c++
291-
bool connecttofile(filesystem, filename)
306+
bool connectToFile(filesystem, filename)
292307
```
293308
```c++
294-
bool connecttofile(filesystem, filename, offset)
309+
bool connectToFile(filesystem, filename, offset)
295310
```
296311
`filesystem` has to be mounted.
297312
Note: Local file playbacks requires psram
@@ -378,7 +393,7 @@ Returns the eof url or path.
378393
Also called if a stream or file times out/errors.
379394

380395
You can use this function for coding a playlist.
381-
Use `connecttohost()` or `connecttofile()` inside this function to start the next item.
396+
Use `connectToHost()` or `connectToFile()` inside this function to start the next item.
382397
## License
383398

384399
MIT License

0 commit comments

Comments
 (0)