1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Spacebrew update

This commit is contained in:
Federico Fissore 2014-04-01 09:35:56 +02:00
parent 2f7e712fb9
commit 88ed5d53a6
8 changed files with 679 additions and 683 deletions

View File

@ -40,9 +40,7 @@ void setup() {
// for debugging, wait until a serial console is connected // for debugging, wait until a serial console is connected
delay(4000); delay(4000);
while (!Serial) { while (!Serial) { ; }
;
}
// start-up the bridge // start-up the bridge
Bridge.begin(); Bridge.begin();
@ -68,7 +66,8 @@ void setup() {
// connect to cloud spacebrew server at "sandbox.spacebrew.cc" // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
sb.connect("sandbox.spacebrew.cc"); sb.connect("sandbox.spacebrew.cc");
// we give some time to arduino to connect to sandbox, otherwise the first sb.monitor(); call will give an error
delay(1000);
} }
@ -94,6 +93,7 @@ void loop() {
} }
} }
delay(1000);
} }
// define handler methods, all standard data type handlers take two appropriate arguments // define handler methods, all standard data type handlers take two appropriate arguments

View File

@ -40,9 +40,7 @@ void setup() {
// for debugging, wait until a serial console is connected // for debugging, wait until a serial console is connected
delay(4000); delay(4000);
while (!Serial) { while (!Serial) { ; }
;
}
// start-up the bridge // start-up the bridge
Bridge.begin(); Bridge.begin();

View File

@ -41,9 +41,7 @@ void setup() {
// for debugging, wait until a serial console is connected // for debugging, wait until a serial console is connected
delay(4000); delay(4000);
while (!Serial) { while (!Serial) { ; }
;
}
// start-up the bridge // start-up the bridge
Bridge.begin(); Bridge.begin();

View File

@ -38,9 +38,7 @@ void setup() {
// for debugging, wait until a serial console is connected // for debugging, wait until a serial console is connected
delay(4000); delay(4000);
while (!Serial) { while (!Serial) { ; }
;
}
// start-up the bridge // start-up the bridge
Bridge.begin(); Bridge.begin();

View File

@ -1,4 +1,4 @@
SpacebrewYun KEYWORD3 SpacebrewYun KEYWORD1
addPublish KEYWORD2 addPublish KEYWORD2
addSubscribe KEYWORD2 addSubscribe KEYWORD2
connect KEYWORD2 connect KEYWORD2

View File

@ -1,8 +1,10 @@
name=SpacebrewYun name=SpacebrewYun
version=1.0
author=Julio Terra author=Julio Terra
maintainer=Julio Terra <julioterra@gmail.com> email=julioterra@gmail.com
sentence=Easily connect the Arduino Yun to Spacebrew sentence=Easily connect the Arduino Yun to Spacebrew
paragraph=This library was developed to enable you to easily connect the Arduino Yun to Spacebrew. To learn more about Spacebrew visit Spacebrew.cc paragraph=This library was developed to enable you to easily connect the Arduino Yun to Spacebrew. To learn more about Spacebrew visit Spacebrew.cc
url=https://github.com/julioterra/yunSpacebrew url=https://github.com/julioterra/yunSpacebrew
architectures=* architectures=avr
version=1.0
dependencies=Bridge
core-dependencies=arduino (>=1.5.0)

View File

@ -10,6 +10,7 @@ SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
server = "sandbox.spacebrew.cc"; server = "sandbox.spacebrew.cc";
port = 9000; port = 9000;
_started = false;
_connected = false; _connected = false;
_verbose = false; _verbose = false;
_error_msg = false; _error_msg = false;
@ -21,6 +22,9 @@ SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
read_name = false; read_name = false;
read_msg = false; read_msg = false;
connect_attempt = 0;
connect_attempt_inter = 10000;
for ( int i = 0; i < pidLength; i++ ) { for ( int i = 0; i < pidLength; i++ ) {
pid [i] = '\0'; pid [i] = '\0';
} }
@ -45,25 +49,25 @@ SpacebrewYun::OnSBOpen SpacebrewYun::_onOpen = NULL;
SpacebrewYun::OnSBClose SpacebrewYun::_onClose = NULL; SpacebrewYun::OnSBClose SpacebrewYun::_onClose = NULL;
SpacebrewYun::OnSBError SpacebrewYun::_onError = NULL; SpacebrewYun::OnSBError SpacebrewYun::_onError = NULL;
void SpacebrewYun::onOpen(OnSBOpen function) { void SpacebrewYun::onOpen(OnSBOpen function){
_onOpen = function; _onOpen = function;
} }
void SpacebrewYun::onClose(OnSBClose function) { void SpacebrewYun::onClose(OnSBClose function){
_onClose = function; _onClose = function;
} }
void SpacebrewYun::onRangeMessage(OnRangeMessage function) { void SpacebrewYun::onRangeMessage(OnRangeMessage function){
_onRangeMessage = function; _onRangeMessage = function;
} }
void SpacebrewYun::onStringMessage(OnStringMessage function) { void SpacebrewYun::onStringMessage(OnStringMessage function){
_onStringMessage = function; _onStringMessage = function;
} }
void SpacebrewYun::onBooleanMessage(OnBooleanMessage function) { void SpacebrewYun::onBooleanMessage(OnBooleanMessage function){
_onBooleanMessage = function; _onBooleanMessage = function;
} }
void SpacebrewYun::onCustomMessage(OnCustomMessage function) { void SpacebrewYun::onCustomMessage(OnCustomMessage function){
_onCustomMessage = function; _onCustomMessage = function;
} }
void SpacebrewYun::onError(OnSBError function) { void SpacebrewYun::onError(OnSBError function){
_onError = function; _onError = function;
} }
@ -88,13 +92,13 @@ void SpacebrewYun::addPublish(const String& name, const String& type) {
name.toCharArray(p->name, name.length() + 1); name.toCharArray(p->name, name.length() + 1);
type.toCharArray(p->type, type.length() + 1); type.toCharArray(p->type, type.length() + 1);
if (publishers == NULL) { if (publishers == NULL){
publishers = p; publishers = p;
} }
else { else {
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
int counter = 1; int counter = 1;
while (curr->next != NULL) { while(curr->next != NULL){
curr = curr->next; curr = curr->next;
counter++; counter++;
} }
@ -110,12 +114,12 @@ void SpacebrewYun::addSubscribe(const String& name, const String& type) {
name.toCharArray(s->name, name.length() + 1); name.toCharArray(s->name, name.length() + 1);
type.toCharArray(s->type, type.length() + 1); type.toCharArray(s->type, type.length() + 1);
if (subscribers == NULL) { if (subscribers == NULL){
subscribers = s; subscribers = s;
} }
else { else {
struct Subscriber *curr = subscribers; struct Subscriber *curr = subscribers;
while (curr->next != NULL) { while(curr->next != NULL){
curr = curr->next; curr = curr->next;
} }
curr->next = s; curr->next = s;
@ -123,8 +127,11 @@ void SpacebrewYun::addSubscribe(const String& name, const String& type) {
} }
void SpacebrewYun::connect(String _server, int _port) { void SpacebrewYun::connect(String _server, int _port) {
Serial.print(F("NEW LIB"));
_started = true;
server = _server; server = _server;
port = _port; port = _port;
connect_attempt = millis();
killPids(); killPids();
@ -142,7 +149,7 @@ void SpacebrewYun::connect(String _server, int _port) {
if (subscribers != NULL) { if (subscribers != NULL) {
struct Subscriber *curr = subscribers; struct Subscriber *curr = subscribers;
while (curr != NULL) { while(curr != NULL){
if (_verbose) { if (_verbose) {
Serial.print(F("Creating subscribers: ")); Serial.print(F("Creating subscribers: "));
Serial.print(curr->name); Serial.print(curr->name);
@ -163,7 +170,7 @@ void SpacebrewYun::connect(String _server, int _port) {
} }
if (publishers != NULL) { if (publishers != NULL) {
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
while (curr != NULL) { while(curr != NULL){
if (_verbose) { if (_verbose) {
Serial.print(F("Creating publishers: ")); Serial.print(F("Creating publishers: "));
Serial.print(curr->name); Serial.print(curr->name);
@ -189,34 +196,42 @@ void SpacebrewYun::connect(String _server, int _port) {
if (_verbose) { if (_verbose) {
Serial.println(F("Brew started ")); Serial.println(F("Brew started "));
} }
while (!Console) { while (!Console) { ; }
;
}
} }
void SpacebrewYun::monitor() { void SpacebrewYun::monitor() {
// if not connected try to reconnect after appropriate interval
if (_started && !_connected) {
if ((millis() - connect_attempt) > connect_attempt_inter) {
connect(server, port);
}
}
// if message received from console, then process it
while (Console.available() > 0) { while (Console.available() > 0) {
char c = Console.read(); char c = Console.read();
if (c == char(CONNECTION_START) && !_connected) { if (c == char(CONNECTION_START) && _started && !_connected) {
if (_verbose) { if (_verbose) {
Serial.print(F("Connected to spacebrew server at: ")); Serial.print(F("Connected to spacebrew server at: "));
Serial.println(server); Serial.println(server);
Serial.print(F("Application name set to: ")); Serial.print(F("Application name set to: "));
Serial.println(name); Serial.println(name);
} }
if (_onOpen != NULL) { if (_onOpen != NULL){
_onOpen(); _onOpen();
} }
_connected = true; _connected = true;
} }
else if (c == char(CONNECTION_END) && _connected) { else if (c == char(CONNECTION_END) && _connected) {
_connected = false; _connected = false;
if (_verbose) { if (_verbose) {
Serial.print(F("Disconnected from spacebrew server at: ")); Serial.print(F("Disconnected from spacebrew server at: "));
Serial.println(server); Serial.println(server);
} }
if (_onClose != NULL) { if (_onClose != NULL){
_onClose(); _onClose();
} }
} }
@ -269,9 +284,10 @@ void SpacebrewYun::monitor() {
} }
} }
if (publishers != NULL) { // check if received confirmation that linino received messages
if (publishers != NULL && _connected) {
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
while ((curr != NULL)) { while((curr != NULL)){
if ( (curr->confirmed == 0) && ((millis() - curr->time) > 50) ) { if ( (curr->confirmed == 0) && ((millis() - curr->time) > 50) ) {
if (_verbose) { if (_verbose) {
@ -289,13 +305,9 @@ void SpacebrewYun::monitor() {
void SpacebrewYun::onConfirm() { void SpacebrewYun::onConfirm() {
if (publishers != NULL) { if (publishers != NULL) {
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
while ((curr != NULL)) { while((curr != NULL)){
if (sub_name.equals(curr->name) == true) { if (sub_name.equals(curr->name) == true) {
curr->confirmed = true; curr->confirmed = true;
// if (_verbose) {
// Serial.print(F("confirmed "));
// Serial.println(curr->name);
// }
break; break;
} }
curr = curr->next; curr = curr->next;
@ -318,7 +330,7 @@ void SpacebrewYun::verbose(boolean verbose = true) {
void SpacebrewYun::onMessage() { void SpacebrewYun::onMessage() {
if (subscribers != NULL) { if (subscribers != NULL) {
struct Subscriber *curr = subscribers; struct Subscriber *curr = subscribers;
while ((curr != NULL) && (sub_type == "")) { while((curr != NULL) && (sub_type == "")){
if (sub_name.equals(curr->name) == true) { if (sub_name.equals(curr->name) == true) {
sub_type = curr->type; sub_type = curr->type;
} }
@ -358,7 +370,7 @@ void SpacebrewYun::onMessage() {
} }
void SpacebrewYun::send(const String& name, const String& value) { void SpacebrewYun::send(const String& name, const String& value){
if (publishers != NULL) { if (publishers != NULL) {
Console.print(char(29)); Console.print(char(29));
@ -369,7 +381,7 @@ void SpacebrewYun::send(const String& name, const String& value) {
Console.flush(); Console.flush();
struct Publisher *curr = publishers; struct Publisher *curr = publishers;
while (curr != NULL) { while(curr != NULL){
if (name.equals(curr->name) == true) { if (name.equals(curr->name) == true) {
int msg_len = 0; int msg_len = 0;
@ -422,7 +434,7 @@ void SpacebrewYun::getPids() {
sbPids[sbPidsIndex] = atoi(pid); sbPids[sbPidsIndex] = atoi(pid);
if ( sbPidsIndex < (sbPidsLen - 1) ) sbPidsIndex = (sbPidsIndex + 1); if ( sbPidsIndex < (sbPidsLen - 1) ) sbPidsIndex = (sbPidsIndex + 1);
for ( int i = 0; i < pidLength; i++ ) { for( int i = 0; i < pidLength; i++ ){
pid[i] = '\0'; pid[i] = '\0';
pidCharIndex = 0; pidCharIndex = 0;
} }

View File

@ -26,7 +26,7 @@ struct Publisher {
long time; long time;
}; };
struct Subscriber { struct Subscriber{
char *name; char *name;
char *type; char *type;
Subscriber *next; Subscriber *next;
@ -44,12 +44,8 @@ class SpacebrewYun {
void addSubscribe(const String&, const String&); void addSubscribe(const String&, const String&);
void connect(String, int); void connect(String, int);
void connect() { void connect() { connect(server, port); };
connect(server, port); void connect(String _server) { connect(String(_server), port); };
};
void connect(String _server) {
connect(String(_server), port);
};
void monitor(); void monitor();
void onMessage(); void onMessage();
@ -58,21 +54,11 @@ class SpacebrewYun {
boolean connected(); boolean connected();
void send(const String&, const String&); void send(const String&, const String&);
void send(const String& name, char * value) { void send(const String& name, char * value) { send(name, String(value)); }
send(name, String(value)); void send(const String& name, bool value){ send(name, (value ? String("true") : String("false"))); };
} void send(const String& name, int value) { send(name, String(value)); };
void send(const String& name, bool value) { void send(const String& name, long value) { send(name, String(value)); };
send(name, (value ? String("true") : String("false"))); void send(const String& name, float value) { send(name, String(value)); };
};
void send(const String& name, int value) {
send(name, String(value));
};
void send(const String& name, long value) {
send(name, String(value));
};
void send(const String& name, float value) {
send(name, String(value));
};
void verbose(boolean); void verbose(boolean);
@ -98,6 +84,7 @@ class SpacebrewYun {
String name; String name;
String server; String server;
String description; String description;
boolean _started;
boolean _connected; boolean _connected;
boolean _error_msg; boolean _error_msg;
boolean _verbose; boolean _verbose;
@ -125,8 +112,9 @@ class SpacebrewYun {
static int sub_msg_str_max; static int sub_msg_str_max;
static int sub_msg_int_max; static int sub_msg_int_max;
static int sub_msg_bool_max; static int sub_msg_bool_max;
// int sub_name_max;
// int sub_msg_str_max; long connect_attempt;
int connect_attempt_inter;
Process pids; Process pids;
char pid [6]; char pid [6];
@ -135,12 +123,12 @@ class SpacebrewYun {
void killPids(); void killPids();
void getPids(); void getPids();
static char * createString(int len) { static char * createString(int len){
char * out = ( char * ) malloc ( len + 1 ); char * out = ( char * ) malloc ( len + 1 );
return out; return out;
} }
static void emptyString(char * str, int len) { static void emptyString(char * str, int len){
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
str[i] = '\0'; str[i] = '\0';
} }