Implement first communicator classes

This commit is contained in:
GHOSCHT 2022-02-06 16:06:24 +01:00
parent 4dbc3ab836
commit 0b8b18f294
No known key found for this signature in database
GPG key ID: A35BD466B8871994
6 changed files with 76 additions and 0 deletions

View file

@ -0,0 +1,17 @@
#include <I2CCommunicator.h>
#include <Wire.h>
I2CCommunicator::I2CCommunicator(TwoWire &w_out, int slaveAddr, int timeout) : StreamCommunicator(w_out)
{
w_out.begin();
w_out.setTimeout(timeout);
this->slaveAddr = slaveAddr;
}
void I2CCommunicator::sendMessage(const char message[])
{
TwoWire *foo = static_cast<TwoWire *>(&stream);
foo->beginTransmission(slaveAddr);
StreamCommunicator::sendMessage(message);
foo->endTransmission(slaveAddr);
}

View file

@ -0,0 +1,12 @@
#include <StreamCommunicator.h>
#include <Wire.h>
class I2CCommunicator : public StreamCommunicator
{
private:
int slaveAddr;
public:
I2CCommunicator(TwoWire &w_out, int slaveAddr, int timeout);
void sendMessage(const char message[]) override;
};

View file

@ -0,0 +1,8 @@
#include <SerialCommunicator.h>
#include <HardwareSerial.h>
SerialCommunicator::SerialCommunicator(HardwareSerial &p_out, int baudRate, int timeout) : StreamCommunicator(p_out)
{
p_out.begin(baudRate);
p_out.setTimeout(timeout);
}

View file

@ -0,0 +1,8 @@
#include <StreamCommunicator.h>
#include <HardwareSerial.h>
class SerialCommunicator : public StreamCommunicator
{
public:
SerialCommunicator(HardwareSerial &p_out, int baudRate, int timeout);
};

View file

@ -0,0 +1,15 @@
#include <StreamCommunicator.h>
StreamCommunicator::StreamCommunicator(Stream &s_out) : stream(s_out)
{
}
void StreamCommunicator::sendMessage(const char message[])
{
stream.println(message);
}
Stream *StreamCommunicator::getStream()
{
return &stream;
}

View file

@ -0,0 +1,16 @@
#include "Stream.h"
#ifndef _STREAM_COMMUNICATOR_INCLUDED_
#define _STREAM_COMMUNICATOR_INCLUDED_
class StreamCommunicator
{
protected:
Stream &stream;
public:
StreamCommunicator(Stream &s_out);
virtual void sendMessage(const char message[]);
Stream *getStream();
};
#endif