initial work on buttons
This commit is contained in:
parent
3167ab25d7
commit
ad276591fc
56
buttons.py
Normal file
56
buttons.py
Normal file
@ -0,0 +1,56 @@
|
||||
import os
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
from dotenv import load_dotenv
|
||||
from lib.mqtt import create_client, update_disc, control_player, create_config, check_current_disc
|
||||
|
||||
# Load the .env file
|
||||
load_dotenv()
|
||||
|
||||
# Define the MQTT server details
|
||||
broker = os.environ.get("broker")
|
||||
port = int(os.environ.get("port"))
|
||||
# MQTT username and password
|
||||
username = os.environ.get("username")
|
||||
password = os.environ.get("password")
|
||||
|
||||
#print(f"Broker: {broker}, Port: {port}, Username: {username}")
|
||||
|
||||
client = create_client(broker, port, username, password)
|
||||
|
||||
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
|
0
media_change.sh
Normal file → Executable file
0
media_change.sh
Normal file → Executable file
Loading…
x
Reference in New Issue
Block a user