2015-04-17 86 views
0

我曾嘗試使用pigpio庫做抖,但沒有成功PIGPIO去抖

import pigpio 
captureFlag=1 
pi=pigpio.pi() 
def ImgCap(gpio.level.ticks): 
    global captureFlag 
    if captureFlag==0: 
     print "OFF" 
    if captureFlag==1: 
     captureflag=0 
     print "call back" 
     for x in xrange (1000000): 
       x=x+1 
     print x 
     captureflag=1 
def main(): 
     calf=pi.callback(4,pigpio.RISING_EDGE,ImgCap) 
     while True: 
      pass 
if __name__=='__main__' 
     main() 

這不是成功的回調從不打印OFF,是它讓打印回呼x值不斷。 我做了什麼錯誤如何正確編碼去抖動。 什麼是其他可能的方式

回答

0

您的程序從不打印「關閉」,因爲您正在測試的變量在回調觸發時從未設置爲0。

我建議你看看標準的RPi.GPIO庫(內置Raspbian的庫),因爲它本身處理交換機跳動,並允許您爲上升和下降邊緣設置回調一個功能使用GPIO.BOTH

import RPi.GPIO as GPIO 
GPIO.setmode(GPIO.BCM) 
GPIO.setup(4, GPIO.IN) 

def ImgCap(channel): 
    print("Flag Captured?") 

GPIO.add_event_detect(4, GPIO.BOTH, callback=ImgCap, bouncetime=200) 

while True: 
    pass 

http://raspi.tv/2014/rpi-gpio-update-and-detecting-both-rising-and-falling-edges