#include StreamCommunicator::StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize) : stream(s_out) { this->messageBuffer = new char[bufferSize]; this->bufferSize = bufferSize; } void StreamCommunicator::sendMessage(int *values, __SIZE_TYPE__ numberOfValues) { char message[calculateMessageOutSize(numberOfValues)]; parseIDs(values, numberOfValues, message); stream.println(message); } void StreamCommunicator::sendMessage(const char message[]) { stream.println(message); } char *StreamCommunicator::receiveMessage() { if (stream.available()) { clearBuffer(); stream.readBytesUntil('\n', getBuffer(), getBufferSize()); } 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; }