Browse Source

alarm playback failsafe

main
Hendrik Langer 7 years ago
parent
commit
9a49542f32
  1. 33
      src/main.cpp
  2. 13
      src/mp3.cpp
  3. 1
      src/mp3.h

33
src/main.cpp

@ -71,6 +71,7 @@ uint32_t lastActive = 0;
uint32_t lastUpdate = 0;
uint32_t lastTransmit = 0;
bool sensorON = false;
bool alarmRunning = false;
//Variables for the mqtt packages and topics
String commandTopic;
@ -383,11 +384,12 @@ struct tm getAlarmTime() {
}
bool isAlarmActive(){
time_t now;
/* time_t now;
time(&now);
double seconds = difftime(now, mktime(&(state_persistant.alarmTime)));
if (seconds > -24*60*60 && seconds <=0) return true;
else return false;
else return false;*/
return state_persistant.alarmArmed;
}
@ -439,24 +441,28 @@ void loop()
Serial.printf("alarm in %f seconds (%d)\n", seconds, state_persistant.alarmArmed);
if (seconds >= 0 && state_persistant.alarmArmed) {
state_persistant.alarmArmed = false;
lastActive = millis();
if (mp3.aborted) alarmRunning = false;
if (!mp3.playing && !alarmRunning) {
led.wakeUpLight(0);
mp3.setVolume(2);
Serial.println("WAKEUP TIME!!!!!");
// mp3.setVolume(10);
// mp3.start_Progmem();
// delay(1500);
alarmRunning = true;
if (!mp3.aborted && iot.wifi.status() == WL_CONNECTED) {
mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3");
} else {
static const char starwars[] PROGMEM =
"Cantina:d=8,o=5,b=250:a,p,d6,p,a,p,d6,p,a,d6,p,a,p,g#,4a,a,g#,a,4g,f#,g,f#,4f.,d.,16p,4p.,a,p,d6,p,a,p,d6,p,a,d6,p,a,p,g#,a,p,g,p,4g.,f#,g,p,c6,4a#,4a,4g";
// 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.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3");
// mp3.start_Progmem();
}
led.changeAnimation(2, 0);
}
if (lastButtonPress != 0 && millis() - lastButtonPress <= 10*1000) state_persistant.alarmArmed = false; // is this okay?
if (seconds >= 360) state_persistant.alarmArmed = false; // auto disable after time
if (state_persistant.alarmArmed == false) mp3.stop();
lastActive = millis();
}
if (state_persistant.alarmArmed && seconds >= -5*60 && seconds <= 0) {
led.wakeUpLight(255*(seconds+300)/300);
}
@ -471,6 +477,9 @@ void loop()
Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap());
Serial.print("lastButtonPress: ");
Serial.println(millis() - lastButtonPress);
/* if (sensorON && digitalRead(sensorPin) == LOW) {
uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, "OFF");
sensorON = false;

13
src/mp3.cpp

@ -52,6 +52,7 @@ void MP3::stop() {
Serial.println("Stopping mp3 playback");
strcpy(titleStr, "stopping..");
playing = false;
aborted = false;
// while (audioTaskHandle && eTaskGetState(audioTaskHandle) != eDeleted) {
// Serial.println("waiting for audio to finish");
delay(100);
@ -71,6 +72,7 @@ void MP3::start() {
}
Serial.println("Starting mp3 playback");
Serial.printf("%x\n", audioTaskHandle);
aborted = false;
if (playing == false) {
xTaskCreate(
&cTaskWrapper, /* Task function. */
@ -166,17 +168,20 @@ void MP3::mp3_decoder_task(void *pvParameters) {
volumeChanged = false;
out->SetGain(((float)volume)/(100.0/1.50)); // gain from 0.1 to 4.0
}
if (!decoder->loop()) break;
if (!decoder->loop()) {
aborted = true;
break;
}
if (!playing) break;
vTaskDelay(5 / portTICK_PERIOD_MS);
}
Serial.printf("MP3 done\n");
strcpy(titleStr, "stopped");
playing = false;
i2s_zero_dma_buffer(I2S_NUM_0);
Serial.printf("MP3 done\n");
strcpy(titleStr, "stopped");
if (decoder) {
decoder->stop();
delete decoder;

1
src/mp3.h

@ -27,6 +27,7 @@ class MP3 {
void setVolume(int);
int getVolume();
bool playing = false;
bool aborted = false;
int volume = 20;
bool volumeChanged = true;
uint32_t getBuffFill(void);

Loading…
Cancel
Save