HAHA! commit

Initial commit
This commit is contained in:
dviid
2013-11-30 21:22:19 +01:00
commit 2e9b87dab6
26 changed files with 4842 additions and 0 deletions
@@ -0,0 +1,114 @@
//
// WaspFrameParser.cpp
// waspmote_gateway_parser
//
// Created by dviid on 11/23/13.
//
//
#include "WaspFrameParser.h"
#define FRAME_DELIM "<=>"
#define FRAME_SEP '#'
#include <sstream>
#include <iostream>
ostream& operator<<(ostream& out, const WaspFrame& wf)
{
out << "--- WaspFrame ---" << endl;
out << "Serial Id: " << wf.serial_id << endl;
out << "Mote Id: " << wf.mote_id << endl;
out << "Frame Seq.: " << wf.frame_seq << endl;
out << "MAC: " << wf.mac << endl;
out << "ADC: " << wf.adc_data << endl;
out << "BATT: " << wf.battery_level << endl;
out << "-----------------" << endl;
return out;
}
void WaspFrameParser::ASCII_split_frame(string &input, vector<string> &output)
{
//cout << "PARSE: " << input << endl;
string d = FRAME_DELIM;
string token;
size_t i, j = 0;
// CASE A
if((i = input.find(d)) == std::string::npos) {
input.erase(0, input.length());
return;
}
while((j = input.find(d)) != std::string::npos) {
token = input.substr(0, j);
//cout << "TOKEN: " << token << endl;
output.push_back(string(token));
input.erase(0, j + d.length());
//cout << "NEXT: " << input << endl;
}
}
void WaspFrameParser::ASCII_token_frame(string &input, vector<string> &output)
{
//cout << endl << "frame -- " << input << endl;
stringstream ss(input);
string tok;
while(getline(ss, tok, FRAME_SEP))
{
output.push_back(tok);
}
}
void WaspFrameParser::ASCII_process(string &input, vector<WaspFrame> &output)
{
vector<string> r;
WaspFrameParser::ASCII_split_frame(input, r);
for(int i = 0; i < r.size(); i++) {
vector<string> t;
WaspFrameParser::ASCII_token_frame(r[i], t);
//cout << "SIZE:::" << t.size() << endl;
if(t.size() < MIN_FRAME_ENTRIES) continue; // not valid frame
WaspFrame* wp = new WaspFrame();
wp->serial_id = t[1];
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->battery_level = atoi(WaspFrameParser::ASCII_parse("BAT:", t[6]).c_str());
//cout << (*wp) << endl;
output.push_back(*wp);
}
}
string WaspFrameParser::ASCII_parse(string id, string input)
{
size_t i;
if((i = input.find(id)) != std::string::npos) {
return input.substr(i + id.length(), input.length());
}
return "";
}
@@ -0,0 +1,50 @@
//
// WaspFrameParser.h
// waspmote_gateway_parser
//
// Created by dviid on 11/23/13.
//
//
#ifndef __waspmote_gateway_parser__WaspFrameParser__
#define __waspmote_gateway_parser__WaspFrameParser__
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define MIN_FRAME_ENTRIES 7
class WaspFrame {
public:
WaspFrame(){;}
string serial_id;
string mote_id;
int frame_seq;
string mac;
int adc_data;
int battery_level;
friend ostream& operator<<(ostream& os, const WaspFrame& wf);
};
class WaspFrameParser {
public:
static void ASCII_split_frame(string &input, vector<string> &output);
static void ASCII_token_frame(string &input, vector<string> &output);
static void ASCII_process(string &input, vector<WaspFrame> &output);
static string ASCII_parse(string id, string input);
};
#endif /* defined(__waspmote_gateway_parser__WaspFrameParser__) */
+13
View File
@@ -0,0 +1,13 @@
#include "ofMain.h"
#include "testApp.h"
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new testApp());
}
@@ -0,0 +1,105 @@
#include "testApp.h"
#include "WaspFrameParser.h"
#include <string>
#define wasp_signature "usbserial"
#define wasp_baud 115200
bool testApp::setup_serial() {
//_serial.enumerateDevices();
vector <ofSerialDeviceInfo> 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();
}
//--------------------------------------------------------------
void testApp::update(){
static vector<WaspFrame> 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;
}
}
}
//--------------------------------------------------------------
void testApp::draw(){
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
@@ -0,0 +1,38 @@
#pragma once
#include "ofMain.h"
#include "OfSerial.h"
#define BUFFER_LEN 136
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
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;
};