Implement communication
This commit is contained in:
parent
591a8c27cd
commit
0326e1e36e
6 changed files with 60 additions and 6 deletions
|
@ -3,9 +3,34 @@
|
|||
Communicator::Communicator(__SIZE_TYPE__ bufferSize)
|
||||
{
|
||||
this->messageBuffer = new char[bufferSize];
|
||||
this->bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
char *Communicator::getBuffer()
|
||||
{
|
||||
return messageBuffer;
|
||||
}
|
||||
|
||||
int Communicator::getBufferSize()
|
||||
{
|
||||
return this->bufferSize;
|
||||
}
|
||||
|
||||
int Communicator::calculateMessageOutSize(int numberOfValues)
|
||||
{
|
||||
return numberOfValues + (numberOfValues - 1) + 1;
|
||||
}
|
||||
|
||||
void Communicator::parseIDs(int *values, int 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++] = ',';
|
||||
}
|
||||
}
|
|
@ -2,11 +2,15 @@ class Communicator
|
|||
{
|
||||
private:
|
||||
char *messageBuffer;
|
||||
__SIZE_TYPE__ bufferSize;
|
||||
|
||||
public:
|
||||
Communicator(__SIZE_TYPE__ bufferSize);
|
||||
virtual void sendMessage(int *values);
|
||||
virtual void sendMessage(int *values, int numberOfValues);
|
||||
virtual void sendMessage(char *message);
|
||||
virtual char *receiveMessage();
|
||||
char *getBuffer();
|
||||
int getBufferSize();
|
||||
void parseIDs(int *values, int numberOfValues, char *out);
|
||||
int calculateMessageOutSize(int numberOfValues);
|
||||
};
|
|
@ -4,14 +4,26 @@
|
|||
I2CCommunicator::I2CCommunicator(int slaveAddr, __SIZE_TYPE__ bufferSize) : Communicator(bufferSize)
|
||||
{
|
||||
Wire.begin(slaveAddr);
|
||||
//Wire.onReceive(receiveEvent);
|
||||
}
|
||||
|
||||
void I2CCommunicator::sendMessage(int *values)
|
||||
void I2CCommunicator::sendMessage(int *values, int numberOfValues)
|
||||
{
|
||||
char message[calculateMessageOutSize(numberOfValues)];
|
||||
parseIDs(values, numberOfValues, message);
|
||||
Serial.println(message);
|
||||
}
|
||||
|
||||
void I2CCommunicator::sendMessage(char *message)
|
||||
{
|
||||
Wire.println(message);
|
||||
}
|
||||
|
||||
char *I2CCommunicator::receiveMessage()
|
||||
{
|
||||
if (Wire.available())
|
||||
{
|
||||
memset(getBuffer(), '\0', getBufferSize());
|
||||
Wire.readBytesUntil('\n', getBuffer(), getBufferSize());
|
||||
}
|
||||
return getBuffer();
|
||||
}
|
|
@ -5,7 +5,7 @@ class I2CCommunicator : public Communicator
|
|||
private:
|
||||
public:
|
||||
I2CCommunicator(int slaveAddr, __SIZE_TYPE__ bufferSize);
|
||||
void sendMessage(int *values);
|
||||
void sendMessage(int *values, int numberOfValues);
|
||||
void sendMessage(char *message);
|
||||
char *receiveMessage();
|
||||
};
|
|
@ -7,11 +7,24 @@ SerialCommunicator::SerialCommunicator(int baudRate, int timeout, __SIZE_TYPE__
|
|||
Serial.setTimeout(timeout);
|
||||
}
|
||||
|
||||
void SerialCommunicator::sendMessage(int *values)
|
||||
void SerialCommunicator::sendMessage(int *values, int numberOfValues)
|
||||
{
|
||||
char message[calculateMessageOutSize(numberOfValues)];
|
||||
parseIDs(values, numberOfValues, message);
|
||||
Serial.println(message);
|
||||
}
|
||||
|
||||
void SerialCommunicator::sendMessage(char *message)
|
||||
{
|
||||
Serial.println(message);
|
||||
}
|
||||
|
||||
char *SerialCommunicator::receiveMessage()
|
||||
{
|
||||
if (Serial.available())
|
||||
{
|
||||
memset(getBuffer(), '\0', getBufferSize());
|
||||
Serial.readBytesUntil('\n', getBuffer(), getBufferSize());
|
||||
}
|
||||
return getBuffer();
|
||||
}
|
|
@ -5,7 +5,7 @@ class SerialCommunicator : public Communicator
|
|||
private:
|
||||
public:
|
||||
SerialCommunicator(int baudRate, int timeout, __SIZE_TYPE__ bufferSize);
|
||||
void sendMessage(int *values);
|
||||
void sendMessage(int *values, int numberOfValues);
|
||||
void sendMessage(char *message);
|
||||
char *receiveMessage();
|
||||
};
|
||||
|
|
Reference in a new issue