Using interrupts to read the push-button state
The previous recipe showed how to read digital signals with the GPIO peripheral. However, the proposed solution is inefficient because the CPU wastes clock cycles waiting for the button to be pressed while it could perform other tasks in the meantime. Furthermore, this could be a situation where we would keep the CPU in low-power mode when there are no other tasks to run.
Therefore, this recipe will show you how to change the sketch developed in the previous recipe to read the push-button state efficiently using interrupts.
Getting ready
Let’s prepare this recipe by learning what an interrupt is and which Mbed OS API we can use to read the push-button efficiently.
Working with interrupts using the Mbed OS API
An interrupt is a signal that temporarily pauses the main program to address an event through a dedicated function known as an interrupt handler or interrupt service routine (ISR). When the ISR ends the...