Skip to content
Snippets Groups Projects
Commit dcc41184 authored by Stefan Gehr's avatar Stefan Gehr
Browse files

implemented queue

parent 4abf1c75
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......@@ -163,10 +163,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);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment