ReDraw limiter (Ruby)
Posted: Mon Nov 26, 2012 10:06 pm
I have fond memories of the many days work, and dozenss of posts that infuzion and me put into the whole problem of getting GUI CPU load down in SM using 'heartbeat' timers and trigger trickery.
And then along comes this new fangled FS3 thingy, and now the same thing, and better, can be done in half an hour with just a few lines of code! Those guys at DSPr are such a bunch of spoilsports sometimes!

Rapidly changing input events will only update the output at the maximum 'frames per second' set in the dialogue (maxes out at 100fps as a 'safety measure'). There will also only be an output event if the input has actually changed.
This can really help to lower CPU load from screen redraws when you have GUI stuff driven from fast moving data.
The code is generic, and will work for pretty much any kind of data, so long as you set the in and out to be the same data type (there are exceptions - some obscure Ruby objects cannot be 'compared', in which case just delete the testing with the @last value). I've commented the code, so hopefully it will be easy to do a cut-and-paste job if putting it inside your code is more appropriate than a separate module.
There are even some advantages over the old green method...
- Independent frame rate for each event limiter, so different displays can have different degrees of animation 'smoothness'
- No complex reading of system time needed to get an accurate 'heartbeat'
- The first event after a long pause is output immediately without waiting for a 'tick' of the clock
- The clock goes to sleep if there is no input event between 'ticks' - so when input activity stops, there is no processing at all going on at all.
And then along comes this new fangled FS3 thingy, and now the same thing, and better, can be done in half an hour with just a few lines of code! Those guys at DSPr are such a bunch of spoilsports sometimes!
Rapidly changing input events will only update the output at the maximum 'frames per second' set in the dialogue (maxes out at 100fps as a 'safety measure'). There will also only be an output event if the input has actually changed.
This can really help to lower CPU load from screen redraws when you have GUI stuff driven from fast moving data.
The code is generic, and will work for pretty much any kind of data, so long as you set the in and out to be the same data type (there are exceptions - some obscure Ruby objects cannot be 'compared', in which case just delete the testing with the @last value). I've commented the code, so hopefully it will be easy to do a cut-and-paste job if putting it inside your code is more appropriate than a separate module.
There are even some advantages over the old green method...
- Independent frame rate for each event limiter, so different displays can have different degrees of animation 'smoothness'
- No complex reading of system time needed to get an accurate 'heartbeat'
- The first event after a long pause is output immediately without waiting for a 'tick' of the clock
- The clock goes to sleep if there is no input event between 'ticks' - so when input activity stops, there is no processing at all going on at all.