diff --git a/examples/sPSN_Broker/sPSN_Broker.ino b/examples/sPSN_Broker/sPSN_Broker.ino index 662ecf03641f9d69a3289d9f9be861024f9514bd..65fed092a36228d51ce23ee0791de8ebc4b162de 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 151cfda4b2b349d60659d7044d3f7d90d537071d..632a18c3a8db96eceac9c79e01607dbc06df9612 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 667ace59869f62e24656d8ada409aab3597389ee..8f72231f6cd890e29b796a2a5a6199f91b2771bd 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 a01de966d48d77722c27888a63d741f0286072bf..ad31487990138e20535fad7c15f1ac5950b210e7 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 60c736ca642600c2f9c29ce5d0c3cb6d16a8d402..c4bf5e3a3ed78cd7e24c5273db8ccaba709f944f 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 0df22f51873c77aab9637725fe5fcd04d6a99cda..d89becbb79b0088ba94a887948c015f7616036ac 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 ae681e5d848dffb445a4d8ed21a925097ee74b39..fe2ba4225b99144dae4d0d8930a98c9e0aa80a2c 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); };