Skip to content
Snippets Groups Projects
Commit dd013d6b authored by Anian Bühler's avatar Anian Bühler
Browse files

minor changes

parent 605831db
No related branches found
No related tags found
2 merge requests!3Dev to master,!2Dev to Master
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
*date: 14.12.2020 *date: 14.12.2020
*version: 1.0 *version: 1.0
*/ */
#include <Arduino.h>
#include "SoftwareSerial.h" #include "SoftwareSerial.h"
#include "didacticNet.h" #include "didacticNet.h"
......
...@@ -72,7 +72,7 @@ void loop() { ...@@ -72,7 +72,7 @@ void loop() {
psnClient.handleNetwork(); //Verarbeiten der Daten, prüfen ob Netzwerk frei und versenden der Daten 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 (nrOfAscii > 0) { //Wenn Daten fertig eingelesen wurden
......
...@@ -76,12 +76,12 @@ void loop() { ...@@ -76,12 +76,12 @@ void loop() {
int currentValue = analogRead(POTI_PIN); //lese Poti ein und speichere Wert 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 itoa(currentValue, payload, 10); //wandle Wert in ASCII-Zeichen
//Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen
newData = true; //Flag: neue Daten zum Senden 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 psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor
newData = false; //Flag: keine neuen Daten vorhanden newData = false; //Flag: keine neuen Daten vorhanden
} }
......
...@@ -72,13 +72,13 @@ void loop() { ...@@ -72,13 +72,13 @@ void loop() {
boolean buttonState = digitalRead(BUTTON_PIN); //lese Poti ein und speichere Wert 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" ledState = !ledState; //Invertiere zu sendenden Wert "LED-Zustand"
itoa(ledState, payload, 10); //wandle Wert in ASCII-Zeichen itoa(ledState, payload, 10); //wandle Wert in ASCII-Zeichen
//Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen
newData = true; //Flag: neue Daten zum Senden 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 psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor
newData = false; //Flag: keine neuen Daten vorhanden newData = false; //Flag: keine neuen Daten vorhanden
} }
......
...@@ -12,11 +12,6 @@ didacticPSNetClient KEYWORD1 ...@@ -12,11 +12,6 @@ didacticPSNetClient KEYWORD1
####################################### #######################################
# Methods and Functions (KEYWORD2) # Methods and Functions (KEYWORD2)
####################################### #######################################
edgeDetected KEYWORD2
valueChanged KEYWORD2
timeElapsed KEYWORD2
readSerialData KEYWORD2
setCallback KEYWORD2 setCallback KEYWORD2
setStream KEYWORD2 setStream KEYWORD2
handleNetwork KEYWORD2 handleNetwork KEYWORD2
...@@ -26,6 +21,11 @@ publish KEYWORD2 ...@@ -26,6 +21,11 @@ publish KEYWORD2
subscribe KEYWORD2 subscribe KEYWORD2
unsubscribe KEYWORD2 unsubscribe KEYWORD2
edgeDetected KEYWORD2
valueChanged KEYWORD2
timeElapsed KEYWORD2
readSerialData KEYWORD2
####################################### #######################################
# Constants (LITERAL1) # Constants (LITERAL1)
####################################### #######################################
...@@ -43,6 +43,9 @@ MAX_NR_TOPICS_BROKER LITERAL1 ...@@ -43,6 +43,9 @@ MAX_NR_TOPICS_BROKER LITERAL1
MAX_LEN_TOPICS LITERAL1 MAX_LEN_TOPICS LITERAL1
MAX_LEN_PAYLOAD LITERAL1 MAX_LEN_PAYLOAD LITERAL1
DN_ASCII_CR LITERAL1
DN_ASCII_NL LITERAL1
DN_ERROR_NOERROR LITERAL1 DN_ERROR_NOERROR LITERAL1
DN_ERROR_TOPIC_LEN LITERAL1 DN_ERROR_TOPIC_LEN LITERAL1
DN_ERROR_PAYLOAD_LEN LITERAL1 DN_ERROR_PAYLOAD_LEN LITERAL1
\ No newline at end of file
...@@ -8,64 +8,6 @@ ...@@ -8,64 +8,6 @@
#include "Arduino.h" #include "Arduino.h"
#include "didacticNet.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 //ROOT
//************************************************************************** //**************************************************************************
...@@ -411,6 +353,63 @@ int didacticPSNetClient::getFreeTopicNr() { ...@@ -411,6 +353,63 @@ int didacticPSNetClient::getFreeTopicNr() {
return -1; 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 //Broker
......
...@@ -41,13 +41,8 @@ ...@@ -41,13 +41,8 @@
#define DN_ERROR_PAYLOAD_LEN -2 #define DN_ERROR_PAYLOAD_LEN -2
//little helpers //little helpers
#define ASCII_CR 13 #define DN_ASCII_CR 13
#define ASCII_NL 10 #define DN_ASCII_NL 10
int edgeDetected(bool);
bool valueChanged(int, int);
bool timeElapsed(long);
int readSerialData(Stream&, char*, char);
class didacticPSNet class didacticPSNet
{ {
...@@ -113,6 +108,12 @@ class didacticPSNetClient : public didacticPSNet ...@@ -113,6 +108,12 @@ class didacticPSNetClient : public didacticPSNet
int subscribe(char*, int); int subscribe(char*, int);
bool unsubscribe(char*); bool unsubscribe(char*);
bool unsubscribe(char*, int); bool unsubscribe(char*, int);
//little helpers
int edgeDetected(bool);
bool valueChanged(int, int);
bool timeElapsed(long);
int readSerialData(Stream&, char*, char);
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment