-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathje_nourish_wiimote.cpp
218 lines (168 loc) · 7.48 KB
/
je_nourish_wiimote.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Internal Includes
#include <osvr/PluginKit/PluginKit.h>
#include <osvr/PluginKit/TrackerInterfaceC.h>
#include <osvr/PluginKit/AnalogInterfaceC.h>
#include <osvr/PluginKit/ButtonInterfaceC.h>
// Generated JSON header file
#include "je_nourish_wiimote_json.h"
// Library/third-party includes
#include "wiiuse.h"
// Standard includes
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#define MAX_WIIMOTES 4
#define TRACKERS_PER_WIIMOTE 2
#define BUTTONS_PER_WIIMOTE 13
#define ANALOG_PER_WIIMOTE 8
#define IS_PRESSED_OSVR(device, button) (IS_PRESSED((device),(button)) ? OSVR_BUTTON_PRESSED : OSVR_BUTTON_NOT_PRESSED)
// Anonymous namespace to avoid symbol collision
namespace {
class WiimoteDevice {
public:
WiimoteDevice(OSVR_PluginRegContext ctx) {
int found, connected;
m_wiimotes = wiiuse_init(MAX_WIIMOTES);
found = wiiuse_find(m_wiimotes, MAX_WIIMOTES, 5);
connected = wiiuse_connect(m_wiimotes, MAX_WIIMOTES);
wiiuse_set_leds(m_wiimotes[0], WIIMOTE_LED_1);
wiiuse_set_leds(m_wiimotes[1], WIIMOTE_LED_2);
wiiuse_set_leds(m_wiimotes[2], WIIMOTE_LED_3);
wiiuse_set_leds(m_wiimotes[3], WIIMOTE_LED_4);
wiiuse_motion_sensing(m_wiimotes[0], 1);
wiiuse_motion_sensing(m_wiimotes[1], 1);
wiiuse_motion_sensing(m_wiimotes[2], 1);
wiiuse_motion_sensing(m_wiimotes[3], 1);
/// Create the initialization options
OSVR_DeviceInitOptions opts = osvrDeviceCreateInitOptions(ctx);
osvrDeviceTrackerConfigure(opts, &m_tracker);
osvrDeviceAnalogConfigure(opts, &m_analog, 20);
osvrDeviceButtonConfigure(opts, &m_button, 52);
/// Create the device token with the options
m_dev.initAsync(ctx, "WiimoteDevice", opts);
/// Send JSON descriptor
m_dev.sendJsonDescriptor(je_nourish_wiimote_json);
/// Register update callback
m_dev.registerUpdateCallback(this);
}
~WiimoteDevice() {
wiiuse_cleanup(m_wiimotes, MAX_WIIMOTES);
}
OSVR_ReturnCode update() {
struct wiimote_t* wm;
OSVR_Vec3 wm_rpy;
OSVR_Quaternion wm_quaternion;
OSVR_Vec3 nc_rpy;
OSVR_Quaternion nc_quaternion;
osvrVec3Zero(&wm_rpy);
osvrQuatSetIdentity(&wm_quaternion);
osvrVec3Zero(&nc_rpy);
osvrQuatSetIdentity(&nc_quaternion);
if (wiimote_connected()) {
wiiuse_poll(m_wiimotes, MAX_WIIMOTES);
OSVR_ButtonState buttons[BUTTONS_PER_WIIMOTE * MAX_WIIMOTES];
OSVR_AnalogState analog[ANALOG_PER_WIIMOTE * MAX_WIIMOTES];
for (int i = 0; i < MAX_WIIMOTES; i++) {
wm = m_wiimotes[i];
if (wm->event == WIIUSE_EVENT) {
buttons[BUTTONS_PER_WIIMOTE*i + 0] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_A);
buttons[BUTTONS_PER_WIIMOTE*i + 1] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_B);
analog[ANALOG_PER_WIIMOTE * i + 5] = IS_PRESSED(wm, WIIMOTE_BUTTON_B) ? 1.0f : -1.0f;
buttons[BUTTONS_PER_WIIMOTE*i + 2] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_UP);
buttons[BUTTONS_PER_WIIMOTE*i + 3] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_DOWN);
buttons[BUTTONS_PER_WIIMOTE*i + 4] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_LEFT);
buttons[BUTTONS_PER_WIIMOTE*i + 5] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_RIGHT);
analog[ANALOG_PER_WIIMOTE * i + 6] = IS_PRESSED(wm, WIIMOTE_BUTTON_RIGHT) ? 1.0f : (IS_PRESSED(wm, WIIMOTE_BUTTON_LEFT) ? -1.0f : 0.0f);
analog[ANALOG_PER_WIIMOTE * i + 7] = IS_PRESSED(wm, WIIMOTE_BUTTON_UP) ? 1.0f : (IS_PRESSED(wm, WIIMOTE_BUTTON_DOWN) ? -1.0f : 0.0f);
buttons[BUTTONS_PER_WIIMOTE*i + 6] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_ONE);
buttons[BUTTONS_PER_WIIMOTE*i + 7] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_TWO);
buttons[BUTTONS_PER_WIIMOTE*i + 8] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_MINUS);
buttons[BUTTONS_PER_WIIMOTE*i + 9] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_PLUS);
buttons[BUTTONS_PER_WIIMOTE*i + 10] = IS_PRESSED_OSVR(wm, WIIMOTE_BUTTON_HOME);
if (WIIUSE_USING_ACC(wm)) {
osvrVec3SetX(&wm_rpy, -wm->orient.roll * M_PI / 180);
osvrVec3SetY(&wm_rpy, -wm->orient.pitch * M_PI / 180);
osvrVec3SetZ(&wm_rpy, -wm->orient.yaw * M_PI / 180);
WiimoteDevice::quaternionFromRPY(&wm_rpy, &wm_quaternion);
osvrDeviceTrackerSendOrientation(m_dev, m_tracker, &wm_quaternion, (TRACKERS_PER_WIIMOTE * i));
}
if (WIIUSE_USING_IR(wm)) {
analog[ANALOG_PER_WIIMOTE*i + 0] = wm->ir.x;
analog[ANALOG_PER_WIIMOTE*i + 1] = wm->ir.y;
analog[ANALOG_PER_WIIMOTE*i + 2] = wm->ir.z;
}
else {
analog[ANALOG_PER_WIIMOTE*i + 0] = 0;
analog[ANALOG_PER_WIIMOTE*i + 1] = 0;
analog[ANALOG_PER_WIIMOTE*i + 2] = 0;
}
if (wm->exp.type == EXP_NUNCHUK || wm->exp.type == EXP_MOTION_PLUS_NUNCHUK) {
struct nunchuk_t* nc = (nunchuk_t*)&wm->exp.nunchuk;
buttons[BUTTONS_PER_WIIMOTE*i + 11] = IS_PRESSED_OSVR(nc, NUNCHUK_BUTTON_C);
buttons[BUTTONS_PER_WIIMOTE*i + 12] = IS_PRESSED_OSVR(nc, NUNCHUK_BUTTON_Z);
analog[ANALOG_PER_WIIMOTE*i + 3] = nc->js.x;
analog[ANALOG_PER_WIIMOTE*i + 4] = nc->js.y;
osvrVec3SetX(&nc_rpy, -nc->orient.roll * M_PI / 180);
osvrVec3SetY(&nc_rpy, -nc->orient.pitch * M_PI / 180);
osvrVec3SetZ(&nc_rpy, -nc->orient.yaw * M_PI / 180);
WiimoteDevice::quaternionFromRPY(&nc_rpy, &nc_quaternion);
osvrDeviceTrackerSendOrientation(m_dev, m_tracker, &nc_quaternion, (TRACKERS_PER_WIIMOTE * i) + 1);
}
else {
buttons[BUTTONS_PER_WIIMOTE*i + 11] = OSVR_BUTTON_NOT_PRESSED;
buttons[BUTTONS_PER_WIIMOTE*i + 12] = OSVR_BUTTON_NOT_PRESSED;
analog[ANALOG_PER_WIIMOTE*i + 3] = 0;
analog[ANALOG_PER_WIIMOTE*i + 4] = 0;
}
}
}
osvrDeviceAnalogSetValues(m_dev, m_analog, analog, ANALOG_PER_WIIMOTE * MAX_WIIMOTES);
osvrDeviceButtonSetValues(m_dev, m_button, buttons, BUTTONS_PER_WIIMOTE * MAX_WIIMOTES);
}
return OSVR_RETURN_SUCCESS;
}
private:
bool wiimote_connected() {
for (int i = 0; i < MAX_WIIMOTES; i++) {
if (m_wiimotes[i] && WIIMOTE_IS_CONNECTED(m_wiimotes[i])) {
return true;
}
}
return false;
}
static void quaternionFromRPY(OSVR_Vec3* rpy, OSVR_Quaternion* quaternion) {
double r = osvrVec3GetX(rpy);
double p = osvrVec3GetY(rpy);
double y = osvrVec3GetZ(rpy);
osvrQuatSetZ(quaternion, sin(r / 2) * cos(p / 2) * cos(y / 2) - cos(r / 2) * sin(p / 2) * sin(y / 2));
osvrQuatSetX(quaternion, cos(r / 2) * sin(p / 2) * cos(y / 2) + sin(r / 2) * cos(p / 2) * sin(y / 2));
osvrQuatSetY(quaternion, cos(r / 2) * cos(p / 2) * sin(y / 2) - sin(r / 2) * sin(p / 2) * cos(y / 2));
osvrQuatSetW(quaternion, cos(r / 2) * cos(p / 2) * cos(y / 2) + sin(r / 2) * sin(p / 2) * sin(y / 2));
}
wiimote** m_wiimotes;
osvr::pluginkit::DeviceToken m_dev;
OSVR_TrackerDeviceInterface m_tracker;
OSVR_AnalogDeviceInterface m_analog;
OSVR_ButtonDeviceInterface m_button;
};
class HardwareDetection {
public:
HardwareDetection() : m_found(false) {}
OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx) {
if (!m_found) {
m_found = true;
osvr::pluginkit::registerObjectForDeletion(
ctx, new WiimoteDevice(ctx));
}
return OSVR_RETURN_SUCCESS;
}
private:
bool m_found;
};
} // namespace
OSVR_PLUGIN(je_nourish_wiimote) {
osvr::pluginkit::PluginContext context(ctx);
/// Register a detection callback function object.
context.registerHardwareDetectCallback(new HardwareDetection());
return OSVR_RETURN_SUCCESS;
}