Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python信号模块设置GPIO状态更改的处理程序_Python_Raspberry Pi_Interrupt_Gpio_Django Signals - Fatal编程技术网

使用python信号模块设置GPIO状态更改的处理程序

使用python信号模块设置GPIO状态更改的处理程序,python,raspberry-pi,interrupt,gpio,django-signals,Python,Raspberry Pi,Interrupt,Gpio,Django Signals,我想检测raspberry pi的gpio输入中的变化,并使用python的信号模块设置处理程序。我对信号模块不熟悉,不懂如何使用它。我现在正在使用此代码: import RPi.GPIO as GPIO import time from datetime import datetime import picamera i=0 j=0 camera= picamera.PiCamera() camera.resolution = (640, 480) # handle the button

我想检测raspberry pi的
gpio
输入中的变化,并使用python的信号模块设置处理程序。我对信号模块不熟悉,不懂如何使用它。我现在正在使用此代码:

import RPi.GPIO as GPIO
import time
from datetime import datetime
import picamera
i=0
j=0
camera= picamera.PiCamera()

camera.resolution = (640, 480)

# handle the button event
def buttonEventHandler (pin):
    global j
    j+=1
    #camera.close()
    print "handling button event"
    print("pressed",str(datetime.now()))
    time.sleep(4)
    camera.capture( 'clicked%02d.jpg' %j )
    #camera.close()

def main():

      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(2,GPIO.IN,pull_up_down=GPIO.PUD_UP)    
      GPIO.add_event_detect(2,GPIO.FALLING)

      GPIO.add_event_callback(2,buttonEventHandler) 
     # RPIO.add_interrupt_callback(2,buttonEventHandler,falling,RPIO.PUD_UP,False,None)

      while True:
          global i
          print "Hello world! {0}".format(i)
          i=i+1
          time.sleep(5)

  #  if(GPIO.input(2)==GPIO.LOW):

     # GPIO.cleanup()

if __name__=="__main__":
    main()

我只是以不同的方式更改了代码,您可以自由地使用信号模块实现相同的代码。您可以启动新线程并轮询或注册回调事件,方法是使用以下代码并在其run()方法中编写任何函数逻辑

上面的代码将迭代,直到引脚输入变高,所以一旦引脚变高,while循环中的条件就会中断,并捕获图片

所以,为了调用上面的线程,请执行以下操作

gpio_thread = GPIOThread()
gpio_thread.start() 
这将调用线程构造函数init,初始化构造函数中的变量(如果有),并执行run方法

import threading
import RPi.GPIO as GPIO
import time
import time
from datetime import datetime
import picamera
i=0
j=0
camera= picamera.PiCamera()

camera.resolution = (640, 480)
PIN = 2

class GPIOThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)              

    def run(self):
        while True:
            if GPIO.input(PIN) == False: # adjust this statement as per your pin status i.e HIGH/LOW
                global j
                j+=1
                #camera.close()
                print "handling button event"
                print("pressed",str(datetime.now()))
                time.sleep(4)
                camera.capture( 'clicked%02d.jpg' %j )

def main():

      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(PIN,GPIO.IN,pull_up_down=GPIO.PUD_UP)    
      GPIO.add_event_detect(PIN,GPIO.FALLING)

      gpio_thread = GPIOThread()
      gpio_thread.start() 

      while True:
          global i
          print "Hello world! {0}".format(i)
          i=i+1
          time.sleep(5)


if __name__=="__main__":
    main()
您还可以调用join()方法,以等待线程完成其执行

gpio_thread.join()  

这对我来说总是有效的,所以干杯

我刚刚以不同的方式更改了代码,您可以自由地使用信号模块实现相同的代码。您可以启动新线程并轮询或注册其回调事件,方法是使用以下代码并在其run()方法中编写您的函数逻辑

上面的代码将迭代,直到引脚输入变高,所以一旦引脚变高,while循环中的条件就会中断,并捕获图片

所以,为了调用上面的线程,请执行以下操作

gpio_thread = GPIOThread()
gpio_thread.start() 
这将调用线程构造函数init,初始化构造函数中的变量(如果有),并执行run方法

import threading
import RPi.GPIO as GPIO
import time
import time
from datetime import datetime
import picamera
i=0
j=0
camera= picamera.PiCamera()

camera.resolution = (640, 480)
PIN = 2

class GPIOThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)              

    def run(self):
        while True:
            if GPIO.input(PIN) == False: # adjust this statement as per your pin status i.e HIGH/LOW
                global j
                j+=1
                #camera.close()
                print "handling button event"
                print("pressed",str(datetime.now()))
                time.sleep(4)
                camera.capture( 'clicked%02d.jpg' %j )

def main():

      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(PIN,GPIO.IN,pull_up_down=GPIO.PUD_UP)    
      GPIO.add_event_detect(PIN,GPIO.FALLING)

      gpio_thread = GPIOThread()
      gpio_thread.start() 

      while True:
          global i
          print "Hello world! {0}".format(i)
          i=i+1
          time.sleep(5)


if __name__=="__main__":
    main()
您还可以调用join()方法,以等待线程完成其执行

gpio_thread.join()  

这对我来说总是有效的,所以干杯

为什么你不起诉GPIO模块我在使用GPIO模块,但我在一次中断中调用了两次处理函数我认为信号模块工作得更好这就是为什么我在考虑使用它为什么你不起诉GPIO模块我在使用GPIO模块,但我在一次中断中调用了两次处理函数信号模块效果更好…这就是为什么我想用它。。。