101 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import os
 | |
| import RPi.GPIO as GPIO
 | |
| import time
 | |
| from lib.mqtt import create_client, control_player, create_config, check_current_status, check_current_disc, update_disc
 | |
| from lib.home_assistant import load_disc, pause_media, play_media_again, set_shuffle, next_track
 | |
| from dotenv import load_dotenv
 | |
| 
 | |
| load_dotenv()
 | |
| 
 | |
| use_mqtt = os.environ["use_mqtt"].lower() == "true"
 | |
| 
 | |
| if use_mqtt:
 | |
|     client = create_client()
 | |
|     create_config(client)
 | |
| 
 | |
| # Debug: Print environment variable
 | |
| print(f"Using MQTT: {use_mqtt}")
 | |
| 
 | |
| # GPIO button pin setup
 | |
| BUTTON_PINS = [17, 18, 27, 22]
 | |
| GPIO.setmode(GPIO.BCM)  # Use BCM numbering
 | |
| 
 | |
| # Set up each button pin as input with internal pull-up resistors
 | |
| for pin in BUTTON_PINS:
 | |
|     GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 | |
| 
 | |
| # Button Handlers
 | |
| def handle_button_1():
 | |
|     if use_mqtt:
 | |
|         if check_current_status(client) == "PLAY":
 | |
|             print("Pausing playback via MQTT.")
 | |
|             control_player(client, "PAUSE")
 | |
|         else:
 | |
|             print("Playing media via MQTT.")
 | |
|             control_player(client, "PLAY")
 | |
|     else:
 | |
|         print("Pausing/Playing via Home Assistant.")
 | |
|         if check_current_status(client) == "PLAY":
 | |
|             pause_media()
 | |
|         else:
 | |
|             play_media_again()
 | |
| 
 | |
| def handle_button_2():
 | |
|     print("Skipping to the next track.")
 | |
|     if use_mqtt:
 | |
|         control_player(client, "SKIP")
 | |
|         control_player(client, "PLAY")
 | |
|     else:
 | |
|         next_track()
 | |
| 
 | |
| def handle_button_3():
 | |
|     start_time = time.time()
 | |
|     if use_mqtt:
 | |
|         disc_data = check_current_disc(client)
 | |
| 
 | |
|         # Wait until the button is released or held for more than 3 seconds
 | |
|         while GPIO.input(BUTTON_PINS[2]) == GPIO.LOW:
 | |
|             if time.time() - start_time >= 3:
 | |
|                 print("Button 3 held for 3 seconds! Resetting to RADIO2 via MQTT.")
 | |
|                 update_disc(client, ["ytmusic", disc_data['name'], disc_data['type'], "RESET"])
 | |
|                 control_player(client, "PLAY")
 | |
|                 update_disc(client, ["ytmusic", disc_data['name'], disc_data['type'], disc_data['id']])
 | |
|                 return  # Exit after sending RADIO2
 | |
| 
 | |
|         print("Button 3 pressed briefly! Sending RADIO via MQTT.")
 | |
|         control_player(client, "RADIO")
 | |
|     else:
 | |
|         print("Button 3 functionality not implemented for Home Assistant.")
 | |
| 
 | |
| def handle_button_4():
 | |
|     print("Toggling shuffle mode.")
 | |
|     if use_mqtt:
 | |
|         control_player(client, "SHUFFLE")
 | |
|         control_player(client, "PLAY")
 | |
|     else:
 | |
|         set_shuffle()
 | |
| 
 | |
| # GPIO Callback Handlers
 | |
| def button_callback(channel):
 | |
|     if channel == BUTTON_PINS[0]:
 | |
|         handle_button_1()
 | |
|     elif channel == BUTTON_PINS[1]:
 | |
|         handle_button_2()
 | |
|     elif channel == BUTTON_PINS[2]:
 | |
|         handle_button_3()
 | |
|     elif channel == BUTTON_PINS[3]:
 | |
|         handle_button_4()
 | |
| 
 | |
| # Add event detection for each button press
 | |
| for pin in BUTTON_PINS:
 | |
|     GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_callback, bouncetime=200)
 | |
| 
 | |
| try:
 | |
|     # Keep the script running to detect button presses
 | |
|     while True:
 | |
|         time.sleep(1)  # Sleep to reduce CPU usage
 | |
| except KeyboardInterrupt:
 | |
|     pass
 | |
| finally:
 | |
|     GPIO.cleanup()  # Clean up GPIO on exit
 | 
