chord.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // chord.c — button chord → USB mode switch (see chord.h).
  2. //
  3. // Checked once per freshly-decoded BT report (raw g_in). The guard is a
  4. // deliberate 5-button combination (both triggers + both bumpers, or the four
  5. // back paddles, plus a face button), so it fires immediately on detection — no
  6. // hold. On-change pads (Xbox) therefore switch on the single press report.
  7. //
  8. // SPDX-License-Identifier: AGPL-3.0-or-later
  9. #include "puck/chord.h"
  10. #include "puck/triton.h"
  11. #include "config/modes.h"
  12. #include "config/picopuck_config.h"
  13. #include "sys/settings.h"
  14. #include "pico/stdlib.h"
  15. #include "hardware/watchdog.h"
  16. // Face → target mode (A always Steam). Mirrors OpenPuck's chordBtn idea, but the
  17. // defaults point at PicoPuck's implemented emulated modes.
  18. #define CHORD_B MODE_PS5
  19. #define CHORD_X MODE_HIDGYRO
  20. #define CHORD_Y MODE_SW_HORI
  21. static void mode_switch_reboot(uint8_t mode)
  22. {
  23. settings_set_mode(mode);
  24. watchdog_reboot(0, 0, 1);
  25. while (1) {
  26. }
  27. }
  28. void chord_note(int slot)
  29. {
  30. if (slot < 0 || slot >= PP_NSLOT)
  31. return;
  32. uint32_t b = g_in[slot].buttons;
  33. const uint32_t back4 = TB_L4 | TB_R4 | TB_L5 | TB_R5;
  34. const uint32_t trig = TB_L2 | TB_R2 | TB_LB | TB_RB;
  35. // Either guard works for any controller: the four back paddles (SC2) OR
  36. // L2+R2+LB+RB (every pad has these).
  37. if (((b & back4) != back4) && ((b & trig) != trig))
  38. return;
  39. uint8_t want = 0xFF;
  40. if (b & TB_A) want = MODE_STEAM;
  41. else if (b & TB_B) want = CHORD_B;
  42. else if (b & TB_X) want = CHORD_X;
  43. else if (b & TB_Y) want = CHORD_Y;
  44. if (want == 0xFF || want == settings_mode())
  45. return; // guard held but no (new) target picked → pass through
  46. mode_switch_reboot(want); // deliberate combo → switch immediately
  47. }