Better button functionality, timer on console

This commit is contained in:
2025-08-12 17:20:55 -04:00
parent ad989978e3
commit 8afb65612f

View File

@@ -15,15 +15,14 @@ I2C_CHIP = 'PCF8574'
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
# Buttons mapped to pins # Buttons mapped to pins
BUTTON_START_STOP = 23 BUTTON_START_STOP = 23 # Green button
BUTTON_NEXT = 27 BUTTON_NEXT = 27
BUTTON_TEAM1 = 22 BUTTON_TEAM1 = 22
BUTTON_TEAM2 = 17 BUTTON_TEAM2 = 17
for btn_pin in (BUTTON_NEXT, BUTTON_TEAM1, BUTTON_TEAM2): 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) GPIO.setup(btn_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_START_STOP, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# --- LCD SETUP --- # --- LCD SETUP ---
lcd = CharLCD(I2C_CHIP, I2C_ADDRESS) lcd = CharLCD(I2C_CHIP, I2C_ADDRESS)
lcd.clear() lcd.clear()
@@ -37,7 +36,7 @@ score_t1 = 0
score_t2 = 0 score_t2 = 0
current_word = "" current_word = ""
lock = threading.Lock() # Protect shared state lock = threading.Lock()
# --- LOAD WORDS --- # --- LOAD WORDS ---
def load_words(path=WORDS_FILE): def load_words(path=WORDS_FILE):
@@ -58,6 +57,8 @@ def lcd_print_lines(line1="", line2=""):
lcd.write_string(line1.ljust(16)[:16]) lcd.write_string(line1.ljust(16)[:16])
lcd.cursor_pos = (1, 0) lcd.cursor_pos = (1, 0)
lcd.write_string(line2.ljust(16)[:16]) lcd.write_string(line2.ljust(16)[:16])
print(line1)
print(line2)
def lcd_show_word_and_timer(word, seconds): def lcd_show_word_and_timer(word, seconds):
lcd.clear() lcd.clear()
@@ -65,14 +66,20 @@ def lcd_show_word_and_timer(word, seconds):
lcd.write_string(f"Time:{seconds:02d} ") lcd.write_string(f"Time:{seconds:02d} ")
lcd.cursor_pos = (1, 0) lcd.cursor_pos = (1, 0)
lcd.write_string(word.center(16)) lcd.write_string(word.center(16))
print(f"Time:{seconds:02d}")
print(word)
def lcd_show_scores(): def lcd_show_scores():
msg = f"T1:{score_t1} T2:{score_t2}".center(16)
lcd.clear() lcd.clear()
lcd.write_string(f"T1:{score_t1} T2:{score_t2}".center(16)) lcd.write_string(msg)
print(msg)
def lcd_show_winner(winner_team): def lcd_show_winner(winner_team):
msg = f"🏆 Team {winner_team} Wins!".center(16)
lcd.clear() lcd.clear()
lcd.write_string(f"🏆 Team {winner_team} Wins!".center(16)) lcd.write_string(msg)
print(msg)
# --- TIMER THREAD --- # --- TIMER THREAD ---
class CountdownTimer(threading.Thread): class CountdownTimer(threading.Thread):
@@ -103,7 +110,6 @@ def pick_random_word():
current_word = random.choice(words) current_word = random.choice(words)
def start_stop_button_callback(channel): def start_stop_button_callback(channel):
print("Start Button Pressed")
global timer_running, timer_thread global timer_running, timer_thread
with lock: with lock:
if timer_running: if timer_running:
@@ -122,33 +128,28 @@ def start_stop_button_callback(channel):
timer_thread.start() timer_thread.start()
def next_button_callback(channel): def next_button_callback(channel):
print("Next Button Pressed")
global current_word global current_word
with lock: with lock:
if timer_running: if timer_running: # Only works while timer runs
pick_random_word() pick_random_word()
def team1_button_callback(channel): def team1_button_callback(channel):
print("Team 1 Button Pressed")
global score_t1 global score_t1
with lock: with lock:
if timer_running: if not timer_running: # Only works when timer stopped
score_t1 += 1 score_t1 += 1
if score_t1 >= 7: if score_t1 >= 7:
timer_running = False
lcd_show_winner(1) lcd_show_winner(1)
reset_game_after_delay() reset_game_after_delay()
else: else:
lcd_show_scores() lcd_show_scores()
def team2_button_callback(channel): def team2_button_callback(channel):
print("Team 2 Button Pressed")
global score_t2 global score_t2
with lock: with lock:
if timer_running: if not timer_running: # Only works when timer stopped
score_t2 += 1 score_t2 += 1
if score_t2 >= 7: if score_t2 >= 7:
timer_running = False
lcd_show_winner(2) lcd_show_winner(2)
reset_game_after_delay() reset_game_after_delay()
else: else:
@@ -162,7 +163,7 @@ def reset_game_after_delay(delay=5):
score_t1 = 0 score_t1 = 0
score_t2 = 0 score_t2 = 0
current_word = "" current_word = ""
lcd_show_scores() lcd_print_lines("Catchphrase started!", "Press Green to Start")
threading.Thread(target=reset, daemon=True).start() threading.Thread(target=reset, daemon=True).start()
# --- Setup GPIO event detection --- # --- Setup GPIO event detection ---
@@ -175,8 +176,7 @@ GPIO.add_event_detect(BUTTON_TEAM2, GPIO.FALLING, callback=team2_button_callback
def main(): def main():
global words global words
words = load_words() words = load_words()
lcd_show_scores() lcd_print_lines("Catchphrase started!", "Press Green to Start")
print("Catchphrase started! Use buttons to play.")
try: try:
while True: while True: