We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using the mindset library; that works perfect; but I am also working with a lot of graphics in the main thread, by using P5. Now, I am trying to log to a text file, the brainwave data, at a higher frequency than the drawing loop can handle, because the graphics are heavy. I created a new thread that can buffer and write data to the disc at the desired frequency, but now I need to poll the headset at this frequency; or otherwise I will just get duplicate points along the time.
When you call an instance of the mindset, you do so by
mindSet = new MindSet(this, "/dev/cu.MindWaveMobile-DevA");
I observed that If I place this statement in the main thread, say, the setup(); it will trigger other functions such as
` void meditationEvent(int meditationLevel) {}
`
But these methods will run at main thread timing, and therefore I will not get a higher sampling rate than the framerate (is that true?) or at least, will share the thread resources with the drawing functions, which is not ok.
Then, I tried to call the mindSet = new MindSet(this, "/dev/cu.MindWaveMobile-DevA"); inside an init function of the thread
class Logger extends Thread {
(...)
public void init(){
mindSet = new MindSet(this, "/dev/cu.MindWaveMobile-DevA");
}
(...)
}
In this case, there will be a compiling error, telling me that "The constructor diablu.processing.mindset.MindSet(Logger, String) does not exist. This is because the thread is a class named Logger, which is obviously an unexpected class for the library.
So there are many ways out:
Lots of thanks for at least reading!
Answers
I'm not sure I follow this logic. Just because the
meditationEvent()
function is called on the main thread doesn't mean that it's called with the same timing as thedraw()
function. For example, you can have severalmouseMoved()
functions between two calls to thedraw()
function. Test this out by changing the frame rate to 1 and seeing how manymeditationEvent()
calls you get!It looks like the library needs an instance of
PApplet
which it then (probably using reflection) finds themeditationEvent()
function to call. I get that you're trying to get calls to themeditationEvent()
function on another thread, but the way you're going about it (extendingThread
and passing that into the library) doesn't make a ton of sense. Even if that would compile, nothing is telling the library to put the call on thatThread
!The first thing you need to do is confirm that you actually need threading. I would bet that you don't. You're assuming that
meditationEvent()
will be limited by the frame rate, and I doubt that's the case.But if it is the case, then what you want to do is this: first, set up a normal single-threaded sketch that successfully calls the
meditationEvent()
on the main thread.Then, create a data structure that you add the events to. You do this addition from the main thread.
Then, create a new
Thread
that processes the events in that data structure. There are about a million and one ways to do this, and Google is your friend, but you might start out looking at theBlockingQueue
interface and the classes that implement it.Thanks a lot, Kevin!.
You are right that the function calls will not be limited to the frame rate. It was not a smart thought after havin used several tuio points for each frame in other application. You seem to know more about the java side, which is what I am lacking. For instance, I have no idea what effect has in the library, when you provide a
this
as parameter. I guess it searches for the callbacks in there. If the library object is instanced in the default voidsetup()
; does it mean that it will share resources with the drawing thread?;also, if the functions that are going to be called by the library (such as
meditationEvent()
) are declared otside, in the main processing sketch, and not inside a thread class; but called by a thread; will them be sharing resources with the processing sketch, or with the thread?It was helpful to see that the required parameter should be of the PApplet type. If I somehow find the way to get the PApplet of the thread (is that even a thing?), I would be able to provide it as parameter. But well, I trust you if you say that it doesn't make that much sense.
There is nothing too magical going on. The
this
keyword just references the current instance. You know how some functions require objects as a parameter? (Like theimage()
function requiring an instance ofPImage
as a parameter.) Well, some functions require an instance ofPApplet
. Your sketch is aPApplet
(in Java terms, your sketch is a class that extendsPApplet
), and you can reference it using thethis
keyword.(This glosses over the fact that the library probably uses reflection to find the functions which is a little magical, but that's outside what we're talking about.)
I'm not sure what you mean. Which resources are you talking about specifically?
Again, I'm not sure what resources you're talking about here. But the short answer is that any variables you have declared in your sketch will be shared by any Threads you have running inside your sketch.
No, that's not a thing. You only have one
PApplet
, and it's the "parent" of everything else. It is your sketch. Try looking at the generated Java to get a better idea of what I'm talking about.But I will say that you seem to be focusing on Threads a little too much. I thought you said the timing of the library events wasn't limited by the framerate? Since that's the case, why are you still trying to use threading?
Threading might help with a few very specific things, but it isn't a catch-all solution. Think about it this way: you can't control which Thread the events are fired on. You don't want to. So trying to pass a thread into the library is a bit of a wild goose chase.
The events will be fired on the main drawing/event thread. That's what you want. From there you could use another thread to process the event, but honestly that's probably overkill for your purposes.