From c5ef3d9366c2616d92e599921040db94b06dd0b6 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sat, 5 Feb 2022 19:08:51 +0100 Subject: [PATCH] Remove unnecessary base class --- .../lib/Communicator/Communicator.cpp | 48 ------------------- .../Firmware/lib/Communicator/Communicator.h | 16 ------- .../StreamCommunicator/StreamCommunicator.cpp | 40 +++++++++++++++- .../StreamCommunicator/StreamCommunicator.h | 12 +++-- 4 files changed, 47 insertions(+), 69 deletions(-) delete mode 100644 Devices/Control/Firmware/lib/Communicator/Communicator.cpp delete mode 100644 Devices/Control/Firmware/lib/Communicator/Communicator.h diff --git a/Devices/Control/Firmware/lib/Communicator/Communicator.cpp b/Devices/Control/Firmware/lib/Communicator/Communicator.cpp deleted file mode 100644 index 69ab6b6..0000000 --- a/Devices/Control/Firmware/lib/Communicator/Communicator.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include - -Communicator::Communicator(__SIZE_TYPE__ bufferSize) -{ - this->messageBuffer = new char[bufferSize]; - this->bufferSize = bufferSize; -} - -char *Communicator::getBuffer() -{ - return messageBuffer; -} - -int Communicator::getBufferSize() -{ - return this->bufferSize; -} - -__SIZE_TYPE__ Communicator::calculateMessageOutSize(__SIZE_TYPE__ numberOfValues) -{ - return numberOfValues + (numberOfValues - 1) + 1; -} - -void Communicator::parseIDs(int *values, __SIZE_TYPE__ numberOfValues, char *output) -{ - __SIZE_TYPE__ outputSize = calculateMessageOutSize(numberOfValues); - __SIZE_TYPE__ outputCharPointer = 0; - output[outputSize - 1] = '\0'; - - for (__SIZE_TYPE__ i = 0; i < numberOfValues; i++) - { - output[outputCharPointer++] = values[i] + '0'; - if (outputCharPointer < outputSize - 1) - output[outputCharPointer++] = ','; - } -} - -void Communicator::sendMessage(int *values, __SIZE_TYPE__ numberOfValues) -{ -} -void Communicator::sendMessage(char *message) -{ -} - -char *Communicator::receiveMessage() -{ - return nullptr; -} \ No newline at end of file diff --git a/Devices/Control/Firmware/lib/Communicator/Communicator.h b/Devices/Control/Firmware/lib/Communicator/Communicator.h deleted file mode 100644 index bc4ab19..0000000 --- a/Devices/Control/Firmware/lib/Communicator/Communicator.h +++ /dev/null @@ -1,16 +0,0 @@ -class Communicator -{ -private: - char *messageBuffer; - __SIZE_TYPE__ bufferSize; - -public: - Communicator(__SIZE_TYPE__ bufferSize); - virtual void sendMessage(int *values, __SIZE_TYPE__ numberOfValues); - virtual void sendMessage(char *message); - virtual char *receiveMessage(); - char *getBuffer(); - int getBufferSize(); - void parseIDs(int *values, __SIZE_TYPE__ numberOfValues, char *out); - __SIZE_TYPE__ calculateMessageOutSize(__SIZE_TYPE__ numberOfValues); -}; \ No newline at end of file diff --git a/Devices/Control/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp b/Devices/Control/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp index dbd2f03..7823810 100644 --- a/Devices/Control/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp +++ b/Devices/Control/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp @@ -1,10 +1,12 @@ #include -StreamCommunicator::StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize) : Communicator(bufferSize), stream(s_out) +StreamCommunicator::StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize) : stream(s_out) { + this->messageBuffer = new char[bufferSize]; + this->bufferSize = bufferSize; } -void StreamCommunicator::sendMessage(int *values, int numberOfValues) +void StreamCommunicator::sendMessage(int *values, __SIZE_TYPE__ numberOfValues) { char message[calculateMessageOutSize(numberOfValues)]; parseIDs(values, numberOfValues, message); @@ -24,4 +26,38 @@ char *StreamCommunicator::receiveMessage() stream.readBytesUntil('\n', getBuffer(), getBufferSize()); } return getBuffer(); +} + +__SIZE_TYPE__ StreamCommunicator::calculateMessageOutSize(__SIZE_TYPE__ numberOfValues) +{ + return numberOfValues + (numberOfValues - 1) + 1; +} + +void StreamCommunicator::parseIDs(int *values, __SIZE_TYPE__ numberOfValues, char *output) +{ + __SIZE_TYPE__ outputSize = calculateMessageOutSize(numberOfValues); + __SIZE_TYPE__ outputCharPointer = 0; + output[outputSize - 1] = '\0'; + + for (__SIZE_TYPE__ i = 0; i < numberOfValues; i++) + { + output[outputCharPointer++] = values[i] + '0'; + if (outputCharPointer < outputSize - 1) + output[outputCharPointer++] = ','; + } +} + +char *StreamCommunicator::getBuffer() +{ + return messageBuffer; +} + +int StreamCommunicator::getBufferSize() +{ + return this->bufferSize; +} + +Stream *StreamCommunicator::getStream() +{ + return &stream; } \ No newline at end of file diff --git a/Devices/Control/Firmware/lib/StreamCommunicator/StreamCommunicator.h b/Devices/Control/Firmware/lib/StreamCommunicator/StreamCommunicator.h index 2f7a8da..b189a3c 100644 --- a/Devices/Control/Firmware/lib/StreamCommunicator/StreamCommunicator.h +++ b/Devices/Control/Firmware/lib/StreamCommunicator/StreamCommunicator.h @@ -1,14 +1,20 @@ -#include #include "Stream.h" -class StreamCommunicator : public Communicator +class StreamCommunicator { protected: Stream &stream; + char *messageBuffer; + __SIZE_TYPE__ bufferSize; + __SIZE_TYPE__ calculateMessageOutSize(__SIZE_TYPE__ numberOfValues); + void parseIDs(int *values, __SIZE_TYPE__ numberOfValues, char *out); public: StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize); - void sendMessage(int *values, int numberOfValues); + void sendMessage(int *values, __SIZE_TYPE__ numberOfValues); void sendMessage(char *message); char *receiveMessage(); + char *getBuffer(); + int getBufferSize(); + Stream *getStream(); }; \ No newline at end of file