Have you ever wanted to treat button presses in Arduino similar to other languages, where you get an event callback when the switch is pressed? Look no further, the IO abstraction library can do that with very little fuss. In fact it can also do the same for rotary encoders as well, treating them similar to how scroll bars work in desktop applications. To start we need to get the IoAbstraction library and open the buttonRotartyEncoder example.
In this tutorial for IoAbstraction’s taskManager I explain the differences between traditional loop based programming; which is very common on the Arduino platform and event based programming based on taskManager. Although event based programming looks slightly more complicated at first, as the sketch and surrounding code gets more complex, eventing will scale to that much easier. Eventing task frameworks make ongoing maintenance much easier. Example: Two LEDs blink at different rates.
Interrupt handling is generally an advanced topic, but this library provides a very simple way to handle interrupts. When you tell the library to handle an interrupt, the library registers the interrupt handler on your behalf, then when the condition is met the internal handler is triggered, it sets a flag to tell the library an interrupt has been raised. The task library treats this as the highest priority, and as soon as the current task is completed the interrupt code runs.
In this guide we assume that you are familiar with the API for scheduling tasks on task manager. Let’s first discuss what we consider an event to be, and what it means to be an interrupt or threaded event. Interrupt or threaded events are subject to external actors (such as threads or interrupts). In this case the event will invariably be triggered by an external event. However, task manager will still ask your event class instance if it is ready to be triggered yet by polling, but in this case you have two choices as listed out below:
In this guide we assume that you are familiar with the API for scheduling tasks on task manager. Let’s first discuss what we consider an event to be, and what it means to be a polled event. By polling we mean no external actors (such as threads or interrupts) are involved. Task manager will ask your event instance frequently if it is ready to be triggered yet, if it is not then task manager will take your instruction on how long to wait, and then call again.
Using TaskManager in your sketches TaskManager is a very simple co-operative coroutines / executor framework that allows work to be scheduled in an internal queue. Instead of writing code using delays, one simply adds jobs to be done at some point in the future. In addition to this, interrupt handling is also supported, such that the interrupt is “marshalled” and handled like a very high priority task to be processed immediately.
Using SwitchInput in your sketches SwitchInput is a button management library that treats button presses as events, similar to how web and UI applications are written. Rotary encoders are also supported. It works along with TaskManager documented on this page and provides: Buttons using either pull down or pull up logic connected directly or via any supported IO expander De-bouncing of events on all buttons to significantly reduce duplicate callbacks.
Troubleshooting IO issues using LoggingIoAbstraction Within the MockIoAbstraction.h header there is an implementation of BasicIoAbstraction that delegates through a logging layer. If you are having difficulty determining what your code is sending and receiving, this could be useful. Unit testing support within the library This library itself is quite well tested using AUnit, and further it makes unit testing your code easier. There is a MockIoAbstraction that provides very easy support for mocking out an IoAbstraction.