Quantcast
Channel: Projects – KwartzLab Makerspace
Viewing all articles
Browse latest Browse all 18

Arduino-Compatible Multi-Threading Library v0.5 Released

$
0
0

It’s been a while since I’ve touched this project, but I’ve decided to re-visit the multi-threading library I wrote for the Arduino (largely because of the amount of consistent traffic those particular blog posts have generated… it’s satisfying to know that someone other than me seems to appreciate my work). Although, I’ve made some minor edits to the internals of the Thread class, the most important update has been the addition of the EventHandler class. As it’s name would suggest, it provides the framework for basic event-handling.

Those who’ve been following the evolution of this library, will probably be familiar with the SwitchInput class. It was intended (among other things) to provide functions that would only be called when a physical switch was closed or opened. The EventHandler class is similar, except that the conditions upon which the event is triggered are configurable and the function isn’t limited to running a single loop when the event occurs. I actually considered re-writing the SwitchInput class to be more like this, but given that it might break programs that people had written with it, I opted not to. Perhaps I’ll create a replacement in the future. Like SwitchInput, EventHandler is drived from the Thread class, with the loop() function overwritten. In its place are two protected virtual functions: condition() and on_event().

The condition() Function:

This is the function that is used to determine when the event is to be triggered. Basically, while the EventHandler object is idle, this function will be called every time the object is invoked by a ThreadList object. If it returns true, the event will be triggered, but if it returns false, the handler will remain in an idle state (this is one of those cases where I wish C++ supported lambdas (but that may be the LISP programming I’ve been doing talking (it’s hard to tell, really))).

The on_event() Function:

Once the event has been triggered, instead of calling the condition() function, the on_event() function will be called in its place. This can be thought of as a conditional loop() function. The difference being that when it returns false the handler will return to an idle state and wait for the condition() function to return true again rather than destroying itself.

A Note about kill_flag:

With a regular Thread object, the loop() function is expected to watch the kill_flag value. When it is set to true, the loop() function is is expected to terminate gracefully. As a help, this functionality is already built into the EventHandler::loop() function. If the handler is in an idle state, it will automatically terminate itself on its next call, however it may be advisable to have the on_event() function watch for this value so that it can facilitate a graceful termination (although, this is not strictly required as the handler will be terminated when the function returns false).

While I don’t feel I’ve done anything particularly ground-breaking with this new release, I hope I’ve created an adequate framework for other Arduino developers who don’t want to re-invent the wheel. As usual, I’ve made all the code and documentation available for this version.

Happy Hacking!


Viewing all articles
Browse latest Browse all 18

Trending Articles