PSMoveSteamVRBridge
ps_ds4_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 
8 namespace steamvrbridge {
9 
11  {
12  public:
13  static const int CONFIG_VERSION;
14 
15  PSDualshock4ControllerConfig(const std::string &fnamebase = "PSDualshock4ControllerConfig")
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  , calibration_offset_meters(0.f)
22  , disable_alignment_gesture(false)
23  , use_orientation_in_hmd_alignment(true)
24  , thumbstick_deadzone(k_defaultThumbstickDeadZoneRadius)
25  , linear_velocity_multiplier(1.f)
26  , linear_velocity_exponent(0.f)
27  {
28  };
29 
30  configuru::Config WriteToJSON() override;
31  bool ReadFromJSON(const configuru::Config &pt) override;
32 
33  // Rumble state
34  bool rumble_suppressed;
35 
36  // Virtual extend controller in meters.
37  float extend_Y_meters;
38  float extend_Z_meters;
39 
40  // Rotate controllers orientation 90 degrees about the z-axis (for gun style games).
41  bool z_rotate_90_degrees;
42 
43  // Settings value: used to determine how many meters in front of the HMD the controller
44  // is held when it's being calibrated.
45  float calibration_offset_meters;
46 
47  // Flag used to completely disable the alignment gesture.
48  bool disable_alignment_gesture;
49 
50  // Flag to tell if we should use the controller orientation as part of the controller alignment.
51  bool use_orientation_in_hmd_alignment;
52 
53  // The inner deadzone of the thumbsticks
54  float thumbstick_deadzone;
55 
56  // Settings values. Used to adjust throwing power using linear velocity and acceleration.
57  float linear_velocity_multiplier;
58  float linear_velocity_exponent;
59  };
60 
61  /* An un-tracked PSNavi controller.
62  The controller class bridges the PSMoveService controller to OpenVR's tracked device.*/
64 
65  public:
66  // Constructor/Destructor
67  PSDualshock4Controller(PSMControllerID psmControllerID, vr::ETrackedControllerRole trackedControllerRole, const char *psmSerialNo);
68  virtual ~PSDualshock4Controller();
69 
70  // Overridden Implementation of vr::ITrackedDeviceServerDriver
71  vr::EVRInitError Activate(vr::TrackedDeviceIndex_t unObjectId) override;
72  void Deactivate() override;
73 
74  // TrackableDevice interface implementation
75  vr::ETrackedDeviceClass GetTrackedDeviceClass() const override { return vr::TrackedDeviceClass_Controller; }
76  void Update() override;
77  void RefreshWorldFromDriverPose() override;
78 
79  // IController interface implementation
80  const char *GetControllerSettingsPrefix() const override { return "playstation_dualshock4"; }
81  bool HasPSMControllerId(int ControllerID) const override { return ControllerID == m_nPSMControllerId; }
82  const PSMController * GetPSMControllerView() const override { return m_PSMServiceController; }
83  std::string GetPSMControllerSerialNo() const override { return m_strPSMControllerSerialNo; }
84  PSMControllerType GetPSMControllerType() const override { return PSMController_Virtual; }
85 
86  protected:
87  const PSDualshock4ControllerConfig *getConfig() const { return static_cast<const PSDualshock4ControllerConfig *>(m_config); }
88  ControllerConfig *AllocateControllerConfig() override {
89  std::string fnamebase= std::string("ds4_") + m_strPSMControllerSerialNo;
90  return new PSDualshock4ControllerConfig(fnamebase);
91  }
92 
93  private:
94  void RemapThumbstick(
95  const float thumb_stick_x, const float thumb_stick_y,
96  float &out_sanitized_x, float &out_sanitized_y);
97  void UpdateThumbsticks();
98  void UpdateEmulatedTrackpad();
99  void UpdateControllerState();
100  void UpdateTrackingState();
101  void UpdateRumbleState(PSMControllerRumbleChannel channel);
102 
103  // Parent controller to send button events to
104  Controller *m_parentController;
105 
106  // Controller State
107  int m_nPSMControllerId;
108  PSMController *m_PSMServiceController;
109  std::string m_strPSMControllerSerialNo;
110 
111  // Used to report the controllers calibration status
112  vr::ETrackingResult m_trackingStatus;
113 
114  // Used to ignore old state from PSM Service
115  int m_nPoseSequenceNumber;
116 
117  std::chrono::time_point<std::chrono::high_resolution_clock> m_lastTouchpadPressTime;
118  bool m_touchpadDirectionsUsed;
119 
120  std::chrono::time_point<std::chrono::high_resolution_clock> m_resetPoseButtonPressTime;
121  bool m_bResetPoseRequestSent;
122  std::chrono::time_point<std::chrono::high_resolution_clock> m_resetAlignButtonPressTime;
123  bool m_bResetAlignRequestSent;
124 
125  // Flag to tell if we should use the controller orientation as part of the controller alignment
126  bool m_bUseControllerOrientationInHMDAlignment;
127 
128  // The last normalized thumbstick values (post dead zone application);
129  float m_lastSanitizedLeftThumbstick_X;
130  float m_lastSanitizedLeftThumbstick_Y;
131  float m_lastSanitizedRightThumbstick_X;
132  float m_lastSanitizedRightThumbstick_Y;
133 
134  // Callbacks
135  static void start_controller_response_callback(const PSMResponseMessage *response, void *userdata);
136  };
137 }
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_ds4_controller.h:10
Definition: controller.h:14
Definition: ps_ds4_controller.h:63