From dd013d6b86c819739b6156dd1ffff5462c89707d 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 14:17:16 +0200
Subject: [PATCH] minor changes

---
 examples/sPSN_Broker/sPSN_Broker.ino   |   2 +-
 examples/sPSN_Chat/sPSN_Chat.ino       |   4 +-
 examples/sPSN_Client1/sPSN_Client1.ino |   4 +-
 examples/sPSN_Client2/sPSN_Client2.ino |   4 +-
 keywords.txt                           |  37 ++++----
 src/didacticNet.cpp                    | 115 ++++++++++++-------------
 src/didacticNet.h                      |  15 ++--
 7 files changed, 92 insertions(+), 89 deletions(-)

diff --git a/examples/sPSN_Broker/sPSN_Broker.ino b/examples/sPSN_Broker/sPSN_Broker.ino
index 662ecf0..65fed09 100644
--- a/examples/sPSN_Broker/sPSN_Broker.ino
+++ b/examples/sPSN_Broker/sPSN_Broker.ino
@@ -26,7 +26,7 @@
  *date:  14.12.2020
  *version: 1.0
  */
-
+#include <Arduino.h>
 #include "SoftwareSerial.h"
 #include "didacticNet.h"
 
diff --git a/examples/sPSN_Chat/sPSN_Chat.ino b/examples/sPSN_Chat/sPSN_Chat.ino
index 151cfda..632a18c 100644
--- a/examples/sPSN_Chat/sPSN_Chat.ino
+++ b/examples/sPSN_Chat/sPSN_Chat.ino
@@ -72,10 +72,10 @@ void loop() {
 
   psnClient.handleNetwork();  //Verarbeiten der Daten, prüfen ob Netzwerk frei und versenden der Daten
 
-  int nrOfAscii = readSerialData(Serial, readData, 13); //Einlesen der Nutzereingabe am SerialMonitor (Rueckgabewert = Anzahl der gelesenen Zeichen)
+  int nrOfAscii = psnClient.readSerialData(Serial, readData, 13); //Einlesen der Nutzereingabe am SerialMonitor (Rueckgabewert = Anzahl der gelesenen Zeichen)
 
   if (nrOfAscii > 0) { //Wenn Daten fertig eingelesen wurden
-  
+
     if (readData[0] == '@') {         //Wenn '@' vorne steht, dann neuer Chatpartner anlegen (neues Topic abonnieren)
       strcpy(readData, readData + 1); //verschiebe um ein Zeichen (entferne '@')
       psnClient.subscribe(readData);  //Lege fest zu welchem Topic Daten empfangen werden sollen (den Namen des Chatpartners)
diff --git a/examples/sPSN_Client1/sPSN_Client1.ino b/examples/sPSN_Client1/sPSN_Client1.ino
index 667ace5..8f72231 100644
--- a/examples/sPSN_Client1/sPSN_Client1.ino
+++ b/examples/sPSN_Client1/sPSN_Client1.ino
@@ -76,12 +76,12 @@ void loop() {
 
   int currentValue = analogRead(POTI_PIN); //lese Poti ein und speichere Wert
 
-  if(valueChanged(currentValue, THRESHOLD)){
+  if(psnClient.valueChanged(currentValue, THRESHOLD)){
     itoa(currentValue, payload, 10);                    //wandle Wert in ASCII-Zeichen
     //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen
     newData = true;                                     //Flag: neue Daten zum Senden
   }
-  if(timeElapsed(SEND_DELAY) && newData){     //Sende neue Daten wenn Mindestwaretzeit abgelaufen
+  if(psnClient.timeElapsed(SEND_DELAY) && newData){     //Sende neue Daten wenn Mindestwaretzeit abgelaufen
     psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor
     newData = false;                          //Flag: keine neuen Daten vorhanden
   }
diff --git a/examples/sPSN_Client2/sPSN_Client2.ino b/examples/sPSN_Client2/sPSN_Client2.ino
index a01de96..ad31487 100644
--- a/examples/sPSN_Client2/sPSN_Client2.ino
+++ b/examples/sPSN_Client2/sPSN_Client2.ino
@@ -72,13 +72,13 @@ void loop() {
 
   boolean buttonState = digitalRead(BUTTON_PIN); //lese Poti ein und speichere Wert
 
-  if(edgeDetected(buttonState) == RISING){
+  if(psnClient.edgeDetected(buttonState) == RISING){
     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;                                     //Flag: neue Daten zum Senden
   }
-  if(timeElapsed(SEND_DELAY) && newData){     //Sende neue Daten wenn Mindestwaretzeit abgelaufen
+  if(psnClient.timeElapsed(SEND_DELAY) && newData){     //Sende neue Daten wenn Mindestwaretzeit abgelaufen
     psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor
     newData = false;                          //Flag: keine neuen Daten vorhanden
   }
diff --git a/keywords.txt b/keywords.txt
index 60c736c..c4bf5e3 100644
--- a/keywords.txt
+++ b/keywords.txt
@@ -12,37 +12,40 @@ didacticPSNetClient	KEYWORD1
 #######################################
 # Methods and Functions (KEYWORD2)
 #######################################
-edgeDetected	KEYWORD2
-valueChanged	KEYWORD2
-timeElapsed	    KEYWORD2
-readSerialData  KEYWORD2
-
-setCallback	    KEYWORD2
-setStream	    KEYWORD2
+setCallback	KEYWORD2
+setStream	KEYWORD2
 handleNetwork	KEYWORD2
 isDataToSend	KEYWORD2
 
-publish	    KEYWORD2
+publish	KEYWORD2
 subscribe	KEYWORD2
 unsubscribe	KEYWORD2
 
+edgeDetected	KEYWORD2
+valueChanged	KEYWORD2
+timeElapsed	KEYWORD2
+readSerialData	KEYWORD2
+
 #######################################
 # Constants (LITERAL1)
 #######################################
-MSG_PRELIMITER		LITERAL1
-MSG_DELIMITER		LITERAL1
-MSG_SEPARATOR		LITERAL1
+MSG_PRELIMITER	LITERAL1
+MSG_DELIMITER	LITERAL1
+MSG_SEPARATOR	LITERAL1
 
 MSG_PUBLISH		LITERAL1
-MSG_SUBSCRIBE	LITERAL1
+MSG_SUBSCRIBE		LITERAL1
 MSG_UPDATE		LITERAL1
 MSG_TOPIC_MULTI	LITERAL1
 
 MAX_NR_TOPICS_CLIENT	LITERAL1
 MAX_NR_TOPICS_BROKER	LITERAL1
-MAX_LEN_TOPICS		    LITERAL1
-MAX_LEN_PAYLOAD		    LITERAL1
+MAX_LEN_TOPICS		LITERAL1
+MAX_LEN_PAYLOAD	LITERAL1
+
+DN_ASCII_CR	LITERAL1
+DN_ASCII_NL	LITERAL1
 
-DN_ERROR_NOERROR	    LITERAL1
-DN_ERROR_TOPIC_LEN   	LITERAL1
-DN_ERROR_PAYLOAD_LEN 	LITERAL1
+DN_ERROR_NOERROR	LITERAL1
+DN_ERROR_TOPIC_LEN	LITERAL1
+DN_ERROR_PAYLOAD_LEN	LITERAL1
\ No newline at end of file
diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp
index 0df22f5..d89becb 100644
--- a/src/didacticNet.cpp
+++ b/src/didacticNet.cpp
@@ -8,64 +8,6 @@
 #include "Arduino.h"
 #include "didacticNet.h"
 
-//**************************************************************************
-//LITTLE HELPERS ;-)
-//**************************************************************************
-int edgeDetected(bool edCurrentState){
-  static bool edLastState = false;
-  int edEdge = 0;
-
-  if(edCurrentState && !edLastState){
-    edEdge = RISING;
-  }
-  if(!edCurrentState && edLastState){
-    edEdge = FALLING;
-  }
-  edLastState = edCurrentState;
-  return edEdge;
-}
-
-bool valueChanged(int teValue, int teThreshold){
-  static int vcLastValue = 0;
-
-  if(abs(teValue-vcLastValue) > teThreshold){
-    vcLastValue = teValue;
-    return true;
-  }
-  return false;
-}
-
-bool timeElapsed(long teDelayTime){
-  static long teLastTime = 0L;
-  long currentTime = millis();
-
-  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
 //**************************************************************************
@@ -411,6 +353,63 @@ int didacticPSNetClient::getFreeTopicNr() {
   return -1;
 }
 
+//**************************************************************************
+//LITTLE HELPERS FOR CLIENTS ;-)
+//**************************************************************************
+int didacticPSNetClient::edgeDetected(bool edCurrentState){
+  static bool edLastState = false;
+  int edEdge = 0;
+
+  if(edCurrentState && !edLastState){
+    edEdge = RISING;
+  }
+  if(!edCurrentState && edLastState){
+    edEdge = FALLING;
+  }
+  edLastState = edCurrentState;
+  return edEdge;
+}
+
+bool didacticPSNetClient::valueChanged(int teValue, int teThreshold){
+  static int vcLastValue = 0;
+
+  if(abs(teValue-vcLastValue) > teThreshold){
+    vcLastValue = teValue;
+    return true;
+  }
+  return false;
+}
+
+bool didacticPSNetClient::timeElapsed(long teDelayTime){
+  static long teLastTime = 0L;
+  long currentTime = millis();
+
+  if(teLastTime + (teDelayTime-1) < currentTime){
+    teLastTime = currentTime;
+    return true;
+  }
+  return false;
+}
+
+int didacticPSNetClient::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;
+}
+
 
 //**************************************************************************
 //Broker
diff --git a/src/didacticNet.h b/src/didacticNet.h
index ae681e5..fe2ba42 100644
--- a/src/didacticNet.h
+++ b/src/didacticNet.h
@@ -41,13 +41,8 @@
 #define DN_ERROR_PAYLOAD_LEN -2
 
 //little helpers
-#define ASCII_CR 13
-#define ASCII_NL 10
-
-int  edgeDetected(bool);
-bool valueChanged(int, int);
-bool timeElapsed(long);
-int readSerialData(Stream&, char*, char);
+#define DN_ASCII_CR 13
+#define DN_ASCII_NL 10
 
 class didacticPSNet
 {
@@ -113,6 +108,12 @@ class didacticPSNetClient : public  didacticPSNet
 	int subscribe(char*, int);
 	bool unsubscribe(char*);
 	bool unsubscribe(char*, int);
+
+	//little helpers
+	int edgeDetected(bool);
+	bool valueChanged(int, int);
+	bool timeElapsed(long);
+	int readSerialData(Stream&, char*, char);
 };
 
 
-- 
GitLab