floppy-ytube-player/buttons.py

43 lines
1.3 KiB
Python
Raw Normal View History

2024-09-20 17:23:30 +01:00
import RPi.GPIO as GPIO
import time
2024-09-20 17:44:22 +01:00
from lib.mqtt import create_client, control_player, create_config
2024-09-20 17:23:30 +01:00
2024-09-20 17:44:22 +01:00
client = create_client()
2024-09-20 17:23:30 +01:00
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):
print("Button 1 pressed!")
control_player(client, "PLAY")
def button_callback_2(channel):
print("Button 2 pressed!")
control_player(client, "PAUSE")
def button_callback_3(channel):
print("Button 3 pressed!")
# 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