Execute arbitrary commands based on joystick input
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.5 KiB

  1. #!/usr/bin/env python3
  2. import logging
  3. import sdl2
  4. import subprocess
  5. import time
  6. logging.basicConfig(level=logging.INFO)
  7. logger = logging.getLogger(__name__)
  8. sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK)
  9. joystick = sdl2.SDL_JoystickOpen(0)
  10. last_volume = None
  11. button_commands = {
  12. 6: ["obs-cli", "switch-scene", "Video"],
  13. 5: ["obs-cli", "switch-scene", "AFK"],
  14. 7: ["obs-cli", "switch-scene", "ThumbUp"],
  15. 4: ["obs-cli", "switch-scene", "RaiseHand"],
  16. 8: ["/home/hjacobs/.local/bin/toggle-mic-mute.sh"],
  17. }
  18. last_button_state = {}
  19. while True:
  20. sdl2.SDL_PumpEvents()
  21. try:
  22. for i in range(12):
  23. pressed = sdl2.SDL_JoystickGetButton(joystick, i)
  24. was_pressed = last_button_state.get(i)
  25. if pressed and not was_pressed:
  26. logger.info(f"Joystick button {i} was pressed")
  27. command = button_commands.get(i)
  28. if command:
  29. subprocess.check_call(command)
  30. last_button_state[i] = pressed
  31. offset = 32768
  32. max_val = 65535
  33. value = max_val - (sdl2.SDL_JoystickGetAxis(joystick, 3) + offset)
  34. percentage = int((value / max_val) * 100)
  35. if last_volume is not None and last_volume != percentage:
  36. subprocess.check_call(["amixer", "-q", "set", "Master", f"{percentage}%"])
  37. last_volume = percentage
  38. except KeyboardInterrupt:
  39. raise
  40. except Exception as e:
  41. logger.exception(f"Failed to process joystick events: {e}")
  42. time.sleep(0.1)