如何在Python中创建动态回调函数? (RPi 3 GPIO)

时间:2017-10-30 11:42:41

标签: python python-3.x interrupt raspberry-pi3 gpio

我在python3-6中为我的项目从头开始编程报警系统,使用树莓派3和一些报警组件(PIR,REED等) 为了从组件接收警报,我使用已经内置中断功能的RPi.GPIO库。 这是我到目前为止写的代码:更新31.10.2017 15:44

#Library Imports
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
time_stamp = time.time()

def PIRcallback(channel):
    print ('Alarm detected: ' + PIR.Name + '\nMotion!')
def REEDcallback(channel):
    print ('Alarm detected: ' + REED.Name + '\nDoor!')
def VIBRcallback(channel):
    print ('Alarm detected: ' + VIBR.Name + '\nWindow!')

class Alarm:
    def __init__(self, IO, pin, Name, cb_var):
        self.IO = IO
        self.pin = pin
        self.Name = Name
        self.callback_func = cb_var
        if (IO == 'input'):
            GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        elif (IO == 'output'):
            GPIO.setup(pin, GPIO.OUT)
        else:
            print ('Error: No input/output declared')
        self.addEvent('Up', 500)
    def addEvent(self, UpDown, btime):
        if (UpDown == 'Up'):
            UpDown = GPIO.RISING
        elif (UpDown == 'Down'):
            UpDown = GPIO.FALLING
        GPIO.add_event_detect(self.pin, UpDown, callback=self.cb, bouncetime=btime)
    def getPin(self):
        return self.pin
    def cb(self):
        global time_stamp
        time_now = time.time()
        print (time_now)
        if (time_now - time_stamp) >= 0.4:
            print (time_stamp)
            time_stamp = time_now
            self.callback_func()

#Testing Class Init     
REED = Alarm('input', 37, "REED", REEDcallback)
PIR = Alarm('input', 36, "PIR", PIRcallback)
VIBR = Alarm('input', 38, "VIBR", VIBRcallback)

try:
    while True:
        time.sleep(0.5)
except KeyboardInterrupt:
    print ("Terminating program...")
    time.sleep(1)
finally:
    GPIO.cleanup()
    print ("Cleaned GPIO!")

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在Python中,函数本身就是第一类变量,可以像任何其他变量一样传递。在这种情况下,您应该直接将回调函数传递给Alarm构造函数,而不是名称:

class Alarm:
    def __init__(self, IO, pin, callback):
        self.IO = IO
        self.pin = pin
        self.callback_func = callback
        if (IO == 'input'):
            GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        elif (IO == 'output'):
            GPIO.setup(pin, GPIO.OUT)
        else:
            print ('Error: No input/output declared')
        self.addEvent('Up', self.callback, 500)

    [...]

    def callback(self):
        global time_stamp
        time_now = time.time()
        if (time_now - time_stamp) >= 0.3:
            time_stamp = time_now
            return self.callback_func()  # <- Don't forget the parentheses here


#Testing Class Init    
REED = Alarm('input', 37, REEDcallback)
PIR = Alarm('input', 36, PIRcallback)
VIBR = Alarm('input', 38, VIBRcallback)
# This bit is now done in the constructor:
# VIBR.addEvent('Up', VIBR.callback, 500)
# PIR.addEvent('Up', PIR.callback, 500)
# REED.addEvent('Up', REED.callback, 500)