Skip to content

Instantly share code, notes, and snippets.

@terryspitz
Created May 5, 2019 09:10
Show Gist options
  • Save terryspitz/3153448c92db5182ad592891ccfebfd4 to your computer and use it in GitHub Desktop.
Save terryspitz/3153448c92db5182ad592891ccfebfd4 to your computer and use it in GitHub Desktop.
WeMos ESP8266Audio code for playing Foo Fighter through Conrad the Camel see https://hackaday.io/project/165395-conrad-the-camel-plays-foo-fighters
#include <ESP8266WiFi.h>
#include "AudioFileSourceSPIFFS.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2SNoDAC.h"
#include <ESP8266SAM.h>
AudioGeneratorWAV *audio;
AudioFileSourceSPIFFS *file;
AudioOutputI2SNoDAC *out;
ESP8266SAM *sam;
#define BUTTON_PIN D1
#define MOTOR_PIN D2
void setup()
{
WiFi.mode(WIFI_OFF);
pinMode(RX, INPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(MOTOR_PIN, OUTPUT);
digitalWrite(MOTOR_PIN, HIGH);
//if(digitalRead(BUTTON_PIN)==LOW)
// ESP.deepSleep(2e6);
Serial.begin(115200);
delay(100);
Serial.println("Starting...");
file = NULL;
out = new AudioOutputI2SNoDAC(); //Outputs on RX pin
audio = new AudioGeneratorWAV();
// sam = new ESP8266SAM;
// sam->SetSingMode(true);
// sam->SetSpeed(50);
// //enum SAMVoice { VOICE_SAM, VOICE_ELF, VOICE_ROBOT, VOICE_STUFFY, VOICE_OLDLADY, VOICE_ET };
// //for(ESP8266SAM::SAMVoice v=ESP8266SAM::VOICE_SAM; v<=ESP8266SAM::VOICE_ET; ++(int&)v) {
// //sam->SetVoice(v);
// sam->Say(out, "What if I say I'm not like the others.");
// sam->Say(out, "What if I say I'm not just another, one of your plays.");
// sam->Say(out, "You're the pretender. What if I say I will never surrender.");
}
bool pressed = false;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelayMs = 50; // the debounce time
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long sleepDelayS = 5; // go back into sleep mode
void loop()
{
//Debounce the button (either going up or down) and trigger on button release
int reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
lastButtonState = reading;
bool trigger = false;
if ((millis() - lastDebounceTime) > debounceDelayMs)
{
if(reading==HIGH)
{
pressed = true;
Serial.println("Button high");
}
else if(pressed) //and button now low
{
pressed = false;
trigger = true;
Serial.println("Button low");
}
}
if (audio->isRunning()) {
if (trigger || !audio->loop()) {
audio->stop();
digitalWrite(MOTOR_PIN, HIGH);
}
}
else if(trigger)
{
Serial.print("Starting audio...");
delete file;
file = new AudioFileSourceSPIFFS("/pret4amp16k.wav");
if(audio->begin(file, out))
Serial.println("OK");
else
Serial.println("failed...");
digitalWrite(MOTOR_PIN, LOW);
}
// if ((millis() - lastDebounceTime) > (sleepDelayS*1e3)) {
// Serial.println("Going back to sleep");
// ESP.deepSleep(2e6);
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment