From eed80fb88f51b8eb7c43dfd17ec8d6891592a911 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anian=20B=C3=BChler?=
 <anian.buehler@reutlingen-university.de>
Date: Tue, 13 Jul 2021 11:50:49 +0200
Subject: [PATCH] added readDataSerial and reorganized Client1+2 examples

---
 examples/sPSN_Client1/sPSN_Client1.ino |  6 +--
 examples/sPSN_Client2/sPSN_Client2.ino | 10 ++---
 src/didacticNet.cpp                    | 53 +++++++++++++++++---------
 src/didacticNet.h                      |  1 +
 4 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/examples/sPSN_Client1/sPSN_Client1.ino b/examples/sPSN_Client1/sPSN_Client1.ino
index 1ba99c0..b432b02 100644
--- a/examples/sPSN_Client1/sPSN_Client1.ino
+++ b/examples/sPSN_Client1/sPSN_Client1.ino
@@ -76,11 +76,11 @@ void loop() {
   if(valueChanged(currentValue, THRESHOLD)){
     itoa(currentValue, payload, 10);                    //wandle Wert in ASCII-Zeichen
     //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen
-    newData = true;                                     // neue Daten zum Senden
+    newData = true;                                     //Flag: neue Daten zum Senden
   }
-  if(timeElapsed(SEND_DELAY) && newData){
+  if(timeElapsed(SEND_DELAY) && newData){     //Sende neue Daten wenn Mindestwaretzeit abgelaufen
     psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor
-    newData = false;                          //Daten wurden gesendet
+    newData = false;                          //Flag: keine neuen Daten vorhanden
   }
 }
 
diff --git a/examples/sPSN_Client2/sPSN_Client2.ino b/examples/sPSN_Client2/sPSN_Client2.ino
index a978799..a01de96 100644
--- a/examples/sPSN_Client2/sPSN_Client2.ino
+++ b/examples/sPSN_Client2/sPSN_Client2.ino
@@ -73,14 +73,14 @@ void loop() {
   boolean buttonState = digitalRead(BUTTON_PIN); //lese Poti ein und speichere Wert
 
   if(edgeDetected(buttonState) == RISING){
-    ledState = !ledState;                     //Invertiere zu sendenden Wert "LED-Zustand"
-    itoa(ledState, payload, 10);                     //wandle Wert in ASCII-Zeichen
+    ledState = !ledState;                               //Invertiere zu sendenden Wert "LED-Zustand"
+    itoa(ledState, payload, 10);                        //wandle Wert in ASCII-Zeichen
     //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen 
-    newData = true;                                     // neue Daten zum Senden
+    newData = true;                                     //Flag: neue Daten zum Senden
   }
-  if(timeElapsed(SEND_DELAY) && newData){
+  if(timeElapsed(SEND_DELAY) && newData){     //Sende neue Daten wenn Mindestwaretzeit abgelaufen
     psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor
-    newData = false;                          //Daten wurden gesendet
+    newData = false;                          //Flag: keine neuen Daten vorhanden
   }
 }
 
diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp
index 0052e72..f54cf08 100644
--- a/src/didacticNet.cpp
+++ b/src/didacticNet.cpp
@@ -11,41 +11,60 @@
 //**************************************************************************
 //LITTLE HELPERS ;-)
 //**************************************************************************
-int edgeDetected(bool currentState){
-  static bool lastState = false;
-  int edge = 0;
+int edgeDetected(bool edCurrentState){
+  static bool edLastState = false;
+  int edEdge = 0;
 
-  if(currentState && !lastState){
-    edge = RISING;
+  if(edCurrentState && !edLastState){
+    edEdge = RISING;
   }
-  if(!currentState && lastState){
-    edge = FALLING;
+  if(!edCurrentState && edLastState){
+    edEdge = FALLING;
   }
-  lastState = currentState;
-  return edge;
+  edLastState = edCurrentState;
+  return edEdge;
 }
 
-bool valueChanged(int value, int threshold){
-  static int lastValue = 0;
+bool valueChanged(int teValue, int teThreshold){
+  static int vcLastValue = 0;
 
-  if(abs(value-lastValue) > threshold){
-    lastValue = value;
+  if(abs(teValue-vcLastValue) > teThreshold){
+    vcLastValue = teValue;
     return true;
   }
   return false;
 }
 
-bool timeElapsed(long delayTime){
-  static long lastTime = 0L;
+bool timeElapsed(long teDelayTime){
+  static long teLastTime = 0L;
   long currentTime = millis();
 
-  if(lastTime + (delayTime-1) < currentTime){
-    lastTime = currentTime;
+  if(teLastTime + (teDelayTime-1) < currentTime){
+    teLastTime = currentTime;
     return true;
   }
   return false;
 }
 
+int readSerialData(Stream& rsStream, char* rsDataArray, char rsEndSign) {
+  static int rsCounter = 0;
+
+  if (rsStream.available()) {
+    char charBuffer = rsStream.read();
+    rsDataArray[rsCounter] = charBuffer;
+
+    if (charBuffer == rsEndSign) {
+      rsDataArray[rsCounter] = '\0';
+      int nrOfChars = rsCounter-1;
+      rsCounter = 0;
+      return nrOfChars; 
+    } else {
+      rsCounter++;
+    }
+  }
+  return 0;
+}
+
 
 //**************************************************************************
 //ROOT
diff --git a/src/didacticNet.h b/src/didacticNet.h
index 05b0990..1132420 100644
--- a/src/didacticNet.h
+++ b/src/didacticNet.h
@@ -43,6 +43,7 @@
 int  edgeDetected(bool);
 bool valueChanged(int, int);
 bool timeElapsed(long);
+int readSerialData(Stream&, char*, char);
 
 class didacticPSNet
 {
-- 
GitLab