From 28e80c393bae0cf6c296c1a11a096d10c20a1ae7 Mon Sep 17 00:00:00 2001 From: Stefan Gehr <stefan.kerman.gehr@fau.de> Date: Thu, 13 Oct 2022 15:02:28 +0200 Subject: [PATCH] mp3 stuff --- solarpump/mp3.h | 2 ++ solarpump/mp3.ino | 64 ++++++++++++++++++++++++++++++++++++++++- solarpump/pins.h | 1 + solarpump/solarpump.ino | 15 +++++----- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/solarpump/mp3.h b/solarpump/mp3.h index f2e258f..bcc64ed 100644 --- a/solarpump/mp3.h +++ b/solarpump/mp3.h @@ -3,5 +3,7 @@ void mp3Setup(void); void playFile(uint8_t fileNumber); +void mp3Loop(void); +void playFileQueue(uint8_t fileNumber); #endif diff --git a/solarpump/mp3.ino b/solarpump/mp3.ino index e315690..98e94f2 100644 --- a/solarpump/mp3.ino +++ b/solarpump/mp3.ino @@ -3,13 +3,75 @@ static SoftwareSerial mp3(MP3RX, MP3TX); -static uint8_t fileQueue[16]; +#define QUEUESIZE 16 +static uint8_t fileQueue[QUEUESIZE]; +static uint8_t currentPlayPos = 0; // fileQueue[currentPlayPos] is currently played +static uint8_t currentQueueEnd = 0; // fileQueue[currentQueue] is the next free place in the queue void mp3Setup(void) { mp3.begin(4800); + pinMode(MP3BUSY, INPUT_PULLUP); //mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card } void playFile(uint8_t fileNumber) { mp3.write(fileNumber); } + +// play next file in queue if device is not busy anymore +void mp3Loop(void) { + // only do things if we were just playing a file + // and we are not busy + if (fileQueue[currentPlayPos] == 0) { + Serial.print("No file was being played at pos "); + Serial.println(currentPlayPos); + return; + } + if (digitalRead(MP3BUSY) == LOW) { + Serial.println("mp3 still busy"); + return; + } + + Serial.print("Emptying at playpos "); + Serial.println(currentPlayPos); + fileQueue[currentPlayPos] = 0; + + // advance one position further + ++currentPlayPos; + // in case of wraparound + if (currentPlayPos == QUEUESIZE) { + currentPlayPos = 0; + } + Serial.print("new playpos "); + Serial.println(currentPlayPos); + + // here is no file to play? + if (fileQueue[currentPlayPos] == 0) { + Serial.println("There is no file to play here"); + return; + } + + Serial.print("Playing file "); + Serial.print(fileQueue[currentPlayPos]); + Serial.print(" at playpos "); + Serial.println(currentPlayPos); + mp3.write(fileQueue[currentPlayPos]); +} + +// add file to the queue and play start playing the queue +void playFileQueue(uint8_t fileNumber) { + Serial.print("Adding "); + Serial.print(fileNumber); + Serial.print(" to queue at pos "); + Serial.println(currentQueueEnd); + fileQueue[currentQueueEnd] = fileNumber; + // the end is now one pos further + ++currentQueueEnd; + // in case of wraparound + if (currentQueueEnd == QUEUESIZE) { + currentQueueEnd = 0; + } + Serial.print("New queue end at pos "); + Serial.println(currentQueueEnd); + mp3Loop(); +} diff --git a/solarpump/pins.h b/solarpump/pins.h index 23c054f..f12e2c0 100644 --- a/solarpump/pins.h +++ b/solarpump/pins.h @@ -8,6 +8,7 @@ const uint8_t T2DOWN = 5; const uint8_t PUMP1 = 8; const uint8_t PUMP2 = 9; const uint8_t BUTTON = 10; +const uint8_t MP3BUSY = 11; const uint8_t MP3TX = 12; const uint8_t MP3RX = 13; #endif diff --git a/solarpump/solarpump.ino b/solarpump/solarpump.ino index bf894c2..22cf814 100644 --- a/solarpump/solarpump.ino +++ b/solarpump/solarpump.ino @@ -125,7 +125,7 @@ void displayText(float t1up_C, float t1down_C, bool pump1, float t2up_C, float t } void displayLight(bool turnOn) { - const uint32_t lightMillis = 10000; + const uint32_t lightMillis = 30000; static bool curState = false; static uint32_t prevMillis = 0; // turn on @@ -159,11 +159,14 @@ void loop(void) { static float t2up_C = 0.0; static float t2down_C = 0.0; + mp3Loop(); + if (checkButton() == LOW) { displayLight(true); - playFile(SOUND_TEMPERATURE); - delay(2000); - playFile(SOUND_HOCHSTER_WERT); + Serial.println("Adding temperature to queue"); + playFileQueue(SOUND_TEMPERATURE); + Serial.println("Adding höchstwert to queue"); + playFileQueue(SOUND_HOCHSTER_WERT); } else { displayLight(false); } @@ -171,19 +174,15 @@ void loop(void) { if (t1up(&t1up_C) == LONG_TIME_ERROR || t1down(&t1down_C) == LONG_TIME_ERROR) { // TODO: Warning sound // Turn pump on to be safe - Serial.println("LONG TIME ERROR for solar system 1"); digitalWrite(PUMP1, HIGH); } else { - Serial.println("controlPump for solar system 1"); controlPump(PUMP1, t1up_C, t1down_C); } if (t2up(&t2up_C) == LONG_TIME_ERROR || t2down(&t2down_C) == LONG_TIME_ERROR) { // TODO: Warning sound // Turn pump on to be safe - Serial.println("LONG TIME ERROR for solar system 2"); digitalWrite(PUMP2, HIGH); } else { - Serial.println("controlPump for solar system 2"); controlPump(PUMP2, t2up_C, t2down_C); } -- GitLab