我正在处理Python脚本。
如果输入18为0,则打印,低低低低低。如果输入18更改为1,它将打印“高”并保持(执行subprocess.call一次)。如果输入再次更改为zu 0,它将再次打印,低低低。
问题在于,如果低,低,低,则subprocess.call(“ / home / pi / th / aus”)现在会连续触发。
如何编写脚本,subprocesss.call仅在上升沿或下降沿执行一次?
#!/usr/bin/env python
#coding: utf8
import time
from time import sleep
import RPi.GPIO as GPIO
import subprocess
GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
channel = 18
def doIfHigh(channel):
if GPIO.input(channel):
print "Rising edge detected on 18"
subprocess.call("/home/pi/th/aus")
else:
print "Falling edge detected on 18"
subprocess.call("/home/pi/th/aus")
channel = 18
GPIO.add_event_detect(channel, GPIO.BOTH, callback = doIfHigh, bouncetime = 200)
while 1:
sleep(0.1)
如果我停止脚本,则会显示此错误:
^X^CTraceback (most recent call last):
File "/home/pi/th/flanke.py", line 35, in <module>
sleep(0.1)
KeyboardInterrupt
sys.excepthook is missing
lost sys.stderr