PSMoveSteamVRBridge
ps_move_controller.h
1 #pragma once
2 #include "trackable_device.h"
3 #include "constants.h"
4 #include "controller.h"
5 #include "PSMoveClient_CAPI.h"
6 #include <openvr_driver.h>
7 #include <chrono>
8 
9 namespace steamvrbridge {
10 
12  {
13  public:
14 
15  PSMoveControllerConfig(const std::string &fnamebase = "PSMoveControllerConfig")
16  : ControllerConfig(fnamebase)
17  , rumble_suppressed(false)
18  , extend_Y_meters(0.f)
19  , extend_Z_meters(0.f)
20  , z_rotate_90_degrees(false)
21  , delay_after_touchpad_press(false)
22  , meters_per_touchpad_axis_units(7.5f/100.f)
23  , calibration_offset_meters(0.f)
24  , disable_alignment_gesture(false)
25  , use_orientation_in_hmd_alignment(true)
26  , linear_velocity_multiplier(1.f)
27  , linear_velocity_exponent(0.f)
28  {
29  ps_button_id_to_emulated_touchpad_action[k_PSMButtonID_Move]= eEmulatedTrackpadAction::k_EmulatedTrackpadAction_Press;
30  };
31 
32  configuru::Config WriteToJSON() override;
33  bool ReadFromJSON(const configuru::Config &pt) override;
34 
35  // Rumble state
36  bool rumble_suppressed;
37 
38  // Virtual extend controller in meters.
39  float extend_Y_meters;
40  float extend_Z_meters;
41 
42  // Rotate controllers orientation 90 degrees about the z-axis (for gun style games).
43  bool z_rotate_90_degrees;
44 
45  // Delay in resetting touchpad position after touchpad press.
46  bool delay_after_touchpad_press;
47 
48  // Settings values. Used to determine whether we'll map controller movement after touchpad
49  // presses to touchpad axis values.
50  float meters_per_touchpad_axis_units;
51 
52  // Settings value: used to determine how many meters in front of the HMD the controller
53  // is held when it's being calibrated.
54  float calibration_offset_meters;
55 
56  // Flag used to completely disable the alignment gesture.
57  bool disable_alignment_gesture;
58 
59  // Flag to tell if we should use the controller orientation as part of the controller alignment.
60  bool use_orientation_in_hmd_alignment;
61 
62  // Settings values. Used to adjust throwing power using linear velocity and acceleration.
63  float linear_velocity_multiplier;
64  float linear_velocity_exponent;
65  };
66 
67  /* A trackable PS Move controller. The controller class bridges the PSMoveService controller to
68  OpenVR's tracked device.*/
69  class PSMoveController : public Controller {
70 
71  public:
72 
73  // Constructor/Destructor
74  PSMoveController(PSMControllerID psmControllerID, vr::ETrackedControllerRole trackedControllerRole, const char *psmSerialNo);
75  virtual ~PSMoveController();
76 
77  // Overridden Implementation of vr::ITrackedDeviceServerDriver
78  vr::EVRInitError Activate(vr::TrackedDeviceIndex_t unObjectId) override;
79  void Deactivate() override;
80 
81  // TrackableDevice interface implementation
82  vr::ETrackedDeviceClass GetTrackedDeviceClass() const override { return vr::TrackedDeviceClass_Controller; }
83  void Update() override;
84  void RefreshWorldFromDriverPose() override;
85 
86  // IController interface implementation
87  const char *GetControllerSettingsPrefix() const override { return "playstation_move"; }
88  bool HasPSMControllerId(int ControllerID) const override { return ControllerID == m_nPSMControllerId; }
89  const PSMController * GetPSMControllerView() const override { return m_PSMServiceController; }
90  std::string GetPSMControllerSerialNo() const override { return m_strPSMControllerSerialNo; }
91  PSMControllerType GetPSMControllerType() const override { return PSMController_Move; }
92 
93  protected:
94  const PSMoveControllerConfig *getConfig() const { return static_cast<const PSMoveControllerConfig *>(m_config); }
95  ControllerConfig *AllocateControllerConfig() override {
96  std::string fnamebase= std::string("psmove_") + m_strPSMControllerSerialNo;
97  return new PSMoveControllerConfig(fnamebase);
98  }
99 
100  private:
101  void UpdateEmulatedTrackpad();
102  void UpdateBatteryChargeState(PSMBatteryState newBatteryEnum);
103  void UpdateControllerState();
104  void UpdateTrackingState();
105  void UpdateRumbleState();
106 
107  // Controller State
108  int m_nPSMControllerId;
109  PSMController *m_PSMServiceController;
110  std::string m_strPSMControllerSerialNo;
111 
112  // Used to report the controllers calibration status
113  vr::ETrackingResult m_trackingStatus;
114 
115  // Used to ignore old state from PSM Service
116  int m_nPoseSequenceNumber;
117 
118  // Cached for answering version queries from vrserver
119  bool m_bIsBatteryCharging;
120  float m_fBatteryChargeFraction;
121 
122  // True while the touchpad is considered active (touched or pressed)
123  // after the initial touchpad delay, if any.
124  bool m_bTouchpadWasActive;
125 
126  std::chrono::time_point<std::chrono::high_resolution_clock> m_lastTouchpadPressTime;
127 
128  std::chrono::time_point<std::chrono::high_resolution_clock> m_resetPoseButtonPressTime;
129  bool m_bResetPoseRequestSent;
130  std::chrono::time_point<std::chrono::high_resolution_clock> m_resetAlignButtonPressTime;
131  bool m_bResetAlignRequestSent;
132 
133  // The position of the controller in meters in driver space relative to its own rotation
134  // at the time when the touchpad was most recently pressed (after being up).
135  PSMVector3f m_posMetersAtTouchpadPressTime;
136 
137  // The orientation of the controller in driver space at the time when
138  // the touchpad was most recently pressed (after being up).
139  PSMQuatf m_driverSpaceRotationAtTouchpadPressTime;
140 
141  // Callbacks
142  static void start_controller_response_callback(const PSMResponseMessage *response, void *userdata);
143  };
144 }
Definition: controller.h:39
Provides printf-style line logging via the vr::IVRDriverLog interface provided by SteamVR during init...
Definition: config.cpp:18
Definition: ps_move_controller.h:69
Definition: controller.h:14
Definition: ps_move_controller.h:11