Thanks for the tip, I suspected as much.
I've implemented a version with threads (mostly by poking around the shiffman tutorial and java docs) and it works very well except for one (potentially deal-breaking) issue. The values make it python okay, but with a very consistent half-second or so lag. Here is the code in question...
Code:
import java.io.*;
DataThread ping_thread;
float time = 0;
int min = 160;
int max = 170;
int index = 0;
float[] numbers;
void setup() {
size(640,480);
frameRate(33);
numbers = new float[100];
ping_thread = new DataThread();
ping_thread.start();
background(255);
fill(0,40);
stroke(0,40);
smooth();
}
void draw() {
background(255);
time += 0.1/20;
stroke(0,30);
line(0,height/2-height/8,width,height/2-height/8);
line(0,height/2+height/8,width,height/2+height/8);
line(0,height/2+height-height/6,width,height/2+height-height/6);
numbers[index % numbers.length] = ping_thread.getLatest(0);
//println(ping_thread.getLatest(0));
beginShape();
for (int i = 0; i < numbers.length + 1; i++) {
float value = numbers[(index+i+1)%numbers.length];
float spacing = (float) width / (float) numbers.length;
value = map(value,5000,pow(2,16),height/2+height/8,height/2-height/8);
noFill();
curveVertex(spacing * i, value);
//ellipse(spacing * i, value, 20, 20);
stroke(0,180);
}
endShape();
index = (index + 1) % numbers.length;
}
/* Classes */
public class DataThread extends Thread {
private boolean running; // Is the thread running? Yes or no?
private boolean fresh; // Is there fresh data to be polled?
int[] labjack_fields = new int[16];
// Constructor, create the thread
// It is not running by default
public DataThread() {
running = false;
}
public void start()
{
// Set running equal to true
running = true;
fresh = false;
// Print messages
System.out.println("Starting data thread...");
// Do whatever start does in Thread, don't forget this!
super.start();
}
public void run () {
try {
String line;
OutputStream stdin = null;
Process p = Runtime.getRuntime().exec("python C:/code/grabber.py");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
String[] m = split(line, " ");
labjack_fields[int(m[0])] = int(m[1]);
Thread.currentThread().sleep(1); # Mysteriously Important Sleep Line
}
} catch (Exception err) {
err.printStackTrace();
}
}
public int getLatest(int index) {
return labjack_fields[index];
}
}
The python script's output is along the lines of:
Code:
0 193411
1 56782
2 343434
3 893
0 342
1 1111
2
With on the order of a few hundred lines being output per second. The line commented "mysteriously important sleep line" was added on a spooky hunch after initial tests showed that the strip chart was only being updated every half-second or so with the last sample received from the python script... it cleared that issue up (now every sample is received beautifully) but a delay occurs somewhere between the python script and the screen.
What can I do to get rid of the delay? Is the buffer simply too large? Can I change the size?
Thanks for your help.