Implement first communicator classes
This commit is contained in:
parent
4dbc3ab836
commit
0b8b18f294
6 changed files with 76 additions and 0 deletions
|
@ -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);
|
||||
}
|
|
@ -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;
|
||||
};
|
|
@ -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);
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
#include <StreamCommunicator.h>
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
class SerialCommunicator : public StreamCommunicator
|
||||
{
|
||||
public:
|
||||
SerialCommunicator(HardwareSerial &p_out, int baudRate, int timeout);
|
||||
};
|
|
@ -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;
|
||||
}
|
|
@ -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
|
Reference in a new issue