Monado OpenXR Runtime
Loading...
Searching...
No Matches
rift_internal.h
Go to the documentation of this file.
1// Copyright 2025, Beyley Cardellio
2// Copyright 2026, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Interface to Oculus Rift driver code.
7 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
8 * @ingroup drv_rift
9 */
10
11#pragma once
12
13#include "xrt/xrt_byte_order.h"
14
15#include "util/u_device.h"
16#include "util/u_logging.h"
18
19#include "math/m_imu_3dof.h"
20#include "math/m_api.h"
21#include "math/m_mathinclude.h"
23#include "math/m_filter_fifo.h"
24
25#include "tracking/t_imu.h"
27
28#include "hid/h_hid.h"
29#include "os/os_threading.h"
30
32
33#include <stdlib.h>
34#include <stdio.h>
35#include <assert.h>
36
37#include "rift_interface.h"
38
39
40#define HMD_TRACE(hmd, ...) U_LOG_XDEV_IFL_T(&hmd->base, hmd->log_level, __VA_ARGS__)
41#define HMD_DEBUG(hmd, ...) U_LOG_XDEV_IFL_D(&hmd->base, hmd->log_level, __VA_ARGS__)
42#define HMD_INFO(hmd, ...) U_LOG_XDEV_IFL_I(&hmd->base, hmd->log_level, __VA_ARGS__)
43#define HMD_WARN(hmd, ...) U_LOG_XDEV_IFL_W(&hmd->base, hmd->log_level, __VA_ARGS__)
44#define HMD_ERROR(hmd, ...) U_LOG_XDEV_IFL_E(&hmd->base, hmd->log_level, __VA_ARGS__)
45
46#define REPORT_MAX_SIZE 69 // max size of a feature report (FEATURE_REPORT_CALIBRATE)
47#define KEEPALIVE_INTERVAL_NS 10000000000 // 10 seconds
48// give a 5% breathing room (at 10 seconds, this is 500 milliseconds of breathing room)
49#define KEEPALIVE_SEND_RATE_NS ((KEEPALIVE_INTERVAL_NS * 19) / 20)
50#define IMU_SAMPLE_RATE (1000) // 1000hz
51#define NS_PER_SAMPLE (1000 * 1000) // 1ms (1,000,000 ns) per sample
52#define SERIAL_NUMBER_LENGTH 14
53#define RIFT_USB_LATENCY_BIAS (U_TIME_1US_IN_NS * 200LL) // 200us latency bias over USB
54#define RIFT_RADIO_LATENCY_BIAS (U_TIME_1MS_IN_NS * 4LL) // 4ms bias over radio
55// used when the display report can't be read, the persistence fallback is the same one Oculus uses, and the
56// total rows (active + vertical blank) are from the CV1's EDID
57#define RIFT_CV1_FALLBACK_PERSISTENCE_NS (U_TIME_1MS_IN_NS * 2LL)
58#define RIFT_CV1_FALLBACK_TOTAL_ROWS 1472
59
60#define CALIBRATION_HASH_BYTE_OFFSET 0x1bf0
61#define CALIBRATION_HASH_BYTE_LENGTH 0x10
62
63#define RIFT_CONFIG_SUBDIR "rift"
64
65#define CALIBRATION_HEADER_BYTE_OFFSET 0x0
66#define CALIBRATION_HEADER_BYTE_LENGTH 0x4
67
68#define CALIBRATION_BODY_BYTE_OFFSET 0x4
69#define CALIBRATION_BODY_BYTE_CHUNK_LENGTH 0x14
70
71#define MICROMETERS_TO_METERS(microns) ((float)microns / 1000000.0f)
72
73// value taken from LibOVR 0.4.4
74#define DEFAULT_EXTRA_EYE_ROTATION DEG_TO_RAD(30.0f)
75
76#define IN_REPORT_DK2 11 // sent on the HMD HID interface
77#define IN_REPORT_RADIO_DATA 12 // sent on the radio HID interface
78#define IN_REPORT_CV1_RADIO_KEEPALIVE 13 // sent on the HMD HID interface when no devices are connected
79
80#define IN_REPORT_RADIO_DATA_SIZE 64
81
82#ifdef __cplusplus
83extern "C" {
84#endif
85
86// asserts the size of a type is equal to the byte size provided
87#define SIZE_ASSERT(type, size) \
88 static_assert(sizeof(type) == (size), "Size of " #type " is not " #size " bytes as was expected")
89
90enum rift_feature_reports
91{
92 // DK1
93 FEATURE_REPORT_CONFIG = 2, // get + set
94 FEATURE_REPORT_CALIBRATE = 3, // get + set
95 FEATURE_REPORT_RANGE = 4, // get + set
96 FEATURE_REPORT_REGISTER = 5, // get + set
97 FEATURE_REPORT_DFU = 6, // get + set
98 FEATURE_REPORT_DK1_KEEP_ALIVE = 8, // get + set
99 FEATURE_REPORT_DISPLAY_INFO = 9, // get + set
100 FEATURE_REPORT_SERIAL = 10, // get + set
101
102 // DK2
103 FEATURE_REPORT_TRACKING = 12, // get + set
104 FEATURE_REPORT_DISPLAY = 13, // get + set
105 FEATURE_REPORT_MAG_CALIBRATION = 14, // get + set
106 FEATURE_REPORT_POS_CALIBRATION = 15, // get + set
107 FEATURE_REPORT_CUSTOM_PATTERN = 16, // get + set
108 FEATURE_REPORT_KEEPALIVE_MUX = 17, // get + set
109 FEATURE_REPORT_MANUFACTURING = 18, // get + set
110 FEATURE_REPORT_UUID = 19, // get + set
111 FEATURE_REPORT_TEMPERATURE = 20, // get + set
112 FEATURE_REPORT_GYROOFFSET = 21, // get only
113 FEATURE_REPORT_LENS_DISTORTION = 22, // get + set
114
115 // CV1
116 FEATURE_REPORT_RADIO_CONTROL = 26, // get + set
117 FEATURE_REPORT_RADIO_READ_DATA_CMD = 27, // @todo: get + ???
118 FEATURE_REPORT_ENABLE_COMPONENTS = 29, // @todo: ??? + set
119};
120
121enum rift_config_report_flags
122{
123 // output the sample data raw from the sensors without converting them to known units
124 RIFT_CONFIG_REPORT_USE_RAW = 1,
125 // internal test mode for calibrating zero rate drift on gyro
126 RIFT_CONFIG_REPORT_INTERNAL_CALIBRATION = 1 << 1,
127 // use the calibration parameters stored on the device
128 RIFT_CONFIG_REPORT_USE_CALIBRATION = 1 << 2,
129 // recalibrate the gyro zero rate offset when the device is stationary
130 RIFT_CONFIG_REPORT_AUTO_CALIBRATION = 1 << 3,
131 // stop sending IN reports when the device has stopped moving for Interval milliseconds
132 RIFT_CONFIG_REPORT_MOTION_KEEP_ALIVE = 1 << 4,
133 // stop sending IN reports when the device has stopped receiving feature reports for Interval milliseconds
134 RIFT_CONFIG_REPORT_COMMAND_KEEP_ALIVE = 1 << 5,
135 // output the IN report data in the coordinate system used by LibOVR relative to the tracker, otherwise, report
136 // in the coordinate system of the device
137 RIFT_CONFIG_REPORT_USE_SENSOR_COORDINATES = 1 << 6,
138 // override the power state of the USB hub, forcing it to act as if the external power source is connected (DK2
139 // only, does nothing on DK1)
140 RIFT_CONFIG_REPORT_OVERRIDE_POWER = 1 << 7,
141};
142
143enum rift_distortion_type
144{
145 RIFT_DISTORTION_TYPE_DIMS = 1,
146 RIFT_DISTORTION_TYPE_K = 2,
147};
148
149enum rift_lens_type
150{
151 // firmware indirectly states lens type A is 0
152 RIFT_LENS_TYPE_A = 0,
153 // firmware does not state what lens type B is, 1 is an educated guess
154 RIFT_LENS_TYPE_B = 1,
155};
156
157enum rift_lens_distortion_version
158{
159 // no distortion data is stored
160 RIFT_LENS_DISTORTION_NONE = 0,
161 // standard distortion matrix
162 RIFT_LENS_DISTORTION_LCSV_CATMULL_ROM_10_VERSION_1 = 1,
163};
164
165enum rift_component_flags
166{
167 RIFT_COMPONENT_DISPLAY = 1 << 0,
168 RIFT_COMPONENT_AUDIO = 1 << 1,
169 RIFT_COMPONENT_LEDS = 1 << 2,
170};
171
172/*
173 *
174 * Packed structs for USB communication
175 *
176 */
177
178#pragma pack(push, 1)
179
181{
182 uint16_t command_id;
183 uint8_t config_flags;
184 // the IN report rate of the headset, rate is calculated as `sample_rate / (1 + interval)`
185 uint8_t interval;
186 // sample rate of the IMU, always 1000hz on DK1/DK2, read-only
187 uint16_t sample_rate;
188};
189
190SIZE_ASSERT(struct rift_config_report, 6);
191
193{
194 uint16_t command_id;
195 uint8_t distortion_type;
196 // the horizontal resolution of the display, in pixels
197 uint16_t resolution_x;
198 // the vertical resolution of the display, in pixels
199 uint16_t resolution_y;
200 // width in micrometers
201 uint32_t display_width;
202 // height in micrometers
203 uint32_t display_height;
204 // the vertical center of the display, in micrometers
205 uint32_t center_v;
206 // the separation between the two lenses, in micrometers
207 uint32_t lens_separation;
208 uint32_t lens_distance[2];
209 float distortion[6];
210};
211
212SIZE_ASSERT(struct rift_display_info_report, 55);
213
214#define CATMULL_COEFFICIENTS 11
215
217{
218 // eye relief setting, in micrometers from front surface of lens
219 uint16_t eye_relief;
220 // the k coeffecients of the distortion
221 uint16_t k[CATMULL_COEFFICIENTS];
222 uint16_t max_r;
223 uint16_t meters_per_tan_angle_at_center;
224 uint16_t chromatic_abberation[U_RIFT_CHROMATIC_ABBERATION_COUNT];
225 uint8_t unused[14];
226};
227
228SIZE_ASSERT(struct rift_catmull_rom_distortion_report_data, 50);
229
231{
232 uint16_t command_id;
233 // the amount of distortions on this device
234 uint8_t num_distortions;
235 // the index of this distortion in the devices array
236 uint8_t distortion_idx;
237 // unused bitmask field
238 uint8_t bitmask;
239 // the type of the lenses
240 uint16_t lens_type;
241 // the version of the lens distortion data
242 uint16_t distortion_version;
243
244 union {
245 struct rift_catmull_rom_distortion_report_data lcsv_catmull_rom_10;
246 } data;
247};
248
249SIZE_ASSERT(struct rift_lens_distortion_report, 9 + sizeof(struct rift_catmull_rom_distortion_report_data));
250
251enum rift_position_calibration_version
252{
253 // no data stored
254 RIFT_POSITION_CALIBRATION_VERSION_NONE = 0,
255 // hard-coded default positions
256 RIFT_POSITION_CALIBRATION_VERSION_DEFAULT = 1,
257 // factory calibrated
258 RIFT_POSITION_CALIBRATION_VERSION_FACTORY = 2,
259 // user calibrated
260 RIFT_POSITION_CALIBRATION_VERSION_USER = 3,
261};
262
263enum rift_position_calibration_type
264{
265 RIFT_POSITION_CALIBRATION_TYPE_LED = 0,
266 RIFT_POSITION_CALIBRATION_TYPE_INERTIAL_SENSOR = 1,
267};
268
270{
271 uint16_t command_id;
272 // the version/type of calibration, see rift_position_calibration_version
273 uint8_t version;
274 // the x/y/z position of the object, this is a signed integer in micrometers, position is relative to the center
275 // of the emitter plane of the display at nominal focus.
276 int32_t position[3];
277 // the x/y/z axis normal of the object, this is a signed integer in micrometers, normal is relative to the
278 // position
279 int16_t normal[3];
280 // rotation around the normal, in units of 10^-4 radians
281 uint16_t rotation;
282 // the current position in the array of LEDs, increments on reads, gets set to the value on writes
283 uint16_t position_index;
284 // read-only value of the number of LEDs
285 uint16_t position_count;
286 // the type of the object being described, see rift_position_calibration_type
287 uint16_t position_type;
288};
289
290SIZE_ASSERT(struct rift_position_calibration_report, 29);
291
292enum rift_custom_pattern_state
293{
294 RIFT_CUSTOM_PATTERN_STAT_OFF = 0,
295 RIFT_CUSTOM_PATTERN_STAT_LOW = 1,
296 RIFT_CUSTOM_PATTERN_STAT_HIGH = 3,
297};
298
300{
301 uint16_t command_id;
302 // the length of the sequence that each LED goes through
303 uint8_t sequence_length;
304 // the sequence the specific LED goes through, 2 bits per state, 0 (off), 1 (low), and 3 (high), ordered from
305 // LSB to MSB
306 uint32_t sequence;
307 // the current LED being described, increments on reads, gets set to the value on writes
308 uint16_t led_index;
309 // the number of tracking LEDs present on the device
310 uint16_t led_count;
311};
312
313SIZE_ASSERT(struct rift_custom_pattern_report, 11);
314
316{
317 uint16_t command;
318 uint8_t in_report;
319 uint16_t interval;
320};
321
322SIZE_ASSERT(struct rift_dk2_keepalive_mux_report, 5);
323
324enum rift_display_mode
325{
326 RIFT_DISPLAY_MODE_GLOBAL,
327 RIFT_DISPLAY_MODE_ROLLING_TOP_BOTTOM,
328 RIFT_DISPLAY_MODE_ROLLING_LEFT_RIGHT,
329 RIFT_DISPLAY_MODE_ROLLING_RIGHT_LEFT,
330};
331
332enum rift_display_limit
333{
334 RIFT_DISPLAY_LIMIT_ACL_OFF = 0,
335 RIFT_DISPLAY_LIMIT_ACL_30 = 1,
336 RIFT_DISPLAY_LIMIT_ACL_25 = 2,
337 RIFT_DISPLAY_LIMIT_ACL_50 = 3,
338};
339
340enum rift_display_flags
341{
342 // read only, see rift_display_mode, follows RIFT_DISPLAY_USE_ROLLING on the DK2 but not the CV1
343 RIFT_DISPLAY_MODE_MASK = 0xF,
344 RIFT_DISPLAY_USE_ROLLING = 1 << 6,
345 RIFT_DISPLAY_REVERSE_ROLLING = 1 << 7,
346 RIFT_DISPLAY_HIGH_BRIGHTNESS = 1 << 8,
347 RIFT_DISPLAY_SELF_REFRESH = 1 << 9,
348 RIFT_DISPLAY_READ_PIXEL = 1 << 10,
349 RIFT_DISPLAY_DIRECT_PENTILE = 1 << 11,
350};
351
353{
354 uint16_t command_id;
355 // relative brightness setting independent of pixel persistence, only effective when high brightness is disabled
356 uint8_t brightness;
357 // a set of flags, ordered from LSB -> MSB
358 // - panel mode/shutter type (4 bits), read only, see rift_display_mode
359 // - current limit (2 bits), see rift_display_limit
360 // - use rolling (1 bit)
361 // - reverse rolling (1 bit), unavailable on released DK2 firmware for unknown reason
362 // - high brightness (1 bit), unavailable on released DK2 firmware for unpublished reason
363 // - self refresh (1 bit)
364 // - read pixel (1 bit)
365 // - direct pentile (1 bit)
366 uint32_t flags;
367 // the length of time in rows that the display is lit each frame, defaults to the full size of the display, full
368 // persistence
369 uint16_t persistence;
370 // the offset in rows from vsync that the panel is lit when using global shutter, no effect in rolling shutter,
371 // disabled on released DK2 firmware for unknown reason
372 uint16_t lighting_offset;
373 // the time in microseconds it is estimated for a pixel to settle to one value after it is set, read only
374 uint16_t pixel_settle;
375 // the number of rows including active area and blanking period used with persistence and lightingoffset, read
376 // only
377 uint16_t total_rows;
378};
379
380SIZE_ASSERT(struct rift_display_report, 15);
381
383{
384 uint8_t data[8];
385};
386
387SIZE_ASSERT(struct rift_dk2_sensor_sample, 8);
388
390{
391 struct rift_dk2_sensor_sample accel;
392 struct rift_dk2_sensor_sample gyro;
393};
394
395SIZE_ASSERT(struct rift_dk2_sample_pack, sizeof(struct rift_dk2_sensor_sample) * 2);
396
398{
399 int16_t mag_x;
400 int16_t mag_y;
401 int16_t mag_z;
402};
403
404SIZE_ASSERT(struct rift_dk2_version_data, 6);
405
407{
408 uint16_t presence_sensor;
409 uint16_t iad_adc_value;
410 uint16_t unk;
411};
412
413SIZE_ASSERT(struct rift_cv1_version_data, 6);
414static_assert(sizeof(struct rift_cv1_version_data) == sizeof(struct rift_dk2_version_data),
415 "Incorrect version data size");
416
417#define DK2_MAX_SAMPLES 2
419{
420 uint16_t command_id;
421 uint8_t num_samples;
422 uint16_t sample_count;
423 uint16_t temperature;
424 uint32_t sample_timestamp;
425 struct rift_dk2_sample_pack samples[DK2_MAX_SAMPLES];
426 union {
427 struct rift_dk2_version_data dk2;
428 struct rift_cv1_version_data cv1;
429 };
430 uint16_t frame_count;
431 uint32_t frame_timestamp;
432 uint8_t frame_id;
433 uint8_t tracking_pattern;
434 uint16_t tracking_count;
435 uint32_t tracking_timestamp;
436};
437
438SIZE_ASSERT(struct dk2_in_report, 63);
439
441{
442 uint16_t command_id;
443 // which components to enable, see rift_component_flags
444 uint8_t flags;
445};
446
447SIZE_ASSERT(struct rift_enable_components_report, 3);
448
450{
451 uint16_t command_id;
452 struct rift_dk2_sample_pack offset;
453 struct rift_dk2_sample_pack matrix_samples[3];
454 uint16_t temperature;
455};
456
457SIZE_ASSERT(struct rift_imu_calibration_report, sizeof(struct rift_dk2_sample_pack) * 4 + 4);
458
459enum rift_radio_read_cmd
460{
461 RIFT_RADIO_READ_CMD_FLASH_CONTROL = 0x0a,
462 RIFT_RADIO_READ_CMD_SERIAL = 0x88,
463};
464
466{
467 uint16_t command_id;
468 uint8_t a;
469 uint8_t b;
470 uint8_t c;
471};
472
473SIZE_ASSERT(struct rift_radio_cmd_report, 5);
474
476{
477 uint16_t command_id;
478 uint16_t offset;
479 uint16_t length;
480 uint8_t unk[28];
481};
482
483SIZE_ASSERT(struct rift_radio_data_read_cmd, 34);
484
486{
487 uint8_t unk[5];
488 __le16 data_length;
489};
490SIZE_ASSERT(struct rift_radio_flash_read_response_header, 7);
491
493{
494 uint16_t command_id;
495 uint8_t radio_address[5];
496};
497
498SIZE_ASSERT(struct rift_radio_address_radio_report, 7);
499
500enum rift_radio_report_remote_button_masks
501{
502 RIFT_REMOTE_BUTTON_MASK_DPAD_UP = 0x001,
503 RIFT_REMOTE_BUTTON_MASK_DPAD_DOWN = 0x002,
504 RIFT_REMOTE_BUTTON_MASK_DPAD_LEFT = 0x004,
505 RIFT_REMOTE_BUTTON_MASK_DPAD_RIGHT = 0x008,
506 RIFT_REMOTE_BUTTON_MASK_SELECT = 0x010,
507 RIFT_REMOTE_BUTTON_MASK_VOLUME_UP = 0x020,
508 RIFT_REMOTE_BUTTON_MASK_VOLUME_DOWN = 0x040,
509 RIFT_REMOTE_BUTTON_MASK_OCULUS = 0x080,
510 RIFT_REMOTE_BUTTON_MASK_BACK = 0x100,
511};
512
514{
515 // the button state of the controller, see rift_radio_report_remote_button_masks
516 uint16_t buttons;
517};
518
519SIZE_ASSERT(struct rift_radio_report_remote_message, 2);
520
521enum rift_radio_report_touch_buttons
522{
523 RIFT_TOUCH_CONTROLLER_BUTTON_A = 0x01,
524 RIFT_TOUCH_CONTROLLER_BUTTON_X = 0x01,
525 RIFT_TOUCH_CONTROLLER_BUTTON_B = 0x02,
526 RIFT_TOUCH_CONTROLLER_BUTTON_Y = 0x02,
527 RIFT_TOUCH_CONTROLLER_BUTTON_MENU = 0x04,
528 RIFT_TOUCH_CONTROLLER_BUTTON_OCULUS = 0x04,
529 RIFT_TOUCH_CONTROLLER_BUTTON_STICK = 0x08,
530};
531
532enum rift_radio_report_adc_channel
533{
534 RIFT_TOUCH_CONTROLLER_ADC_STICK = 0x01,
535 RIFT_TOUCH_CONTROLLER_ADC_B_Y = 0x02,
536 RIFT_TOUCH_CONTROLLER_ADC_TRIGGER = 0x03,
537 RIFT_TOUCH_CONTROLLER_ADC_A_X = 0x04,
538 RIFT_TOUCH_CONTROLLER_ADC_THUMBREST = 0x08,
539 // seen with values varying per controller, maybe power draw? temperature? my left controller while powered on
540 // had the value slowly rise from 2800 to 3000 over the span of a couple minutes, dunno what that could be tbh
541 RIFT_TOUCH_CONTROLLER_ADC_UNK1 = 0x20,
542 RIFT_TOUCH_CONTROLLER_ADC_BATTERY = 0x21,
543 RIFT_TOUCH_CONTROLLER_ADC_HAPTIC_COUNTER = 0x23,
544};
545
547{
548 uint32_t timestamp;
549 int16_t accel[3];
550 int16_t gyro[3];
551 uint8_t buttons;
552 uint8_t touch_grip_stick_state[5];
553 // see rift_radio_report_adc_channel
554 uint8_t adc_channel;
555 uint16_t adc_value;
556};
557
558SIZE_ASSERT(struct rift_radio_report_touch_message, 25);
559
560enum rift_radio_device_type
561{
562 RIFT_RADIO_DEVICE_REMOTE = 1,
563 RIFT_RADIO_DEVICE_LEFT_TOUCH = 2,
564 RIFT_RADIO_DEVICE_RIGHT_TOUCH = 3,
565 RIFT_RADIO_DEVICE_TRACKED_OBJECT = 6,
566};
567
569{
570 uint16_t flags;
571 // the type of device sending the message, see rift_radio_device_type
572 uint8_t device_type;
573 union {
576 };
577};
578
579SIZE_ASSERT(struct rift_radio_report_message, 3 + sizeof(struct rift_radio_report_touch_message));
580
582{
583 uint16_t command_id;
584 struct rift_radio_report_message messages[2];
585};
586
587enum rift_tracking_flags
588{
589 // enable the tracking LED exposure and updating
590 RIFT_TRACKING_ENABLE = 1 << 0,
591 // automatically increment the pattern index after each exposure
592 RIFT_TRACKING_AUTO_INCREMENT = 1 << 1,
593 // modulate the tracking LEDs at 85kHz to allow wireless sync, defaults to on
594 RIFT_TRACKING_USE_CARRIER = 1 << 2,
595 // trigger LED exposure using a rising edge of GPIO1, else triggered on a timer
596 RIFT_TRACKING_SYNC_INPUT = 1 << 3,
597 // trigger LED exposure on each vsync rather than an internal or external timer
598 RIFT_TRACKING_VSYNC_LOCK = 1 << 4,
599 // use the custom pattern given to the headset
600 RIFT_TRACKING_CUSTOM_PATTERN = 1 << 5,
601};
602
604{
605 uint16_t command_id;
606 // the index of the current pattern being flashed, pattern 255 is reserved for "all high"
607 uint8_t pattern_idx;
608 // the enabled tracking flags, see rift_tracking_flags
609 uint16_t flags;
610 // the amount of time to enable the LEDs for during an exposure, sync output also follows this length, cannot be
611 // longer than frame_interval, and has a minimum of 10 microseconds
612 uint16_t exposure_length;
613 // when SYNCINPUT and VSYNC_LOCK are false, the tracking LEDs are exposed on the interval set here, in
614 // microseconds
615 uint16_t frame_interval;
616 // when VSYNC_LOCK is true, this gives a fixed microsecond offset from the vsync to when the LEDs are triggered
617 uint16_t vsync_offset;
618 // the duty cycle of the 85kHz modulation, defaults to 128, resulting in a 50% duty cycle
619 uint8_t duty_cycle;
620};
621
622SIZE_ASSERT(struct rift_tracking_report, 12);
623
624#pragma pack(pop)
625
626/*
627 *
628 * Parsed structs for internal use
629 *
630 */
631
633{
634 // the k coeffecients of the distortion
635 float k[CATMULL_COEFFICIENTS];
636 float max_r;
637 float meters_per_tan_angle_at_center;
638 float chromatic_abberation[U_RIFT_CHROMATIC_ABBERATION_COUNT];
639};
640
642{
643 // the version of the lens distortion data
644 uint16_t distortion_version;
645 // eye relief setting, in meters from surface of lens
646 float eye_relief;
647
648 union {
649 struct rift_catmull_rom_distortion_data lcsv_catmull_rom_10;
650 } data;
651};
652
654{
655 struct xrt_vec3 gyro_offset;
656 struct xrt_vec3 accel_offset;
657 struct xrt_matrix_3x3 gyro_matrix;
658 struct xrt_matrix_3x3 accel_matrix;
659 float temperature;
660};
661
662enum rift_touch_controller_input
663{
664 // left
665 RIFT_TOUCH_CONTROLLER_INPUT_X_CLICK = 0,
666 RIFT_TOUCH_CONTROLLER_INPUT_X_TOUCH = 1,
667 RIFT_TOUCH_CONTROLLER_INPUT_Y_CLICK = 2,
668 RIFT_TOUCH_CONTROLLER_INPUT_Y_TOUCH = 3,
669 RIFT_TOUCH_CONTROLLER_INPUT_SYSTEM_CLICK = 4,
670 // right
671 RIFT_TOUCH_CONTROLLER_INPUT_A_CLICK = 0,
672 RIFT_TOUCH_CONTROLLER_INPUT_A_TOUCH = 1,
673 RIFT_TOUCH_CONTROLLER_INPUT_B_CLICK = 2,
674 RIFT_TOUCH_CONTROLLER_INPUT_B_TOUCH = 3,
675 RIFT_TOUCH_CONTROLLER_INPUT_MENU_CLICK = 4,
676 // both
677 RIFT_TOUCH_CONTROLLER_INPUT_SQUEEZE_VALUE = 5,
678 RIFT_TOUCH_CONTROLLER_INPUT_TRIGGER_TOUCH = 6,
679 RIFT_TOUCH_CONTROLLER_INPUT_TRIGGER_VALUE = 7,
680 RIFT_TOUCH_CONTROLLER_INPUT_THUMBSTICK_CLICK = 8,
681 RIFT_TOUCH_CONTROLLER_INPUT_THUMBSTICK_TOUCH = 9,
682 RIFT_TOUCH_CONTROLLER_INPUT_THUMBSTICK = 10,
683 RIFT_TOUCH_CONTROLLER_INPUT_THUMBREST_TOUCH = 11,
684 RIFT_TOUCH_CONTROLLER_INPUT_GRIP_POSE = 12,
685 RIFT_TOUCH_CONTROLLER_INPUT_AIM_POSE = 13,
686 RIFT_TOUCH_CONTROLLER_INPUT_TRIGGER_PROXIMITY = 14,
687 RIFT_TOUCH_CONTROLLER_INPUT_THUMB_PROXIMITY = 15,
688 RIFT_TOUCH_CONTROLLER_INPUT_COUNT = 16,
689};
690
692{
693 struct xrt_vec3 position;
694 struct xrt_vec3 normal;
695 struct xrt_vec3 angles;
696};
697
699{
700 uint16_t joy_x_range[2];
701 uint16_t joy_x_dead[2];
702 uint16_t joy_y_range[2];
703 uint16_t joy_y_dead[2];
704
705 // min - mid - max
706 uint16_t trigger_range[3];
707
708 // min - mid - max
709 uint16_t middle_range[3];
710 bool middle_flipped;
711
712 uint16_t cap_sense_min[8];
713 uint16_t cap_sense_touch[8];
714
715 float gyro_calibration[3][3];
716 struct xrt_vec3 gyro_offset;
717 float accel_calibration[3][3];
718 struct xrt_vec3 accel_offset;
719
720 struct xrt_vec3 imu_position;
721
722 size_t num_leds;
723 struct rift_touch_controller_led *leds;
724};
725
727{
728 uint8_t buttons;
729 float trigger;
730 float grip;
731 struct xrt_vec2 stick;
732 uint8_t haptic_counter;
733 float cap_stick;
734 float cap_b_y;
735 float cap_a_x;
736 float cap_trigger;
737 float cap_thumbrest;
738};
739
740/*!
741 * A Rift Touch controller device.
742 *
743 * @implements xrt_device
744 */
746{
747 struct xrt_device base;
748 struct xrt_frame_node node;
749
750 struct rift_hmd *hmd;
751
752 enum rift_radio_device_type device_type;
753
754 struct
755 {
756 bool mutex_created;
757 struct os_mutex mutex;
758
760
761 xrt_atomic_s32_t battery_status;
762
763 uint32_t last_device_remote_us;
764 timepoint_ns device_remote_ns;
765 timepoint_ns device_local_ns;
766
767 struct imu_fusion *imu_fusion;
768 struct xrt_imu_sample last_imu_sample;
769
770 struct m_clock_windowed_skew_tracker *clock_tracker;
771
772 bool calibration_read;
773 struct rift_touch_controller_calibration calibration;
774
775 struct
776 {
777 timepoint_ns end_time_ns;
778 bool high_freq;
779 float amplitude;
780
781 bool set_enabled;
782 float set_amplitude;
783 bool set_high_freq;
784 } haptic;
785 } input;
786
787 //! Locked by radio_state.thread
788 struct
789 {
790 bool serial_valid;
791
792 uint8_t calibration_hash[CALIBRATION_HASH_BYTE_LENGTH];
793
794 uint8_t calibration_data_buffer[CALIBRATION_BODY_BYTE_CHUNK_LENGTH];
795
796 uint8_t *calibration_body_json;
797 uint16_t calibration_body_json_length;
798
800};
801
802enum rift_remote_inputs
803{
804 RIFT_REMOTE_INPUT_DPAD_UP,
805 RIFT_REMOTE_INPUT_DPAD_DOWN,
806 RIFT_REMOTE_INPUT_DPAD_LEFT,
807 RIFT_REMOTE_INPUT_DPAD_RIGHT,
808 RIFT_REMOTE_INPUT_SELECT,
809 RIFT_REMOTE_INPUT_VOLUME_UP,
810 RIFT_REMOTE_INPUT_VOLUME_DOWN,
811 RIFT_REMOTE_INPUT_BACK,
812 RIFT_REMOTE_INPUT_OCULUS,
813 RIFT_REMOTE_INPUT_COUNT,
814};
815
816/*!
817 * A Rift Remote device.
818 *
819 * @implements xrt_device
820 */
822{
823 struct xrt_device base;
824 struct xrt_frame_node node;
825
826 //! The button state of the remote, stored as an atomic to avoid needing a mutex.
827 xrt_atomic_s32_t buttons;
828
829 //! Locked by radio_state.thread
831};
832
833enum rift_radio_command
834{
835 RIFT_RADIO_COMMAND_NONE = 0,
836 RIFT_RADIO_COMMAND_READ_SERIAL,
837 RIFT_RADIO_COMMAND_READ_FLASH,
838 RIFT_RADIO_COMMAND_SEND_HAPTICS,
839};
840
842{
843 //! A pointer to store the serial string. Must contain at least SERIAL_NUMBER_LENGTH bytes.
844 char *serial;
845 //! A pointer to store when reading the serial was successful.
847};
848
849typedef int (*flash_read_callback_t)(void *user_data, uint16_t address, uint16_t length);
850
852{
853 void *user_data;
854
855 uint16_t address;
856 uint16_t length;
857
858 uint8_t *buffer;
859
860 flash_read_callback_t read_callback;
861};
862
864 struct rift_radio_command_data_read_serial read_serial;
865 struct rift_radio_command_data_read_flash read_flash;
866};
867
868//! How many past exposures a frame can be matched against. At ~60 Hz this is a little over a quarter second.
869#define RIFT_EXPOSURE_HISTORY_SIZE 16
870
871//! One camera exposure the HMD told us about, held so that late frames can still find the exposure they belong to.
873{
874 //! The value of rift_hmd::exposure_counter at this exposure.
875 uint32_t sequence_id;
876 //! When the exposure started, in local monotonic time. This is what frames matched to it are timestamped with.
878 /*!
879 * When the IN report announcing this exposure arrived, in local monotonic time.
880 *
881 * Frames are matched against this rather than against @ref timestamp_ns. Both the report and the frame have
882 * travelled over USB before we see them, so their arrival times share most of that delay and land near each
883 * other; the exposure instant on the HMD's own clock is a good deal earlier than either.
884 */
886};
887
888/*!
889 * A rift HMD device.
890 *
891 * @implements xrt_device
892 * @implements t_constellation_tracker_device
893 */
895{
896 struct xrt_device base;
897 struct xrt_frame_node node;
898
899 struct xrt_frame_context *xfctx;
900
901 enum u_logging_level log_level;
902
903 // has built-in mutex so thread safe
904 struct m_relation_history *relation_hist;
905
906 bool use_constellation_poses;
907
908 struct h_hid_device *hmd_dev;
909 struct h_hid_device *radio_dev;
910
911 struct os_thread_helper sensor_thread;
912
913 struct m_clock_bit_extender clock_extender;
914 timepoint_ns last_sample_local_timestamp_ns;
915
916 //! A total counter for how many exposures have occurred
918 uint16_t last_tracking_count;
919
920 //! The most recent exposures, newest at `(exposure_history_pushed - 1) % RIFT_EXPOSURE_HISTORY_SIZE`, locked by
921 //! sensor_thread.
923 //! How many exposures have ever been pushed into the history, locked by sensor_thread.
925
926 struct m_imu_3dof fusion;
927 struct m_clock_windowed_skew_tracker *clock_tracker;
928
929 timepoint_ns last_keepalive_time;
930 enum rift_variant variant;
931 struct rift_config_report config;
933 //! Panel timing (persistence and total rows), zeroed if it could not be read or was invalid.
935
936 struct t_timing_event_sink *timing_event_sink;
937 struct t_timing_event_source *timing_event_source;
938
939 struct rift_tracking_report tracking;
940
941 const struct rift_lens_distortion *lens_distortions;
942 uint16_t num_lens_distortions;
943 uint16_t distortion_in_use;
944
945 struct u_rift_panel panel;
946 struct u_rift_eye_profile eye_profile;
947
948 //! Interpupillary distance of the headset, in meters.
950 float icd_override_m;
951
952 bool presence;
953
954 bool imu_needs_calibration;
955 struct rift_imu_calibration imu_calibration;
956
957 uint8_t radio_address[5];
958
959 //! Mutex to protect access to the device array, device count == -1 means uninitialized
961
962 int device_count;
963 int added_devices;
964 struct xrt_device *devices[4]; // left touch, right touch, tracked object, remote
965
966 struct t_constellation_tracker *constellation_tracker;
967 struct t_constellation_tracker_device constellation_device;
968 t_constellation_device_id_t constellation_device_id;
969
970 struct xrt_imu_sink *constellation_imu_sink;
971
972 struct t_constellation_tracker_led_model led_model;
973 struct xrt_pose T_imu_device;
974 struct xrt_pose T_device_imu;
975
976 //! Generic state for the radio state machine
977 struct
978 {
980
981 struct rift_touch_controller *touch_controllers[3];
982 struct rift_remote *remote;
983
984 enum rift_radio_command current_command;
985 union rift_radio_command_data command_data;
987};
988
989//! Casting helper function from xrt_device->rift_hmd
990static inline struct rift_hmd *
991rift_hmd(struct xrt_device *xdev)
992{
993 return (struct rift_hmd *)xdev;
994}
995
996static inline struct rift_hmd *
997rift_hmd_from_node(struct xrt_frame_node *node)
998{
999 return (struct rift_hmd *)container_of(node, struct rift_hmd, node);
1000}
1001
1002static inline struct rift_touch_controller *
1004{
1005 return (struct rift_touch_controller *)xdev;
1006}
1007
1008static inline struct rift_touch_controller *
1009rift_touch_controller_from_node(struct xrt_frame_node *node)
1010{
1011 return (struct rift_touch_controller *)container_of(node, struct rift_touch_controller, node);
1012}
1013
1014static inline struct rift_remote *
1015rift_remote(struct xrt_device *xdev)
1016{
1017 return (struct rift_remote *)xdev;
1018}
1019
1020static inline struct rift_remote *
1021rift_remote_from_node(struct xrt_frame_node *node)
1022{
1023 return (struct rift_remote *)container_of(node, struct rift_remote, node);
1024}
1025
1026static inline size_t
1027rift_radio_device_type_to_touch_index(enum rift_radio_device_type device_type)
1028{
1029 switch (device_type) {
1030 case RIFT_RADIO_DEVICE_LEFT_TOUCH: return 0;
1031 case RIFT_RADIO_DEVICE_RIGHT_TOUCH: return 1;
1032 case RIFT_RADIO_DEVICE_TRACKED_OBJECT: return 2;
1033 default: assert(false);
1034 }
1035
1036 return -1;
1037}
1038
1039static inline enum rift_radio_device_type
1040rift_radio_touch_index_to_device_type(size_t index)
1041{
1042 switch (index) {
1043 case 0: return RIFT_RADIO_DEVICE_LEFT_TOUCH;
1044 case 1: return RIFT_RADIO_DEVICE_RIGHT_TOUCH;
1045 case 2: return RIFT_RADIO_DEVICE_TRACKED_OBJECT;
1046 default: assert(false);
1047 }
1048
1049 return (enum rift_radio_device_type)0;
1050}
1051
1052static inline float
1053rift_min_mid_max_cap(struct rift_touch_controller_calibration *calibration, size_t index, float value)
1054{
1055 return (value - calibration->cap_sense_min[index]) /
1056 (calibration->cap_sense_touch[index] - calibration->cap_sense_min[index]);
1057}
1058
1059static inline float
1060rift_min_mid_max_range_to_float(uint16_t range[3], uint16_t value)
1061{
1062 if (value < range[1]) {
1063 return 1.0f - ((float)value - range[0]) / (range[1] - range[0]) * 0.5f;
1064 } else {
1065 return 0.5f - ((float)value - range[1]) / (range[2] - range[1]) * 0.5f;
1066 }
1067}
1068
1069bool
1070rift_touch_calibration_parse(const char *calibration_data,
1071 size_t calibration_size,
1072 struct rift_touch_controller_calibration *out_calibration);
1073
1074#ifdef __cplusplus
1075} // extern "C"
1076#endif
u_logging_level
Logging level enum.
Definition u_logging.h:45
int64_t timepoint_ns
Integer timestamp type.
Definition u_time.h:77
#define container_of(ptr, type, field)
Get the holder from a pointer to a field.
Definition xrt_compiler.h:298
Wrapper around OS native hid functions.
C interface to math library.
Helpers to estimate offsets between clocks.
A fifo that also lets you dynamically filter.
A IMU fusion specially made for 3dof devices.
Wrapper header for <math.h> to ensure pi-related math constants are defined.
Wrapper around OS threading native functions.
Interface to Oculus Rift driver code.
#define RIFT_EXPOSURE_HISTORY_SIZE
How many past exposures a frame can be matched against. At ~60 Hz this is a little over a quarter sec...
Definition rift_internal.h:869
Definition oh_device.c:484
Definition rift_internal.h:419
Definition m_space.cpp:87
Representing a single hid interface on a device.
Definition h_hid.h:43
Definition t_imu.cpp:25
Storage for a 32-bit unsigned -> 64-bit signed clock extension.
Definition m_clock_tracking.h:24
Definition m_clock_tracking.c:40
Definition m_imu_3dof.h:35
A wrapper around a native mutex.
Definition os_threading.h:69
All in one helper that handles locking, waiting for change and starting a thread.
Definition os_threading.h:587
Definition rift_internal.h:633
Definition rift_internal.h:217
Definition rift_internal.h:181
Definition rift_internal.h:300
Definition rift_internal.h:407
Definition rift_internal.h:193
Definition rift_internal.h:353
Definition rift_internal.h:316
Definition rift_internal.h:390
Definition rift_internal.h:383
Definition rift_internal.h:398
Definition rift_internal.h:441
One camera exposure the HMD told us about, held so that late frames can still find the exposure they ...
Definition rift_internal.h:873
timepoint_ns timestamp_ns
When the exposure started, in local monotonic time. This is what frames matched to it are timestamped...
Definition rift_internal.h:877
timepoint_ns recv_timestamp_ns
When the IN report announcing this exposure arrived, in local monotonic time.
Definition rift_internal.h:885
uint32_t sequence_id
The value of rift_hmd::exposure_counter at this exposure.
Definition rift_internal.h:875
A rift HMD device.
Definition rift_internal.h:895
struct rift_hmd::@184 radio_state
Generic state for the radio state machine.
struct rift_exposure_event exposure_history[16]
The most recent exposures, newest at (exposure_history_pushed - 1) % RIFT_EXPOSURE_HISTORY_SIZE,...
Definition rift_internal.h:922
struct os_mutex device_mutex
Mutex to protect access to the device array, device count == -1 means uninitialized.
Definition rift_internal.h:960
struct rift_display_report display
Panel timing (persistence and total rows), zeroed if it could not be read or was invalid.
Definition rift_internal.h:934
float default_icd
Interpupillary distance of the headset, in meters.
Definition rift_internal.h:949
uint32_t exposure_counter
A total counter for how many exposures have occurred.
Definition rift_internal.h:917
uint64_t exposure_history_pushed
How many exposures have ever been pushed into the history, locked by sensor_thread.
Definition rift_internal.h:924
Definition rift_internal.h:450
Definition rift_internal.h:654
Definition rift_internal.h:231
Definition rift_internal.h:642
Definition rift_internal.h:270
Definition rift_internal.h:493
Definition rift_internal.h:466
Definition rift_internal.h:852
Definition rift_internal.h:842
char * serial
A pointer to store the serial string. Must contain at least SERIAL_NUMBER_LENGTH bytes.
Definition rift_internal.h:844
bool * serial_valid
A pointer to store when reading the serial was successful.
Definition rift_internal.h:846
Definition rift_internal.h:476
Definition rift_internal.h:486
Definition rift_internal.h:569
Definition rift_internal.h:514
Definition rift_internal.h:547
Definition rift_internal.h:582
A Rift Remote device.
Definition rift_internal.h:822
xrt_atomic_s32_t buttons
The button state of the remote, stored as an atomic to avoid needing a mutex.
Definition rift_internal.h:827
bool serial_valid
Locked by radio_state.thread.
Definition rift_internal.h:830
Definition rift_internal.h:699
Definition rift_internal.h:727
Definition rift_internal.h:692
A Rift Touch controller device.
Definition rift_internal.h:746
struct rift_touch_controller::@182 radio_data
Locked by radio_state.thread.
Definition rift_internal.h:604
A constellation tracker device is a device that the constellation tracker will attempt to track in 6d...
Definition t_constellation.h:415
The LED model is a series of points which define the real-world positions of all LEDs.
Definition t_constellation.h:313
A time sync sink is a component that receives timing events from a stable timing source,...
Definition t_time_sync.h:69
A time sync source is a component that generates timing events for an t_timing_event_sink to consume.
Definition t_time_sync.h:102
Definition u_worker.c:38
Definition u_distortion_mesh.h:239
Definition u_distortion_mesh.h:220
A single HMD or input device.
Definition xrt_device.h:345
Object used to track all sinks and frame producers in a graph.
Definition xrt_frame.h:108
A interface object used for destroying a frame graph.
Definition xrt_frame.h:87
IMU Sample.
Definition xrt_tracking.h:179
An object to send IMU samples to.
Definition xrt_tracking.h:268
A tightly packed 3x3 matrix of floats.
Definition xrt_defines.h:602
A pose composed of a position and orientation.
Definition xrt_defines.h:535
A 2 element vector with single floats.
Definition xrt_defines.h:292
A 3 element vector with single floats.
Definition xrt_defines.h:323
Header defining the tracking system integration in Monado.
C interface to basic IMU fusion.
Header defining interfaces for time synchronization in Monado.
Misc helpers for device drivers.
Code to generate disortion meshes.
Basic logging functionality.
Definition rift_internal.h:863
Endian-specific byte order defines.