// Tai Chi Program // Copyright 2009 Les Hall // variables Event pop; class accelerometer { // variables 0 => int device; 0.95 => float tau; // low-pass filter constant 0.999 => float eternity; float in[3]; // the input values float f_in[3]; // the low-pass filtered input values float g_force; // the magnitude of the g force measured float avg_g_force; // long term average of the g force // initialize HID Hid hid; HidMsg hidmsg; if (!hid.openJoystick (device) ) { <<<"Accelerometer not found at device", device>>>; me.exit(); } else { <<<"Accelerometer '" + hid.name() + "' ready.", "">>>; } // time loop fun void timeLoop() { while (true) { hid => now; while (hid.recv (hidmsg) ) { if (hidmsg.isAxisMotion() ) { // record the input value for debug purposes hidmsg.axisPosition => in[hidmsg.which]; // low pass filter the inputs for (int i; i<3; i++) { tau * f_in[i] + (1.0 - tau) * in[i] => f_in[i]; } // calculate g force Math.sqrt(f_in[0]*f_in[0] + f_in[1]*f_in[1] + f_in[2]*f_in[2]) => g_force; eternity * avg_g_force + (1 - eternity) * g_force => avg_g_force; } } } } spork ~ timeLoop(); } accelerometer acc; SinOsc osc[3]; gain mult; 3 => mult.op; for (int i; i<3; i++) { osc[i] => mult; 1.0 => osc[i].gain; } mult => dac; // time loop while(true) { for (int i; i<3; i++) { 250 + 200 * acc.f_in[i] => osc[i].freq; } 100::ms => now; }