From 9a49542f320e070134085ef6444ba4452756070f Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 11 Jun 2018 16:07:33 +0200 Subject: [PATCH] alarm playback failsafe --- src/main.cpp | 45 +++++++++++++++++++++++++++------------------ src/mp3.cpp | 13 +++++++++---- src/mp3.h | 1 + 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d836c0d..9ff6c7b 100644 --- a/src/main.cpp +++ b/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,23 +441,27 @@ 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(); - led.wakeUpLight(0); - Serial.println("WAKEUP TIME!!!!!"); -// mp3.setVolume(10); + if (mp3.aborted) alarmRunning = false; + if (!mp3.playing && !alarmRunning) { + led.wakeUpLight(0); + mp3.setVolume(2); + Serial.println("WAKEUP TIME!!!!!"); + 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)); // mp3.start_Progmem(); -// delay(1500); - - 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"); - led.changeAnimation(2, 0); + } + 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; diff --git a/src/mp3.cpp b/src/mp3.cpp index 2d3d835..34cf0a1 100644 --- a/src/mp3.cpp +++ b/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; diff --git a/src/mp3.h b/src/mp3.h index 1595698..f82b2b2 100644 --- a/src/mp3.h +++ b/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);