Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This sample code is for Audio Dorbell Tutorial.
#pragma mark - Depend ESP8266Audio and ESP8266_Spiram libraries
#include <M5Stack.h>
#include <WiFi.h>
#include "AudioFileSourceSD.h"
#include "AudioFileSourceID3.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"
AudioGeneratorMP3 *mp3;
AudioFileSourceSD *file;
AudioOutputI2S *out;
AudioFileSourceID3 *id3;
bool playing = false;
bool curr = false;
bool prev = false;
void setup() {
M5.begin();
M5.Power.begin();
pinMode(36, INPUT);
Serial.begin(9600);
}
void loop() {
curr = digitalRead(36);
if (curr != prev) {
if (curr == true) {
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setCursor(30, 70);
M5.Lcd.setTextSize(10);
M5.Lcd.printf("Hello!Welcome");
play();
}
prev = curr;
}
if (playing) {
if (mp3->isRunning()) {
if (!mp3->loop()) {
mp3->stop();
playing = false;
M5.Lcd.fillScreen(BLACK);
delay(500);
}
} else {
delay(1000);
}
}
M5.update();
}
bool play() {
if (file) {
delete file;
file = NULL;
}
if (mp3) {
delete mp3;
mp3 = NULL;
}
if (out) {
delete out;
out = NULL;
}
file = new AudioFileSourceSD("/Hello.mp3");
id3 = new AudioFileSourceID3(file);
out = new AudioOutputI2S(0, 1);
out->SetOutputModeMono(true);
mp3 = new AudioGeneratorMP3();
mp3->begin(id3, out);
playing = true;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment