Skip to content

Instantly share code, notes, and snippets.

@100ideas
Created May 19, 2010 00:32
Show Gist options
  • Save 100ideas/405769 to your computer and use it in GitHub Desktop.
Save 100ideas/405769 to your computer and use it in GitHub Desktop.
import processing.serial.*;
import cc.arduino.*;
import TUIO.*;
Arduino arduino;
int servoX = 9;
int servoY = 11;
int mapX = 0;
int mapY = 0;
TuioProcessing tuioClient;
final static float tuioCursorSpeedMult = 0.02f; // the iphone screen is so small, easy to rack up huge velocities! need to scale down
final static float tuioStationaryForce = 0.001; // force exerted when cursor is stationary
boolean tuioDoubleTap = false;
int xmap = 0; //holds tuio cursor x-movement
void setup() {
size(512, 200);
arduino = new Arduino(this, Arduino.list()[0], 57600);
//arduino.pinMode(servoPin, Arduino.OUTPUT);
println("\n zeroing servos\n");
arduino.analogWrite(servoX, 90);
arduino.analogWrite(servoY, 90);
for (int i=0;i<180;i++) {
servo( i );
delay(100);
}
// init TUIO
tuioClient = new TuioProcessing(this);
}
public void servo( int x ){
println("### plug event method. received a message /servo.");
println(" servoXWrite: "+x);
arduino.analogWrite(servoX, x);
}
void updateTUIO() {
Vector tuioCursorList = tuioClient.getTuioCursors();
//println("tuio found " + tuioCursorList.size() + "cursors");
for (int i=0;i<tuioCursorList.size();i++) {
TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i);
float vx = tcur.getXSpeed() * tuioCursorSpeedMult;
float vy = tcur.getYSpeed() * tuioCursorSpeedMult;
/* if(vx == 0 && vy == 0) {
vx = random(-tuioStationaryForce, tuioStationaryForce);
vy = random(-tuioStationaryForce, tuioStationaryForce);
}
addForce(tcur.getX(), tcur.getY(), vx, vy); */
mapX = round ( map( tcur.getX(), 0, 1, 30, 150 ) );
mapY = round ( map( tcur.getY(), 0, 1, 30, 150 ) );
println( mapX + ", " + mapY );
arduino.analogWrite(servoX, mapX );
arduino.analogWrite(servoY, mapY );
}
}
void draw() {
updateTUIO();
background(constrain(mouseX / 2, 0, 255));
// arduino.analogWrite(servoPin, constrain(mouseX / 2, 0, 180));
// arduino.analogWrite(11, constrain(255 - mouseX / 2, 0, 30));
}
void mousePressed() {
println(" clicked: " + mouseX);
}
// TUIOLIB required methods
void addTuioObject(TuioObject tobj) {
}
void updateTuioObject(TuioObject tobj) {
}
void removeTuioObject(TuioObject tobj) {
}
void addTuioCursor(TuioCursor tcur) {
println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
}
// called when a cursor is moved
void updateTuioCursor (TuioCursor tcur) {
//println("update cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
}
// called when a cursor is removed from the scene
void removeTuioCursor(TuioCursor tcur) {
//println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
}
void refresh(TuioTime bundleTime) {
redraw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment