如何检测树莓派GPIO输入的变化

Atr*_*ors 3 python gpio raspberry-pi

有没有一种方法可以在不使用无限循环的情况下检测树莓派 GPIO 的变化?

您可以使用以下方法检测上升或下降:

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)
Run Code Online (Sandbox Code Playgroud)

但您只能将事件检测器设置为一次下降或上升。有没有办法在不检查无限循环中的输入的情况下做到这一点?

dhr*_*s90 5

您可以在 上使用线程回调event_detect。根据raspberry-gpio-python,您可以利用这样的东西。

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)

其中 event 可以是GPIO.RISING,GPIO.FALLING或GPIO.BOTH,my_callback是一个普通的 python 函数,其行为类似于在不同线程中运行的 ISR。

希望能帮助到你。