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