Created
December 8, 2011 22:42
-
-
Save companje/1449041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////////////// | |
#pragma once | |
#include "ofMain.h" | |
class testApp : public ofBaseApp{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void mousePressed(int x, int y, int button); | |
//void gotMessage(ofMessage msg); | |
ofSerial serial; | |
}; | |
//////////////////////////////////////////////////////////////////// | |
#include "testApp.h" | |
string ofxGetSerialString(ofSerial &serial, char until) { | |
static string str; | |
stringstream ss; | |
char ch; | |
int ttl=1000; | |
while ((ch=serial.readByte())>0 && ttl-->0 && ch!=until) { | |
ss << ch; | |
} | |
str+=ss.str(); | |
if (ch==until) { | |
string tmp=str; | |
str=""; | |
return tmp; | |
} else { | |
return ""; | |
} | |
} | |
//-------------------------------------------------------------- | |
void testApp::setup(){ | |
ofSetFrameRate(60); | |
serial.listDevices(); | |
serial.setup(0, 9600); | |
} | |
//-------------------------------------------------------------- | |
void testApp::update(){ | |
// while (serial.available()) { | |
// char b = serial.readByte(); | |
// cout << b; | |
// if (b=='\n') | |
// } | |
string result = ofxGetSerialString(serial, '\n'); | |
if (result!="") { | |
vector<string> params = ofSplitString(result,","); | |
if (params.size()==4) { | |
int nr3 = ofToInt(params[3]); | |
cout << nr3 << endl; | |
if (nr3==10) { | |
serial.writeByte('b'); | |
} | |
} | |
} | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw(){ | |
} | |
void testApp::mousePressed(int x, int y, int button) { | |
serial.writeByte('b'); | |
} | |
// | |
//only useful if there's a addon/class or function calling the ofNotifyEvent function | |
//void testApp::gotMessage(ofMessage msg) { | |
// if (msg.message=="dataReceived") { | |
// serial.writeByte('b'); | |
// } | |
//} | |
// | |
////////////////////////////////////////////////////////////////// | |
/* | |
Blink | |
Turns on an LED on for one second, then off for one second, repeatedly. | |
This example code is in the public domain. | |
*/ | |
int counter = 0; | |
void setup() { | |
// initialize the digital pin as an output. | |
// Pin 13: Arduino has an LED connected on pin 13 | |
// Pin 11: Teensy 2.0 has the LED on pin 11 | |
// Pin 6: Teensy++ 2.0 has the LED on pin 6 | |
pinMode(13, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
if (Serial.available()) { | |
Serial.read(); | |
digitalWrite(13, HIGH); | |
delay(100); | |
digitalWrite(13, LOW); | |
delay(100); | |
} | |
Serial.print(5); | |
Serial.print(","); | |
Serial.print(225); | |
Serial.print(","); | |
Serial.print(524); | |
Serial.print(","); | |
Serial.print(counter); | |
Serial.println(); | |
counter++; | |
if (counter>50) counter=0; | |
delay(100); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment