Browse Source

initial work on buttons

Karl Hudgell 6 months ago
parent
commit
ad276591fc
2 changed files with 56 additions and 0 deletions
  1. 56 0
      buttons.py
  2. 0 0
      media_change.sh

+ 56 - 0
buttons.py

@@ -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 - 0
media_change.sh