python-DiskPlayer/buttons.py

96 lines
2.2 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-09 11:25:37 +00:00
import beepy as beep
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:
# 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-09 11:25:37 +00:00
# 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")
2021-11-08 12:11:09 +00:00
result = subprocess.run(
[f"python ./player.py next"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
2021-11-09 11:25:37 +00:00
elif btn._pin.number == 24:
# print("back")
2021-11-08 12:11:09 +00:00
result = subprocess.run(
[f"python ./player.py previous"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
2021-11-09 11:25:37 +00:00
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")
2021-11-08 12:11:09 +00:00
result = subprocess.run(
[f"python ./player.py shuffle"],
stdout=subprocess.PIPE,
shell=True,
).stdout.decode("utf-8")
2021-11-09 11:25:37 +00:00
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()