From 351b1ddc60dc528dcf82f2b5b2b7ebe78b01849f Mon Sep 17 00:00:00 2001 From: ezn Date: Sun, 17 Nov 2024 17:13:19 +0000 Subject: [PATCH] next random video and press button >> relevant next video --- p33_from31.py | 62 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/p33_from31.py b/p33_from31.py index a17f4e1..d3dab2f 100644 --- a/p33_from31.py +++ b/p33_from31.py @@ -141,41 +141,55 @@ def wait_for_button_press(video_info): print(f"Waiting for button press from {video_info['buttons']}...") while True: + # Check for button presses + pressed_button = None 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: last_button_press = button + pressed_button = button print(f"Button {button} pressed.") no_press_count = 0 time.sleep(0.5) # Debounce delay (0.5 seconds) - break - else: - elapsed_time = time.time() - start_time - if elapsed_time >= timeout: - print(f"Timeout reached after {elapsed_time:.2f} seconds.") - no_press_count += 1 - if no_press_count >= 2: - print("Two consecutive timeouts. Restarting from Video 1.") - no_press_count = 0 + break # Exit loop after a button is pressed + + 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 + if elapsed_time >= timeout: + print(f"Timeout reached after {elapsed_time:.2f} seconds.") + no_press_count += 1 + if no_press_count >= 2: + print("Two consecutive timeouts. Restarting from Video 1.") + no_press_count = 0 + print(f"Restarting service: {service_name}") + subprocess.run(["sudo", "systemctl", "restart", service_name], check=True) + restart_service(service_name) + print(f"Service {service_name} restarted successfully.") + exit() + else: + # Handle transition to the next video after timeout + if video_info["next_videos"]: + next_video_id = random.choice(video_info["next_videos"]) # Randomly choose from next_videos + handle_video_chain(next_video_id) + else: + print("No next video available. Restarting from Video 1.") print(f"Restarting service: {service_name}") subprocess.run(["sudo", "systemctl", "restart", service_name], check=True) restart_service(service_name) print(f"Service {service_name} restarted successfully.") exit() - else: - # Handle transition to the next video after timeout - if video_info["next_videos"]: - next_video_id = video_info["next_videos"][0] - handle_video_chain(next_video_id) - else: - print("No next video available. Restarting from Video 1.") - print(f"Restarting service: {service_name}") - subprocess.run(["sudo", "systemctl", "restart", service_name], c> - restart_service(service_name) - print(f"Service {service_name} restarted successfully.") - exit() - - time.sleep(0.1) + + time.sleep(0.1) # Small delay to reduce CPU usage # Function to handle the video chain def handle_video_chain(video_id):