// thread for parsing Scratch data // sending data from Scratch as OSC // configuring the bridge from Scratch class SimpleThread extends Thread { // from: http://wiki.processing.org/w/Threading boolean running; // Is the thread running? Yes or no? int wait; // How many milliseconds should we wait in between executions? String id; // Thread name int count; // counter // Constructor, create the thread // It is not running by default SimpleThread (int w, String s) { wait = w; running = false; id = s; count = 0; } // Overriding "start()" void start () { // Set running equal to true running = true; // Print messages println("Starting thread (will execute every " + wait + " milliseconds.)"); // Do whatever start does in Thread, don't forget this! super.start(); } // We must implement run, this gets triggered by start() void run () { while (running) { parseScratchData(); try { sleep((long)(wait)); } catch (Exception e) { } } } void parseScratchData() { if (scratchClient.available()>0) { int expectedStringLength; String receivedString = scratchClient.readString(); // we split (Java split, Processing split doesn't work with regex) on first 3 null bytes. // 4th byte is the length of the expected string String[] m = receivedString.split("\\x00\\x00\\x00"); if(m!=null) { for (int i = 1; i < m.length; i++) { expectedStringLength = int(m[i].charAt(0)); // cut the first character m[i] = m[i].substring(1, m[i].length()); // remove 1st character if((m[i].length())==expectedStringLength) { String[][] incomingMatch = matchAll(m[i], "\"[^\"]+\"|-?[0-9]+[.][0-9]+|-?[0-9]+"); if (incomingMatch != null) { //println("after match: "+m[i]); String[] checkIsSensor = match(m[i],"sensor"); if(checkIsSensor != null) parseSensorData(incomingMatch); else parseBroadcastData(incomingMatch); } } } } scratchClient.clear(); } } void parseSensorData(String[][] incomingMatch) { //println("sensor"); String label; for(int i=0;i