From 0b8b18f294b4bfe997f4affda1bd251417dbec30 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sun, 6 Feb 2022 16:06:24 +0100 Subject: [PATCH] Implement first communicator classes --- .../lib/I2CCommunicator/I2CCommunicator.cpp | 17 +++++++++++++++++ .../lib/I2CCommunicator/I2CCommunicator.h | 12 ++++++++++++ .../SerialCommunicator/SerialCommunicator.cpp | 8 ++++++++ .../lib/SerialCommunicator/SerialCommunicator.h | 8 ++++++++ .../StreamCommunicator/StreamCommunicator.cpp | 15 +++++++++++++++ .../lib/StreamCommunicator/StreamCommunicator.h | 16 ++++++++++++++++ 6 files changed, 76 insertions(+) create mode 100644 Devices/Console/Firmware/lib/I2CCommunicator/I2CCommunicator.cpp create mode 100644 Devices/Console/Firmware/lib/I2CCommunicator/I2CCommunicator.h create mode 100644 Devices/Console/Firmware/lib/SerialCommunicator/SerialCommunicator.cpp create mode 100644 Devices/Console/Firmware/lib/SerialCommunicator/SerialCommunicator.h create mode 100644 Devices/Console/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp create mode 100644 Devices/Console/Firmware/lib/StreamCommunicator/StreamCommunicator.h diff --git a/Devices/Console/Firmware/lib/I2CCommunicator/I2CCommunicator.cpp b/Devices/Console/Firmware/lib/I2CCommunicator/I2CCommunicator.cpp new file mode 100644 index 0000000..e7ab5b4 --- /dev/null +++ b/Devices/Console/Firmware/lib/I2CCommunicator/I2CCommunicator.cpp @@ -0,0 +1,17 @@ +#include +#include + +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(&stream); + foo->beginTransmission(slaveAddr); + StreamCommunicator::sendMessage(message); + foo->endTransmission(slaveAddr); +} \ No newline at end of file diff --git a/Devices/Console/Firmware/lib/I2CCommunicator/I2CCommunicator.h b/Devices/Console/Firmware/lib/I2CCommunicator/I2CCommunicator.h new file mode 100644 index 0000000..f429eac --- /dev/null +++ b/Devices/Console/Firmware/lib/I2CCommunicator/I2CCommunicator.h @@ -0,0 +1,12 @@ +#include +#include + +class I2CCommunicator : public StreamCommunicator +{ +private: + int slaveAddr; + +public: + I2CCommunicator(TwoWire &w_out, int slaveAddr, int timeout); + void sendMessage(const char message[]) override; +}; diff --git a/Devices/Console/Firmware/lib/SerialCommunicator/SerialCommunicator.cpp b/Devices/Console/Firmware/lib/SerialCommunicator/SerialCommunicator.cpp new file mode 100644 index 0000000..f22ea4a --- /dev/null +++ b/Devices/Console/Firmware/lib/SerialCommunicator/SerialCommunicator.cpp @@ -0,0 +1,8 @@ +#include +#include + +SerialCommunicator::SerialCommunicator(HardwareSerial &p_out, int baudRate, int timeout) : StreamCommunicator(p_out) +{ + p_out.begin(baudRate); + p_out.setTimeout(timeout); +} \ No newline at end of file diff --git a/Devices/Console/Firmware/lib/SerialCommunicator/SerialCommunicator.h b/Devices/Console/Firmware/lib/SerialCommunicator/SerialCommunicator.h new file mode 100644 index 0000000..ac9a38f --- /dev/null +++ b/Devices/Console/Firmware/lib/SerialCommunicator/SerialCommunicator.h @@ -0,0 +1,8 @@ +#include +#include + +class SerialCommunicator : public StreamCommunicator +{ +public: + SerialCommunicator(HardwareSerial &p_out, int baudRate, int timeout); +}; diff --git a/Devices/Console/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp b/Devices/Console/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp new file mode 100644 index 0000000..9240a72 --- /dev/null +++ b/Devices/Console/Firmware/lib/StreamCommunicator/StreamCommunicator.cpp @@ -0,0 +1,15 @@ +#include + +StreamCommunicator::StreamCommunicator(Stream &s_out) : stream(s_out) +{ +} + +void StreamCommunicator::sendMessage(const char message[]) +{ + stream.println(message); +} + +Stream *StreamCommunicator::getStream() +{ + return &stream; +} \ No newline at end of file diff --git a/Devices/Console/Firmware/lib/StreamCommunicator/StreamCommunicator.h b/Devices/Console/Firmware/lib/StreamCommunicator/StreamCommunicator.h new file mode 100644 index 0000000..e614659 --- /dev/null +++ b/Devices/Console/Firmware/lib/StreamCommunicator/StreamCommunicator.h @@ -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 \ No newline at end of file