Monado OpenXR Runtime
Loading...
Searching...
No Matches
wmr_controller_base.h
Go to the documentation of this file.
1// Copyright 2020-2021, N Madsen.
2// Copyright 2020-2021, Collabora, Ltd.
3// Copyright 2021-2023, Jan Schmidt
4// SPDX-License-Identifier: BSL-1.0
5//
6/*!
7 * @file
8 * @brief Common implementation for WMR controllers, handling
9 * shared behaviour such as communication, configuration reading,
10 * IMU integration.
11 * @author Jan Schmidt <jan@centricular.com>
12 * @author Nis Madsen <nima_zero_one@protonmail.com>
13 * @ingroup drv_wmr
14 */
15#pragma once
16
17#include "os/os_threading.h"
18#include "math/m_imu_3dof.h"
19#include "util/u_device.h"
20#include "util/u_logging.h"
21#include "xrt/xrt_device.h"
22
24#include "wmr_config.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
31
32/*!
33 * A connection for communicating with the controller.
34 * The mechanism is implementation specific, so there are
35 * two variants for either communicating directly with a
36 * controller via bluetooth, and another for talking
37 * to a controller through a headset tunnelled mapping.
38 *
39 * The controller implementation doesn't need to care how
40 * the communication is implemented.
41 *
42 * The HMD-tunnelled version of the connection is reference
43 * counted and mutex protected, as both the controller and
44 * the HMD need to hold a reference to it to clean up safely.
45 * For bluetooth controllers, destruction of the controller
46 * xrt_device calls disconnect and destroys the connection
47 * object (and bluetooth listener) immediately.
48 */
50{
51 //! The controller this connection is talking to.
53
54 bool (*send_bytes)(struct wmr_controller_connection *wcc, const uint8_t *buffer, uint32_t buf_size);
55 void (*receive_bytes)(struct wmr_controller_connection *wcc,
56 uint64_t time_ns,
57 uint8_t *buffer,
58 uint32_t buf_size);
59 int (*read_sync)(struct wmr_controller_connection *wcc, uint8_t *buffer, uint32_t buf_size, int timeout_ms);
60
61 void (*disconnect)(struct wmr_controller_connection *wcc);
62};
63
64static inline bool
65wmr_controller_connection_send_bytes(struct wmr_controller_connection *wcc, const uint8_t *buffer, uint32_t buf_size)
66{
67 assert(wcc->send_bytes != NULL);
68 return wcc->send_bytes(wcc, buffer, buf_size);
69}
70
71static inline int
72wmr_controller_connection_read_sync(struct wmr_controller_connection *wcc,
73 uint8_t *buffer,
74 uint32_t buf_size,
75 int timeout_ms)
76{
77 return wcc->read_sync(wcc, buffer, buf_size, timeout_ms);
78}
79
80static inline void
81wmr_controller_connection_disconnect(struct wmr_controller_connection *wcc)
82{
83 wcc->disconnect(wcc);
84}
85
86/*!
87 * Common base for all WMR controllers.
88 *
89 * @ingroup drv_wmr
90 * @implements xrt_device
91 */
93{
94 //! Base struct.
96
97 //! Mutex protects the controller connection
99
100 //! The connection for this controller.
102
103 //! Centralized data source for WMR. Controller will send IMU samples to this.
104 struct xrt_fs *source;
105
106 //! Callback from the connection when a packet has been received.
107 void (*receive_bytes)(struct wmr_controller_base *wcb, uint64_t time_ns, uint8_t *buffer, uint32_t buf_size);
108
109 enum u_logging_level log_level;
110
111 //! Mutex protects shared data used from OpenXR callbacks
113
114 //! Callback to parse a controller update packet and update the input / imu info. Called with the
115 // data lock held.
117 uint64_t time_ns,
118 uint8_t *buffer,
119 uint32_t buf_size);
120
121 /* firmware configuration block */
122 struct wmr_controller_config config;
123
124 //! Time of last IMU sample, in CPU time.
126 //! Main fusion calculator.
128 //! The last angular velocity from the IMU, for prediction.
130};
131
132bool
133wmr_controller_base_init(struct wmr_controller_base *wcb,
134 struct wmr_controller_connection *conn,
135 enum xrt_device_type controller_type,
136 enum u_logging_level log_level,
138 struct xrt_fs *src);
139
140void
141wmr_controller_base_deinit(struct wmr_controller_base *wcb);
142
143//! Handle IMU sample after packet parsing
144void
146 uint64_t time_ns,
147 uint64_t timestamp_ticks,
148 const struct xrt_vec3 *acc,
149 const struct xrt_vec3 *gyro);
150
151static inline void
152wmr_controller_connection_receive_bytes(struct wmr_controller_connection *wcc,
153 uint64_t time_ns,
154 uint8_t *buffer,
155 uint32_t buf_size)
156{
157
158 if (wcc->receive_bytes != NULL) {
159 wcc->receive_bytes(wcc, time_ns, buffer, buf_size);
160 } else {
161 /* Default: deliver directly to the controller instance */
162 struct wmr_controller_base *wcb = wcc->wcb;
163 assert(wcb->receive_bytes != NULL);
164 wcb->receive_bytes(wcb, time_ns, buffer, buf_size);
165 }
166}
167
168#ifdef __cplusplus
169}
170#endif
u_logging_level
Logging level enum.
Definition u_logging.h:45
void(* u_device_destroy_function_t)(struct xrt_device *xdev)
Function pointer type for the device's destroy function.
Definition u_device.h:228
xrt_device_type
How an xrt_device can be used.
Definition xrt_defines.h:837
A IMU fusion specially made for 3dof devices.
Wrapper around OS threading native functions.
Definition m_imu_3dof.h:35
A wrapper around a native mutex.
Definition os_threading.h:69
Common base for all WMR controllers.
Definition wmr_controller_base.h:93
struct os_mutex conn_lock
Mutex protects the controller connection.
Definition wmr_controller_base.h:98
struct os_mutex data_lock
Mutex protects shared data used from OpenXR callbacks.
Definition wmr_controller_base.h:112
struct wmr_controller_connection * wcc
The connection for this controller.
Definition wmr_controller_base.h:101
struct m_imu_3dof fusion
Main fusion calculator.
Definition wmr_controller_base.h:127
struct xrt_vec3 last_angular_velocity
The last angular velocity from the IMU, for prediction.
Definition wmr_controller_base.h:129
uint64_t last_imu_timestamp_ns
Time of last IMU sample, in CPU time.
Definition wmr_controller_base.h:125
struct xrt_fs * source
Centralized data source for WMR. Controller will send IMU samples to this.
Definition wmr_controller_base.h:104
bool(* handle_input_packet)(struct wmr_controller_base *wcb, uint64_t time_ns, uint8_t *buffer, uint32_t buf_size)
Callback to parse a controller update packet and update the input / imu info. Called with the.
Definition wmr_controller_base.h:116
struct xrt_device base
Base struct.
Definition wmr_controller_base.h:95
void(* receive_bytes)(struct wmr_controller_base *wcb, uint64_t time_ns, uint8_t *buffer, uint32_t buf_size)
Callback from the connection when a packet has been received.
Definition wmr_controller_base.h:107
Definition wmr_config.h:174
A connection for communicating with the controller.
Definition wmr_controller_base.h:50
struct wmr_controller_base * wcb
The controller this connection is talking to.
Definition wmr_controller_base.h:52
A single HMD or input device.
Definition xrt_device.h:341
Frameserver that generates frames.
Definition xrt_frameserver.h:70
A 3 element vector with single floats.
Definition xrt_defines.h:299
Misc helpers for device drivers.
Basic logging functionality.
WMR and MS HoloLens configuration structures.
void wmr_controller_base_handle_imu_sample(struct wmr_controller_base *wcb, uint64_t time_ns, uint64_t timestamp_ticks, const struct xrt_vec3 *acc, const struct xrt_vec3 *gyro)
Handle IMU sample after packet parsing.
Definition wmr_controller_base.c:495
WMR Motion Controller protocol constants, structures and helpers.
Header defining an xrt display or controller device.