From 1ee009e7defa5c7819086cce3a7e79d3c1255e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anian=20B=C3=BChler?= <anian.buehler@reutlingen-university.de> Date: Wed, 20 Oct 2021 15:24:37 +0200 Subject: [PATCH] Moved readSerial and timeElapsed to extra classes --- src/didacticNet.cpp | 72 ++++++++++++++++++++++++--------------------- src/didacticNet.h | 24 ++++++++++++--- 2 files changed, 59 insertions(+), 37 deletions(-) diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp index 5cb5070..4003039 100644 --- a/src/didacticNet.cpp +++ b/src/didacticNet.cpp @@ -343,39 +343,6 @@ bool didacticPSNetClient::valueChanged(int vcValue, int vcThreshold){ return false; } -//************************************************************************** -//LITTLE HELPERS FOR CLIENTS ;-) -//************************************************************************* -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; - rsCounter = 0; - return nrOfChars; - } else { - rsCounter++; - } - } - return 0; -} - //************************************************************************** //Broker @@ -474,3 +441,42 @@ int didacticPSNetBroker::getFreeTopicNr() { } return -1; } + + +//************************************************************************** +//LITTLE HELPERS FOR CLIENTS ;-) +//************************************************************************* + +unblockingTimer::unblockingTimer(){} +unblockingTimer::~unblockingTimer(){} + +bool unblockingTimer::timeElapsed(long delayTime){ + long currentTime = millis(); + + if(lastTime + (delayTime-1) < currentTime){ + lastTime = currentTime; + return true; + } + return false; +} + +serialReader::serialReader(){} +serialReader::~serialReader(){} + +int serialReader::readSerialData(Stream& rsStream, char* rsDataArray, char rsEndSign) { + + if (rsStream.available()) { + char charBuffer = rsStream.read(); + rsDataArray[charCounter] = charBuffer; + + if (charBuffer == rsEndSign) { + rsDataArray[charCounter] = '\0'; + int nrOfChars = charCounter; + charCounter = 0; + return nrOfChars; + } else { + charCounter++; + } + } + return 0; +} \ No newline at end of file diff --git a/src/didacticNet.h b/src/didacticNet.h index da688cb..63f1d6d 100644 --- a/src/didacticNet.h +++ b/src/didacticNet.h @@ -123,10 +123,6 @@ class didacticPSNetClient : public didacticPSNet int subscribe(char*, int); bool unsubscribe(char*); bool unsubscribe(char*, int); - - //little helpers - bool timeElapsed(long); - int readSerialData(Stream&, char*, char); }; @@ -150,5 +146,25 @@ class didacticPSNetBroker: public didacticPSNet bool update(char*, int, char*, int); +}; + + +class unblockingTimer +{ + private: + long lastTime = 0L; + + public: + bool timeElapsed(long); + +}; +class serialReader +{ + private: + int charCounter = 0; + + public: + int readSerialData(Stream&, char*, char); + }; #endif -- GitLab