python-DiskPlayer/buttons.py
2021-11-14 19:29:35 +00:00

110 lines
2.7 KiB
Python
Executable File

from gpiozero import Button
from signal import pause
import time
import subprocess
import pathlib
Button.was_held = False
def vol_up(btn):
btn.was_held = True
while btn.was_held:
print("Vol Up")
result = subprocess.run(
[f"mpc volume +10"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
time.sleep(0.3)
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")
time.sleep(0.3)
def mute(btn):
btn.was_held = True
while btn.was_held:
print("Mute")
result = subprocess.run(
[f"mpc volume -10"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
time.sleep(0.3)
def playpause_down(btn):
btn.was_held = True
while btn.was_held:
print("playpause")
time.sleep(0.3)
def released(btn):
if not btn.was_held:
pressed(btn)
btn.was_held = False
def pressed(btn):
if btn._pin.number == up_pin:
print("next")
result = subprocess.run(
[f"{pathlib.Path().resolve()}/venv/bin/python3 ./player.py next"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
elif btn._pin.number == down_pin:
print("back")
result = subprocess.run(
[f"{pathlib.Path().resolve()}/venv/bin/python3 ./player.py previous"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
elif btn._pin.number == playpause_pin:
print("playpause")
result = subprocess.run(
[f"{pathlib.Path().resolve()}/venv/bin/python3 ./player.py playpause"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
elif btn._pin.number == shuffle_pin:
print("shuffle")
result = subprocess.run(
[f"{pathlib.Path().resolve()}/venv/bin/python3 ./player.py shuffle"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
up_pin = 24
down_pin = 15
playpause_pin = 14
shuffle_pin = 23
up_btn = Button(up_pin)
down_button = Button(down_pin)
playpause_button = Button(playpause_pin)
shuffle_button = Button(shuffle_pin)
up_btn.when_held = vol_up
up_btn.when_released = released
down_button.when_held = vol_down
down_button.when_released = released
playpause_button.when_held = mute
playpause_button.when_released = released
shuffle_button.when_released = released
pause()