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