123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- from gpiozero import Button
- from signal import pause
- import time
- import subprocess
- import pathlib
- venv_path = f"{pathlib.Path(__file__).parent.resolve()}/venv/bin/python3"
- player_path = f"{pathlib.Path(__file__).parent.resolve()}/player.py"
- 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"{venv_path} {player_path} next"],
- stdout=subprocess.PIPE,
- shell=True,
- ).stdout.decode("utf-8")
- elif btn._pin.number == down_pin:
- print("back")
- result = subprocess.run(
- [f"{venv_path} {player_path} previous"],
- stdout=subprocess.PIPE,
- shell=True,
- ).stdout.decode("utf-8")
- elif btn._pin.number == playpause_pin:
- print("playpause")
- result = subprocess.run(
- [f"{venv_path} {player_path} playpause"],
- stdout=subprocess.PIPE,
- shell=True,
- ).stdout.decode("utf-8")
- elif btn._pin.number == shuffle_pin:
- print("shuffle")
- result = subprocess.run(
- [f"{venv_path} {player_path} 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()
|