eboymusic
YaBB Newbies
Offline
Posts: 3
OSC and Processing
May 21st , 2008, 10:05pm
Hi, I'm new to Processing and trying to send accelerometer data to Max with OSC. Is there any suggestions? My code: // Draw a cylinder centered on the y-axis, going down from y=0 to y=height. // The radius at the top can be different from the radius at the bottom, // and the number of sides drawn is variable. import oscP5.*; import netP5.*; import processing.serial.*; import fullscreen.*; // for updates: http://www.superduper.org/processing/fullscreen_api/ import processing.opengl.*; //OPENGL is in an experimental mode when entering fullscreen import quicktime.std.movies.MovieDrawingComplete; // this enable fullscreen and exit fullscreen import quicktime.std.movies.MovieController; // This enable fullscreen //Keyboard shortcuts: To enter/leave fullscreen mode: ⌘+F OscP5 oscP5; NetAddress myRemoteLocation; Serial myPort; char inByte; int xVal, yVal, zVal = 0; int[] serialInArray = new int[3]; int serialCount = 0; boolean firstContact = true; String theValues = null; //string holds FullScreen fs; void setup(){ // set size to 1440X900 size(1440, 900, OPENGL); // 5 fps frameRate(24); oscP5 = new OscP5(this,12000); myRemoteLocation = new NetAddress("127.0.0.1",12000); // Create the fullscreen object fs = new FullScreen(this); // enter fullscreen mode fs.enter(); //This will hide the mouse cursor try { Robot robot = new Robot(); robot.mouseMove(int(width),int(height)); } catch (AWTException e) { } //setup serial port myPort = new Serial(this, Serial.list()[0], 9600); //what is wrong? myPort.clear(); } void draw() { background(127, 202, 235); fill(155,1 ,1); lights(); translate(width/2, height/2); rotateY(map(xVal*20, 0, width, 0, PI)); rotateZ(map(yVal*20, 0, height, 0, -PI)); //rect(xVal*6, yVal*4, 250, 250); noStroke(); translate(0, -40, 0); drawCylinder(xVal, 2*zVal, yVal, 64); // Draw a cylinder and defrom the object! } void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) { // here the object is born! float angle = 0; float angleIncrement = TWO_PI / sides; beginShape(QUAD_STRIP); for (int i = 0; i < sides + 1; ++i) { vertex(topRadius*cos(angle), 0, topRadius*sin(angle)); vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle)); angle += angleIncrement; } endShape(); // If it is not a cone, draw the circular top cap if (topRadius != 0) { angle = 0; beginShape(TRIANGLE_FAN); // Center point vertex(0, 0, 0); for (int i = 0; i < sides + 1; i++) { vertex(topRadius * cos(angle), 0, topRadius * sin(angle)); angle += angleIncrement; } endShape(); } // If it is not a cone, draw the circular bottom cap if (bottomRadius != 0) { angle = 0; beginShape(TRIANGLE_FAN); // Center point vertex(0, tall, 0); for (int i = 0; i < sides+1; i++) { vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle)); angle += angleIncrement; } endShape(); } } void serialEvent(Serial port) { // read a byte from the serial port: int inByte = port.read(); // if this is the first byte received, // take note of that fact. Otherwise, add it to the array: if (firstContact == false) { if (inByte == 'A') { port.clear(); // clear the serial port buffer firstContact = true; port.write('A'); } } else { // Add the latest byte from the serial port to array: serialInArray[serialCount] = inByte; serialCount++; // If we have 3 bytes: if (serialCount > 2 ) { xVal = serialInArray[0]; yVal = serialInArray[1]; zVal = serialInArray[2]; // print the values (for debugging purposes only): println(xVal + "\t" + yVal + "\t" + zVal); // Send a capital A to request new sensor readings: port.write('A'); // Reset serialCount: serialCount = 0; } } } void simpleOscMessage1() { /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage("/xVal"); myMessage.add(123); /* add an int to the osc message */ /* send the message */ oscP5.send(myMessage, myRemoteLocation); }