buttons.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from gpiozero import Button
  2. from signal import pause
  3. import time
  4. import subprocess
  5. import pathlib
  6. Button.was_held = False
  7. def vol_up(btn):
  8. btn.was_held = True
  9. while btn.was_held:
  10. print("Vol Up")
  11. result = subprocess.run(
  12. [f"mpc volume +10"],
  13. stdout=subprocess.PIPE,
  14. shell=True,
  15. ).stdout.decode("utf-8")
  16. time.sleep(0.3)
  17. def vol_down(btn):
  18. btn.was_held = True
  19. while btn.was_held:
  20. print("Vol Down")
  21. result = subprocess.run(
  22. [f"mpc volume -10"],
  23. stdout=subprocess.PIPE,
  24. shell=True,
  25. ).stdout.decode("utf-8")
  26. time.sleep(0.3)
  27. def mute(btn):
  28. btn.was_held = True
  29. while btn.was_held:
  30. print("Mute")
  31. result = subprocess.run(
  32. [f"mpc volume -10"],
  33. stdout=subprocess.PIPE,
  34. shell=True,
  35. ).stdout.decode("utf-8")
  36. time.sleep(0.3)
  37. def playpause_down(btn):
  38. btn.was_held = True
  39. while btn.was_held:
  40. print("playpause")
  41. time.sleep(0.3)
  42. def released(btn):
  43. if not btn.was_held:
  44. pressed(btn)
  45. btn.was_held = False
  46. def pressed(btn):
  47. if btn._pin.number == up_pin:
  48. print("next")
  49. result = subprocess.run(
  50. [f"{pathlib.Path().resolve()}/venv/bin/python3 ./player.py next"],
  51. stdout=subprocess.PIPE,
  52. shell=True,
  53. ).stdout.decode("utf-8")
  54. elif btn._pin.number == down_pin:
  55. print("back")
  56. result = subprocess.run(
  57. [f"{pathlib.Path().resolve()}/venv/bin/python3 ./player.py previous"],
  58. stdout=subprocess.PIPE,
  59. shell=True,
  60. ).stdout.decode("utf-8")
  61. elif btn._pin.number == playpause_pin:
  62. print("playpause")
  63. result = subprocess.run(
  64. [f"{pathlib.Path().resolve()}/venv/bin/python3 ./player.py playpause"],
  65. stdout=subprocess.PIPE,
  66. shell=True,
  67. ).stdout.decode("utf-8")
  68. elif btn._pin.number == shuffle_pin:
  69. print("shuffle")
  70. result = subprocess.run(
  71. [f"{pathlib.Path().resolve()}/venv/bin/python3 ./player.py shuffle"],
  72. stdout=subprocess.PIPE,
  73. shell=True,
  74. ).stdout.decode("utf-8")
  75. up_pin = 24
  76. down_pin = 15
  77. playpause_pin = 14
  78. shuffle_pin = 23
  79. up_btn = Button(up_pin)
  80. down_button = Button(down_pin)
  81. playpause_button = Button(playpause_pin)
  82. shuffle_button = Button(shuffle_pin)
  83. up_btn.when_held = vol_up
  84. up_btn.when_released = released
  85. down_button.when_held = vol_down
  86. down_button.when_released = released
  87. playpause_button.when_held = mute
  88. playpause_button.when_released = released
  89. shuffle_button.when_released = released
  90. pause()