I tried to do it by using saveStrings() in one sketch and loadStrings() in the other.
writer.pde
String[] Pipe = new String[2]; // Declare, create
void setup() {
size(400, 400);
noStroke();
}
void draw() {
background(126);
ellipse(mouseX, mouseY, 30, 30);
Pipe[0] = nf(mouseX, 3);
Pipe[1] = nf(mouseY, 3);
saveStrings("../fifo.txt", Pipe);
}
reader.pde
String[] vals = loadStrings("../fifo.txt");
//void draw() {
println("There are " + vals.length + " lines");
for (int i = 0; i < vals.length; i++) {
println(vals[i]);
}
//}
This almost works. loadStrings() works if it is reading the values once but if I put the same code into draw() the error is java.lang.reflect.InvocationTargetException
Help please!
Hi,
A quick note on your writer.pde
file :
- If you call
saveStrings()
in the draw function, then it’s going to replace and write a new file called fifo.txt
with the new mouseX
and mouseY
at the same location on every frame (roughly 60 times per second). I assume it’s not what you want, you need to save all the mouse position for a certain time and at the end save the strings to a text file.
For your reader.pde
code, are you sure that your text file is in the right folder? You shouldn’t save it outside the sketch folder (with “…/”) because saveStrings()
does it already.
Yes, I am sure the text file is in the right location. reader.pde does not send any information nor does it save a text file. It only reads and uses the values it reads.
Thanks @josephh. What you suggest would work but it is not what I want. I am using the mouse locations to represent variables that continuously change value (non static?). The ellipse in the minimal example (reader.pde) should mirror the movements of the ellipse in writer.pde.
Oh ok, you need to use ChildApplet
to communicate between different windows : check the example by going here in the Processing IDE : File -> Examples -> Demos -> Tests -> MultipleWindows
Before I look at ChildApplet
I have found a working solution!
I split this line: String[] vals = loadStrings("../fifo.txt");
into two parts (see below). The changing values are loaded in draw() with vals = loadStrings("../fifo.txt");
String[] vals;
void setup() {
}
void draw() {
vals = loadStrings("../fifo.txt");
println("There are " + vals.length + " lines");
for (int i = 0; i < vals.length; i++) {
println(vals[i]);
}
}
1 Like
I don’t know what kind of disk your PC has, but writing a new file at 60 Hz looks like a severe test, risk of destroying the disk after a while, and I wouldn’t do it. Suggest you install the UDP library (Sketch, Add Library, UDP… Install) then try the UDP examples. I’ve used these examples as teaching/entertainment where the young student is moving the mouse on one window, and it’s drawing on another (on same or other PC).
1 Like
Ah ha, good point. I might have misled you with the mouse positions. I used mouseX
and mouseY
in my example because it is reproducible. The information I am actually going to pass from sketch to sketch is some Brightness levels from an image and I can do this at frameRate(1).
To represent this, I have written slowWrite.pde and slowRead.pde (below). The one remaining problem is sometimes there is nothing in fifo.txt and so I get the error report ArrayIndexOutOfBoundsException: 0
Is there a way to check that the file is present and correct before trying to read it?
pseudocode:
if (something !=null) { // what is the something?
println(Val[0]);
} else {
//return null; // what is Processing code here?
}
slowWrite.pde
String[] Pipe = new String[1]; // Declare, create
void setup() {
frameRate(1);
}
void draw() {
Pipe[0] = str(minute());
saveStrings("../fifo.txt", Pipe);
}
and slowRead.pde
String[] Val = new String[1]; // Declare, create
void setup() {
frameRate(1);
}
void draw() {
Val = loadStrings("../fifo.txt");
println(Val[0]);
}
Hi,
Your issue is a famous one in computer science : https://en.wikipedia.org/wiki/Readers–writers_problem
And using semaphores can help : https://www.geeksforgeeks.org/semaphore-in-java/
But in this case, why are you using two running sketches instead of one? It would be much simpler
Interesting read! Thank you.
Why? One sketch is all about image analysis and the colorMode is HSB, the other sketch uses some results from the first sketch and is all about creating new images and is in colorMode LCH. So, one sketch makes it simpler for the variable yes, but more difficult in every other way. Also, over and above that, I want to investigate, learn and understand.
I like the semaphore very much. But what causes the flag to go up (or down)?
if (something !=null) { // what is the something? // flag down, 1
println(Val[0]);
} else { // flag up, 0
//return null; // what is Processing code here?
}