Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_Sensor2ImplUtil.h
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : OVR_Sensor2ImplUtil.h
4 Content : DK2 sensor device feature report utils.
5 Created : January 27, 2014
6 Authors : Lee Cooper
7 
8 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
9 
10 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
14 
15 You may obtain a copy of the License at
16 
17 http://www.oculusvr.com/licenses/LICENSE-3.1
18 
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24 
25 *************************************************************************************/
26 
27 #ifndef OVR_Sensor2ImplUtil_h
28 #define OVR_Sensor2ImplUtil_h
29 
30 #include "OVR_Device.h"
31 #include "OVR_SensorImpl_Common.h"
32 #include "Kernel/OVR_Alg.h"
33 
34 namespace OVR {
35 
36 using namespace Alg;
37 
38 // Tracking feature report.
40 {
41  enum { PacketSize = 13 };
42  UByte Buffer[PacketSize];
43 
45 
47  {
48  for (int i=0; i<PacketSize; i++)
49  {
50  Buffer[i] = 0;
51  }
52 
53  Buffer[0] = 12;
54  }
55 
56  TrackingImpl(const TrackingReport& settings)
57  : Settings(settings)
58  {
59  Pack();
60  }
61 
62  void Pack()
63  {
64 
65  Buffer[0] = 12;
66  EncodeUInt16 ( Buffer+1, Settings.CommandId );
67  Buffer[3] = Settings.Pattern;
68  Buffer[4] = UByte(Settings.Enable << 0 |
69  Settings.Autoincrement << 1 |
70  Settings.UseCarrier << 2 |
71  Settings.SyncInput << 3 |
72  Settings.VsyncLock << 4 |
73  Settings.CustomPattern << 5);
74  Buffer[5] = 0;
75  EncodeUInt16 ( Buffer+6, Settings.ExposureLength );
76  EncodeUInt16 ( Buffer+8, Settings.FrameInterval );
77  EncodeUInt16 ( Buffer+10, Settings.VsyncOffset );
78  Buffer[12] = Settings.DutyCycle;
79  }
80 
81  void Unpack()
82  {
83  Settings.CommandId = DecodeUInt16(Buffer+1);
84  Settings.Pattern = Buffer[3];
85  Settings.Enable = (Buffer[4] & 0x01) != 0;
86  Settings.Autoincrement = (Buffer[4] & 0x02) != 0;
87  Settings.UseCarrier = (Buffer[4] & 0x04) != 0;
88  Settings.SyncInput = (Buffer[4] & 0x08) != 0;
89  Settings.VsyncLock = (Buffer[4] & 0x10) != 0;
90  Settings.CustomPattern = (Buffer[4] & 0x20) != 0;
91  Settings.ExposureLength = DecodeUInt16(Buffer+6);
92  Settings.FrameInterval = DecodeUInt16(Buffer+8);
93  Settings.VsyncOffset = DecodeUInt16(Buffer+10);
94  Settings.DutyCycle = Buffer[12];
95  }
96 };
97 
98 // Display feature report.
100 {
101  enum { PacketSize = 16 };
102  UByte Buffer[PacketSize];
103 
105 
107  {
108  for (int i=0; i<PacketSize; i++)
109  {
110  Buffer[i] = 0;
111  }
112 
113  Buffer[0] = 13;
114  }
115 
116  DisplayImpl(const DisplayReport& settings)
117  : Settings(settings)
118  {
119  Pack();
120  }
121 
122  void Pack()
123  {
124 
125  Buffer[0] = 13;
126  EncodeUInt16 ( Buffer+1, Settings.CommandId );
127  Buffer[3] = Settings.Brightness;
128  Buffer[4] = UByte( (Settings.ShutterType & 0x0F) |
129  (Settings.CurrentLimit & 0x03) << 4 |
130  (Settings.UseRolling ? 0x40 : 0) |
131  (Settings.ReverseRolling ? 0x80 : 0));
132  Buffer[5] = UByte( (Settings.HighBrightness ? 0x01 : 0) |
133  (Settings.SelfRefresh ? 0x02 : 0) |
134  (Settings.ReadPixel ? 0x04 : 0) |
135  (Settings.DirectPentile ? 0x08 : 0));
136  EncodeUInt16 ( Buffer+8, Settings.Persistence );
137  EncodeUInt16 ( Buffer+10, Settings.LightingOffset );
138  EncodeUInt16 ( Buffer+12, Settings.PixelSettle );
139  EncodeUInt16 ( Buffer+14, Settings.TotalRows );
140  }
141 
142  void Unpack()
143  {
144 
145  Settings.CommandId = DecodeUInt16(Buffer+1);
146  Settings.Brightness = Buffer[3];
147  Settings.ShutterType = DisplayReport::ShutterTypeEnum(Buffer[4] & 0x0F);
148  Settings.CurrentLimit = DisplayReport::CurrentLimitEnum((Buffer[4] >> 4) & 0x02);
149  Settings.UseRolling = (Buffer[4] & 0x40) != 0;
150  Settings.ReverseRolling = (Buffer[4] & 0x80) != 0;
151  Settings.HighBrightness = (Buffer[5] & 0x01) != 0;
152  Settings.SelfRefresh = (Buffer[5] & 0x02) != 0;
153  Settings.ReadPixel = (Buffer[5] & 0x04) != 0;
154  Settings.DirectPentile = (Buffer[5] & 0x08) != 0;
155  Settings.Persistence = DecodeUInt16(Buffer+8);
156  Settings.LightingOffset = DecodeUInt16(Buffer+10);
157  Settings.PixelSettle = DecodeUInt16(Buffer+12);
158  Settings.TotalRows = DecodeUInt16(Buffer+14);
159  }
160 };
161 
162 // MagCalibration feature report.
164 {
165  enum { PacketSize = 52 };
166  UByte Buffer[PacketSize];
167 
169 
171  {
172  memset(Buffer, 0, sizeof(Buffer));
173  Buffer[0] = 14;
174  }
175 
177  : Settings(settings)
178  {
179  Pack();
180  }
181 
182  void Pack()
183  {
184  Buffer[0] = 14;
185  EncodeUInt16(Buffer+1, Settings.CommandId);
186  Buffer[3] = Settings.Version;
187 
188  for (int i = 0; i < 3; i++)
189  for (int j = 0; j < 4; j++)
190  {
191  SInt32 value = SInt32(Settings.Calibration.M[i][j] * 1e4f);
192  EncodeSInt32(Buffer + 4 + 4 * (4 * i + j), value);
193  }
194  }
195 
196  void Unpack()
197  {
198  Settings.CommandId = DecodeUInt16(Buffer+1);
199  Settings.Version = Buffer[3];
200 
201  for (int i = 0; i < 3; i++)
202  for (int j = 0; j < 4; j++)
203  {
204  SInt32 value = DecodeSInt32(Buffer + 4 + 4 * (4 * i + j));
205  Settings.Calibration.M[i][j] = (float)value * 1e-4f;
206  }
207  }
208 };
209 
210 //-------------------------------------------------------------------------------------
211 // PositionCalibration feature report.
212 // - Sensor interface versions before 5 do not support Normal and Rotation.
213 
215 {
216  enum { PacketSize = 30 };
217  UByte Buffer[PacketSize];
218 
220 
222  {
223  for (int i=0; i<PacketSize; i++)
224  {
225  Buffer[i] = 0;
226  }
227 
228  Buffer[0] = 15;
229  }
230 
232  : Settings(settings)
233  {
234  Pack();
235  }
236 
237  void Pack()
238  {
239 
240  Buffer[0] = 15;
241  EncodeUInt16(Buffer+1, Settings.CommandId);
242  Buffer[3] = Settings.Version;
243 
244  Vector3d position = Settings.Position * 1e6;
245  EncodeSInt32(Buffer+4, (SInt32) position.x);
246  EncodeSInt32(Buffer+8, (SInt32) position.y);
247  EncodeSInt32(Buffer+12, (SInt32) position.z);
248 
249  Vector3d normal = Settings.Normal * 1e6;
250  EncodeSInt16(Buffer+16, (SInt16) normal.x);
251  EncodeSInt16(Buffer+18, (SInt16) normal.y);
252  EncodeSInt16(Buffer+20, (SInt16) normal.z);
253 
254  double rotation = Settings.Angle * 1e4;
255  EncodeSInt16(Buffer+22, (SInt16) rotation);
256 
257  EncodeUInt16(Buffer+24, Settings.PositionIndex);
258  EncodeUInt16(Buffer+26, Settings.NumPositions);
259  EncodeUInt16(Buffer+28, UInt16(Settings.PositionType));
260  }
261 
262  void Unpack()
263  {
264  Settings.CommandId = DecodeUInt16(Buffer+1);
265  Settings.Version = Buffer[3];
266 
267  Settings.Position.x = DecodeSInt32(Buffer + 4) * 1e-6;
268  Settings.Position.y = DecodeSInt32(Buffer + 8) * 1e-6;
269  Settings.Position.z = DecodeSInt32(Buffer + 12) * 1e-6;
270 
271  Settings.Normal.x = DecodeSInt16(Buffer + 16) * 1e-6;
272  Settings.Normal.y = DecodeSInt16(Buffer + 18) * 1e-6;
273  Settings.Normal.z = DecodeSInt16(Buffer + 20) * 1e-6;
274 
275  Settings.Angle = DecodeSInt16(Buffer + 22) * 1e-4;
276 
277  Settings.PositionIndex = DecodeUInt16(Buffer + 24);
278  Settings.NumPositions = DecodeUInt16(Buffer + 26);
279 
280  Settings.PositionType = PositionCalibrationReport::PositionTypeEnum(DecodeUInt16(Buffer + 28));
281  }
282 };
283 
285 {
286  enum { PacketSize = 22 };
287  UByte Buffer[PacketSize];
288 
290 
292  {
293  for (int i=0; i<PacketSize; i++)
294  {
295  Buffer[i] = 0;
296  }
297 
298  Buffer[0] = 15;
299  }
300 
302  : Settings(settings)
303  {
304  Pack();
305  }
306 
307  void Pack()
308  {
309 
310  Buffer[0] = 15;
311  EncodeUInt16(Buffer+1, Settings.CommandId);
312  Buffer[3] = Settings.Version;
313 
314  Vector3d position = Settings.Position * 1e6;
315  EncodeSInt32(Buffer+4 , (SInt32) position.x);
316  EncodeSInt32(Buffer+8 , (SInt32) position.y);
317  EncodeSInt32(Buffer+12, (SInt32) position.z);
318 
319  EncodeUInt16(Buffer+16, Settings.PositionIndex);
320  EncodeUInt16(Buffer+18, Settings.NumPositions);
321  EncodeUInt16(Buffer+20, UInt16(Settings.PositionType));
322  }
323 
324  void Unpack()
325  {
326 
327  Settings.CommandId = DecodeUInt16(Buffer+1);
328  Settings.Version = Buffer[3];
329 
330  Settings.Position.x = DecodeSInt32(Buffer + 4) * 1e-6;
331  Settings.Position.y = DecodeSInt32(Buffer + 8) * 1e-6;
332  Settings.Position.z = DecodeSInt32(Buffer + 12) * 1e-6;
333 
334  Settings.PositionIndex = DecodeUInt16(Buffer + 16);
335  Settings.NumPositions = DecodeUInt16(Buffer + 18);
336  Settings.PositionType = PositionCalibrationReport::PositionTypeEnum(DecodeUInt16(Buffer + 20));
337  }
338 };
339 
340 // CustomPattern feature report.
342 {
343  enum { PacketSize = 12 };
344  UByte Buffer[PacketSize];
345 
347 
349  {
350  for (int i=0; i<PacketSize; i++)
351  {
352  Buffer[i] = 0;
353  }
354 
355  Buffer[0] = 16;
356  }
357 
359  : Settings(settings)
360  {
361  Pack();
362  }
363 
364  void Pack()
365  {
366 
367  Buffer[0] = 16;
368  EncodeUInt16(Buffer+1, Settings.CommandId);
369  Buffer[3] = Settings.SequenceLength;
370  EncodeUInt32(Buffer+4 , Settings.Sequence);
371  EncodeUInt16(Buffer+8 , Settings.LEDIndex);
372  EncodeUInt16(Buffer+10, Settings.NumLEDs);
373  }
374 
375  void Unpack()
376  {
377  Settings.CommandId = DecodeUInt16(Buffer+1);
378  Settings.SequenceLength = Buffer[3];
379  Settings.Sequence = DecodeUInt32(Buffer+4);
380  Settings.LEDIndex = DecodeUInt16(Buffer+8);
381  Settings.NumLEDs = DecodeUInt16(Buffer+10);
382  }
383 };
384 
385 // Manufacturing feature report.
387 {
388  enum { PacketSize = 16 };
389  UByte Buffer[PacketSize];
390 
392 
394  {
395  memset(Buffer, 0, sizeof(Buffer));
396  Buffer[0] = 18;
397  }
398 
400  : Settings(settings)
401  {
402  Pack();
403  }
404 
405  void Pack()
406  {
407  Buffer[0] = 18;
408  EncodeUInt16(Buffer+1, Settings.CommandId);
409  Buffer[3] = Settings.NumStages;
410  Buffer[4] = Settings.Stage;
411  Buffer[5] = Settings.StageVersion;
412  EncodeUInt16(Buffer+6, Settings.StageLocation);
413  EncodeUInt32(Buffer+8, Settings.StageTime);
414  EncodeUInt32(Buffer+12, Settings.Result);
415  }
416 
417  void Unpack()
418  {
419  Settings.CommandId = DecodeUInt16(Buffer+1);
420  Settings.NumStages = Buffer[3];
421  Settings.Stage = Buffer[4];
422  Settings.StageVersion = Buffer[5];
423  Settings.StageLocation = DecodeUInt16(Buffer+6);
424  Settings.StageTime = DecodeUInt32(Buffer+8);
425  Settings.Result = DecodeUInt32(Buffer+12);
426  }
427 };
428 
429 // UUID feature report.
430 struct UUIDImpl
431 {
432  enum { PacketSize = 23 };
433  UByte Buffer[PacketSize];
434 
436 
438  {
439  memset(Buffer, 0, sizeof(Buffer));
440  Buffer[0] = 19;
441  }
442 
443  UUIDImpl(const UUIDReport& settings)
444  : Settings(settings)
445  {
446  Pack();
447  }
448 
449  void Pack()
450  {
451  Buffer[0] = 19;
452  EncodeUInt16(Buffer+1, Settings.CommandId);
453  for (int i = 0; i < 20; ++i)
454  Buffer[3 + i] = Settings.UUIDValue[i];
455  }
456 
457  void Unpack()
458  {
459  Settings.CommandId = DecodeUInt16(Buffer+1);
460  for (int i = 0; i < 20; ++i)
461  Settings.UUIDValue[i] = Buffer[3 + i];
462  }
463 };
464 
465 // LensDistortion feature report.
467 {
468  enum { PacketSize = 64 };
469  UByte Buffer[PacketSize];
470 
472 
474  {
475  memset(Buffer, 0, sizeof(Buffer));
476  Buffer[0] = 22;
477  }
478 
480  : Settings(settings)
481  {
482  Pack();
483  }
484 
485  void Pack()
486  {
487  Buffer[0] = 19;
488  EncodeUInt16(Buffer+1, Settings.CommandId);
489 
490  Buffer[3] = Settings.NumDistortions;
491  Buffer[4] = Settings.DistortionIndex;
492  Buffer[5] = Settings.Bitmask;
493  EncodeUInt16(Buffer+6, Settings.LensType);
494  EncodeUInt16(Buffer+8, Settings.Version);
495  EncodeUInt16(Buffer+10, Settings.EyeRelief);
496 
497  for (int i = 0; i < 11; ++i)
498  EncodeUInt16(Buffer+12+2*i, Settings.KCoefficients[i]);
499 
500  EncodeUInt16(Buffer+34, Settings.MaxR);
501  EncodeUInt16(Buffer+36, Settings.MetersPerTanAngleAtCenter);
502 
503  for (int i = 0; i < 4; ++i)
504  EncodeUInt16(Buffer+38+2*i, Settings.ChromaticAberration[i]);
505  }
506 
507  void Unpack()
508  {
509  Settings.CommandId = DecodeUInt16(Buffer+1);
510 
511  Settings.NumDistortions = Buffer[3];
512  Settings.DistortionIndex = Buffer[4];
513  Settings.Bitmask = Buffer[5];
514  Settings.LensType = DecodeUInt16(Buffer+6);
515  Settings.Version = DecodeUInt16(Buffer+8);
516  Settings.EyeRelief = DecodeUInt16(Buffer+10);
517 
518  for (int i = 0; i < 11; ++i)
519  Settings.KCoefficients[i] = DecodeUInt16(Buffer+12+2*i);
520 
521  Settings.MaxR = DecodeUInt16(Buffer+34);
522  Settings.MetersPerTanAngleAtCenter = DecodeUInt16(Buffer+36);
523 
524  for (int i = 0; i < 4; ++i)
525  Settings.ChromaticAberration[i] = DecodeUInt16(Buffer+38+2*i);
526  }
527 };
528 
529 // KeepAliveMux feature report.
531 {
532  enum { PacketSize = 6 };
533  UByte Buffer[PacketSize];
534 
536 
538  {
539  memset(Buffer, 0, sizeof(Buffer));
540  Buffer[0] = 17;
541  }
542 
544  : Settings(settings)
545  {
546  Pack();
547  }
548 
549  void Pack()
550  {
551  Buffer[0] = 17;
552  EncodeUInt16(Buffer+1, Settings.CommandId);
553  Buffer[3] = Settings.INReport;
554  EncodeUInt16(Buffer+4, Settings.Interval);
555  }
556 
557  void Unpack()
558  {
559  Settings.CommandId = DecodeUInt16(Buffer+1);
560  Settings.INReport = Buffer[3];
561  Settings.Interval = DecodeUInt16(Buffer+4);
562  }
563 };
564 
565 // Temperature feature report.
567 {
568  enum { PacketSize = 24 };
569  UByte Buffer[PacketSize];
570 
572 
574  {
575  memset(Buffer, 0, sizeof(Buffer));
576  Buffer[0] = 20;
577  }
578 
580  : Settings(settings)
581  {
582  Pack();
583  }
584 
585  void Pack()
586  {
587 
588  Buffer[0] = 20;
589  EncodeUInt16(Buffer + 1, Settings.CommandId);
590  Buffer[3] = Settings.Version;
591 
592  Buffer[4] = Settings.NumBins;
593  Buffer[5] = Settings.Bin;
594  Buffer[6] = Settings.NumSamples;
595  Buffer[7] = Settings.Sample;
596 
597  EncodeSInt16(Buffer + 8 , SInt16(Settings.TargetTemperature * 1e2));
598  EncodeSInt16(Buffer + 10, SInt16(Settings.ActualTemperature * 1e2));
599 
600  EncodeUInt32(Buffer + 12, Settings.Time);
601 
602  Vector3d offset = Settings.Offset * 1e4;
603  PackSensor(Buffer + 16, (SInt16) offset.x, (SInt16) offset.y, (SInt16) offset.z);
604  }
605 
606  void Unpack()
607  {
608  Settings.CommandId = DecodeUInt16(Buffer + 1);
609  Settings.Version = Buffer[3];
610 
611  Settings.NumBins = Buffer[4];
612  Settings.Bin = Buffer[5];
613  Settings.NumSamples = Buffer[6];
614  Settings.Sample = Buffer[7];
615 
616  Settings.TargetTemperature = DecodeSInt16(Buffer + 8) * 1e-2;
617  Settings.ActualTemperature = DecodeSInt16(Buffer + 10) * 1e-2;
618 
619  Settings.Time = DecodeUInt32(Buffer + 12);
620 
621  SInt32 x, y, z;
622  UnpackSensor(Buffer + 16, &x, &y, &z);
623  Settings.Offset = Vector3d(x, y, z) * 1e-4;
624  }
625 };
626 
627 // GyroOffset feature report.
629 {
630  enum { PacketSize = 18 };
631  UByte Buffer[PacketSize];
632 
634 
636  {
637  memset(Buffer, 0, sizeof(Buffer));
638  Buffer[0] = 21;
639  }
640 
642  : Settings(settings)
643  {
644  Pack();
645  }
646 
647  void Pack()
648  {
649 
650  Buffer[0] = 21;
651  Buffer[1] = UByte(Settings.CommandId & 0xFF);
652  Buffer[2] = UByte(Settings.CommandId >> 8);
653  Buffer[3] = UByte(Settings.Version);
654 
655  Vector3d offset = Settings.Offset * 1e4;
656  PackSensor(Buffer + 4, (SInt32) offset.x, (SInt32) offset.y, (SInt32) offset.z);
657 
658  EncodeSInt16(Buffer + 16, SInt16(Settings.Temperature * 1e2));
659  }
660 
661  void Unpack()
662  {
663  Settings.CommandId = DecodeUInt16(Buffer + 1);
664  Settings.Version = GyroOffsetReport::VersionEnum(Buffer[3]);
665 
666  SInt32 x, y, z;
667  UnpackSensor(Buffer + 4, &x, &y, &z);
668  Settings.Offset = Vector3d(x, y, z) * 1e-4f;
669 
670  Settings.Temperature = DecodeSInt16(Buffer + 16) * 1e-2;
671  }
672 };
673 
674 } // namespace OVR
675 
676 #endif // OVR_Sensor2ImplUtil_h
UUIDImpl(const UUIDReport &settings)
ManufacturingReport Settings
ManufacturingImpl(const ManufacturingReport &settings)
KeepAliveMuxReport Settings
LensDistortionImpl(const LensDistortionReport &settings)
void EncodeUInt32(UByte *buffer, UInt32 val)
Definition: OVR_Alg.h:1027
CustomPatternReport Settings
SInt32 DecodeSInt32(const UByte *buffer)
Definition: OVR_Alg.h:1001
void EncodeUInt16(UByte *buffer, UInt16 val)
Definition: OVR_Alg.h:1017
uint16_t UInt16
Definition: OVR_Types.h:251
TemperatureImpl(const TemperatureReport &settings)
UInt32 DecodeUInt32(const UByte *buffer)
Definition: OVR_Alg.h:996
UInt16 DecodeUInt16(const UByte *buffer)
Definition: OVR_Alg.h:986
uint8_t UByte
Definition: OVR_Types.h:249
TemperatureReport Settings
DisplayImpl(const DisplayReport &settings)
PositionCalibrationImpl_Pre5(const PositionCalibrationReport &settings)
MagCalibrationReport Settings
LensDistortionReport Settings
void PackSensor(UByte *buffer, SInt32 x, SInt32 y, SInt32 z)
PositionCalibrationReport Settings
Vector3< double > Vector3d
Definition: OVR_Math.h:555
CustomPatternImpl(const CustomPatternReport &settings)
int16_t SInt16
Definition: OVR_Types.h:250
GyroOffsetImpl(const GyroOffsetReport &settings)
MagCalibrationImpl(const MagCalibrationReport &settings)
int32_t SInt32
Definition: OVR_Types.h:252
PositionCalibrationReport Settings
KeepAliveMuxImpl(const KeepAliveMuxReport &settings)
PositionCalibrationImpl(const PositionCalibrationReport &settings)
__BEGIN_NAMESPACE_STD void void __END_NAMESPACE_STD void __BEGIN_NAMESPACE_STD void * memset(void *__s, int __c, size_t __n) __THROW __nonnull((1))
void UnpackSensor(const UByte *buffer, SInt32 *x, SInt32 *y, SInt32 *z)
void EncodeSInt16(UByte *buffer, SInt16 val)
Definition: OVR_Alg.h:1022
TrackingReport Settings
DisplayReport Settings
TrackingImpl(const TrackingReport &settings)
SInt16 DecodeSInt16(const UByte *buffer)
Definition: OVR_Alg.h:991
GyroOffsetReport Settings
void EncodeSInt32(UByte *buffer, SInt32 val)
Definition: OVR_Alg.h:1032