Break apart StreamCommunicator into subclass
This commit is contained in:
parent
5d6aa75ea8
commit
49f120b656
4 changed files with 89 additions and 56 deletions
62
Firmware/lib/Communicator/Communicator.cpp
Normal file
62
Firmware/lib/Communicator/Communicator.cpp
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
#include <Communicator.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <WString.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
21
Firmware/lib/Communicator/Communicator.h
Normal file
21
Firmware/lib/Communicator/Communicator.h
Normal file
|
@ -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
|
|
@ -1,9 +1,7 @@
|
||||||
#include <StreamCommunicator.h>
|
#include <StreamCommunicator.h>
|
||||||
|
|
||||||
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)
|
void StreamCommunicator::sendMessage(int *values, __SIZE_TYPE__ numberOfValues)
|
||||||
|
@ -28,46 +26,5 @@ char *StreamCommunicator::receiveMessage()
|
||||||
return getBuffer();
|
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;
|
|
||||||
}
|
|
|
@ -1,25 +1,18 @@
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
|
#include "Communicator.h"
|
||||||
|
|
||||||
#ifndef _STREAM_COMMUNICATOR_INCLUDED_
|
#ifndef _STREAM_COMMUNICATOR_INCLUDED_
|
||||||
#define _STREAM_COMMUNICATOR_INCLUDED_
|
#define _STREAM_COMMUNICATOR_INCLUDED_
|
||||||
|
|
||||||
class StreamCommunicator
|
class StreamCommunicator: public Communicator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
Stream &stream;
|
Stream &stream;
|
||||||
char *messageBuffer;
|
|
||||||
__SIZE_TYPE__ bufferSize;
|
|
||||||
__SIZE_TYPE__ calculateMessageOutSize(__SIZE_TYPE__ numberOfValues);
|
|
||||||
void parseIDs(const int values[], __SIZE_TYPE__ numberOfValues, char *out);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize);
|
StreamCommunicator(Stream &s_out, __SIZE_TYPE__ bufferSize);
|
||||||
void sendMessage(int *values, __SIZE_TYPE__ numberOfValues);
|
void sendMessage(int *values, __SIZE_TYPE__ numberOfValues) override;
|
||||||
void sendMessage(const char message[]);
|
void sendMessage(const char message[]) override;
|
||||||
char *receiveMessage();
|
char *receiveMessage() override;
|
||||||
char *getBuffer();
|
|
||||||
void clearBuffer();
|
|
||||||
int getBufferSize();
|
|
||||||
Stream *getStream();
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue