From 97bee4ff3bdc386822d5879732079f1d05b2083a Mon Sep 17 00:00:00 2001
From: Stefan Gehr <stefan.kerman.gehr@fau.de>
Date: Fri, 14 Oct 2022 10:19:07 +0200
Subject: [PATCH] using queue for error sound; setting LONG_TIME_ERROR time to
 10 seconds for testing and showing

---
 solarpump/mp3.h           |  1 +
 solarpump/mp3.ino         | 17 +++++++++++++----
 solarpump/solarpump.ino   |  5 ++++-
 solarpump/temperature.ino |  2 +-
 4 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/solarpump/mp3.h b/solarpump/mp3.h
index 038879b..2c33eb5 100644
--- a/solarpump/mp3.h
+++ b/solarpump/mp3.h
@@ -2,6 +2,7 @@
 #define MP3_H
 
 void mp3Setup(void);
+uint8_t queueLength(void);
 void playFile(uint8_t fileNumber, bool respectBusyPin);
 void mp3Loop(void);
 void playFileQueue(uint8_t fileNumber);
diff --git a/solarpump/mp3.ino b/solarpump/mp3.ino
index 7b0c704..39e38b9 100644
--- a/solarpump/mp3.ino
+++ b/solarpump/mp3.ino
@@ -7,6 +7,10 @@ static SoftwareSerial mp3(MP3RX, MP3TX);
 // the queue for playing sounds
 #define QUEUESIZE 64
 static uint8_t fileQueue[QUEUESIZE];
+// next playing position in the queue
+static uint8_t currentPlayPos = 0;
+// position of end of queue
+static uint8_t currentQueueEnd = 0;
 
 // needs to be run once at the start
 void mp3Setup(void) {
@@ -15,6 +19,15 @@ void mp3Setup(void) {
 	//mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card
 }
 
+// return the number of files waiting in the queue
+uint8_t queueLength(void) {
+	// in case of wrapround in the queue
+	if (currentQueueEnd < currentPlayPos) {
+		return currentQueueEnd + QUEUESIZE - currentPlayPos;
+	}
+	return currentPlayPos - currentQueueEnd;
+}
+
 // immediately play a file
 void playFile(uint8_t fileNumber, bool respectBusyPin) {
 	// do nothing if we want to wait for the BUSY pin
@@ -34,8 +47,6 @@ void mp3Loop(void) {
 	 * 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) {
@@ -72,8 +83,6 @@ void mp3Loop(void) {
 
 // add a file to the queue and start playing the queue
 void playFileQueue(uint8_t fileNumber) {
-	// position of end of queue
-	static uint8_t currentQueueEnd = 0;
 	// save file to queue
 	fileQueue[currentQueueEnd] = fileNumber;
 
diff --git a/solarpump/solarpump.ino b/solarpump/solarpump.ino
index e10331c..67cc7e9 100644
--- a/solarpump/solarpump.ino
+++ b/solarpump/solarpump.ino
@@ -246,7 +246,10 @@ void loop(void) {
 
 	if (t1upState == LONG_TIME_ERROR || t1downState == LONG_TIME_ERROR || t2upState == LONG_TIME_ERROR || t2downState == LONG_TIME_ERROR) {
 		// continuously say that there is an error
-		playFile(SOUND_FEHLER, true);
+		if (queueLength() == 0) {
+			playFileQueue(SOUND_FEHLER);
+			playFileQueue(SOUND_STILLE);
+		}
 	}
 
 	// control pump of system 1
diff --git a/solarpump/temperature.ino b/solarpump/temperature.ino
index 7b749e6..a4e9243 100644
--- a/solarpump/temperature.ino
+++ b/solarpump/temperature.ino
@@ -5,7 +5,7 @@
 
 /* No new valid temperature for < 60 seconds? I sleep.
  * No new valid temperature for > 60 seconds? REAL PROBLEM! */
-const uint32_t ERRORTIME =  60000;
+const uint32_t ERRORTIME =  10000;
 // Update the temperature only every second
 const uint32_t TEMPUPDATETIME = 1000;
 
-- 
GitLab