Implement communication

This commit is contained in:
GHOSCHT 2022-02-04 09:42:15 +01:00
parent 591a8c27cd
commit 0326e1e36e
No known key found for this signature in database
GPG key ID: A35BD466B8871994
6 changed files with 60 additions and 6 deletions

View file

@ -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++] = ',';
}
}

View file

@ -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);
};

View file

@ -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();
}

View file

@ -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();
};

View file

@ -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();
}

View file

@ -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();
};