Skip to content

Commit

Permalink
Add header file.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmackrodt committed Apr 9, 2018
1 parent 2af8035 commit b7b6af2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
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?)
21 changes: 21 additions & 0 deletions examples/HardwareSerial.ino
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);
}
24 changes: 24 additions & 0 deletions src/SerialMessenger.cpp
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) {

}
35 changes: 35 additions & 0 deletions src/SerialMessenger.h
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

0 comments on commit b7b6af2

Please sign in to comment.