2024-09-20 20:58:48 +01:00

52 lines
1.6 KiB
Python

import RPi.GPIO as GPIO
import time
from lib.mqtt import create_client, control_player, create_config, check_current_status
client = create_client()
create_config(client)
# Set up GPIO pins for each button
BUTTON_PINS = [17, 18, 27] # Replace with your GPIO pin numbers
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)
# Callback functions to handle button presses
def button_callback_1(channel):
client = create_client()
if check_current_status(client) == "PLAY":
print("Button 1 pressed! Pausing playback.")
control_player(client, "PAUSE")
else:
print("Button 1 pressed! Playing.")
control_player(client, "PLAY")
def button_callback_2(channel):
print("Button 2 pressed!")
client = create_client()
control_player(client, "SKIP")
control_player(client, "PLAY")
def button_callback_3(channel):
print("Button 3 pressed!")
client = create_client()
control_player(client, "RADIO")
# Add event detection for each button press
GPIO.add_event_detect(BUTTON_PINS[0], GPIO.FALLING, callback=button_callback_1, bouncetime=200)
GPIO.add_event_detect(BUTTON_PINS[1], GPIO.FALLING, callback=button_callback_2, bouncetime=200)
GPIO.add_event_detect(BUTTON_PINS[2], GPIO.FALLING, callback=button_callback_3, 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