Skip to content
Jim Lee edited this page Oct 1, 2020 · 15 revisions

idler

Abstract:

Many times when coding you want something to "Just run by itself". Have an LED blink without having to deal with it, keep a buffer full from a file, run an animation, things of that nature. This is where idlers come into play. Classes that derive from idler have the ability to run in the "background". At least enough like running in the background that it frees the program writer up for thinking about other things. And that's really all that needs to be accomplished.

When a class inherits form idler, it gets a virtual idle() method that it can fill in with what it want's to do during "idle time". Its like getting it's own little piece of the main loop() to use for itself.

There are a couple of caveats to using idlers. First is to add idle(); as the first call on your main loop() method. This is the call that allows all the idlers to function. The second? The second is to avoid using delay() like the plague! If you call delay() anywhere, you stop EVERYTHING in the entire program! Don't use it. What to do? No worries. Idler gives you a sleep() method you can call in your main loop() that acts like delay(). Except it leaves everything else running. Won't have any effect when called in while running in the background.

Constructors.

idler(void);
Idler constructor, no parameters. You don't actually use this except to mix it into your classes to let them run in the background.

void hookup(void);
Links your idler class into the idler queue. This would be something wonderful to put into the idler constructor and not bother the end user with, but.. It relies on the idlers globals and things being already setup. And, there is no way to make this happen after the globals are set up, unless the code is already running. So you need to call this in your class using something like a begin() or anything else that's called -after- the constructor.

I know, lots of words but the rule is : If a idler class -can- be created as a global, it can NOT call hookup() during its constructor.

void idle(void);
This is the virtual method you override in your classes to do things while running in the background. Think of it as your own private loop() routine. The watchword here is to be quick, do what you need to do then exit. No calling of delay()!

void sleep(int ms);
This is the method you can use in your main loop() instead of delay(). Unlike delay(), everything stays running. Its just the main loop() that holds at a point until the time has expired.

Basically that's all there is to using idlers. Place the idle() call in your main loop() method, inherit idler in your classes and suddenly you'll find you have all sorts of things going on at the same time. Its actually really fun!

idlers - with an s

Abstract:

idler & idlers The first one, idler, is the one that is inherited for building new classes. To use the idler functionality, you only need know about the first one. The second one, ilders, is the one that does the other half of the magic behind the scenes. All of the dirty work, none of the glamor. You can skip this part if you want.

What idlers does. Idlers manages a dynamic list of the objects that inherit idler and have called, during runtime, their hookup() methods. Yes, it uses dynamically allocated and memory to do this. Get over it!

When the function idle() is called in the main loop() method, idlers runs down its list of idler objects and calls each ones idle() method. And that's it. Its amazing to me how something so simple can end up being so incredibly useful. I guess early man probably thought the same thing about knifes.

Constructors:

idlers(void);
No parameters, this is not called by the user. It is automatically set up as a global.

Other methods:

void idle(void);

No parameters this is indirectly called by the user through the idle() function called in the main loop() method.

There are three globals that are set up that work with idlers.

extern idlers theIdlers;
This is the global idlers object. It is the manager for the list of idler objects in the program.

extern void idle(void);
The global function idle() that the user calls in their main loop();

extern bool idling;
A boolean that is set to true while the idlers are being called. Sometimes objects would like to know if they are running in "loop time" or "idle time".

NOTES on idlers

idler was originally intended for doing simple things like blinking an LED without delay kind of stuff. With the incorporation of displays and touch screens, it ended up taking over about 95% of everything going on in the code that I develop. All of the screen management and UI is run in the background by idler. Filling data buffers from SD drives for running .mp3 hardware, managing communication between processors.. I'm so proud of my little idler class, all grown up!

And that's about it for idlers.

Clone this wiki locally