Browse Source

button and mp3 example

main
Hendrik Langer 6 years ago
parent
commit
8084428f01
  1. 89
      src/image.h
  2. 25
      src/main.cpp
  3. 103
      src/mp3.cpp
  4. 7
      src/mp3.h

89
src/image.h

@ -0,0 +1,89 @@
#define IMG_1872_width 128
#define IMG_1872_height 64
static const unsigned char IMG_1872_bits[] U8X8_PROGMEM = {
0x00, 0x00, 0x04, 0x00, 0x80, 0xa0, 0xef, 0xff, 0xff, 0xff, 0xea, 0x6b,
0x55, 0x38, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x5b, 0xff,
0xfb, 0x7b, 0x0f, 0x00, 0x64, 0xfa, 0xf6, 0xff, 0x00, 0x00, 0x00, 0x00,
0x80, 0x80, 0xfc, 0xfc, 0xff, 0x6f, 0x11, 0x00, 0xf0, 0x7f, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0xcd, 0xef, 0xff, 0xdf, 0x9b, 0x01,
0xf0, 0xff, 0xbf, 0xff, 0x00, 0x00, 0x20, 0x00, 0x00, 0xa0, 0xa6, 0xea,
0xff, 0x3f, 0x05, 0x01, 0x80, 0xff, 0xff, 0xfb, 0x00, 0x00, 0x06, 0x00,
0x00, 0xa0, 0xfe, 0xff, 0xff, 0xf5, 0x00, 0x00, 0xb0, 0xff, 0xcf, 0xff,
0x00, 0x40, 0x00, 0x00, 0x00, 0x80, 0xfc, 0xdf, 0xff, 0x1f, 0x01, 0x01,
0x70, 0x1f, 0xde, 0xbf, 0x00, 0x40, 0xbf, 0xbe, 0x52, 0x30, 0x7f, 0xc3,
0xff, 0x5f, 0x04, 0x00, 0x00, 0x0f, 0x7e, 0xff, 0x55, 0xbf, 0x00, 0x00,
0x00, 0x6a, 0xff, 0x9d, 0xff, 0x07, 0x04, 0x00, 0x00, 0x80, 0xbd, 0xff,
0x00, 0x00, 0x00, 0x00, 0x80, 0x2a, 0x7e, 0xf5, 0xff, 0xbd, 0x02, 0x00,
0x00, 0xa8, 0xff, 0xef, 0x00, 0x00, 0x00, 0x00, 0x60, 0xb5, 0xfc, 0xef,
0xff, 0x37, 0x04, 0x01, 0xa0, 0xf8, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0x54, 0xa2, 0xd7, 0xff, 0xff, 0xff, 0x41, 0x00, 0xc0, 0xff, 0xff, 0xdf,
0x00, 0x00, 0x00, 0x80, 0xaa, 0x80, 0xfe, 0xff, 0xff, 0x7f, 0x05, 0x03,
0x00, 0x97, 0xff, 0x3d, 0x80, 0x42, 0x00, 0xbb, 0x0d, 0x80, 0xff, 0xff,
0xff, 0x3f, 0x40, 0x00, 0x00, 0x88, 0x76, 0xf7, 0xe0, 0x40, 0x00, 0xda,
0x26, 0x20, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x01, 0x60, 0x21, 0xf8, 0xeb,
0x40, 0x52, 0xfd, 0xbf, 0xee, 0x20, 0xff, 0xff, 0xff, 0x6f, 0x00, 0x01,
0xe0, 0xfd, 0xfa, 0xdf, 0xe0, 0xda, 0x5d, 0xaf, 0xaa, 0x80, 0xfe, 0xff,
0xff, 0xdf, 0x00, 0x02, 0xe0, 0xe7, 0xff, 0xee, 0x40, 0xca, 0xd5, 0xad,
0x2e, 0x00, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x01, 0xe0, 0xdc, 0xff, 0x7f,
0x40, 0xfe, 0x55, 0xed, 0xaa, 0xc0, 0xfe, 0xff, 0xff, 0xff, 0x00, 0x00,
0x40, 0xb5, 0xff, 0xff, 0x40, 0xfa, 0x57, 0xaf, 0x2d, 0x00, 0xff, 0xff,
0xff, 0xdf, 0x01, 0x01, 0xf8, 0xff, 0xff, 0xfd, 0xf0, 0x5f, 0x00, 0x00,
0x00, 0xa0, 0xfe, 0xff, 0xff, 0xdf, 0x16, 0x02, 0xff, 0xef, 0xff, 0xe6,
0xa5, 0x0a, 0x00, 0x00, 0x00, 0xa0, 0xff, 0xff, 0xff, 0xfb, 0x16, 0xe2,
0xff, 0xff, 0xff, 0xf5, 0x75, 0x01, 0x00, 0x00, 0x14, 0xa0, 0xff, 0xff,
0x4f, 0x6f, 0x0b, 0x01, 0xfc, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff,
0xff, 0x81, 0xf7, 0xff, 0x59, 0xfc, 0x1a, 0x17, 0xf8, 0xff, 0xff, 0xf7,
0x7a, 0x57, 0x55, 0x41, 0x28, 0xb5, 0xff, 0xff, 0x0f, 0xf9, 0x36, 0x15,
0xf8, 0xff, 0xff, 0xf7, 0x81, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x3f, 0x51, 0x7c, 0x0f, 0xf0, 0xff, 0xff, 0xf7, 0xff, 0xff, 0x9f, 0xff,
0xfe, 0xff, 0xff, 0xff, 0x7f, 0x81, 0x3b, 0x3f, 0xf7, 0xff, 0xff, 0xf5,
0xfc, 0xfe, 0x2f, 0xc7, 0xff, 0xfb, 0xff, 0xff, 0x7f, 0x07, 0x79, 0x1f,
0xf0, 0xff, 0xff, 0xf7, 0xdf, 0xff, 0x5f, 0xff, 0xfe, 0xff, 0xff, 0xff,
0xff, 0x95, 0xe3, 0x1e, 0xb0, 0xff, 0xff, 0xff, 0xbf, 0xfe, 0x5d, 0xd9,
0xff, 0xff, 0xff, 0xff, 0xff, 0x6f, 0x67, 0x9f, 0xf4, 0xff, 0xff, 0xff,
0xff, 0xff, 0x97, 0xdc, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xdf, 0x15, 0xf6,
0xfb, 0xff, 0xff, 0xff, 0xfd, 0xff, 0x9f, 0xfa, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0x27, 0xbe, 0xfe, 0xff, 0xff, 0xff, 0xbd, 0xbe, 0x5f, 0xff,
0xfe, 0xff, 0xff, 0xff, 0xff, 0xbf, 0x28, 0x34, 0xfe, 0xff, 0xff, 0x7f,
0x71, 0xed, 0x5f, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xdf, 0xfc,
0xdf, 0xff, 0xff, 0x5f, 0xd6, 0xff, 0xbf, 0x7e, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xff, 0x4b, 0xeb, 0x9f, 0xff, 0xff, 0x27, 0xf2, 0xff, 0x87, 0xff,
0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0x1f, 0x68, 0x2f, 0xff, 0xff, 0x6f,
0xfe, 0xff, 0xbf, 0xfe, 0xff, 0xbf, 0xf5, 0xff, 0xff, 0xff, 0xff, 0xc4,
0x0a, 0xfe, 0xff, 0xdf, 0xc7, 0xff, 0xbf, 0xff, 0xff, 0xff, 0xdf, 0xff,
0xff, 0xff, 0x5f, 0xd9, 0x04, 0xff, 0xff, 0xda, 0xaa, 0x3f, 0x5f, 0xff,
0xff, 0x3f, 0xe8, 0xff, 0xff, 0xff, 0xff, 0xf6, 0xc0, 0xff, 0xff, 0x5f,
0xad, 0x9f, 0x9e, 0xfe, 0xff, 0x6f, 0xf5, 0xff, 0xff, 0xff, 0xbf, 0xff,
0xff, 0xff, 0xff, 0xff, 0x9f, 0xd3, 0x4b, 0xff, 0xff, 0xd7, 0x7e, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xb7, 0x7f, 0x9a, 0x1b, 0xff,
0xff, 0x92, 0xf4, 0xed, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff,
0xaf, 0x07, 0x3f, 0xff, 0x6f, 0x15, 0x51, 0xfd, 0xff, 0xff, 0x7f, 0xff,
0xff, 0xff, 0xff, 0xef, 0xbf, 0x1a, 0x3e, 0xff, 0x8b, 0x26, 0xa5, 0xf2,
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0x2d, 0x1b, 0xff,
0xb0, 0x28, 0xa1, 0x5f, 0xff, 0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff,
0x7f, 0x81, 0x3e, 0x7f, 0xb5, 0xf3, 0x8d, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x17, 0xb8, 0xff, 0x9e, 0x2e, 0xff, 0xdf,
0xfe, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x27, 0xb2, 0x7f,
0x32, 0x98, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfd, 0xff, 0xff, 0xff,
0xff, 0x65, 0xa7, 0x7f, 0x81, 0x76, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
0xf0, 0xff, 0xff, 0x7f, 0xbf, 0x4f, 0x92, 0xbf, 0xbe, 0xde, 0xff, 0xff,
0xff, 0xff, 0xff, 0x3f, 0xfe, 0xff, 0xbf, 0x6e, 0xfd, 0xff, 0x0a, 0xff,
0xd4, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0xfc, 0xff, 0x5f, 0x01,
0xf7, 0xf5, 0xbe, 0x2f, 0xf5, 0xa6, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x07,
0xf8, 0xff, 0xbf, 0x04, 0xfb, 0x89, 0x5d, 0x6d, 0xfd, 0xeb, 0xff, 0xff,
0x9f, 0xff, 0xff, 0x0f, 0xe0, 0xff, 0x1f, 0x00, 0xef, 0x3b, 0x09, 0x9f,
0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x0f, 0x00, 0xfe, 0x3f, 0x00,
0xdb, 0x7d, 0xbc, 0x7f, 0x7f, 0xab, 0x07, 0xed, 0xef, 0xff, 0xff, 0x1f,
0x00, 0x00, 0x00, 0x01, 0x5f, 0xf9, 0x2b, 0xff, 0xfc, 0xff, 0x2f, 0xfc,
0x9f, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xff, 0xbf, 0x5f, 0xbb,
0xfe, 0xf5, 0x53, 0x00, 0x5f, 0xfe, 0x7f, 0xf8, 0x3f, 0x00, 0x00, 0x00,
0xfa, 0xff, 0x5f, 0x7e, 0xf5, 0xff, 0xd7, 0x06, 0xff, 0xf7, 0x7f, 0xf8,
0xff, 0x01, 0x00, 0x00, 0xfc, 0xc3, 0xbf, 0x89, 0xea, 0xff, 0xff, 0x22,
0xff, 0xff, 0x7f, 0xf8, 0xff, 0x5f, 0x00, 0x00, 0xab, 0xff, 0x55, 0xdf,
0xd5, 0xff, 0xbe, 0xe8, 0xff, 0xfb, 0x7f, 0xfc, 0xff, 0xff, 0x03, 0x00,
0x57, 0xdd, 0xbd, 0x2d, 0xab, 0x1f, 0x2d, 0x51, 0xfa, 0xef, 0x7f, 0xfc,
0xff, 0xff, 0x1d, 0x00, 0x5d, 0x30, 0x29, 0x7b, 0xad, 0xaf, 0xad, 0xa6,
0xfe, 0xff, 0x7f, 0xfc, 0xff, 0xff, 0x00, 0x00, 0x13, 0x8c, 0x5a, 0xf5,
0xfe, 0xff, 0x3f, 0xd7, 0xf7, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0x00, 0x00,
0x84, 0x10, 0x55, 0xa5, 0xbf, 0xff, 0x3f, 0xa8, 0xf4, 0xff, 0x3f, 0xfe,
0xff, 0xff, 0x01, 0x00 };

