From cd7fa5c9084b7e7552ba18fcb24e5f7a6dafe580 Mon Sep 17 00:00:00 2001 From: dviid Date: Sun, 1 Dec 2013 23:55:00 +0100 Subject: [PATCH] Added WaspMote --> Spacebrew Bridge class --- README.md | 4 +- of/waspmote_spacebrew_bridge/src/Bridge.cpp | 125 ++++++++++++++++++ of/waspmote_spacebrew_bridge/src/Bridge.h | 84 ++++++++++++ .../src/WaspFrameParser.cpp | 8 +- .../src/WaspFrameParser.h | 12 +- of/waspmote_spacebrew_bridge/src/main.cpp | 2 +- of/waspmote_spacebrew_bridge/src/testApp.cpp | 39 +----- of/waspmote_spacebrew_bridge/src/testApp.h | 17 +-- .../project.pbxproj | 6 + .../simple_broadcast/simple_broadcast.pde | 3 +- 10 files changed, 240 insertions(+), 60 deletions(-) create mode 100644 of/waspmote_spacebrew_bridge/src/Bridge.cpp create mode 100644 of/waspmote_spacebrew_bridge/src/Bridge.h diff --git a/README.md b/README.md index f5c8911..3c574a7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ CRITICAL-INFRASTRUCTURE ======================= - -CRITICAL INFRASTRUCTURE / Tranmsmediale 2014 -Jamie Allen & David Gauthier +[Jamie Allen](http://heaviside.net) & [David Gauthier](http://gauthiier.info) CRITICAL INFRASTRUCTURE unearths the undergiring of media technics, as a ‘media archeology of the present’ — understanding the post-digital as not just horizontal and temporal but material, stratal, and vertical — infra-digital and infra-technical. diff --git a/of/waspmote_spacebrew_bridge/src/Bridge.cpp b/of/waspmote_spacebrew_bridge/src/Bridge.cpp new file mode 100644 index 0000000..69d7a6b --- /dev/null +++ b/of/waspmote_spacebrew_bridge/src/Bridge.cpp @@ -0,0 +1,125 @@ +// +// Bridge.cpp +// waspmote_spacebrew_bridge +// +// Created by dviid on 12/1/13. +// +// + +#include "Bridge.h" + +#define wasp_signature "usbserial" +#define wasp_baud 115200 + +bool Bridge::setup_spacebrew() { + _sb_connection.connect(SPACEBREW_SERVER, "CRITTICAL INFRASTRUCTURE"); // blocking ?? + sleep(5); + return _sb_connection.isConnected(); +} + +bool Bridge::setup_serial() { + + //_serial.enumerateDevices(); + + vector list = _serial.getDeviceList(); + + for(int i = 0; i < list.size(); i++) { + if (list[i].getDeviceName().find(wasp_signature) != std::string::npos) { + _serial.setup(list[i].getDevicePath(), wasp_baud); + return true; + } + } + + return false; +} + +void Bridge::update(){ + + static vector r; + r.clear(); + + if(_serial.available() >= SERIAL_BUFFER_LEN) { + _serial.readBytes(_buffer, SERIAL_BUFFER_LEN); + _payload += string(_buffer, _buffer + SERIAL_BUFFER_LEN); + + _wp.ASCII_process(_payload, r, false); + + //LOOK up MAP !!!!!!!!!!!!!! + + for(int i = 0; i < r.size(); i++) { + + WaspFrame wf = r[i]; + + map::iterator it = _map.find(wf.mote_id); + + if(it == _map.end()) { + + Publisher* p = new Publisher(wf.mote_id, &_sb_connection); + p->data(wf.adc_data, wf.battery_level, wf.frame_seq); + _map[wf.mote_id] = p; + + } else { + + Publisher* p = it->second; + p->data(wf.adc_data, wf.battery_level, wf.frame_seq); + + } + } + + + } + +} + +Publisher::Publisher(string id, Spacebrew::Connection* sbc) : _id(id), _sb_connection(sbc), _current_seq(0), _is_publish_ready(false) +{ + cout << "NEW PUBLISHER -- " << id << endl; + + _ci_data_id = _id + "-DATA"; + _batt_data_id = _id + "-BATT"; + + setup_spacebrew_publisher(); + +} + +void Publisher::setup_spacebrew_publisher() +{ + if(!_is_publish_ready && _sb_connection->isConnected()) { + // ADD PUBLISHER INFO -- 2 publishers per waspmote (DATA, BATT) + _sb_connection->addPublish(_batt_data_id, "range"); + _sb_connection->addPublish(_ci_data_id, "range"); + _is_publish_ready = true; + } +} + + +void Publisher::data(int ci_data, int batt_data, int seq) +{ + +// if(seq <= _current_seq) return; +// else _current_seq= seq; + + _current_seq= seq; + + cout << "Publisher: " << _id << endl; + cout << seq << " - " << ci_data << " - " << batt_data << endl; + + if(_sb_connection->isConnected()) { + if (!_is_publish_ready) { + setup_spacebrew_publisher(); + sleep(1); + } + _sb_connection->sendRange(_ci_data_id, ci_data); + _sb_connection->sendRange(_batt_data_id, batt_data); + } + + if (_ci_data.size() >= SERIAL_BUFFER_LEN) _ci_data.pop_front(); + if (_batt_data.size() >= SERIAL_BUFFER_LEN) _batt_data.pop_front(); + + _ci_data.push_back(ci_data); + _batt_data.push_back(batt_data); + +} + + + diff --git a/of/waspmote_spacebrew_bridge/src/Bridge.h b/of/waspmote_spacebrew_bridge/src/Bridge.h new file mode 100644 index 0000000..889371c --- /dev/null +++ b/of/waspmote_spacebrew_bridge/src/Bridge.h @@ -0,0 +1,84 @@ +// +// Bridge.h +// waspmote_spacebrew_bridge +// +// Created by dviid on 12/1/13. +// +// + +#ifndef __waspmote_spacebrew_bridge__Bridge__ +#define __waspmote_spacebrew_bridge__Bridge__ + +#include + +#include "ofxSpacebrew.h" +#include "WaspFrameParser.h" + +static const int MAX_CACHED_DATA = 50; +static const int SERIAL_BUFFER_LEN = 136; +static const std::string SPACEBREW_SERVER = "sandbox.spacebrew.cc"; +//static const std::string SPACEBREW_SERVER = "localhost"; + +class Publisher +{ +public: + + Publisher(string id, Spacebrew::Connection* sbc); + + void setup_spacebrew_publisher(); + + void data(int ci_data, int batt_data, int seq); + + string _id; + string _ci_data_id; + string _batt_data_id; + + // cache + deque _ci_data; + deque _batt_data; + int _current_seq; + + // spacebrew connection + Spacebrew::Connection* _sb_connection; + bool _is_publish_ready; + // - todo - + +}; + +class Bridge +{ + +public: + + Bridge(){;} + + bool setup_spacebrew(); + + bool setup_serial(); + + void update(); // threadable + + // wasp frame cb + void data(WaspFrame* wf); + + Publisher* newPublisher(string id); + + + // serial interface + ofSerial _serial; + bool _serial_set; + unsigned char _buffer[SERIAL_BUFFER_LEN]; + string _payload; + + WaspFrameParser _wp; + + // spacebrew interface + + // map of spacebrew publishers + map _map; + + Spacebrew::Connection _sb_connection; +}; + + +#endif /* defined(__waspmote_spacebrew_bridge__Bridge__) */ diff --git a/of/waspmote_spacebrew_bridge/src/WaspFrameParser.cpp b/of/waspmote_spacebrew_bridge/src/WaspFrameParser.cpp index ec55514..1672603 100644 --- a/of/waspmote_spacebrew_bridge/src/WaspFrameParser.cpp +++ b/of/waspmote_spacebrew_bridge/src/WaspFrameParser.cpp @@ -70,7 +70,7 @@ void WaspFrameParser::ASCII_token_frame(string &input, vector &output) } } -void WaspFrameParser::ASCII_process(string &input, vector &output) +void WaspFrameParser::ASCII_process(string &input, vector &output, bool emit) { vector r; WaspFrameParser::ASCII_split_frame(input, r); @@ -90,12 +90,16 @@ void WaspFrameParser::ASCII_process(string &input, vector &output) wp->mote_id = t[2]; wp->frame_seq = atoi(t[3].c_str()); wp->mac = t[4]; - wp->adc_data = atoi(WaspFrameParser::ASCII_parse("CI0:", t[5]).c_str()); + wp->adc_data = atoi(WaspFrameParser::ASCII_parse(wp->mote_id + ":", t[5]).c_str()); wp->battery_level = atoi(WaspFrameParser::ASCII_parse("BAT:", t[6]).c_str()); //cout << (*wp) << endl; output.push_back(*wp); + + if(emit) { + ofNotifyEvent(_frame_event, wp); + } } diff --git a/of/waspmote_spacebrew_bridge/src/WaspFrameParser.h b/of/waspmote_spacebrew_bridge/src/WaspFrameParser.h index c41e890..1570206 100644 --- a/of/waspmote_spacebrew_bridge/src/WaspFrameParser.h +++ b/of/waspmote_spacebrew_bridge/src/WaspFrameParser.h @@ -13,6 +13,9 @@ #include #include +#include "ofEvents.h" + + using namespace std; #define MIN_FRAME_ENTRIES 7 @@ -38,13 +41,16 @@ class WaspFrameParser { public: + WaspFrameParser(){;} + static void ASCII_split_frame(string &input, vector &output); static void ASCII_token_frame(string &input, vector &output); - - static void ASCII_process(string &input, vector &output); - static string ASCII_parse(string id, string input); + void ASCII_process(string &input, vector &output, bool emit); + + ofEvent _frame_event; + }; #endif /* defined(__waspmote_gateway_parser__WaspFrameParser__) */ diff --git a/of/waspmote_spacebrew_bridge/src/main.cpp b/of/waspmote_spacebrew_bridge/src/main.cpp index a7d241d..2b6b58a 100644 --- a/of/waspmote_spacebrew_bridge/src/main.cpp +++ b/of/waspmote_spacebrew_bridge/src/main.cpp @@ -3,7 +3,7 @@ //======================================================================== int main( ){ - ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context + ofSetupOpenGL(200,200,OF_WINDOW); // <-------- setup the GL context // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN diff --git a/of/waspmote_spacebrew_bridge/src/testApp.cpp b/of/waspmote_spacebrew_bridge/src/testApp.cpp index a022a95..56d38b3 100644 --- a/of/waspmote_spacebrew_bridge/src/testApp.cpp +++ b/of/waspmote_spacebrew_bridge/src/testApp.cpp @@ -8,50 +8,17 @@ #define wasp_baud 115200 -bool testApp::setup_serial() { - - //_serial.enumerateDevices(); - - vector list = _serial.getDeviceList(); - - for(int i = 0; i < list.size(); i++) { - if (list[i].getDeviceName().find(wasp_signature) != std::string::npos) { - _serial.setup(list[i].getDevicePath(), wasp_baud); - return true; - } - } - - return false; -} - -string wp_ascii_split(string input) -{ - -} - //-------------------------------------------------------------- void testApp::setup(){ - _serial_set = setup_serial(); + _bridge.setup_serial(); + _bridge.setup_spacebrew(); } //-------------------------------------------------------------- void testApp::update(){ - static vector r; - r.clear(); - - if(_serial.available() >= BUFFER_LEN) { - _serial.readBytes(_buffer, BUFFER_LEN); - _payload += string(_buffer, _buffer + BUFFER_LEN); - - WaspFrameParser::ASCII_process(_payload, r); - for(int i = 0; i < r.size(); i++) { - ofLog() << r[i] << endl; - } - - } - + _bridge.update(); } //-------------------------------------------------------------- diff --git a/of/waspmote_spacebrew_bridge/src/testApp.h b/of/waspmote_spacebrew_bridge/src/testApp.h index 5b1f0d5..26bcfaf 100644 --- a/of/waspmote_spacebrew_bridge/src/testApp.h +++ b/of/waspmote_spacebrew_bridge/src/testApp.h @@ -3,7 +3,8 @@ #include "ofMain.h" #include "OfSerial.h" -#define BUFFER_LEN 136 +#include "Bridge.h" + class testApp : public ofBaseApp{ @@ -22,17 +23,5 @@ class testApp : public ofBaseApp{ void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); - bool setup_serial(); - - string wp_ascii_split(string input); - - public: - - ofSerial _serial; - bool _serial_set; - - unsigned char _buffer[BUFFER_LEN]; - string _payload; - - + Bridge _bridge; }; diff --git a/of/waspmote_spacebrew_bridge/waspmote_spacebrew_bridge.xcodeproj/project.pbxproj b/of/waspmote_spacebrew_bridge/waspmote_spacebrew_bridge.xcodeproj/project.pbxproj index 8a690dd..0a90bef 100644 --- a/of/waspmote_spacebrew_bridge/waspmote_spacebrew_bridge.xcodeproj/project.pbxproj +++ b/of/waspmote_spacebrew_bridge/waspmote_spacebrew_bridge.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ B5F6FF34184A6F0E0063881D /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF2F184A6F0E0063881D /* main.cpp */; }; B5F6FF35184A6F0E0063881D /* testApp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF30184A6F0E0063881D /* testApp.cpp */; }; B5F6FF36184A6F0E0063881D /* WaspFrameParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */; }; + B5F6FF39184B97040063881D /* Bridge.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF37184B97040063881D /* Bridge.cpp */; }; BBAB23CB13894F3D00AA2426 /* GLUT.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = BBAB23BE13894E4700AA2426 /* GLUT.framework */; }; C5688FC85C0AE0B5E4E5F9CE /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C53FF33B244987E09C8CC63F /* Server.cpp */; }; C6E1C4962CC70798B430DCBC /* jsoncpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9413D837F3E273803A5F776E /* jsoncpp.cpp */; }; @@ -81,6 +82,8 @@ B5F6FF31184A6F0E0063881D /* testApp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testApp.h; sourceTree = ""; }; B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WaspFrameParser.cpp; sourceTree = ""; }; B5F6FF33184A6F0E0063881D /* WaspFrameParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WaspFrameParser.h; sourceTree = ""; }; + B5F6FF37184B97040063881D /* Bridge.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Bridge.cpp; sourceTree = ""; }; + B5F6FF38184B97040063881D /* Bridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bridge.h; sourceTree = ""; }; BBAB23BE13894E4700AA2426 /* GLUT.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GLUT.framework; path = ../../../of_v0.8.0_osx_release/libs/glut/lib/osx/GLUT.framework; sourceTree = ""; }; C49850835B8C1A87752B848B /* Client.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = Client.cpp; path = ../../../of_v0.8.0_osx_release/addons/ofxLibwebsockets/libs/ofxLibwebsockets/src/Client.cpp; sourceTree = SOURCE_ROOT; }; C53FF33B244987E09C8CC63F /* Server.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = Server.cpp; path = ../../../of_v0.8.0_osx_release/addons/ofxLibwebsockets/libs/ofxLibwebsockets/src/Server.cpp; sourceTree = SOURCE_ROOT; }; @@ -274,6 +277,8 @@ B5F6FF31184A6F0E0063881D /* testApp.h */, B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */, B5F6FF33184A6F0E0063881D /* WaspFrameParser.h */, + B5F6FF37184B97040063881D /* Bridge.cpp */, + B5F6FF38184B97040063881D /* Bridge.h */, ); path = src; sourceTree = SOURCE_ROOT; @@ -385,6 +390,7 @@ B5F6FF34184A6F0E0063881D /* main.cpp in Sources */, B5F6FF35184A6F0E0063881D /* testApp.cpp in Sources */, B5F6FF36184A6F0E0063881D /* WaspFrameParser.cpp in Sources */, + B5F6FF39184B97040063881D /* Bridge.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/waspmote/applications/DEVELOPMENT/simple_broadcast/simple_broadcast.pde b/waspmote/applications/DEVELOPMENT/simple_broadcast/simple_broadcast.pde index 0404e2c..ffefef6 100644 --- a/waspmote/applications/DEVELOPMENT/simple_broadcast/simple_broadcast.pde +++ b/waspmote/applications/DEVELOPMENT/simple_broadcast/simple_broadcast.pde @@ -11,6 +11,7 @@ SENSOR_CI3 SENSOR_CI4 */ +#define CI_NODE_NAME "CI4" #define SENSOR_PIN ANALOG1 // PIN for the ADC reading // declare ADC reading variable @@ -82,7 +83,7 @@ void loop() { // B. BROADCAST PACKET //B.1 create ASCII frame - frame.createFrame(ASCII,"CRITICAL-0"); + frame.createFrame(ASCII, CI_NODE_NAME); //B.1.1 add CRITICAL INFRASTRUCTURE sensor descriptors frame.addSensor(SENSOR_MAC, macLow); // MAC