oculus1

view libovr/Src/OVR_SensorImpl.h @ 17:cfe4979ab3eb

ops, minor error in the last commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 07:09:48 +0300
parents
children
line source
1 /************************************************************************************
3 Filename : OVR_SensorImpl.h
4 Content : Sensor device specific implementation.
5 Created : March 7, 2013
6 Authors : Lee Cooper
8 Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
10 Use of this software is subject to the terms of the Oculus license
11 agreement provided at the time of installation or download, or which
12 otherwise accompanies this software in either electronic or hard copy form.
14 *************************************************************************************/
16 #ifndef OVR_SensorImpl_h
17 #define OVR_SensorImpl_h
19 #include "OVR_HIDDeviceImpl.h"
21 namespace OVR {
23 struct TrackerMessage;
24 class ExternalVisitor;
26 //-------------------------------------------------------------------------------------
27 // SensorDeviceFactory enumerates Oculus Sensor devices.
28 class SensorDeviceFactory : public DeviceFactory
29 {
30 public:
31 static SensorDeviceFactory Instance;
33 // Enumerates devices, creating and destroying relevant objects in manager.
34 virtual void EnumerateDevices(EnumerateVisitor& visitor);
36 virtual bool MatchVendorProduct(UInt16 vendorId, UInt16 productId) const;
37 virtual bool DetectHIDDevice(DeviceManager* pdevMgr, const HIDDeviceDesc& desc);
38 protected:
39 DeviceManager* getManager() const { return (DeviceManager*) pManager; }
40 };
43 // Describes a single a Oculus Sensor device and supports creating its instance.
44 class SensorDeviceCreateDesc : public HIDDeviceCreateDesc
45 {
46 public:
47 SensorDeviceCreateDesc(DeviceFactory* factory, const HIDDeviceDesc& hidDesc)
48 : HIDDeviceCreateDesc(factory, Device_Sensor, hidDesc) { }
50 virtual DeviceCreateDesc* Clone() const
51 {
52 return new SensorDeviceCreateDesc(*this);
53 }
55 virtual DeviceBase* NewDeviceInstance();
57 virtual MatchResult MatchDevice(const DeviceCreateDesc& other,
58 DeviceCreateDesc**) const
59 {
60 if ((other.Type == Device_Sensor) && (pFactory == other.pFactory))
61 {
62 const SensorDeviceCreateDesc& s2 = (const SensorDeviceCreateDesc&) other;
63 if (MatchHIDDevice(s2.HIDDesc))
64 return Match_Found;
65 }
66 return Match_None;
67 }
69 virtual bool MatchHIDDevice(const HIDDeviceDesc& hidDesc) const
70 {
71 // should paths comparison be case insensitive?
72 return ((HIDDesc.Path.CompareNoCase(hidDesc.Path) == 0) &&
73 (HIDDesc.SerialNumber == hidDesc.SerialNumber));
74 }
76 virtual bool GetDeviceInfo(DeviceInfo* info) const;
77 };
80 //-------------------------------------------------------------------------------------
81 // ***** OVR::SensorDisplayInfoImpl
83 // DisplayInfo obtained from sensor; these values are used to report distortion
84 // settings and other coefficients.
85 // Older SensorDisplayInfo will have all zeros, causing the library to apply hard-coded defaults.
86 // Currently, only resolutions and sizes are used.
87 struct SensorDisplayInfoImpl
88 {
89 enum { PacketSize = 56 };
90 UByte Buffer[PacketSize];
92 enum
93 {
94 Mask_BaseFmt = 0x0f,
95 Mask_OptionFmts = 0xf0,
96 Base_None = 0,
97 Base_ScreenOnly = 1,
98 Base_Distortion = 2,
99 };
101 UInt16 CommandId;
102 UByte DistortionType;
103 UInt16 HResolution, VResolution;
104 float HScreenSize, VScreenSize;
105 float VCenter;
106 float LensSeparation;
107 float EyeToScreenDistance[2];
108 float DistortionK[6];
110 SensorDisplayInfoImpl();
112 void Unpack();
113 };
116 //-------------------------------------------------------------------------------------
117 // ***** OVR::SensorDeviceImpl
119 // Oculus Sensor interface.
121 class SensorDeviceImpl : public HIDDeviceImpl<OVR::SensorDevice>
122 {
123 public:
124 SensorDeviceImpl(SensorDeviceCreateDesc* createDesc);
125 ~SensorDeviceImpl();
128 // DeviceCommaon interface
129 virtual bool Initialize(DeviceBase* parent);
130 virtual void Shutdown();
132 virtual void SetMessageHandler(MessageHandler* handler);
134 // HIDDevice::Notifier interface.
135 virtual void OnInputReport(UByte* pData, UInt32 length);
136 virtual UInt64 OnTicks(UInt64 ticksMks);
138 // HMD-Mounted sensor has a different coordinate frame.
139 virtual void SetCoordinateFrame(CoordinateFrame coordframe);
140 virtual CoordinateFrame GetCoordinateFrame() const;
142 // SensorDevice interface
143 virtual bool SetRange(const SensorRange& range, bool waitFlag);
144 virtual void GetRange(SensorRange* range) const;
146 // Sets report rate (in Hz) of MessageBodyFrame messages (delivered through MessageHandler::OnMessage call).
147 // Currently supported maximum rate is 1000Hz. If the rate is set to 500 or 333 Hz then OnMessage will be
148 // called twice or thrice at the same 'tick'.
149 // If the rate is < 333 then the OnMessage / MessageBodyFrame will be called three
150 // times for each 'tick': the first call will contain averaged values, the second
151 // and third calls will provide with most recent two recorded samples.
152 virtual void SetReportRate(unsigned rateHz);
153 // Returns currently set report rate, in Hz. If 0 - error occurred.
154 // Note, this value may be different from the one provided for SetReportRate. The return
155 // value will contain the actual rate.
156 virtual unsigned GetReportRate() const;
158 // Hack to create HMD device from sensor display info.
159 static void EnumerateHMDFromSensorDisplayInfo(const SensorDisplayInfoImpl& displayInfo,
160 DeviceFactory::EnumerateVisitor& visitor);
161 protected:
163 void openDevice();
164 void closeDeviceOnError();
166 Void setCoordinateFrame(CoordinateFrame coordframe);
167 bool setRange(const SensorRange& range);
169 Void setReportRate(unsigned rateHz);
171 // Called for decoded messages
172 void onTrackerMessage(TrackerMessage* message);
174 // Helpers to reduce casting.
175 /*
176 SensorDeviceCreateDesc* getCreateDesc() const
177 { return (SensorDeviceCreateDesc*)pCreateDesc.GetPtr(); }
179 HIDDeviceDesc* getHIDDesc() const
180 { return &getCreateDesc()->HIDDesc; }
181 */
183 // Set if the sensor is located on the HMD.
184 // Older prototype firmware doesn't support changing HW coordinates,
185 // so we track its state.
186 CoordinateFrame Coordinates;
187 CoordinateFrame HWCoordinates;
188 UInt64 NextKeepAliveTicks;
190 bool SequenceValid;
191 SInt16 LastTimestamp;
192 UByte LastSampleCount;
193 float LastTemperature;
194 Vector3f LastAcceleration;
195 Vector3f LastRotationRate;
196 Vector3f LastMagneticField;
198 // Current sensor range obtained from device.
199 SensorRange MaxValidRange;
200 SensorRange CurrentRange;
202 UInt16 OldCommandId;
203 };
206 } // namespace OVR
208 #endif // OVR_SensorImpl_h