PSMoveSteamVRBridge
controller.h
1 #pragma once
2 #include "PSMoveClient_CAPI.h"
3 #include "constants.h"
4 #include "config.h"
5 #include "trackable_device.h"
6 
7 #include <map>
8 
9 namespace steamvrbridge {
10 
11  /*
12  Base class for the controller configuration
13  */
14  class ControllerConfig : public Config
15  {
16  public:
17  static const int CONFIG_VERSION;
18 
19  ControllerConfig(const std::string &fnamebase = "ControllerConfig");
20 
21  virtual configuru::Config WriteToJSON();
22  virtual bool ReadFromJSON(const configuru::Config &pt);
23 
24  void ReadEmulatedTouchpadAction(const configuru::Config &pt, const ePSMButtonID psButtonID);
25  void WriteEmulatedTouchpadAction(configuru::Config &pt, const ePSMButtonID psButtonID);
26 
27  bool is_valid;
28  long version;
29 
30  std::string override_model;
31 
32  // Used to map buttons to the emulated touchpad
33  eEmulatedTrackpadAction ps_button_id_to_emulated_touchpad_action[k_PSMButtonID_Count];
34  };
35 
36  /*
37  Interface that defines what a controller is and can do in the context of this driver.
38  */
39  class Controller : public TrackableDevice {
40 
41  /*
42  The following methods must be overridden in order for an implementing object to be considered
43  a Controller object. They also offer guidelines as to how to they should generally be
44  implemented while still respecting each controllers implementation may be different.
45 
46  With regards to function naming conventions used, functions prefixed with 'Update' suggest
47  both object state change and OpenVR/PSMS notification whereas functions prefixed with 'Set/Get'
48  suggest object state change only.
49  */
50  public:
51  struct HapticState
52  {
53  vr::VRInputComponentHandle_t hapticComponentHandle;
54  float pendingHapticDurationSecs;
55  float pendingHapticAmplitude;
56  float pendingHapticFrequency;
57  std::chrono::time_point<std::chrono::high_resolution_clock> lastTimeRumbleSent;
58  bool lastTimeRumbleSentValid;
59  };
60 
61  public:
62 
64  Controller();
65  virtual ~Controller();
66 
67  bool CreateButtonComponent(ePSMButtonID button_id);
68  bool CreateAxisComponent(ePSMAxisID axis_id);
69  bool CreateHapticComponent(ePSMHapicID haptic_id);
70 
71  void UpdateButton(ePSMButtonID button_id, PSMButtonState button_state, double time_offset=0.0);
72  void UpdateAxis(ePSMAxisID axis_id, float axis_value, double time_offset=0.0);
73  void UpdateHaptics(const vr::VREvent_HapticVibration_t &hapticData);
74 
75  bool HasButton(ePSMButtonID button_id) const;
76  bool HasAxis(ePSMAxisID axis_id) const;
77  bool HasHapticState(ePSMHapicID haptic_id) const;
78 
79  bool GetButtonState(ePSMButtonID button_id, PSMButtonState &out_button_state) const;
80  bool GetAxisState(ePSMAxisID axis_id, float &out_axis_value) const;
81  HapticState * GetHapticState(ePSMHapicID haptic_id);
82 
83  // Returns the controller name used to look-up the input profile.
84  virtual const char *GetControllerSettingsPrefix() const = 0;
85 
86  // Returns true if the controller has a PSM assigned the given ControllerID.
87  virtual bool HasPSMControllerId(int ControllerID) const = 0;
88 
89  // Returns the PSM controller view.
90  virtual const PSMController * GetPSMControllerView() const = 0;
91 
92  // Returns the PSM controller serial number.
93  virtual std::string GetPSMControllerSerialNo() const = 0;
94 
95  // Returns the PSM controller type.
96  virtual PSMControllerType GetPSMControllerType() const = 0;
97 
99  vr::EVRInitError Activate(vr::TrackedDeviceIndex_t unObjectId) override;
100  void Deactivate() override;
101 
102  protected:
103  virtual ControllerConfig *AllocateControllerConfig() { return new ControllerConfig(); }
104 
105  private:
106  struct ButtonState
107  {
108  vr::VRInputComponentHandle_t buttonComponentHandle;
109  PSMButtonState lastButtonState;
110  };
111 
112  struct AxisState
113  {
114  vr::VRInputComponentHandle_t axisComponentHandle;
115  float lastAxisState;
116  };
117 
118  // Component handle registered upon Activate() and called to update button/touch/axis/haptic events
119  std::map<ePSMButtonID, ButtonState> m_buttonStates;
120  std::map<ePSMAxisID, AxisState> m_axisStates;
121  std::map<ePSMHapicID, HapticState> m_hapticStates;
122 
123  protected:
124  ControllerConfig *m_config;
125  };
126 }
Definition: controller.h:39
Provides printf-style line logging via the vr::IVRDriverLog interface provided by SteamVR during init...
Definition: config.cpp:18
Definition: controller.h:51
Definition: config.h:27
Definition: trackable_device.h:11
Definition: controller.h:14