python-DiskPlayer/buttons.py

111 lines
2.7 KiB
Python
Raw Normal View History

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