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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
* Copyright (c) 2017-2018 Wei Shuai <cpuwolf@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
// silence #pragma deprecated in bluetoothapis.h
#undef _WIN32_WINNT
#define _WIN32_WINNT _WIN32_WINNT_VISTA
#undef NTDDI_VERSION
#define NTDDI_VERSION NTDDI_VISTASP1
#include "wii_camera.h"
#include "wii_frame.hpp"
#include "compat/sleep.hpp"
#include "compat/camera-names.hpp"
#include "compat/math-imports.hpp"
#include <opencv2/imgproc.hpp>
#include <bthsdpdef.h>
#include <bluetoothapis.h>
using namespace pt_module;
WIICamera::WIICamera(const QString& module_name) : s { module_name }
{
cam_info.fps = 70;
cam_info.res_x = 1024;
cam_info.res_y = 768;
cam_info.fov = 42.0f;
cam_info.idx = 0;
}
WIICamera::~WIICamera()
{
//stop();
}
QString WIICamera::get_desired_name() const
{
return desired_name;
}
QString WIICamera::get_active_name() const
{
return active_name;
}
void WIICamera::show_camera_settings()
{
}
WIICamera::result WIICamera::get_info() const
{
if (cam_info.res_x == 0 || cam_info.res_y == 0)
return { false, {} };
return { true, cam_info };
}
WIICamera::result WIICamera::get_frame(pt_frame& frame_)
{
cv::Mat& frame = frame_.as<WIIFrame>()->mat;
struct wii_info& wii = frame_.as<WIIFrame>()->wii;
const wii_camera_status new_frame = get_frame_(frame);
//create a fake blank frame
frame = cv::Mat(cam_info.res_x, cam_info.res_y, CV_8UC3, cv::Scalar(0, 0, 0));
wii.status = new_frame;
switch (new_frame)
{
case wii_cam_data_change:
get_status(wii);
get_points(wii);
break;
case wii_cam_data_no_change:
return { false, cam_info };
default:
break;
}
return { true, cam_info };
}
bool WIICamera::start(int, int, int, int)
{
m_pDev = std::make_unique<wiimote>();
m_pDev->ChangedCallback = on_state_change;
m_pDev->CallbackTriggerFlags = (state_change_flags)(CONNECTED|EXTENSION_CHANGED |MOTIONPLUS_CHANGED);
return true;
}
void WIICamera::stop()
{
onExit = true;
m_pDev->ChangedCallback = nullptr;
m_pDev->Disconnect();
Beep(1000, 200);
if (m_pDev) {
m_pDev=nullptr;
m_pDev = nullptr;
}
desired_name = QString();
active_name = QString();
cam_info = pt_camera_info();
cam_desired = pt_camera_info();
}
wii_camera_status WIICamera::pair()
{
#if defined __MINGW32__
// missing prototypes and implib entries
return wii_cam_wait_for_connect;
#else
wii_camera_status ret = wii_cam_wait_for_sync;
HBLUETOOTH_RADIO_FIND hbt;
BLUETOOTH_FIND_RADIO_PARAMS bt_param;
HANDLE hbtlist[10];
int ibtidx = 0;
bool wiifound = false;
bt_param.dwSize = sizeof(bt_param);
hbt = BluetoothFindFirstRadio(&bt_param, hbtlist + ibtidx);
if (!hbt) { ret = wii_cam_wait_for_dongle; return ret; }
do
{
ibtidx++;
} while (BluetoothFindNextRadio(&bt_param, hbtlist + ibtidx));
BluetoothFindRadioClose(hbt);
int i;
bool error = false;
for (i = 0; i < ibtidx; i++)
{
BLUETOOTH_RADIO_INFO btinfo;
btinfo.dwSize = sizeof(btinfo);
if (ERROR_SUCCESS != BluetoothGetRadioInfo(hbtlist[i], &btinfo)) {break;}
HBLUETOOTH_DEVICE_FIND hbtdevfd;
BLUETOOTH_DEVICE_SEARCH_PARAMS btdevparam;
BLUETOOTH_DEVICE_INFO btdevinfo;
btdevinfo.dwSize = sizeof(btdevinfo);
btdevparam.dwSize = sizeof(btdevparam);
btdevparam.fReturnAuthenticated = TRUE;
btdevparam.fReturnConnected = TRUE;
btdevparam.fReturnRemembered = TRUE;
btdevparam.fIssueInquiry = TRUE;
btdevparam.cTimeoutMultiplier = 1;
btdevparam.hRadio = hbtlist[i];
hbtdevfd=BluetoothFindFirstDevice(&btdevparam, &btdevinfo);
if (!hbtdevfd) {
int error= GetLastError();
qDebug() << error;
break;
}
do
{
if (wcscmp(btdevinfo.szName, L"Nintendo RVL-CNT-01-TR") && wcscmp(btdevinfo.szName, L"Nintendo RVL-CNT-01"))
{
continue;
}
if ((btdevinfo.fRemembered)&&error) {
BluetoothRemoveDevice(&btdevinfo.Address);
}
wiifound = true;
if (btdevinfo.fConnected) {
break;
}
WCHAR pwd[6];
pwd[0] = btinfo.address.rgBytes[0];
pwd[1] = btinfo.address.rgBytes[1];
pwd[2] = btinfo.address.rgBytes[2];
pwd[3] = btinfo.address.rgBytes[3];
pwd[4] = btinfo.address.rgBytes[4];
pwd[5] = btinfo.address.rgBytes[5];
if (ERROR_SUCCESS != BluetoothAuthenticateDevice(nullptr, hbtlist[i], &btdevinfo, pwd, 6)) { error = true; continue; }
DWORD servicecount = 32;
GUID guids[32];
if (ERROR_SUCCESS != BluetoothEnumerateInstalledServices(hbtlist[i], &btdevinfo, &servicecount, guids)) { error = true; continue; }
if (ERROR_SUCCESS != BluetoothSetServiceState(hbtlist[i], &btdevinfo, &HumanInterfaceDeviceServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE)) { error = true; continue; }
break;
} while (BluetoothFindNextDevice(hbtdevfd, &btdevinfo));
BluetoothFindDeviceClose(hbtdevfd);
}
for (i = 0; i < ibtidx; i++)
{
CloseHandle(hbtlist[i]);
}
if (wiifound) { ret = wii_cam_wait_for_connect; }
return ret;
#endif
}
wii_camera_status WIICamera::get_frame_(cv::Mat& frame)
{
(void)frame;
wii_camera_status ret = wii_cam_wait_for_connect;
if (!m_pDev->IsConnected()) {
qDebug() << "wii wait";
ret = pair();
switch (ret) {
case wii_cam_wait_for_sync:
m_pDev->Disconnect();
goto goodbye;
case wii_cam_wait_for_connect:
break;
default:
break;
}
if (!m_pDev->Connect(wiimote::FIRST_AVAILABLE)) {
Beep(500, 30); Sleep(1000);
goto goodbye;
}
}
if (m_pDev->RefreshState() == NO_CHANGE) {
Sleep(14); // don't hog the CPU if nothing changed
ret = wii_cam_data_no_change;
goto goodbye;
}
// did we loose the connection?
if (m_pDev->ConnectionLost())
{
goto goodbye;
}
ret = wii_cam_data_change;
goodbye:
return ret;
}
bool WIICamera::get_points(struct wii_info& wii)
{
bool dot_sizes = (m_pDev->IR.Mode == wiimote_state::ir::EXTENDED);
bool ret = false;
int point_count = 0;
for (unsigned index = 0; index < 4; index++)
{
wiimote_state::ir::dot &dot = m_pDev->IR.Dot[index];
if (dot.bVisible) {
wii.Points[index].ux = dot.RawX;
wii.Points[index].uy = dot.RawY;
if (dot_sizes) {
wii.Points[index].isize = dot.Size;
} else {
wii.Points[index].isize = 1;
}
wii.Points[index].bvis = dot.bVisible;
point_count++;
ret = true;
} else {
wii.Points[index].ux = 0;
wii.Points[index].uy = 0;
wii.Points[index].isize = 0;
wii.Points[index].bvis = dot.bVisible;
}
}
m_pDev->SetLEDs(0);
return ret;
}
void WIICamera::get_status(struct wii_info& wii)
{
//draw battery status
wii.BatteryPercent = m_pDev->BatteryPercent;
wii.bBatteryDrained = m_pDev->bBatteryDrained;
//draw horizon
if (m_pDev->Nunchuk.Acceleration.Orientation.UpdateAge < 10)
{
horizon.p = m_pDev->Acceleration.Orientation.Pitch;
horizon.r = m_pDev->Acceleration.Orientation.Roll;
}
wii.Pitch = horizon.p;
wii.Roll = horizon.r;
}
void WIICamera::on_state_change(wiimote &remote,
state_change_flags changed,
const wiimote_state &new_state)
{
// the wiimote just connected
if (changed & CONNECTED)
{
/* wiimote connected */
remote.SetLEDs(0x0f);
Beep(1000, 300); Sleep(500);
qDebug() << "wii connected";
if (new_state.ExtensionType != wiimote::BALANCE_BOARD)
{
if (new_state.bExtension)
remote.SetReportType(wiimote::IN_BUTTONS_ACCEL_IR_EXT); // no IR dots
else
remote.SetReportType(wiimote::IN_BUTTONS_ACCEL_IR); // IR dots
}
}
// another extension was just connected:
else if (changed & EXTENSION_CONNECTED)
{
Beep(1000, 200);
// switch to a report mode that includes the extension data (we will
// loose the IR dot sizes)
// note: there is no need to set report types for a Balance Board.
if (!remote.IsBalanceBoard())
remote.SetReportType(wiimote::IN_BUTTONS_ACCEL_IR_EXT);
}
else if (changed & EXTENSION_DISCONNECTED)
{
Beep(200, 300);
// use a non-extension report mode (this gives us back the IR dot sizes)
remote.SetReportType(wiimote::IN_BUTTONS_ACCEL_IR);
}
}
|