// Mapping Wii remote data to text file, with visual feedback // Code appropriated and cobbled together by Scott Brown, 14 September, 2009 // // Data capture: // A demonstration of the use of arrays to buffer data being captured from the // mouse so that it can be saved to file when convenient, rather than saving to // file all the time and producing delays in the response time of the display // Author: Rob Saunders // Date: 9 September, 2009 // // OSC: // osc, processing and the wiimote, using OSCulator based on oscReceiveWiiIR by andreas schlegel, andi@sojamo.de import oscP5.*; import netP5.*; OscP5 osc; float[] wiiPitchDraw; float[] wiiRollDraw; float[] wiiYawDraw; float[] nunPitchDraw; float[] nunRollDraw; float[] nunYawDraw; // Declare and set a constant for the size of the arrays // This value could be much larger to avoid saving to disk for longer sessions int NUM_COORDS = 1000; // Declare the arrays that will store the coordinate data float[] wiiPitch_coords; float[] wiiRoll_coords; float[] wiiYaw_coords; float[] nunPitch_coords; float[] nunRoll_coords; float[] nunYaw_coords; // The index into the coordinate arrays int coords_index; void setup() { // Set the size of the window size(screen.width/2, 450); frameRate(25); // open an up port for listening to incoming osc messages from OSCulator osc = new OscP5(this,"localhost",9000); osc.plug(this,"wiiPry","/wii/1/accel/pry"); osc.plug(this,"nunPry","/wii/1/nunchuk/accel/pry"); // Set the visual characteristics of the window background(255); smooth(); noFill(); // Create the arrays to store the x and y coordinate data wiiPitch_coords = new float[NUM_COORDS]; wiiRoll_coords = new float[NUM_COORDS]; wiiYaw_coords = new float[NUM_COORDS]; nunPitch_coords = new float[NUM_COORDS]; nunRoll_coords = new float[NUM_COORDS]; nunYaw_coords = new float[NUM_COORDS]; // Initialise the index into the coordinate arrays coords_index = 0; // Drawing Wii values wiiPitchDraw = new float[width]; wiiRollDraw = new float[width]; wiiYawDraw = new float[width]; nunPitchDraw = new float[width]; nunRollDraw = new float[width]; nunYawDraw = new float[width]; } float wiiPitch; float wiiRoll; float wiiYaw; float nunPitch; float nunRoll; float nunYaw; void wiiPry(float _wiiPitch, float _wiiRoll, float _wiiYaw, float _wiiAccel) { wiiPitch = _wiiPitch; wiiRoll = _wiiRoll; wiiYaw = _wiiYaw; } void nunPry(float _nunPitch, float _nunRoll, float _nunYaw, float _nunAccel) { nunPitch = _nunPitch; nunRoll = _nunRoll; nunYaw = _nunYaw; } void draw() { background(255); noStroke(); fill(164, 199, 209); rect(0, 0, width, height/3); fill(190, 207, 211); rect(0, height/3, width, height-height/3); fill(164, 199, 209); rect(0, height-height/3, width, height); // Drawing Wii data movement /////////////// Wii stroke(19, 131, 63); // Wii Pitch for (int i = wiiPitchDraw.length-1; i > 0; i--) { wiiPitchDraw[i] = wiiPitchDraw[i-1]; } wiiPitchDraw[0] = map(wiiPitch, 0.0, 1.0, height/3, 0.0); for(int i = 1; i < wiiPitchDraw.length; i++) { line(i, wiiPitchDraw[i], i-1, wiiPitchDraw[i-1]); } // Wii Roll for (int i = wiiRollDraw.length-1; i > 0; i--) { wiiRollDraw[i] = wiiRollDraw[i-1]; } wiiRollDraw[0] = map(wiiRoll, 0.0, 1.0, height-height/3, height/3); for(int i = 1; i < wiiRollDraw.length; i++) { line(i, wiiRollDraw[i], i-1, wiiRollDraw[i-1]); } // Wii Yaw for (int i = wiiYawDraw.length-1; i > 0; i--) { wiiYawDraw[i] = wiiYawDraw[i-1]; } wiiYawDraw[0] = map(wiiYaw, 0.0, 1.0, height, height-height/3); for(int i = 1; i < wiiYawDraw.length; i++) { line(i, wiiYawDraw[i], i-1, wiiYawDraw[i-1]); } /////////////// Nun stroke(227, 84, 27); // Nun Pitch for (int i = nunPitchDraw.length-1; i > 0; i--) { nunPitchDraw[i] = nunPitchDraw[i-1]; } nunPitchDraw[0] = map(nunPitch, 0.0, 1.0, height/3, 0.0); for(int i = 1; i < nunPitchDraw.length; i++) { line(i, nunPitchDraw[i], i-1, nunPitchDraw[i-1]); } // Nun Roll for (int i = nunRollDraw.length-1; i > 0; i--) { nunRollDraw[i] = nunRollDraw[i-1]; } nunRollDraw[0] = map(nunRoll, 0.0, 1.0, height-height/3, height/3); for(int i = 1; i < nunRollDraw.length; i++) { line(i, nunRollDraw[i], i-1, nunRollDraw[i-1]); } // Nun Yaw for (int i = nunYawDraw.length-1; i > 0; i--) { nunYawDraw[i] = nunYawDraw[i-1]; } nunYawDraw[0] = map(nunYaw, 0.0, 1.0, height, height-height/3); for(int i = 1; i < nunYawDraw.length; i++) { line(i, nunYawDraw[i], i-1, nunYawDraw[i-1]); } // Store the data in the arrays for the x and y coordinates wiiPitch_coords[coords_index] = wiiPitch*1000; wiiRoll_coords[coords_index] = wiiRoll*1000; wiiYaw_coords[coords_index] = wiiYaw*1000; nunPitch_coords[coords_index] = nunPitch*1000; nunRoll_coords[coords_index] = nunRoll*1000; nunYaw_coords[coords_index] = nunYaw*1000; // Increment the current index into the arrays ready to store next coords coords_index += 1; // If the arrays are full (coords_index >= NUM_COORDS) // Then write the coords to a file with a unique filename (based on the date and time) if (coords_index >= NUM_COORDS) { saveData(); } } void keyReleased() { // If the spacebar has been pressed (keyPressed && key == ' ') // Then write the coords to a file with a unique filename (based on the date and time) if (key == ' ') { saveData(); } } void saveData() { println("coords_index = " + coords_index); // Create a PrintWriter to handle the actual writing to the file PrintWriter output = createWriter("positions_"+unique()+".txt"); // For each recorded coordinate pair write a line in the file for (int i = 0; i < coords_index; i++) { output.println("wiiPitch: "+wiiPitch_coords[i]+" || wiiRoll: "+wiiRoll_coords[i]+" || wiiYaw: "+wiiYaw_coords[i]+ " || nunPitch: "+nunPitch_coords[i]+" || nunRoll: "+nunRoll_coords[i]+" || nunYaw: "+nunYaw_coords[i]); } // Flush and close the file to make sure the data is safe output.flush(); output.close(); // Reset the index into the arrays to the beginning coords_index = 0; // Clear the display to indicate that the data has been flushed background(255); } String unique() { return "date"+year()+""+month()+""+day()+"time"+hour()+""+minute(); }