Hello,
I'm trying to implement a sensor based OSC controller. So far I've set up a handler for accelerometer events and I can see from the console output that accelerometer data is being captured properly in an onAccelerometerEvent handler. However, I haven't been able to send OSC directly from within the handler. I've seen e.g. in this example that, even though all necessary data is available within the handler, actual sending is done in the draw function. I've been able to do it like that in my own sketch as well but preferably I'd like to do it in the handler and not be dependant on the framerate at which the draw function executes. Why is it not possible to send OSC from within a sensor event handler? (or, if it is possible, what am I doing wrong?)
example code (I've moved over to AndroidStudio, so the below is translated back to processing - hope my code makes sense...)
import ketai.sensors.*;
import oscP5.*;
OscP5 oscP5;
NetAddress broadcastLoc;
OscMessage oscAcc;
KetaiSensor sensor;
void setup() {
oscP5 = new OscP5(this, 32000);
broadcastLoc = new NetAddress("192.168.1.5", 57120);
sensor = new KetaiSensor(this);
}
void draw() {
}
void onAccelerometerEvent(float x, float y, float z, long time, int accuracy) {
println("x: " + x + "\ny: " + y + "\nz: " + z + "\naccuracy: " + accuracy);
oscAcc = new OscMessage("/acc");
oscAcc.add(x);
oscAcc.add(y);
oscAcc.add(z);
println("broadcastLoc: " + broadcastLoc + "\nOSC msg: " + oscAcc + "\noscP5: " + oscP5);
try {
oscP5.send(oscAcc, broadcastLoc);
} catch(Exception e) {
// prints just "sending failed: null"
println("sending failed: " + e.getMessage());
}
}