next random video and press button >> relevant next video

master
ezn 1 year ago
parent 032237bf66
commit 351b1ddc60

@ -141,15 +141,29 @@ def wait_for_button_press(video_info):
print(f"Waiting for button press from {video_info['buttons']}...") print(f"Waiting for button press from {video_info['buttons']}...")
while True: while True:
# Check for button presses
pressed_button = None
for button in video_info["buttons"]: for button in video_info["buttons"]:
if GPIO.input(button) == GPIO.LOW: if GPIO.input(button) == GPIO.LOW: # Button pressed
if last_button_press != button: if last_button_press != button:
last_button_press = button last_button_press = button
pressed_button = button
print(f"Button {button} pressed.") print(f"Button {button} pressed.")
no_press_count = 0 no_press_count = 0
time.sleep(0.5) # Debounce delay (0.5 seconds) time.sleep(0.5) # Debounce delay (0.5 seconds)
break break # Exit loop after a button is pressed
else:
if pressed_button:
# Find the index of the pressed button in the video_info['buttons']
button_index = video_info["buttons"].index(pressed_button)
# Now select the next video based on this index
if button_index < len(video_info["next_videos"]):
next_video_id = video_info["next_videos"][button_index] # Use the index of the button press
handle_video_chain(next_video_id) # Play the correct next video
return
# If no button was pressed, handle timeout
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
if elapsed_time >= timeout: if elapsed_time >= timeout:
print(f"Timeout reached after {elapsed_time:.2f} seconds.") print(f"Timeout reached after {elapsed_time:.2f} seconds.")
@ -165,17 +179,17 @@ def wait_for_button_press(video_info):
else: else:
# Handle transition to the next video after timeout # Handle transition to the next video after timeout
if video_info["next_videos"]: if video_info["next_videos"]:
next_video_id = video_info["next_videos"][0] next_video_id = random.choice(video_info["next_videos"]) # Randomly choose from next_videos
handle_video_chain(next_video_id) handle_video_chain(next_video_id)
else: else:
print("No next video available. Restarting from Video 1.") print("No next video available. Restarting from Video 1.")
print(f"Restarting service: {service_name}") print(f"Restarting service: {service_name}")
subprocess.run(["sudo", "systemctl", "restart", service_name], c> subprocess.run(["sudo", "systemctl", "restart", service_name], check=True)
restart_service(service_name) restart_service(service_name)
print(f"Service {service_name} restarted successfully.") print(f"Service {service_name} restarted successfully.")
exit() exit()
time.sleep(0.1) time.sleep(0.1) # Small delay to reduce CPU usage
# Function to handle the video chain # Function to handle the video chain
def handle_video_chain(video_id): def handle_video_chain(video_id):

Loading…
Cancel
Save