From fc1ca449d94220803823a6872bba2b24466925bf Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sun, 10 Apr 2022 11:50:44 +0200 Subject: [PATCH] Break apart StreamCommunicator into subclass --- .../lib/Communicator/Communicator.cpp | 62 +++++++++++++++++++ .../Firmware/lib/Communicator/Communicator.h | 21 +++++++ .../StreamCommunicator/StreamCommunicator.cpp | 45 +------------- .../StreamCommunicator/StreamCommunicator.h | 17 ++--- 4 files changed, 89 insertions(+), 56 deletions(-) create mode 100644 modules/control/Firmware/lib/Communicator/Communicator.cpp create mode 100644 modules/control/Firmware/lib/Communicator/Communicator.h diff --git a/modules/control/Firmware/lib/Communicator/Communicator.cpp b/modules/control/Firmware/lib/Communicator/Communicator.cpp new file mode 100644 index 0000000..3cd6a57 --- /dev/null +++ b/modules/control/Firmware/lib/Communicator/Communicator.cpp @@ -0,0 +1,62 @@ +#include +#include +#include + +Communicator::Communicator(__SIZE_TYPE__ bufferSize){ + this->messageBuffer = new char[bufferSize]; + this->bufferSize = bufferSize; +} + +void Communicator::sendMessage(int *values, __SIZE_TYPE__ numberOfValues) +{ + +} + +void Communicator::sendMessage(const char message[]) +{ + +} + +char *Communicator::receiveMessage() +{ + +} + +__SIZE_TYPE__ Communicator::calculateMessageOutSize(__SIZE_TYPE__ numberOfValues) +{ + return numberOfValues + (numberOfValues - 1) + 1; +} + +void Communicator::parseIDs(const int values[], __SIZE_TYPE__ numberOfValues, char *output) +{ + String out = ""; + __SIZE_TYPE__ outputSize = calculateMessageOutSize(numberOfValues); + __SIZE_TYPE__ outputCharPointer = 0; + + for (__SIZE_TYPE__ i = 0; i < numberOfValues; i++) + { + out += values[i]; + outputCharPointer++; + if (outputCharPointer < outputSize - 1) + { + out += ','; + outputCharPointer++; + } + } + strcpy(output, out.c_str()); +} + +char *Communicator::getBuffer() +{ + return messageBuffer; +} + +void Communicator::clearBuffer() +{ + memset(getBuffer(), '\0', getBufferSize()); +} + +int Communicator::getBufferSize() +{ + return this->bufferSize; +} \ No newline at end of file diff --git a/modules/control/Firmware/lib/Communicator/Communicator.h b/modules/control/Firmware/lib/Communicator/Communicator.h new file mode 100644 index 0000000..c0680e6 --- /dev/null +++ b/modules/control/Firmware/lib/Communicator/Communicator.h @@ -0,0 +1,21 @@ +#ifndef _COMMUNICATOR_INCLUDED_ +#define _COMMUNICATOR_INCLUDED_ + +class Communicator +{ +protected: + char *messageBuffer; + __SIZE_TYPE__ bufferSize; + __SIZE_TYPE__ calculateMessageOutSize(__SIZE_TYPE__ numberOfValues); + void parseIDs(const int values[], __SIZE_TYPE__ numberOfValues, char *out); + +public: + Communicator(__SIZE_TYPE__ bufferSize); + virtual void sendMessage(int *values, __SIZE_TYPE__ numberOfValues); + virtual void sendMessage(const char message[]); + virtual char *receiveMessage(); + char *getBuffer(); + void clearBuffer(); + int getBufferSize(); +}; +#endif \ No newline at end of file diff --git a/modules/control/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp b/modules/control/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp index 57c021a..26e387e 100644 --- a/modules/control/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp +++ b/modules/control/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp @@ -1,9 +1,7 @@ #include -StreamCommunicator::StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize) : stream(s_out) +StreamCommunicator::StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize) :Communicator(bufferSize), stream(s_out) { - this->messageBuffer = new char[bufferSize]; - this->bufferSize = bufferSize; } void StreamCommunicator::sendMessage(int *values, __SIZE_TYPE__ numberOfValues) @@ -28,46 +26,5 @@ char *StreamCommunicator::receiveMessage() return getBuffer(); } -__SIZE_TYPE__ StreamCommunicator::calculateMessageOutSize(__SIZE_TYPE__ numberOfValues) -{ - return numberOfValues + (numberOfValues - 1) + 1; -} -void StreamCommunicator::parseIDs(const int values[], __SIZE_TYPE__ numberOfValues, char *output) -{ - String out = ""; - __SIZE_TYPE__ outputSize = calculateMessageOutSize(numberOfValues); - __SIZE_TYPE__ outputCharPointer = 0; - for (__SIZE_TYPE__ i = 0; i < numberOfValues; i++) - { - out += values[i]; - outputCharPointer++; - if (outputCharPointer < outputSize - 1) - { - out += ','; - outputCharPointer++; - } - } - strcpy(output, out.c_str()); -} - -char *StreamCommunicator::getBuffer() -{ - return messageBuffer; -} - -void StreamCommunicator::clearBuffer() -{ - memset(getBuffer(), '\0', getBufferSize()); -} - -int StreamCommunicator::getBufferSize() -{ - return this->bufferSize; -} - -Stream *StreamCommunicator::getStream() -{ - return &stream; -} \ No newline at end of file diff --git a/modules/control/Firmware/lib/StreamCommunicator/StreamCommunicator.h b/modules/control/Firmware/lib/StreamCommunicator/StreamCommunicator.h index 91eb326..6ed298f 100644 --- a/modules/control/Firmware/lib/StreamCommunicator/StreamCommunicator.h +++ b/modules/control/Firmware/lib/StreamCommunicator/StreamCommunicator.h @@ -1,25 +1,18 @@ #include "Stream.h" +#include "Communicator.h" #ifndef _STREAM_COMMUNICATOR_INCLUDED_ #define _STREAM_COMMUNICATOR_INCLUDED_ -class StreamCommunicator +class StreamCommunicator: public Communicator { protected: Stream &stream; - char *messageBuffer; - __SIZE_TYPE__ bufferSize; - __SIZE_TYPE__ calculateMessageOutSize(__SIZE_TYPE__ numberOfValues); - void parseIDs(const int values[], __SIZE_TYPE__ numberOfValues, char *out); public: StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize); - void sendMessage(int *values, __SIZE_TYPE__ numberOfValues); - void sendMessage(const char message[]); - char *receiveMessage(); - char *getBuffer(); - void clearBuffer(); - int getBufferSize(); - Stream *getStream(); + void sendMessage(int *values, __SIZE_TYPE__ numberOfValues) override; + void sendMessage(const char message[]) override; + char *receiveMessage() override; }; #endif \ No newline at end of file