controllers.h 1.9 KB

123456789101112131415161718192021222324252627282930313233
  1. // controllers.h -- the emulated-USB-controller abstraction.
  2. //
  3. // Each USB personality (Steam puck, Xbox/XInput, Switch HORIPAD, Switch Pro, PS5 DualSense, DS4 gyro) is an
  4. // IController living in its own mode_*.cpp. Exactly ONE is active per boot, selected by g_usbMode. The split
  5. // of responsibilities:
  6. // begin() -- called once from setup(): set the USB device VID/PID/strings and register this mode's
  7. // interface(s). The shared USB lifecycle (detach, clear/keep CDC, serial suffix, WebUSB,
  8. // attach) stays in setup(); begin() only adds what's specific to this controller.
  9. // onReport45() -- called from rf_link.cpp each time a controller input report 0x45 is decoded. PUSH-style
  10. // modes (Xbox, puck/lizard) build + send their host report here. STREAM-style modes ignore
  11. // it and instead emit at a fixed cadence from task(); they read the decoded g_in directly.
  12. // task() -- called every loop(): streaming emit, handshake/subcommand draining, mode-specific upkeep.
  13. // isPuck() -- true for Steam/Lizard (keep the boot CDC composite; different USB lifecycle in setup()).
  14. //
  15. // To add a new controller: implement IController in a new mode_*.cpp, give it a singleton, and wire it into
  16. // controllerFor() in controllers.cpp. Nothing else needs to change.
  17. #pragma once
  18. #include <stdint.h>
  19. class IController {
  20. public:
  21. virtual ~IController() {}
  22. virtual void begin() = 0;
  23. virtual void onReport45(const uint8_t* rep, bool fresh, uint8_t bodyTlen) { (void)rep; (void)fresh; (void)bodyTlen; }
  24. virtual void task() {}
  25. virtual bool isPuck() const { return false; }
  26. };
  27. // The singleton for a given mode (nullptr if the mode is unknown). Defined in controllers.cpp.
  28. IController* controllerFor(uint8_t mode);
  29. // The active controller for this boot (set in setup()). rf_link/loop dispatch through it.
  30. extern IController* g_active;