Implement an interrupt handler in C++, which is triggered by change of Input pin value?
The Expander Pi is an Analogue, Digital and RTC development Interface for the Raspberry Pi
10/05/2021
Posted by:
eirik
I have a working solution that polls the value on a pin connected to flow senser. An interrupt based implementation would be much nicer.
There are references to "interrupt" in the sdk, but I can not find a way to specify the callback.
Thank you for the support.
/eirik
11/05/2021
Posted by:
andrew
I have uploaded a new interrupt demo to GitHub at demo-io-interrupts.cpp which shows the basics of how interrupts work on the Expander Pi.
The interrupt hardware built into the MCP23017 controller allows you to trigger an output on the IA and IB pins when an event occurs on the input pins. You still need to regularly check the status of the interrupts to see if an event has occurred but it does allow you to check at less frequent intervals without missing out on a change on one of the input pins.
We have two interrupt tutorials for the IO Pi which use the same IO chip as the Expander Pi.
Introducing Interrupts
More Interrupts
Both tutorials use Python instead of C++ but I have written our libraries to be similar across the different programming languages so you should be able to use the tutorials to get a good idea of how interrupts work in C++.
For a more detailed explanation of how the interrupts work in the MCP23017, I would recommend reading the datasheet for the MCP23017. Page 27 has a description of the interrupt logic.
12/05/2021
Posted by:
eirik
thank you again for a quick response.
I wondered if there was a way to implement a solution where I pass a pointer to a callback on my side, to avoid the loop() with the sleeps()?
Is there a huge difference between polling for interrupts, and getting/comparing the value of a pin?
Will read the docs you referred to.
/eirik
12/05/2021
Posted by:
andrew
Our IO Pi interrupts tutorial explains how this works in Python. I have never worked with GPIO interrupts in C++ but I believe that the WiringPi library includes that functionality.
The main difference between polling for interrupts and getting the value of a pin is that with interrupts you can catch state changes that occur between polling. For example, if you are checking a pin every 100ms for a button press if the button is pressed for 50ms between each check you may miss the event. By polling for interrupts instead of the pin changes the interrupt will tell you that an event occurred since you last checked so you could poll the interrupt every second and not miss a button press. The downside of using interrupts this way is if two button presses occurred between polling checks the interrupt would only register one event.
Connecting the interrupt pins to the GPIO header and using an interrupt event on the Raspberry Pi side will get around this problem as your interrupt function would be called as soon as a button is pressed.
12/05/2021
Posted by:
eirik
thank you. Excellent explanation.
/eirik
Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.