buttons.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from gpiozero import Button
  2. from signal import pause
  3. import time
  4. import subprocess
  5. import beepy as beep
  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. # beep(sound=1)
  17. time.sleep(0.5)
  18. def vol_down(btn):
  19. btn.was_held = True
  20. while btn.was_held:
  21. # print("Vol Down")
  22. result = subprocess.run(
  23. [f"mpc volume -10"],
  24. stdout=subprocess.PIPE,
  25. shell=True,
  26. ).stdout.decode("utf-8")
  27. # beep(sound=1)
  28. time.sleep(0.5)
  29. def playpause_down(btn):
  30. btn.was_held = True
  31. while btn.was_held:
  32. print("playpause")
  33. time.sleep(1)
  34. def released(btn):
  35. if not btn.was_held:
  36. pressed(btn)
  37. btn.was_held = False
  38. def pressed(btn):
  39. if btn._pin.number == 15:
  40. # print("next")
  41. result = subprocess.run(
  42. [f"python ./player.py next"],
  43. stdout=subprocess.PIPE,
  44. shell=True,
  45. ).stdout.decode("utf-8")
  46. elif btn._pin.number == 24:
  47. # print("back")
  48. result = subprocess.run(
  49. [f"python ./player.py previous"],
  50. stdout=subprocess.PIPE,
  51. shell=True,
  52. ).stdout.decode("utf-8")
  53. elif btn._pin.number == 14:
  54. # print("playpause")
  55. result = subprocess.run(
  56. [f"python ./player.py playpause"],
  57. stdout=subprocess.PIPE,
  58. shell=True,
  59. ).stdout.decode("utf-8")
  60. elif btn._pin.number == 23:
  61. # print("shuffle")
  62. result = subprocess.run(
  63. [f"python ./player.py shuffle"],
  64. stdout=subprocess.PIPE,
  65. shell=True,
  66. ).stdout.decode("utf-8")
  67. up_btn = Button(15)
  68. down_button = Button(24)
  69. playpause_button = Button(14)
  70. shuffle_button = Button(23)
  71. down_button.when_held = vol_down
  72. down_button.when_released = released
  73. up_btn.when_held = vol_up
  74. up_btn.when_released = released
  75. playpause_button.when_released = released
  76. shuffle_button.when_released = released
  77. pause()