From b7b6af24dc13093ee1b5ad393a3cc634882eb9f4 Mon Sep 17 00:00:00 2001 From: Eric Mackrodt Date: Mon, 9 Apr 2018 22:52:50 +1000 Subject: [PATCH] Add header file. --- README.md | 4 ++++ examples/HardwareSerial.ino | 21 +++++++++++++++++++++ src/SerialMessenger.cpp | 24 ++++++++++++++++++++++++ src/SerialMessenger.h | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 examples/HardwareSerial.ino create mode 100644 src/SerialMessenger.cpp create mode 100644 src/SerialMessenger.h diff --git a/README.md b/README.md index fa64ab8..6ddf4f9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # SerialMessenger Allows easy communication between two arduinos (or arduino like boards) via Serial or SoftwareSerial. + +Limitations +----------- +- One function per channel (Why would you need more anyway?) diff --git a/examples/HardwareSerial.ino b/examples/HardwareSerial.ino new file mode 100644 index 0000000..f175277 --- /dev/null +++ b/examples/HardwareSerial.ino @@ -0,0 +1,21 @@ +#import + +String channels[] = { + "Channel1", + "Channel2" +}; + +SerialMessenger messenger(channels); + +void setup() { + messenger.subscribe("Channel1", &channel1); + messenger.subscribe("Channel2", &channel1); +} + +void loop() { + messenger.handle(); +} + +void channel1(String message) { + Serial.println(message); +} diff --git a/src/SerialMessenger.cpp b/src/SerialMessenger.cpp new file mode 100644 index 0000000..62f0604 --- /dev/null +++ b/src/SerialMessenger.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +#ifdef SoftwareSerial_h +#include +#endif + +SerialMessenger::SerialMessenger() {} + +#ifdef SoftwareSerial_h +SerialMessenger::SerialMessenger(SoftwareSerial * serial) { + _softSerial = serial; +} +#endif + +SerialMessenger::subscribe(String channel, void(*func)(String)) { + int index = _channels.indexOf(channel); + _channelFunctions[index] = func; +} + +SerialMessenger::publish(String channel, String message) { + +} diff --git a/src/SerialMessenger.h b/src/SerialMessenger.h new file mode 100644 index 0000000..0d1dc22 --- /dev/null +++ b/src/SerialMessenger.h @@ -0,0 +1,35 @@ +/* + SerialMessenger.h - Easy Serial Communication for Arduino. + Created by Eric Mackrodt 2018. + Released into the public domain. MIT License. +*/ +#ifndef SerialMessenger_h +#define SerialMessenger_h + +#include "Arduino.h" + +#ifdef SoftwareSerial_h +#include "SoftwareSerial.h" +#endif + +class SerialMessenger +{ + public: + #ifdef SoftwareSerial_h + SerialMessenger(String channels[], SoftwareSerial * serial); + #endif + SerialMessenger(String channels[]); + void subscribe(String channel, void (*func)(String)); + void send(String channel, String message); + void setPrefix(char prefix); + void setSuffix(char suffix); + void setDelimiter(char delimiter); + void handle(); + ~SerialMessenger(); + private: + String _channels[]; + (void (*func)(String)) _channelFunctions[]; + SoftwareSerial * _softSerial; +}; + +#endif