Skip to content

Instantly share code, notes, and snippets.

@suadanwar
Created September 10, 2020 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suadanwar/9adb586cee6df3425f2aab5cd4991f30 to your computer and use it in GitHub Desktop.
Save suadanwar/9adb586cee6df3425f2aab5cd4991f30 to your computer and use it in GitHub Desktop.
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