What are some practical use cases for GPIO Interrupts?
Raspberry Pi Interrupts Python (GPIO Library) - Stack Overflow
c++ - How to handle GPIO interrupt-like handling in Linux userspace - Stack Overflow
GPIO interrupt example
Videos
I've only had the esp32-s2 for a little bit and I've been playing with different ways of performing time-critical tasks. One of these involved reading a HIGH value when a push button is held down on a GPIO pin to go into a GPIO interrupt ISR. However, as long as you are holding the pin down it consumes too much CPU time and then the main watchdog thread throws an exception and restarts the esp32. This is by design it seems so the programmer can make the appropriate changes since it is not very good to have one thing taking all resources.
The way I circumvented this issue was to just use a timer-based interrupt to periodically poll the pins state to see whether it's being held or not at a very slow frequency (1ms). This way it doesn't consume too many cycles and throw the restart error. But it's made me wonder what practical use cases could you use the strict "GPIO Interrupt" feature. Maybe like a hall sensor or something? What have you used this for in your projects?
Also: is there a way I can simply trigger the GPIO interrupt from a purely binary HIGH/LOW change of state? The problem with using the rising edge or falling edge modes is that the incoming signal from the button is pulsed so it triggers very rapidly and clutters up the call stack, consuming cpu with it.
There are several things going on in your code. I suspect that what's really causing the problem is that you need to exit the interrupt handler before another interrupt callback can be triggered...but there is also a confusing mix of callback-based handlers and the GPIO.event_detected method.
I think you can simplify things by performing less manipulation of your interrupt configuration. Just have a state variable that starts at 0, increment it to 1 on the first interrupt, so the next time the interrupt method is called you know it's the second interrupt. No need to try setting multiple handlers like that.
Keeping in mind that I don't actually know what you're trying to do...I imagine something like this:
import RPi.GPIO as GPIO
import time
state = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def interrupt_handler(channel):
global state
print("interrupt handler")
if channel == 19:
if state == 1:
state = 0
print("state reset by event on pin 19")
elif channel == 26:
if state == 0:
state = 1
print("state set by event on pin 26")
GPIO.add_event_detect(26, GPIO.RISING,
callback=interrupt_handler,
bouncetime=200)
GPIO.add_event_detect(19, GPIO.RISING,
callback=interrupt_handler,
bouncetime=200)
while (True):
time.sleep(0)
That sleep(0) just causes the process to idle there while waiting for switch interrupts. Since nothing in there programmatically ends the process, doing a Ctl-C will stop it.