Compare commits
4 Commits
lcd-test
...
buzzer-tes
| Author | SHA1 | Date | |
|---|---|---|---|
| 8afb65612f | |||
| ad989978e3 | |||
| c0014aa7d9 | |||
| 79c92c23aa |
300
random_timer.py
300
random_timer.py
@@ -1,183 +1,191 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
import RPi.GPIO as GPIO
|
||||||
Catchphrase with LCD Output (Scores & Reset)
|
|
||||||
"""
|
|
||||||
|
|
||||||
import random
|
|
||||||
import time
|
import time
|
||||||
|
import random
|
||||||
import threading
|
import threading
|
||||||
import sys
|
|
||||||
from RPLCD.i2c import CharLCD
|
from RPLCD.i2c import CharLCD
|
||||||
|
|
||||||
# ---------- CONFIG ----------
|
# --- CONFIG ---
|
||||||
WORDS_FILE = "words.txt" # file with one word per line
|
WORDS_FILE = "words.txt"
|
||||||
TIMER_SECONDS = 30 # how long the timer runs
|
TIMER_SECONDS = 30
|
||||||
I2C_ADDRESS = 0x27 # change if your LCD address is different
|
I2C_ADDRESS = 0x27
|
||||||
I2C_CHIP = 'PCF8574' # common backpack chip
|
I2C_CHIP = 'PCF8574'
|
||||||
|
|
||||||
# ---------- LCD ----------
|
# --- GPIO SETUP ---
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
|
||||||
|
# Buttons mapped to pins
|
||||||
|
BUTTON_START_STOP = 23 # Green button
|
||||||
|
BUTTON_NEXT = 27
|
||||||
|
BUTTON_TEAM1 = 22
|
||||||
|
BUTTON_TEAM2 = 17
|
||||||
|
|
||||||
|
for btn_pin in (BUTTON_START_STOP, BUTTON_NEXT, BUTTON_TEAM1, BUTTON_TEAM2):
|
||||||
|
GPIO.setup(btn_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
|
|
||||||
|
# --- LCD SETUP ---
|
||||||
lcd = CharLCD(I2C_CHIP, I2C_ADDRESS)
|
lcd = CharLCD(I2C_CHIP, I2C_ADDRESS)
|
||||||
|
lcd.clear()
|
||||||
|
|
||||||
def lcd_print(text, line=0):
|
# --- GLOBALS ---
|
||||||
"""Print text centered on given LCD line (0 or 1), padded to clear leftovers."""
|
words = []
|
||||||
lcd.cursor_pos = (line, 0)
|
timer_thread = None
|
||||||
lcd.write_string(text.center(16))
|
timer_running = False
|
||||||
|
time_left = TIMER_SECONDS
|
||||||
|
score_t1 = 0
|
||||||
|
score_t2 = 0
|
||||||
|
current_word = ""
|
||||||
|
|
||||||
def lcd_update_scores():
|
lock = threading.Lock()
|
||||||
"""Update the bottom LCD line with team scores (T1 left, T2 right)."""
|
|
||||||
lcd.cursor_pos = (1, 0)
|
|
||||||
score_str = f"{score_t1}{' ' * 14}{score_t2}"
|
|
||||||
lcd.write_string(score_str)
|
|
||||||
|
|
||||||
# ---------- HELPERS ----------
|
# --- LOAD WORDS ---
|
||||||
def load_words(path=WORDS_FILE):
|
def load_words(path=WORDS_FILE):
|
||||||
try:
|
try:
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
words = [line.strip() for line in f if line.strip()]
|
lines = [line.strip() for line in f if line.strip()]
|
||||||
if not words:
|
if not lines:
|
||||||
raise ValueError("Word file is empty.")
|
raise ValueError("Word file is empty.")
|
||||||
return words
|
return lines
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"❌ Error loading words: {e}")
|
print(f"❌ Error loading words: {e}")
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
def pick_random(words):
|
# --- LCD HELPERS ---
|
||||||
return random.choice(words)
|
def lcd_print_lines(line1="", line2=""):
|
||||||
|
lcd.clear()
|
||||||
|
lcd.cursor_pos = (0, 0)
|
||||||
|
lcd.write_string(line1.ljust(16)[:16])
|
||||||
|
lcd.cursor_pos = (1, 0)
|
||||||
|
lcd.write_string(line2.ljust(16)[:16])
|
||||||
|
print(line1)
|
||||||
|
print(line2)
|
||||||
|
|
||||||
# ---------- TIMER THREAD ----------
|
def lcd_show_word_and_timer(word, seconds):
|
||||||
|
lcd.clear()
|
||||||
|
lcd.cursor_pos = (0, 0)
|
||||||
|
lcd.write_string(f"Time:{seconds:02d} ")
|
||||||
|
lcd.cursor_pos = (1, 0)
|
||||||
|
lcd.write_string(word.center(16))
|
||||||
|
print(f"Time:{seconds:02d}")
|
||||||
|
print(word)
|
||||||
|
|
||||||
|
def lcd_show_scores():
|
||||||
|
msg = f"T1:{score_t1} T2:{score_t2}".center(16)
|
||||||
|
lcd.clear()
|
||||||
|
lcd.write_string(msg)
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
def lcd_show_winner(winner_team):
|
||||||
|
msg = f"🏆 Team {winner_team} Wins!".center(16)
|
||||||
|
lcd.clear()
|
||||||
|
lcd.write_string(msg)
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
# --- TIMER THREAD ---
|
||||||
class CountdownTimer(threading.Thread):
|
class CountdownTimer(threading.Thread):
|
||||||
def __init__(self, seconds=TIMER_SECONDS, on_finish=None):
|
def __init__(self, seconds):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.seconds = seconds
|
self.seconds = seconds
|
||||||
self.on_finish = on_finish
|
|
||||||
self._running = threading.Event()
|
self._running = threading.Event()
|
||||||
self._running.set()
|
self._running.set()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
for _ in range(self.seconds):
|
global time_left, timer_running
|
||||||
if not self._running.is_set():
|
time_left = self.seconds
|
||||||
break
|
while time_left > 0 and self._running.is_set():
|
||||||
|
with lock:
|
||||||
|
lcd_show_word_and_timer(current_word, time_left)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
self._running.clear()
|
time_left -= 1
|
||||||
if self.on_finish:
|
with lock:
|
||||||
self.on_finish()
|
timer_running = False
|
||||||
|
lcd_show_scores()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self._running.clear()
|
self._running.clear()
|
||||||
|
|
||||||
@property
|
# --- GAME LOGIC ---
|
||||||
def is_running(self):
|
def pick_random_word():
|
||||||
return self._running.is_set()
|
global current_word
|
||||||
|
current_word = random.choice(words)
|
||||||
|
|
||||||
# ---------- SCORE VARIABLES ----------
|
def start_stop_button_callback(channel):
|
||||||
score_t1 = 0
|
global timer_running, timer_thread
|
||||||
score_t2 = 0
|
with lock:
|
||||||
|
if timer_running:
|
||||||
def add_score(team):
|
# Stop timer
|
||||||
global score_t1, score_t2
|
if timer_thread:
|
||||||
if team == 1:
|
|
||||||
score_t1 += 1
|
|
||||||
if score_t1 >= 7:
|
|
||||||
print("🏆 Team 1 wins! Scores reset.")
|
|
||||||
score_t1 = score_t2 = 0
|
|
||||||
elif team == 2:
|
|
||||||
score_t2 += 1
|
|
||||||
if score_t2 >= 7:
|
|
||||||
print("🏆 Team 2 wins! Scores reset.")
|
|
||||||
score_t1 = score_t2 = 0
|
|
||||||
lcd_update_scores()
|
|
||||||
|
|
||||||
def reset_scores():
|
|
||||||
global score_t1, score_t2
|
|
||||||
score_t1 = score_t2 = 0
|
|
||||||
lcd_update_scores()
|
|
||||||
|
|
||||||
# ---------- MAIN LOOP ----------
|
|
||||||
def main():
|
|
||||||
global score_t1, score_t2
|
|
||||||
words = load_words()
|
|
||||||
timer_thread = None
|
|
||||||
|
|
||||||
print("=== Catchphrase, with LCD & Scores ===")
|
|
||||||
print("Commands:")
|
|
||||||
print(" start - start 30s timer & show a word")
|
|
||||||
print(" next - show another word while timer runs")
|
|
||||||
print(" stop - stop timer early")
|
|
||||||
print(" t1 - add point to Team 1")
|
|
||||||
print(" t2 - add point to Team 2")
|
|
||||||
print(" reset - reset both scores to 0")
|
|
||||||
print(" exit / quit - leave the program")
|
|
||||||
|
|
||||||
lcd_print("Ready", line=0)
|
|
||||||
lcd_update_scores()
|
|
||||||
|
|
||||||
def announce_finish():
|
|
||||||
print("\n⏰ Time is up! Returning to menu.")
|
|
||||||
lcd_print("Time is up!", line=0)
|
|
||||||
lcd_update_scores()
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
cmd = input("\n> ").strip().lower()
|
|
||||||
except EOFError:
|
|
||||||
break
|
|
||||||
|
|
||||||
if cmd in ("exit", "quit"):
|
|
||||||
if timer_thread and timer_thread.is_running:
|
|
||||||
timer_thread.stop()
|
timer_thread.stop()
|
||||||
timer_thread.join()
|
timer_thread.join()
|
||||||
lcd_print("Goodbye!", line=0)
|
timer_running = False
|
||||||
lcd_update_scores()
|
lcd_show_scores()
|
||||||
print("👋 Goodbye!")
|
|
||||||
break
|
|
||||||
|
|
||||||
elif cmd == "start":
|
|
||||||
if timer_thread and timer_thread.is_running:
|
|
||||||
print("[!] Timer is already running.")
|
|
||||||
else:
|
|
||||||
timer_thread = CountdownTimer(on_finish=announce_finish)
|
|
||||||
timer_thread.daemon = True
|
|
||||||
timer_thread.start()
|
|
||||||
|
|
||||||
word = pick_random(words)
|
|
||||||
print(word)
|
|
||||||
lcd_print(word, line=0)
|
|
||||||
lcd_update_scores()
|
|
||||||
print(f"[+] 30-second timer started. ({TIMER_SECONDS}s)")
|
|
||||||
|
|
||||||
elif cmd == "next":
|
|
||||||
if not (timer_thread and timer_thread.is_running):
|
|
||||||
print("[!] No active timer - use 'start' first.")
|
|
||||||
else:
|
|
||||||
word = pick_random(words)
|
|
||||||
print(word)
|
|
||||||
lcd_print(word, line=0)
|
|
||||||
lcd_update_scores()
|
|
||||||
|
|
||||||
elif cmd == "stop":
|
|
||||||
if timer_thread and timer_thread.is_running:
|
|
||||||
timer_thread.stop()
|
|
||||||
timer_thread.join()
|
|
||||||
lcd_print("Stopped early", line=0)
|
|
||||||
lcd_update_scores()
|
|
||||||
print("[+] Timer stopped early.")
|
|
||||||
else:
|
|
||||||
print("[!] There is no running timer to stop.")
|
|
||||||
|
|
||||||
elif cmd == "t1":
|
|
||||||
add_score(1)
|
|
||||||
print(f"Team 1: {score_t1}, Team 2: {score_t2}")
|
|
||||||
|
|
||||||
elif cmd == "t2":
|
|
||||||
add_score(2)
|
|
||||||
print(f"Team 1: {score_t1}, Team 2: {score_t2}")
|
|
||||||
|
|
||||||
elif cmd == "reset":
|
|
||||||
reset_scores()
|
|
||||||
print("Scores reset.")
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print(f"[!] Unknown command: {cmd}")
|
# Start timer & show first word
|
||||||
|
pick_random_word()
|
||||||
|
timer_thread = CountdownTimer(TIMER_SECONDS)
|
||||||
|
timer_thread.daemon = True
|
||||||
|
timer_running = True
|
||||||
|
timer_thread.start()
|
||||||
|
|
||||||
|
def next_button_callback(channel):
|
||||||
|
global current_word
|
||||||
|
with lock:
|
||||||
|
if timer_running: # Only works while timer runs
|
||||||
|
pick_random_word()
|
||||||
|
|
||||||
|
def team1_button_callback(channel):
|
||||||
|
global score_t1
|
||||||
|
with lock:
|
||||||
|
if not timer_running: # Only works when timer stopped
|
||||||
|
score_t1 += 1
|
||||||
|
if score_t1 >= 7:
|
||||||
|
lcd_show_winner(1)
|
||||||
|
reset_game_after_delay()
|
||||||
|
else:
|
||||||
|
lcd_show_scores()
|
||||||
|
|
||||||
|
def team2_button_callback(channel):
|
||||||
|
global score_t2
|
||||||
|
with lock:
|
||||||
|
if not timer_running: # Only works when timer stopped
|
||||||
|
score_t2 += 1
|
||||||
|
if score_t2 >= 7:
|
||||||
|
lcd_show_winner(2)
|
||||||
|
reset_game_after_delay()
|
||||||
|
else:
|
||||||
|
lcd_show_scores()
|
||||||
|
|
||||||
|
def reset_game_after_delay(delay=5):
|
||||||
|
def reset():
|
||||||
|
global score_t1, score_t2, current_word
|
||||||
|
time.sleep(delay)
|
||||||
|
with lock:
|
||||||
|
score_t1 = 0
|
||||||
|
score_t2 = 0
|
||||||
|
current_word = ""
|
||||||
|
lcd_print_lines("Catchphrase started!", "Press Green to Start")
|
||||||
|
threading.Thread(target=reset, daemon=True).start()
|
||||||
|
|
||||||
|
# --- Setup GPIO event detection ---
|
||||||
|
GPIO.add_event_detect(BUTTON_START_STOP, GPIO.FALLING, callback=start_stop_button_callback, bouncetime=300)
|
||||||
|
GPIO.add_event_detect(BUTTON_NEXT, GPIO.FALLING, callback=next_button_callback, bouncetime=300)
|
||||||
|
GPIO.add_event_detect(BUTTON_TEAM1, GPIO.FALLING, callback=team1_button_callback, bouncetime=300)
|
||||||
|
GPIO.add_event_detect(BUTTON_TEAM2, GPIO.FALLING, callback=team2_button_callback, bouncetime=300)
|
||||||
|
|
||||||
|
# --- Main program ---
|
||||||
|
def main():
|
||||||
|
global words
|
||||||
|
words = load_words()
|
||||||
|
lcd_print_lines("Catchphrase started!", "Press Green to Start")
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
time.sleep(0.1)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
GPIO.cleanup()
|
||||||
|
lcd.clear()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user