Think the callback method is not always working as expected. In case multiple callbacks are attached to one pin, the code is not working as expected.
What I did: connected an GPIO output(BCM 21) to another input(BCM 20) by using a wire. The code toggled the output in a slow rate in a thread.
Followed
https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/, "If you wanted more than one callback function" to setup the callbacks.
Added a small bouncetime to the event detect
GPIO.add_event_detect(20, GPIO.RISING, bouncetime=100 )
Result is same when bouncetime is not used. The GPIO output does not bounce.
TEST_0: Attached a single callback to the pin 20 "callback_with_file".
Code: Select all
0.6.5
TEST_0
toggle HIGH 1561105416.9977474
callback_with_file START 1561105416.9987712
callback_with_file END 1561105417.0036728
toggle HIGH 1561105421.0058208
callback_with_file START 1561105421.0060265
callback_with_file END 1561105421.0103037
Works as expected. The callback is called once on each low-high-edge, the callback is writing files.
TEST_1: Attached single different callback to pin20, no files written "callback".
Code: Select all
0.6.5
TEST_1
toggle HIGH 1561105550.2217839
callback START 1561105550.2221496
callback END 1561105550.2276273
toggle HIGH 1561105554.2293239
callback START 1561105554.2294986
callback END 1561105554.2343802
Same result as TEST_0. Obviously writing files in a callback does not add strange behavior.
TEST_2: Add two callback to one pin:
Code: Select all
if test == "TEST_2":
GPIO.add_event_callback(20, callback )
GPIO.add_event_callback(20, callback2 )
Result:
Code: Select all
0.6.5
TEST_2
toggle HIGH 1561105664.1932065
callback START 1561105664.1945903
callback END 1561105664.199675
callback2 START 1561105664.199804
callback START 1561105664.1998546
callback END 1561105664.2134457
callback2 START 1561105664.2137964
toggle HIGH 1561105668.2016718
callback START 1561105668.202027
callback END 1561105668.2132208
callback2 START 1561105668.213486
callback START 1561105668.2136009
callback END 1561105668.2247682
callback2 START 1561105668.2250352
Both "callback" and "callback2" are called twice on each positive edge.
This result is strange.
Here the source of the test case. The different tests are selected by a variable. So the process was restarted each time.
Code: Select all
import RPi.GPIO as GPIO
import time
import threading
test = "TEST_0"
# test = "TEST_1"
# test = "TEST_2"
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.OUT)
GPIO.output(21, GPIO.LOW)
print( GPIO.VERSION)
print(test)
run = True
def sleep(delay):
""" delay function which can be stopped in small time interval"""
tx = time.time() + delay
while run:
time.sleep(0.1)
if tx < time.time():
break
def toggle():
"""toggle output pin. Interval is 2 sec low, 2 sec high"""
while run:
GPIO.output(21, GPIO.LOW)
sleep(2)
if not run:
break
print()
print("toggle", "HIGH", time.time())
GPIO.output(21, GPIO.HIGH)
sleep(2)
tt = threading.Thread(target=toggle)
tt.start()
def callback2(channel):
print("callback2", "START", time.time())
def callback_with_file(channel):
print("callback_with_file", "START", time.time())
with open("x.dat", "wt") as f:
for _ in range(1000):
f.write("hallo")
with open("y.dat", "wt") as f:
for _ in range(1000):
f.write("hallo")
print("callback_with_file", "END", time.time())
def callback(channel):
print("callback", "START", time.time())
with open("x.dat", "wt") as f:
for _ in range(1000):
f.write("hallo")
with open("y.dat", "wt") as f:
for _ in range(1000):
f.write("hallo")
print("callback", "END", time.time())
GPIO.add_event_detect(20, GPIO.RISING, bouncetime=100 )
if test == "TEST_0":
GPIO.add_event_callback(20, callback_with_file )
if test == "TEST_1":
GPIO.add_event_callback(20, callback )
if test == "TEST_2":
GPIO.add_event_callback(20, callback )
GPIO.add_event_callback(20, callback2 )
event = threading.Event()
try:
event.wait()
except KeyboardInterrupt:
pass
run = False
GPIO.cleanup()
print("finished")