diff --git a/solarpump/mp3.ino b/solarpump/mp3.ino index 98e94f2268d2d098094ee622bc95726492c008b4..a58ec6d5d5aa882a11c4e5b28c7334247a2bc3b1 100644 --- a/solarpump/mp3.ino +++ b/solarpump/mp3.ino @@ -3,37 +3,58 @@ static SoftwareSerial mp3(MP3RX, MP3TX); +// the queue for playing sounds #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 +// needs to be run once at the start void mp3Setup(void) { mp3.begin(4800); pinMode(MP3BUSY, INPUT_PULLUP); //mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card } +// immediately play a file, ignoring any queue or BUSY pin void playFile(uint8_t fileNumber) { mp3.write(fileNumber); } // play next file in queue if device is not busy anymore +// this should be constantly run in the loop-function 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); + /* + * the BUSY pin does not react immediately + * waitForBusy gets set to true when we play a file + * after that we wait until the BUSY pin reacts and becomes LOW + * now we can wait for the flank from LOW to HIGH => then the playing is done + */ + static bool waitForBusy = false; + // next playing position in the queue + static uint8_t currentPlayPos = 0; + + // check if we are busy + if (digitalRead(MP3BUSY) == LOW) { + // we are done waiting for the BUSY pin + waitForBusy = false; return; } - if (digitalRead(MP3BUSY) == LOW) { - Serial.println("mp3 still busy"); + + /* + * BUSY pin is HIGH => we are not busy + * if waytForBusy == true, then we have just started playing a file, + * and we know that soon BUSY pin will be LOW + * if fileQueue[currentPlayPos] == 0, then there is no file in the queue + */ + if (waitForBusy || fileQueue[currentPlayPos] == 0) { return; } - Serial.print("Emptying at playpos "); - Serial.println(currentPlayPos); + // play the file at the current queue position + mp3.write(fileQueue[currentPlayPos]); + // see explanation above + waitForBusy = true; + + // file can be removed off the queue fileQueue[currentPlayPos] = 0; // advance one position further @@ -42,36 +63,22 @@ void mp3Loop(void) { 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 +// add a file to the queue and start playing the queue void playFileQueue(uint8_t fileNumber) { - Serial.print("Adding "); - Serial.print(fileNumber); - Serial.print(" to queue at pos "); - Serial.println(currentQueueEnd); + // position of end of queue + static uint8_t currentQueueEnd = 0; + // save file to queue 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); + + // start playing the queue if it is not playing already mp3Loop(); } diff --git a/solarpump/solarpump.ino b/solarpump/solarpump.ino index e893c5284f008e86d88c977e870a44e215ccec4e..bf814fa9a0246768aae80b8bd5e6db822e253d37 100644 --- a/solarpump/solarpump.ino +++ b/solarpump/solarpump.ino @@ -187,10 +187,9 @@ void loop(void) { if (checkButton() == LOW) { displayLight(true); - Serial.println("Adding temperature to queue"); playFileQueue(SOUND_TEMPERATURE); - Serial.println("Adding höchstwert to queue"); playFileQueue(SOUND_HOCHSTER_WERT); + playFileQueue(SOUND_KOMMA); } else { displayLight(false); }