因此,我创建了以下代码来捕获来自 Raspberry PI 3 B+ 的三个 GPIO 端子的输入。这段代码做了我想要的,除了似乎有一个缓冲区和 event_detect 系统,我想在第一个输入变为活动状态后清除它。该计划的目的是为人们提供类似游戏节目的体验,让人们同时尝试蜂拥而至。但只能有一个“第一个”人进来。程序应该检测第一个活动输入,并基本上忽略其余的 10 秒。
PS - 我对 Python 和 Raspberry pi 的细节非常陌生。
PPS - 我已将计时器减少到 3 秒,以便更轻松地排除故障。
# External module imports
from guizero import App, Text, TextBox, PushButton
import RPi.GPIO as GPIO
import sys
import time
# This procedure will watch three inputs and lock in the team that calls in first for 10seconds
# Change the colors of the buttons to show which team pressed in first.
def action1(dunno):
# Team1 button pressed
team1_btn.bg="green"
team2_btn.bg="red"
team3_btn.bg="red"
sleep10()
def action2(dunno):
# Team1 button pressed
team1_btn.bg="red"
team2_btn.bg="green"
team3_btn.bg="red"
sleep10()
def action3(dunno):
# Team1 button pressed
team1_btn.bg="red"
team2_btn.bg="red"
team3_btn.bg="green"
sleep10()
def sleep10():
#sleep for 10 seconds
delay_timer.visible=True
for i in range(3,0,-1):
delay_timer.value=str(i)
time.sleep(1)
delay_timer.visible=False
team1_btn.bg="light grey"
team2_btn.bg="light grey"
team3_btn.bg="light grey"
# Pin setup
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(16, GPIO.RISING, callback=action1, bouncetime=1800)
GPIO.add_event_detect(20, GPIO.RISING, callback=action2, bouncetime=1800)
GPIO.add_event_detect(21, GPIO.RISING, callback=action3, bouncetime=1800)
app = App(title="A07 DSS Tri-State - Jeopardy", width=700)
blank1 = Text(app, text="")
welcome_message = Text(app, text="This.. is.. Jeopardy!!", size=30, font="Times New Roman", color="blue")
blank2 = Text(app, text="")
blank3 = Text(app, text="")
team1_btn = PushButton(app, text="Team Black & Blue")
team1_btn.bg="light grey"
blank4 = Text(app, text="")
team2_btn = PushButton(app, text="Team Holiday Spirit")
team2_btn.bg="light grey"
blank5 = Text(app, text="")
team3_btn = PushButton(app, text="Team Creamsicle")
team3_btn.bg="light grey"
blank6 = Text(app, text="")
delay_timer = Text(app, text="0")
delay_timer.visible=False
app.display()
@Gajtguy 你还有这个代码吗?正在考虑一款像你提到的那样的游戏。