Skip to content

Commit

Permalink
added example and updated method signatures to be more student-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
Quin Kennedy authored and Quin Kennedy committed Nov 13, 2012
1 parent fd5030c commit 603c461
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 41 deletions.
66 changes: 33 additions & 33 deletions Spacebrew.h
Expand Up @@ -5,7 +5,7 @@
#include <WebSocketClient.h>
#include "Arduino.h"

enum SBType { BOOLEAN, STRING, RANGE };
enum SBType { SB_BOOLEAN, SB_STRING, SB_RANGE };
struct PublisherNode {
char *name;
char *type;
Expand All @@ -23,64 +23,64 @@ class Spacebrew{
public:
Spacebrew();
void monitor();
typedef void (*OnBooleanMessage)(char* name, bool value);
typedef void (*OnRangeMessage)(char* name, int value);
typedef void (*OnStringMessage)(char* name, char* value);
typedef void (*OnBooleanMessage)(char name[], bool value);
typedef void (*OnRangeMessage)(char name[], int value);
typedef void (*OnStringMessage)(char name[], char value[]);
//typedef void (*OnOtherMessage)(char* name, char* value);//duplicate of OnStringMessage
typedef void (*OnSBOpen)();
typedef void (*OnSBClose)(int code, char* message);
typedef void (*OnSBError)(char* message);
typedef void (*OnSBClose)(int code, char message[]);
typedef void (*OnSBError)(char message[]);
void onOpen(OnSBOpen function);
void onClose(OnSBClose function);
void onRangeMessage(OnRangeMessage function);
void onStringMessage(OnStringMessage function);
void onBooleanMessage(OnBooleanMessage function);
void onOtherMessage(OnStringMessage function);
void onError(OnSBError function);
void addPublish(char* name, char* type, char* defaultValue);
void addPublish(char* name, bool defaultValue){
void addPublish(char name[], char type[], char defaultValue[]);
void addPublish(char name[], bool defaultValue){
addPublish(name, (char*)"boolean", (char*)(defaultValue ? "true" : "false"));
}
void addPublish(char* name, char* defaultValue){
void addPublish(char name[], char defaultValue[]){
addPublish(name, "string", defaultValue);
}
void addPublish(char* name, int defaultValue);
void addPublish(char* name, enum SBType type, char* defaultValue){
void addPublish(char name[], int defaultValue);
void addPublish(char name[], enum SBType type, char defaultValue[]){
switch(type){
case STRING:
case SB_STRING:
addPublish(name, "string", defaultValue);
break;
case RANGE:
case SB_RANGE:
addPublish(name, "range", defaultValue);
break;
case BOOLEAN:
case SB_BOOLEAN:
addPublish(name, "boolean", defaultValue);
break;
}
}
void addPublish(char* name, enum SBType type){
void addPublish(char name[], enum SBType type){
switch(type){
case STRING:
case SB_STRING:
addPublish(name, "string", "");
break;
case RANGE:
case SB_RANGE:
addPublish(name, "range", "0");
break;
case BOOLEAN:
case SB_BOOLEAN:
addPublish(name, "boolean", "false");
break;
}
}
void addSubscribe(char* name, char* type);
void addSubscribe(char* name, enum SBType type){
void addSubscribe(char name[], char* type);
void addSubscribe(char name[], enum SBType type){
switch(type){
case STRING:
case SB_STRING:
addSubscribe(name, "string");
break;
case RANGE:
case SB_RANGE:
addSubscribe(name, "range");
break;
case BOOLEAN:
case SB_BOOLEAN:
addSubscribe(name, "boolean");
break;
}
Expand All @@ -91,32 +91,32 @@ class Spacebrew{
//void addSubscribe(char* name, SBType type, OnBooleanMessage function);
//void addSubscribe(char* name, SBType type, OnRangeMessage function);
//void addSubscribe(char* name, SBType type, OnStringMessage function);
void connect(char hostname[], char* clientName, char* description, int port = 9000);
void connect(char hostname[], char clientName[], char description[], int port = 9000);
void disconnect();
bool send(char* name, char* type, char* value);
bool send(char* name, SBType type, char* value){
bool send(char name[], char type[], char value[]);
bool send(char name[], SBType type, char value[]){
switch(type){
case STRING:
case SB_STRING:
send(name, "string", value);
break;
case RANGE:
case SB_RANGE:
send(name, "range", value);
break;
case BOOLEAN:
case SB_BOOLEAN:
send(name, "boolean", value);
break;
}
}
bool send(char* name, char* value){
bool send(char name[], char* value){
send(name, "string", value);
}
bool send(char* name, bool value){
bool send(char name[], bool value){
send(name, (char*)"boolean", (char*)(value ? "true" : "false"));
}
bool send(char* name, boolean value){
bool send(char name[], boolean value){
send(name, (char*)"boolean", (char*)(value ? "true" : "false"));
}
bool send(char* name, int value);
bool send(char name[], int value);
static void onWSError(WebSocketClient client, char* message);//defined in WebSocketClientCallback
static void onWSOpen(WebSocketClient client);
static void onWSClose(WebSocketClient client, int code, char* message);
Expand Down
File renamed without changes
80 changes: 80 additions & 0 deletions examples/button/button.ino
@@ -0,0 +1,80 @@
/*
Developed by the LAB at Rockwell Group
See "basic protoshield setup.PNG" in the examples directory
to see how this example is expecting the Arduino to be wired up.
I have the Ethernet shield sitting on top of the arduino, and then the
Sparkfun ProtoShield on top of that.
You must remember to include the Ethernet library, SPI library,
and the WebSocketClient library
(accessible here: https://github.com/labatrockwell/ArduinoWebsocketClient)
in order to use the Spacebrew library.
visit http://docs.spacebrew.cc/ for more info about Spacebrew!
putting the "toty" back in "Prototyping"
*/


#include <SPI.h>
#include <Spacebrew.h>
#include <Ethernet.h>
#include <WebSocketClient.h>

uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Spacebrew spacebrewConnection;
int lastButton = LOW;

const int buttonPin = 7;
const int ledPin = 8;
const int spacebrew_ledPin = 9;

void setup() {
//connect to message callbacks
spacebrewConnection.onBooleanMessage(onBooleanMessage);

//register publishers and subscribers
spacebrewConnection.addPublish("Button", SB_BOOLEAN);
spacebrewConnection.addSubscribe("Blink LED", SB_BOOLEAN);

//connect to the spacebrew server
Ethernet.begin(mac);
spacebrewConnection.connect("lab-macbookpro-02.rockwellgroup.com", "Rename_Me", "Arduino Input and Output Test");

pinMode(ledPin, OUTPUT);
pinMode(spacebrew_ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
//let the spacebrew library check for any incoming messages
//and re-connect to the server if necessary
spacebrewConnection.monitor();

int buttonIn = digitalRead(buttonPin);
if (buttonIn != lastButton){
boolean buttonPressed = (buttonIn == LOW);
int ledValue;
if (buttonPressed){
ledValue = HIGH;
} else {
ledValue = LOW;
}
digitalWrite(ledPin, ledValue);
//send the button state via spacebrew
spacebrewConnection.send("Button", buttonPressed);
lastButton = buttonIn;
}
}

void onBooleanMessage(char *name, bool value){
//turn the 'digital' LED on and off based on the incoming boolean
int spacebrew_ledValue;
if (value){
spacebrew_ledValue = HIGH;
} else {
spacebrew_ledValue = LOW;
}
digitalWrite(spacebrew_ledPin, spacebrew_ledValue);
}
16 changes: 8 additions & 8 deletions examples/input_output/input_output.ino
@@ -1,8 +1,8 @@
/*
Developed by the LAB at Rockwell Group
There is a .PNG in the examples directory
that shows how this example is expecting the Arduino to be wired up.
See "basic protoshield setup.PNG" in the examples directory
to see how this example is expecting the Arduino to be wired up.
I have the Ethernet shield sitting on top of the arduino, and then the
Sparkfun ProtoShield on top of that.
Expand Down Expand Up @@ -44,12 +44,12 @@ void setup() {
spacebrewConnection.onRangeMessage(onRangeMessage);

//register publishers and subscribers
spacebrewConnection.addPublish("Analog", RANGE);
spacebrewConnection.addPublish("Button", BOOLEAN);
spacebrewConnection.addPublish("Parrot", STRING);
spacebrewConnection.addSubscribe("Blink LED", BOOLEAN);
spacebrewConnection.addSubscribe("Fade LED", RANGE);
spacebrewConnection.addSubscribe("Parrot", STRING);
spacebrewConnection.addPublish("Analog", SB_RANGE);
spacebrewConnection.addPublish("Button", SB_BOOLEAN);
spacebrewConnection.addPublish("Parrot", SB_STRING);
spacebrewConnection.addSubscribe("Blink LED", SB_BOOLEAN);
spacebrewConnection.addSubscribe("Fade LED", SB_RANGE);
spacebrewConnection.addSubscribe("Parrot", SB_STRING);

//connect to the spacebrew server
Ethernet.begin(mac);
Expand Down

0 comments on commit 603c461

Please sign in to comment.