Browse Source

add RTTTL player and alarm

main
Hendrik Langer 7 years ago
parent
commit
dea94c9e14
  1. 4
      platformio.ini
  2. 7
      src/main.cpp
  3. 16
      src/mp3.cpp
  4. 5
      src/mp3.h

4
platformio.ini

@ -24,8 +24,8 @@ lib_deps =
u8g2 u8g2
NTPClient NTPClient
; ESP8266Audio ; ESP8266Audio
; https://github.com/earlephilhower/ESP8266Audio.git https://github.com/earlephilhower/ESP8266Audio.git
https://github.com/h3ndrik/ESP8266Audio.git ; https://github.com/h3ndrik/ESP8266Audio.git
https://github.com/Gianbacchio/ESP8266_Spiram.git https://github.com/Gianbacchio/ESP8266_Spiram.git
Adafruit Unified Sensor Adafruit Unified Sensor
Adafruit BME280 Library Adafruit BME280 Library

7
src/main.cpp

@ -436,6 +436,13 @@ void loop()
// mp3.setVolume(10); // mp3.setVolume(10);
// mp3.start_Progmem(); // mp3.start_Progmem();
// delay(1500); // delay(1500);
static const char starwars[] PROGMEM =
"starwars:d=4,o=5,b=180:8f,8f,8f,2a#.,2f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8d#6,2c6";
// Plenty more at: http://mines.lumpylumpy.com/Electronics/Computers/Software/Cpp/MFC/RingTones.RTTTL
mp3.playRTTTL(starwars, strlen_P(starwars));
delay(15000);
mp3.setVolume(2); mp3.setVolume(2);
mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3");
led.changeAnimation(2, 0); led.changeAnimation(2, 0);

16
src/mp3.cpp

@ -6,6 +6,7 @@
#include "AudioFileSourcePROGMEM.h" #include "AudioFileSourcePROGMEM.h"
#include "AudioFileSourceBuffer.h" #include "AudioFileSourceBuffer.h"
#include "AudioGeneratorMP3.h" #include "AudioGeneratorMP3.h"
#include "AudioGeneratorRTTTL.h"
#include "AudioOutputI2S.h" #include "AudioOutputI2S.h"
//#include "driver/gpio.h" //#include "driver/gpio.h"
//#include "driver/periph_ctrl.h" //#include "driver/periph_ctrl.h"
@ -84,6 +85,13 @@ void MP3::start_Progmem() {
start(); start();
} }
void MP3::playRTTTL(const char* str, size_t len) {
rtttl = str;
rtttl_len = len;
volume = 2;
start();
}
void MP3::setVolume(int volume) { void MP3::setVolume(int volume) {
if (volume > 0 && volume <= 100) { if (volume > 0 && volume <= 100) {
this->volume = volume; this->volume = volume;
@ -109,6 +117,7 @@ void MP3::mp3_decoder_task(void *pvParameters) {
// strcpy(titleStr, "loading..."); // strcpy(titleStr, "loading...");
if (fromProgmem) file = new AudioFileSourcePROGMEM(alarmsound, alarmsound_len); if (fromProgmem) file = new AudioFileSourcePROGMEM(alarmsound, alarmsound_len);
else if (rtttl_len > 0) file = new AudioFileSourcePROGMEM( rtttl, rtttl_len );
else file = new AudioFileSourceICYStream(URL); else file = new AudioFileSourceICYStream(URL);
file->RegisterMetadataCB(MDCallback, (void*)"ICY"); file->RegisterMetadataCB(MDCallback, (void*)"ICY");
buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize); buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize);
@ -118,7 +127,13 @@ void MP3::mp3_decoder_task(void *pvParameters) {
out = new AudioOutputI2S(I2S_NUM_0, false); out = new AudioOutputI2S(I2S_NUM_0, false);
out->SetPinout(12, 13, 25); out->SetPinout(12, 13, 25);
} }
if (rtttl_len > 0) {
decoder = new AudioGeneratorRTTTL();
Serial.println(rtttl_len); Serial.println(FPSTR(rtttl));
rtttl_len = 0;
} else {
decoder = new AudioGeneratorMP3(preallocateCodec, preallocateCodecSize); decoder = new AudioGeneratorMP3(preallocateCodec, preallocateCodecSize);
}
decoder->RegisterStatusCB(StatusCallback, (void*)"mp3"); decoder->RegisterStatusCB(StatusCallback, (void*)"mp3");
decoder->begin(buff, out); decoder->begin(buff, out);
if (volume > 0 && volume <= 100) { if (volume > 0 && volume <= 100) {
@ -143,6 +158,7 @@ void MP3::mp3_decoder_task(void *pvParameters) {
strcpy(titleStr, "stopped"); strcpy(titleStr, "stopped");
playing = false; playing = false;
i2s_zero_dma_buffer(I2S_NUM_0);
if (decoder) { if (decoder) {
decoder->stop(); decoder->stop();

5
src/mp3.h

@ -1,6 +1,8 @@
#ifndef _MP3_H #ifndef _MP3_H
#define _MP3_H #define _MP3_H
#include <Arduino.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#include <SD.h> #include <SD.h>
#include "AudioFileSourceICYStream.h" #include "AudioFileSourceICYStream.h"
@ -21,6 +23,7 @@ class MP3 {
void start(void); void start(void);
void start(const char*); void start(const char*);
void start_Progmem(); void start_Progmem();
void playRTTTL(const char *, size_t);
void setVolume(int); void setVolume(int);
int getVolume(); int getVolume();
bool playing = false; bool playing = false;
@ -28,6 +31,8 @@ class MP3 {
bool volumeChanged = true; bool volumeChanged = true;
uint32_t getBuffFill(void); uint32_t getBuffFill(void);
bool fromProgmem; bool fromProgmem;
const char* rtttl PROGMEM;
size_t rtttl_len = (size_t)0;
private: private:
TaskHandle_t audioTaskHandle; TaskHandle_t audioTaskHandle;
void mp3_decoder_task(void*); void mp3_decoder_task(void*);

Loading…
Cancel
Save