Files
better-catchphrase/random_timer.py
2025-08-11 20:11:32 -04:00

88 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
import time
import threading
import RPi.GPIO as GPIO
from RPLCD.i2c import CharLCD
# GPIO pin setup for buttons
BUTTON_START_STOP = 17
BUTTON_NEXT = 27
BUTTON_TEAM1 = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_START_STOP, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_TEAM1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# LCD setup (adjust address and cols/rows if needed)
lcd = CharLCD('PCF8574', 0x27)
# Game variables
team1_score = 0
team2_score = 0
timer_running = False
timer_thread = None
seconds = 0
winner_displayed = False
# Timer function
def timer():
global seconds, timer_running
while timer_running:
seconds += 1
time.sleep(1)
def update_lcd():
global winner_displayed
lcd.clear()
if winner_displayed:
if team1_score >= 7:
lcd.write_string("Team 1 Wins!")
elif team2_score >= 7:
lcd.write_string("Team 2 Wins!")
else:
lcd.write_string(f"T1:{team1_score} T2:{team2_score}")
lcd.crlf()
lcd.write_string(f"Time: {seconds}s")
def start_stop_timer(channel):
global timer_running, timer_thread
if not timer_running:
timer_running = True
threading.Thread(target=timer, daemon=True).start()
else:
timer_running = False
def next_pressed(channel):
global team1_score, team2_score, winner_displayed
if not winner_displayed:
team2_score += 1
check_winner()
update_lcd()
def team1_pressed(channel):
global team1_score, winner_displayed
if not winner_displayed:
team1_score += 1
check_winner()
update_lcd()
def check_winner():
global winner_displayed, timer_running
if team1_score >= 7 or team2_score >= 7:
winner_displayed = True
timer_running = False
# Attach button event listeners
GPIO.add_event_detect(BUTTON_START_STOP, GPIO.FALLING, callback=start_stop_timer, bouncetime=300)
GPIO.add_event_detect(BUTTON_NEXT, GPIO.FALLING, callback=next_pressed, bouncetime=300)
GPIO.add_event_detect(BUTTON_TEAM1, GPIO.FALLING, callback=team1_pressed, bouncetime=300)
try:
while True:
update_lcd()
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
lcd.clear()