// class and function to listen to OSC and sending the data to Scratch class MyOSCListener { int port; OscP5 osc; String receivedOSCPattern; String value; float f; MyOSCListener(int thePort) { port = thePort; osc = new OscP5(this,port); } public void stop() { osc.stop(); } public void oscEvent(OscMessage theOscMessage) { receivedOSCPattern = theOscMessage.addrPattern(); // store address in oscaddressesHash addOSCaddress(receivedOSCPattern); // check if data has to be send to Scratch if((Integer) oscaddressesHash.get(receivedOSCPattern) == 1) { typetag = theOscMessage.typetag(); if(typetag.length()==0) { // only OSC address without a value. // send as broadcast broadcastDataToScratch.put(receivedOSCPattern ,new String(receivedOSCPattern)); } // display each data item in the monitor. for(int i=0;i1.0) value = nfc(f,2); // assume bigger value so 2 decimal precision is sufficient else value = nfc(f,5); } else if(tag == 's') // message has string { value = theOscMessage.get(i).stringValue(); } else { // see documentation for ohter types: // http://www.sojamo.de/libraries/oscP5/reference/oscP5/OscArgument.html // print the type tag but not the value //monitor1 = monitor1 + " type:" + typetag; } // there can be messages with more numbers for example 3 floats: fff // scratch can only receive one string per address at a time. // this: /pose/orientation -0.009 (float) 0.054 (float) 0.023 (float) // becomes: /pose/orientation1 -0.009 // /pose/orientation2 0.054 // /pose/orientation3 0.023 // use a Hashmap because Scratch cannot cope with to much data. Data that isn't send to Scratch yet is // simply overwritten when there is new data. // it prevents that data of one address is never send (what can be the case with an Arraylist where you'll throw data out. if(typetag.length()>1) sensorDataToScratch.put(receivedOSCPattern+"_"+str(i+1),new String(receivedOSCPattern+"_"+str(i+1)+" "+value) ); else sensorDataToScratch.put(receivedOSCPattern ,new String(receivedOSCPattern +" "+value) ); } } } } // send data to Scratch void sendDataToScratch(String dataOut) { byte [] sizeBytes = {0, 0, 0, 0}; sizeBytes[3] = byte(dataOut.length()); for (int i=0; i<4; i++) { scratchClient.write(sizeBytes[i]); } scratchClient.write(dataOut); // monitor what is send monitorList1.add((String) "- "+dataOut); }