Last active
August 29, 2015 14:15
-
-
Save brainmachine/864690422b7f35deab36 to your computer and use it in GitHub Desktop.
gyro_processing_VFS
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
// Based on Example by Tom Igoe | |
// Edited by Leó Stefánsson | |
import processing.serial.*; | |
Serial myPort; // The serial port | |
PFont font; // The display font | |
String inString; // Input string from serial port | |
int lf = 10; // ASCII linefeed | |
Float x = 0.0; | |
Float y = 0.0; | |
Float z = 1.0; | |
void setup() { | |
size(600, 600); | |
font = loadFont("Monospaced-48.vlw"); | |
// List all the available serial ports: | |
println(Serial.list()); | |
// Open the port you are using at the rate you want: | |
myPort = new Serial(this, Serial.list()[7], 9600); | |
myPort.bufferUntil(lf); | |
} | |
void draw() { | |
background(0); | |
displayData(); | |
graphData(); | |
} | |
void serialEvent(Serial p) { | |
inString = p.readString(); | |
parseSerialString(inString); | |
} | |
void parseSerialString(String event) { | |
String[] t = splitTokens(inString, ","); | |
println(t.length); | |
if (t.length > 2) { | |
x = float(t[0]); | |
y = float(t[1]); | |
z = float(t[2]); | |
} | |
} | |
void displayData() { | |
textFont(font, 32); | |
fill(255); | |
text("X "+x.toString(), 10, 50); | |
text("Y "+y.toString(), 10, 100); | |
text("Z "+z.toString(), 10, 150); | |
} | |
void graphData() { | |
Float xVal = map(x, -2.0, 2.0, 0, height); | |
Float yVal = map(y, -2.0, 2.0, 0, height); | |
Float zVal = map(z, -2.0, 2.0, 0, height); | |
noStroke(); | |
fill(255, 0, 0); | |
rect(300, height/2, 50, height/2-xVal); | |
fill(0, 255, 0); | |
rect(400, height/2, 50, height/2-yVal); | |
fill(0, 127, 255); | |
rect(500, height/2, 50, height/2-zVal); | |
strokeWeight(5); | |
stroke(255); | |
fill(255); | |
line(0, height/2, width, height/2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment