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 lastUpdate = 0;
uint32_t lastTransmit = 0; uint32_t lastTransmit = 0;
bool sensorON = false; bool sensorON = false;
bool alarmRunning = false;
//Variables for the mqtt packages and topics //Variables for the mqtt packages and topics
String commandTopic; String commandTopic;
@ -383,11 +384,12 @@ struct tm getAlarmTime() {
} }
bool isAlarmActive(){ bool isAlarmActive(){
time_t now; /* time_t now;
time(&now); time(&now);
double seconds = difftime(now, mktime(&(state_persistant.alarmTime))); double seconds = difftime(now, mktime(&(state_persistant.alarmTime)));
if (seconds > -24*60*60 && seconds <=0) return true; 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); Serial.printf("alarm in %f seconds (%d)\n", seconds, state_persistant.alarmArmed);
if (seconds >= 0 && state_persistant.alarmArmed) { if (seconds >= 0 && state_persistant.alarmArmed) {
state_persistant.alarmArmed = false; if (mp3.aborted) alarmRunning = false;
lastActive = millis(); if (!mp3.playing && !alarmRunning) {
led.wakeUpLight(0); led.wakeUpLight(0);
mp3.setVolume(2);
Serial.println("WAKEUP TIME!!!!!"); Serial.println("WAKEUP TIME!!!!!");
// mp3.setVolume(10); alarmRunning = true;
// mp3.start_Progmem(); if (!mp3.aborted && iot.wifi.status() == WL_CONNECTED) {
// delay(1500); mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3");
} else {
static const char starwars[] PROGMEM = 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"; "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 // Plenty more at: http://mines.lumpylumpy.com/Electronics/Computers/Software/Cpp/MFC/RingTones.RTTTL
mp3.playRTTTL(starwars, strlen_P(starwars)); mp3.playRTTTL(starwars, strlen_P(starwars));
delay(15000); // mp3.start_Progmem();
}
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) { if (state_persistant.alarmArmed && seconds >= -5*60 && seconds <= 0) {
led.wakeUpLight(255*(seconds+300)/300); led.wakeUpLight(255*(seconds+300)/300);
} }
@ -471,6 +477,9 @@ void loop()
Serial.print("Free Heap: "); Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap()); Serial.println(ESP.getFreeHeap());
Serial.print("lastButtonPress: ");
Serial.println(millis() - lastButtonPress);
/* if (sensorON && digitalRead(sensorPin) == LOW) { /* if (sensorON && digitalRead(sensorPin) == LOW) {
uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, "OFF"); uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, "OFF");
sensorON = false; sensorON = false;

13
src/mp3.cpp

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

1
src/mp3.h

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

Loading…
Cancel
Save