25
src/main.cpp

@ -43,6 +43,7 @@
#include "main.h"
#include "mp3.h"
#include "BME280.h"
#include "image.h"
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
@ -56,6 +57,8 @@ char timeStr[20];
BME280 bme280;
MP3 mp3;
uint32_t lastButtonPress = 0;
//Variables for the sensor and the battery
static const int buttonPin = 0;
static const int ResetPin = 17;
@ -164,6 +167,8 @@ void setup() {
u8g2.sendBuffer();
mp3.begin();
u8g2.setContrast(127);
}
@ -252,9 +257,22 @@ void loop()
{
timeClient.update();
timeClient.getFormattedTime().toCharArray(timeStr, 50);
if (digitalRead(buttonPin) == LOW) {
if(millis() - lastButtonPress >= 1000) {
Serial.println("Button pressed");
lastButtonPress = millis();
if (mp3.playing) {
mp3.stop();
} else {
mp3.start();
}
}
}
u8g2.clearBuffer(); // clear the internal memory
if (digitalRead(buttonPin == HIGH)) {
if (digitalRead(buttonPin) == HIGH) {
u8g2.setFont(u8g2_font_inb19_mf);
u8g2.drawStr(0, 20, timeStr);
@ -269,10 +287,7 @@ void loop()
u8g2.drawUTF8(0, 54, title1);
u8g2.drawUTF8(0, 64, title2);
} else {
u8g2.setFont(u8g2_font_inb19_mf);
u8g2.drawUTF8(0,20, "Hallo!");
u8g2.drawUTF8(20,45, "test");
u8g2.drawUTF8(50,64, "X");
u8g2.drawXBMP(0,0, IMG_1872_width, IMG_1872_height, IMG_1872_bits);
}
u8g2.sendBuffer();
// bme280.printValues();

103
src/mp3.cpp

@ -36,51 +36,86 @@ void MP3::cTaskWrapper(void* parameters) {
static_cast<MP3*>(parameters)->mp3_decoder_task(NULL);
}
void MP3::stop() {
Serial.println("Stopping mp3 playback");
strcpy(titleStr, "");
playing = false;
}
void MP3::start() {
Serial.println("Starting mp3 playback");
Serial.printf("%x\n", audioTaskHandle);
if (playing == false) {
xTaskCreate(
&cTaskWrapper, /* Task function. */
"audioTask", /* String with name of task. */
2048, /* Stack size in words. */
this, /* Parameter passed as input of the task */
tskIDLE_PRIORITY+1, /* Priority of the task. */
&audioTaskHandle); /* Task handle. */
}
}
void MP3::mp3_decoder_task(void *pvParameters) {
while(true) {
if (mp3->isRunning()) {
if (millis()-lastms > 1000) {
lastms = millis();
Serial.printf("Running for %d ms...\n", lastms);
Serial.flush();
}
if (!mp3->loop()) mp3->stop();
} else {
Serial.printf("MP3 done\n");
delay(1000);
file = new AudioFileSourceICYStream(URL);
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize);
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
if (out == NULL) {
out = new AudioOutputI2S(I2S_NUM_0, false, 0);
out->SetPinout(12, 13, 25);
}
mp3 = new AudioGeneratorMP3();
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
mp3->begin(buff, out);
out->SetGain(1.0);
playing = true;
while(mp3->isRunning()) {
if (millis()-lastms > 1000) {
lastms = millis();
Serial.printf("Running for %d ms...\n", lastms);
Serial.flush();
}
vTaskDelay(10 / portTICK_PERIOD_MS);
if (!mp3->loop()) break;
if (!playing) break;
vTaskDelay(5 / portTICK_PERIOD_MS);
}
Serial.printf("MP3 done\n");
playing = false;
if (mp3) {
mp3->stop();
delete mp3;
mp3 = NULL;
}
if (buff) {
buff->close();
delete buff;
buff = NULL;
}
if (file) {
file->close();
delete file;
file = NULL;
}
vTaskDelete(NULL);
// audioTaskHandle = NULL;
}
bool MP3::begin() {
strcpy(titleStr, "StreamTitle");
strcpy(titleStr, ">");
// First, preallocate all the memory needed for the buffering and codecs, never to be freed
preallocateBuffer = malloc(preallocateBufferSize);
preallocateCodec = malloc(preallocateCodecSize);
if (!preallocateBuffer || !preallocateCodec) {
Serial.printf_P(PSTR("FATAL ERROR: Unable to preallocate %d bytes for app\n"), preallocateBufferSize+preallocateCodecSize);
return false;
}
Serial.println("Starting mp3 playback");
file = new AudioFileSourceICYStream(URL);
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize);
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
out = new AudioOutputI2S(I2S_NUM_0, false);
out->SetPinout(12, 13, 25);
out->SetOutputModeMono(false);
out->SetRate(44100);
out->SetBitsPerSample(16);
out->SetChannels(2);
mp3 = new AudioGeneratorMP3(preallocateCodec, preallocateCodecSize);
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
mp3->begin(buff, out);
out->SetGain(1.0);
/*
I2S0.clkm_conf.clka_en = 1; // was 0
@ -95,13 +130,7 @@ I2S0.clkm_conf.clkm_div_num = 1; // was clkmInteger;
i2s_set_clk(I2S_NUM, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_STEREO);
*/
xTaskCreate(
&cTaskWrapper, /* Task function. */
"audioTask", /* String with name of task. */
2048, /* Stack size in words. */
this, /* Parameter passed as input of the task */
tskIDLE_PRIORITY+1, /* Priority of the task. */
&audioTaskHandle); /* Task handle. */
return true;
}

7
src/mp3.h

@ -16,8 +16,11 @@ class MP3 {
public:
MP3();
bool begin(void);
//const char *URL="http://streaming.shoutcast.com/80sPlanet?lang=en-US";
const char *URL="http://swr-swr1-bw.cast.addradio.de/swr/swr1/bw/mp3/64/stream.mp3";
const char *URL="http://streaming.shoutcast.com/80sPlanet?lang=en-US";
//const char *URL="http://swr-swr1-bw.cast.addradio.de/swr/swr1/bw/mp3/64/stream.mp3";
void stop(void);
void start(void);
bool playing = false;
private:
AudioGeneratorMP3 *mp3;
AudioFileSourceICYStream *file;

Loading…
Cancel
Save