Python等待按下按钮(多个输入)

标签 python raspberry-pi

我目前正在考虑向我的脚本中添加另一个按钮。目前,它正在全屏显示我的视频(相机)的输出,直到检测到按下按钮,然后它才会执行某些操作。这看起来像这样我这样做了:

while True:
        camera.preview_fullscreen = True
        camera.preview_alpha = 128
        camera.start_preview()
        GPIO.wait_for_edge(picture_pin, GPIO.FALLING)
        action()

但是,我现在想介绍另一个执行不同操作的按钮。因此,我想加入这个:

import RPi.GPIO as GPIO

actionpin1 = 23
actionpin2 = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(actionpin1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(actionpin2, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def action1():
        print "button pressed 1"

def action2():
        print "button pressed 2"

while True:
        print "waiting for button"
        GPIO.add_event_detect(actionpin1, GPIO.BOTH, callback=action1, bouncetime=800)
        GPIO.add_event_detect(actionpin2, GPIO.BOTH, callback=action2, bouncetime=800)

但是,它给了我这个错误:

Traceback (most recent call last): File "test.py", line 19, in GPIO.add_event_detect(actionpin1, GPIO.BOTH, callback=action1, bouncetime=800) RuntimeError: Conflicting edge detection already enabled for this GPIO channel

这个错误与我读到的关于这个函数的内容冲突,所以我不确定为什么它会给我这个错误。谁能指出我正确的方向?

最佳答案

add_event_detect 方法放在 While 循环之外,这没有用,因为在检测到事件时会调用回调函数:

import RPi.GPIO as GPIO

actionpin1 = 23
actionpin2 = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(actionpin1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(actionpin2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(actionpin1, GPIO.BOTH, callback=action1,bouncetime=800)
GPIO.add_event_detect(actionpin2, GPIO.BOTH, callback=action2, bouncetime=800)

def action1():
    print "button pressed 1"

def action2():
    print "button pressed 2"

while True:
    print "waiting for button"

关于Python等待按下按钮(多个输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847603/

相关文章:

python - 如何在X轴上跳过特定时间段(股市午休时间)

python - 如何找到没有频率的列表或元组的模式?

python - Node.js如何调用Python代码?

RaspberryPi自动连接wifi的python脚本

opencv - 如何关闭Raspberry Pi上的opencv imshow()窗口?

linux - 如何限制 FTP 和 SMB 访问内部网络上的计算机

raspberry-pi - 树莓派自动登录无需etc/inittab

python - 使用Python通过串口与Raspberry Pi通信

python - Pandas 按时间分组,指定开始时间非整数分钟

c++ - 为 Python 扩展设置 C/C++ 编译器