0

This is my complete code, that does not work as expected. The issue is if f_trigger def while loop runs, then f_reset is deaf - not listening anymore and not triggered. therefore alarm_cancel is never turned to True. Is this something documented, that if loop runs in one callback function, then another callback is ignored?

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
_gpin = 11
_gpin_reset = 12
alarm_cancel = False
def f_trigger(channel):
 global alarm_cancel
 while alarm_cancel == False:
 print('alarm cancel status: %s' % (alarm_cancel))
 time.sleep(.3)
 alarm_cancel = False
def f_reset(channel):
 global alarm_cancel
 print('f_reset called')
 alarm_cancel = True
# Define INPUTs
GPIO.setup(
 _gpin,
 GPIO.IN,
 pull_up_down=GPIO.PUD_UP
) # input #1
# Trigger #1
GPIO.add_event_detect(
 _gpin, GPIO.BOTH, callback=f_trigger, bouncetime=100
)
# Define INPUTs
GPIO.setup(
 _gpin_reset,
 GPIO.IN,
 pull_up_down=GPIO.PUD_UP
) # input #2
# Trigger #2
GPIO.add_event_detect(
 _gpin_reset, GPIO.BOTH, callback=f_reset, bouncetime=100
)
while True:
 print('main thread loop running')
 time.sleep(60)
asked Apr 13, 2021 at 16:34

2 Answers 2

3

Callbacks are emitted by one thread. Each callback will therefore run to completion before the next is called.

This is true for RPi.GPIO and all the other Pi GPIO Python and C modules that I am aware of.

Treat callbacks as interrupts. Do the minimum possible before returning, e.g. set a flag to be picked up in the main thread.

answered Apr 13, 2021 at 16:59
1
  • Thanks for explanation, so i need to make different implementation Commented Apr 13, 2021 at 17:11
0

Another option that will make multiple callback work together is to use threading like in example below and run the callback function with loop as specific thread, then if another callback function sets flag to True, this thread will still be able to read the alarm_cancel flag:

import threading
def f_trigger(channel):
 threading.Thread(name='_thread_fireup_alarm', target=fireup_alarm, args=(channel,)).start()
def fireup_alarm(channel):
 global alarm_cancel
 while alarm_cancel == False:
 print('alarm cancel status: %s' % (alarm_cancel))
 time.sleep(1)
 alarm_cancel = False
answered Apr 14, 2021 at 13:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.