rewrite buttons to have vol control

This commit is contained in:
Karl 2021-11-09 11:25:37 +00:00
parent dc1bf227d8
commit e0f4c06676

View File

@ -1,48 +1,95 @@
import RPi.GPIO as GPIO from gpiozero import Button
from signal import pause
import time import time
import subprocess 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: Button.was_held = False
input_state = GPIO.input(14)
if input_state == False:
print('playpause') def vol_up(btn):
btn.was_held = True
while btn.was_held:
# print("Vol Up")
result = subprocess.run( result = subprocess.run(
[f"python ./player.py playpause"], [f"mpc volume +10"],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
shell=True, shell=True,
).stdout.decode("utf-8") ).stdout.decode("utf-8")
time.sleep(0.2) # beep(sound=1)
input_state = GPIO.input(15) time.sleep(0.5)
if input_state == False:
print('next')
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( result = subprocess.run(
[f"python ./player.py next"], [f"python ./player.py next"],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
shell=True, shell=True,
).stdout.decode("utf-8") ).stdout.decode("utf-8")
time.sleep(0.2) elif btn._pin.number == 24:
input_state = GPIO.input(24) # print("back")
if input_state == False:
print('previous')
result = subprocess.run( result = subprocess.run(
[f"python ./player.py previous"], [f"python ./player.py previous"],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
shell=True, shell=True,
).stdout.decode("utf-8") ).stdout.decode("utf-8")
time.sleep(0.2) elif btn._pin.number == 14:
input_state = GPIO.input(23) # print("playpause")
if input_state == False: result = subprocess.run(
print('shuffle') [f"python ./player.py playpause"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
elif btn._pin.number == 23:
# print("shuffle")
result = subprocess.run( result = subprocess.run(
[f"python ./player.py shuffle"], [f"python ./player.py shuffle"],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
shell=True, shell=True,
).stdout.decode("utf-8") ).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()