-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2af8035
commit b7b6af2
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#import <SerialMessenger.h> | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <Arduino.h> | ||
#include <SerialMessenger.h> | ||
#include <vector> | ||
|
||
#ifdef SoftwareSerial_h | ||
#include <SoftwareSerial.h> | ||
#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) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |