4

I'm getting stuck with the wait_for_edge function in the Python package RPi. I added multiple event detections for falling gpio's and everything works fine as long as the callback functions contain not a comand like

 os.system("mpc pause") 

Then the script crashes with the error message: "RunetimeError: Error #5 waiting for edge" Does anybody know, what this error message wants to say? Or where I can look up things like this?

To be specific this code works:

def next(channel):
    print "In next"

GPIO.add_event_detect(buttonnext,GP.FALLING,callback=next,bouncetime=200)

os.system("mpc play")

try:
    GPIO.wait_for_edge(buttonstop, GP.FALLING)
    os.system("mpc stop")
except KeyboardInterrupt:
    GPIO.cleanup()
    os.system("mpc stop")
GPIO.cleanup()

But this code doesnt:

def next(channel):
    print "In next"
    os.system("mpc next")

GPIO.add_event_detect(buttonnext,GP.FALLING,callback=next,bouncetime=200)

os.system("mpc play")
try:
    GPIO.wait_for_edge(buttonstop, GP.FALLING)
    os.system("mpc stop")
except KeyboardInterrupt:
    GPIO.cleanup()
    os.system("mpc stop")
GPIO.cleanup()

It crashes after pushing the button connected to the port buttonstop.

Thorsten
  • 58
  • 5
  • This may sound stupid, but try restarting the Pi. Several times the GPIO pins have had weird bugs like this for me and a restart fixed it – DeadChex Feb 25 '14 at 01:09
  • Unfortunately a restart doesn't help. I've noticed that the os comand is done before the script crashes. – Thorsten Feb 25 '14 at 13:41

1 Answers1

1

Super hacky, but right after the os.system call, if you unbind the event and then rebind it right after, it seems to work.

def next(channel):
    print "In next"
    os.system("mpc next")
    GPIO.remove_event_detect(buttonnext)
    GPIO.add_event_detect(buttonnext,GP.FALLING,callback=next,bouncetime=200)
Hal
  • 1,173
  • 12
  • 11
  • I would hazard to say that the GPIO driver itself is buggy. Maybe someone more versed in the Python GPIO driver can say for sure but obviously, it's not behaving properly between the system and python. I had to find some type of a solution for my GPIO battery monitor, and I came across this exact problem. After googling around, and seeing your post, I knew it wasn't just me. So, kept trying and I (dirty) solved it like this. Hopefully, we'll get a proper fix someday though. – Hal Jun 25 '16 at 21:56
  • From my reading, I get the vague impression that call backs are executed on a different thread, a call back thread, so when a call back is triggered the call back function is executed on a thread different from the main thread. And this call back thread is used for all call backs which implies only one call back at a time can be executed which in turn implies that the processing in a call back should be minimal. It seems to me that `os.system("mpc next")` isn't minimal and this is part of the OP's problem. – Richard Chambers Feb 12 '21 at 01:48