Monado OpenXR Runtime
vive.h
Go to the documentation of this file.
1// Copyright 2016-2019, Philipp Zabel
2// Copyright 2019, Collabora, Ltd.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Common vive header
7 * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
8 * @ingroup drv_vive
9 */
10
11#pragma once
12
13#include <stdint.h>
14#include <asm/byteorder.h>
15
16#include "util/u_debug.h"
17#include "util/u_time.h"
18
19/*
20 *
21 * Printing functions.
22 *
23 */
24
25#define VIVE_TRACE(d, ...) U_LOG_IFL_T(d->log_level, __VA_ARGS__)
26#define VIVE_DEBUG(d, ...) U_LOG_IFL_D(d->log_level, __VA_ARGS__)
27#define VIVE_INFO(d, ...) U_LOG_IFL_I(d->log_level, __VA_ARGS__)
28#define VIVE_WARN(d, ...) U_LOG_IFL_W(d->log_level, __VA_ARGS__)
29#define VIVE_ERROR(d, ...) U_LOG_IFL_E(d->log_level, __VA_ARGS__)
30
31DEBUG_GET_ONCE_LOG_OPTION(vive_log, "VIVE_LOG", U_LOGGING_WARN)
32
33#define VIVE_CLOCK_FREQ 48e6 // 48 MHz
34#define CAMERA_FREQUENCY 54
35#define IMU_FREQUENCY 1000
36
37/*!
38 * Helper function to convert raw device ticks to nanosecond timestamps.
39 *
40 * `inout` params should start as the results from the previous sample call.
41 * For the first call, pass zero for the `inout` params.
42 *
43 * @param sample_ticks_raw Current sample ticks
44 * @param[in, out] inout_prev_ticks Overwritten with `sample_ticks_raw`
45 * @param[in, out] inout_ts_ns Resulting timestamp in nanoseconds
46 */
47static inline void
48ticks_to_ns(uint32_t sample_ticks_raw, uint32_t *inout_prev_ticks, timepoint_ns *inout_ts_ns)
49{
50 uint32_t sample_ticks = __le32_to_cpu(sample_ticks_raw);
51 uint32_t prev_ticks = *inout_prev_ticks;
52
53 // From the C standard https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2912.pdf
54 // "A computation involving unsigned operands can never produce an overflow,
55 // because arithmetic for the unsigned type is performed modulo 2^N"
56 uint32_t delta_ticks = sample_ticks - prev_ticks;
57
58 const double one_tick_in_s = (1 / VIVE_CLOCK_FREQ);
59 const double one_tick_in_ns = one_tick_in_s * U_TIME_1S_IN_NS;
60 time_duration_ns delta_ns = delta_ticks * one_tick_in_ns;
61
62 *inout_prev_ticks = sample_ticks;
63 *inout_ts_ns += delta_ns;
64}
@ U_LOGGING_WARN
Warning messages: indicating a potential problem.
Definition: u_logging.h:47
int64_t timepoint_ns
Integer timestamp type.
Definition: u_time.h:70
int64_t time_duration_ns
Integer duration type in nanoseconds.
Definition: u_time.h:81
Small debug helpers.
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.
#define U_TIME_1S_IN_NS
The number of nanoseconds in a second.
Definition: u_time.h:47
static void ticks_to_ns(uint32_t sample_ticks_raw, uint32_t *inout_prev_ticks, timepoint_ns *inout_ts_ns)
Helper function to convert raw device ticks to nanosecond timestamps.
Definition: vive.h:48