فهرست منبع

rewrite buttons to have vol control

Karl 3 سال پیش
والد
کامیت
e0f4c06676
1فایلهای تغییر یافته به همراه71 افزوده شده و 24 حذف شده
  1. 71 24
      buttons.py

+ 71 - 24
buttons.py

@@ -1,48 +1,95 @@
-import RPi.GPIO as GPIO
+from gpiozero import Button
+from signal import pause
 import time
 import subprocess
+import beepy as beep
 
-GPIO.setmode(GPIO.BCM)
 
-GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
-GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
-GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
-GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
-while True:
-    input_state = GPIO.input(14)
-    if input_state == False:
-        print('playpause')
+Button.was_held = False
+
+
+def vol_up(btn):
+    btn.was_held = True
+    while btn.was_held:
+        # print("Vol Up")
         result = subprocess.run(
-            [f"python ./player.py playpause"],
+            [f"mpc volume +10"],
             stdout=subprocess.PIPE,
             shell=True,
         ).stdout.decode("utf-8")
-        time.sleep(0.2)
-    input_state = GPIO.input(15)
-    if input_state == False:
-        print('next')
+        # beep(sound=1)
+        time.sleep(0.5)
+
+
+def vol_down(btn):
+    btn.was_held = True
+    while btn.was_held:
+        # print("Vol Down")
+        result = subprocess.run(
+            [f"mpc volume -10"],
+            stdout=subprocess.PIPE,
+            shell=True,
+        ).stdout.decode("utf-8")
+        # beep(sound=1)
+        time.sleep(0.5)
+
+
+def playpause_down(btn):
+    btn.was_held = True
+    while btn.was_held:
+        print("playpause")
+        time.sleep(1)
+
+
+def released(btn):
+    if not btn.was_held:
+        pressed(btn)
+    btn.was_held = False
+
+
+def pressed(btn):
+    if btn._pin.number == 15:
+        # print("next")
         result = subprocess.run(
             [f"python ./player.py next"],
             stdout=subprocess.PIPE,
             shell=True,
         ).stdout.decode("utf-8")
-        time.sleep(0.2)
-    input_state = GPIO.input(24)
-    if input_state == False:
-        print('previous')
+    elif btn._pin.number == 24:
+        # print("back")
         result = subprocess.run(
             [f"python ./player.py previous"],
             stdout=subprocess.PIPE,
             shell=True,
         ).stdout.decode("utf-8")
-        time.sleep(0.2)
-    input_state = GPIO.input(23)
-    if input_state == False:
-        print('shuffle')
+    elif btn._pin.number == 14:
+        # print("playpause")
+        result = subprocess.run(
+            [f"python ./player.py playpause"],
+            stdout=subprocess.PIPE,
+            shell=True,
+        ).stdout.decode("utf-8")
+    elif btn._pin.number == 23:
+        # print("shuffle")
         result = subprocess.run(
             [f"python ./player.py shuffle"],
             stdout=subprocess.PIPE,
             shell=True,
         ).stdout.decode("utf-8")
-        time.sleep(0.2)
+
+
+up_btn = Button(15)
+down_button = Button(24)
+playpause_button = Button(14)
+shuffle_button = Button(23)
+
+down_button.when_held = vol_down
+down_button.when_released = released
+
+up_btn.when_held = vol_up
+up_btn.when_released = released
+
+playpause_button.when_released = released
+shuffle_button.when_released = released
+pause()