我使用 Pimoroni 的 Inky 印象屏幕来显示带有树莓派零的幻灯片,上面有 4 个按钮,可以与 RPi.GPIO 完美配合。
如您所见,我在代码附件中简化了特定操作的功能。
一切正常,直到我添加了一些代码以避免在函数执行期间多次按下按钮。
它适用于端口 4 和 5 上的按钮,分别标记为 A 和 B。但对于端口 16 和 24,它使功能无限循环handle_button(pin)
,甚至GPIO.cleanup()
无法修复。
如果有人知道有关暂时停止检测的方法或解决此问题的方法,那将非常酷,谢谢!
import RPi.GPIO as GPIO
print("buttons.py")
BUTTONS = [5, 6, 16, 24]
LABELS = ['A', 'B', 'C', 'D']
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def remove_butt_detect():
for pin in BUTTONS:
GPIO.remove_event_detect(pin)
def add_butt_detect():
for pin in BUTTONS:
GPIO.add_event_detect(pin, GPIO.FALLING, handle_button, bouncetime=250)
def handle_button(pin):
remove_butt_detect()
label = LABELS[BUTTONS.index(pin)]
if label == 'A':
print("random pic")
time.sleep(1)
elif label == 'B':
print("previous pic")
time.sleep(1)
elif label == 'C':
print("clean screen")
time.sleep(1)
else:
print("delete current pic")
time.sleep(1)
add_butt_detect()
add_butt_detect()
signal.pause()