4
4
#include " deps/Fusion/Fusion/Fusion.h"
5
5
#include < iostream>
6
6
#include < mutex>
7
-
7
+ #include < array>
8
+ #include < cstdint>
9
+ #include < vector>
8
10
// Air USB VID and PID
9
11
#define AIR_VID 0x3318
10
12
#define AIR_PID 0x0424
11
13
12
14
// Is Tracking
13
15
bool g_isTracking = false ;
14
16
17
+ // Is Listening
18
+ bool g_isListening = false ;
19
+
15
20
// ticks are in nanoseconds, 1000 Hz packets
16
21
#define TICK_LEN (1 .0f / 1E9f)
17
22
@@ -27,15 +32,16 @@ static FusionVector earth;
27
32
static FusionQuaternion qt;
28
33
29
34
HANDLE trackThread;
35
+ HANDLE listenThread;
30
36
31
37
hid_device* device;
32
38
33
-
39
+ hid_device* device4;
34
40
35
41
#define SAMPLE_RATE (1000 ) // replace this with actual sample rate
36
42
37
43
std::mutex mtx;
38
-
44
+ std::mutex it4;
39
45
40
46
typedef struct {
41
47
uint64_t tick;
@@ -202,7 +208,7 @@ process_accel(const int32_t in_accel[3], float out_vec[])
202
208
out_vec[0 ] = (float )(in_accel[0 ]) * ACCEL_SCALAR;
203
209
out_vec[1 ] = (float )(in_accel[2 ]) * ACCEL_SCALAR;
204
210
out_vec[2 ] = (float )(in_accel[1 ]) * ACCEL_SCALAR;
205
-
211
+
206
212
}
207
213
208
214
@@ -216,6 +222,28 @@ open_device()
216
222
while (devs) {
217
223
if (cur_dev->interface_number == 3 ) {
218
224
device = hid_open_path (cur_dev->path );
225
+ std::cout << " Interface 3 bound" << std::endl;
226
+ break ;
227
+ }
228
+
229
+ cur_dev = cur_dev->next ;
230
+ }
231
+
232
+ hid_free_enumeration (devs);
233
+ return device;
234
+ }
235
+
236
+ static hid_device*
237
+ open_device4 ()
238
+ {
239
+ struct hid_device_info * devs = hid_enumerate (AIR_VID, AIR_PID);
240
+ struct hid_device_info * cur_dev = devs;
241
+ hid_device* device = NULL ;
242
+
243
+ while (devs) {
244
+ if (cur_dev->interface_number == 4 ) {
245
+ device = hid_open_path (cur_dev->path );
246
+ std::cout << " Interface 4 bound" << std::endl;
219
247
break ;
220
248
}
221
249
@@ -269,7 +297,7 @@ DWORD WINAPI track(LPVOID lpParam) {
269
297
.rejectionTimeout = 5 * SAMPLE_RATE, /* 5 seconds */
270
298
};
271
299
FusionAhrsSetSettings (&ahrs, &settings);
272
-
300
+
273
301
274
302
while (g_isTracking) {
275
303
@@ -324,7 +352,51 @@ DWORD WINAPI track(LPVOID lpParam) {
324
352
}
325
353
return 0 ;
326
354
}
355
+ int brightness = 0 ;
356
+ DWORD WINAPI interface4Handler (LPVOID lpParam) {
357
+
358
+ // get initial brightness from device
359
+ std::array<uint8_t , 17 > initBrightness = { 0x00 , 0xfd , 0x1e , 0xb9 , 0xf0 , 0x68 , 0x11 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x03 };
360
+ hid_write (device4, initBrightness.data (), initBrightness.size ());
361
+
327
362
363
+ while (g_isListening) {
364
+ std::array<uint8_t , 65 > recv = {};
365
+ int res = hid_read (device4, recv.data (), recv.size ());
366
+ if (res > 0 ) {
367
+ switch (recv[22 ]) {
368
+
369
+ case 0x03 : // Brightness down press
370
+ it4.lock ();
371
+ brightness = recv[30 ];
372
+ it4.unlock ();
373
+ break ;
374
+
375
+ case 0x02 : // Brightness up press
376
+ it4.lock ();
377
+ brightness = recv[30 ];
378
+ it4.unlock ();
379
+ break ;
380
+ default :
381
+ // std::cout << "Unknown Packet! " << (int)recv[22] << std::endl;
382
+ break ;
383
+ }
384
+
385
+ switch (recv[15 ]) {
386
+
387
+ case 0x03 : // Brightness from cmd
388
+ it4.lock ();
389
+ brightness = recv[23 ];
390
+ it4.unlock ();
391
+ break ;
392
+
393
+ default :
394
+ // todo
395
+ break ;
396
+ }
397
+ }
398
+ }
399
+ }
328
400
329
401
330
402
int StartConnection ()
@@ -339,7 +411,8 @@ int StartConnection()
339
411
std::cout << " Opening Device" << std::endl;
340
412
// open device
341
413
device = open_device ();
342
- if (!device) {
414
+ device4 = open_device4 ();
415
+ if (!device || !device4) {
343
416
std::cout << " Unable to open device" << std::endl;
344
417
return 1 ;
345
418
}
@@ -348,23 +421,34 @@ int StartConnection()
348
421
std::cout << " Sending Payload" << std::endl;
349
422
// open the floodgates
350
423
uint8_t magic_payload[] = { 0x00 , 0xaa , 0xc5 , 0xd1 , 0x21 , 0x42 , 0x04 , 0x00 , 0x19 , 0x01 };
424
+
425
+
351
426
int res = hid_write (device, magic_payload, sizeof (magic_payload));
352
427
if (res < 0 ) {
353
428
std::cout << " Unable to write to device" << std::endl;
354
429
return 1 ;
355
430
}
356
- ThreadParams params = { device };
431
+ ThreadParams trackParams = { device };
357
432
g_isTracking = true ;
358
- std::cout << " Starting Thread" << std::endl;
433
+ std::cout << " Tracking Starting Thread" << std::endl;
359
434
360
435
361
436
// Start Tracking Thread
362
- trackThread = CreateThread (NULL , 0 , track, ¶ms , 0 , NULL );
437
+ trackThread = CreateThread (NULL , 0 , track, &trackParams , 0 , NULL );
363
438
if (trackThread == NULL ) {
364
439
std::cout << " Failed to create thread" << std::endl;
365
440
return 1 ;
366
441
}
367
- std::cout << " Thread Started" << std::endl;
442
+
443
+ ThreadParams listenParams = { };
444
+ g_isListening = true ;
445
+ // Start Interface 4 listener
446
+ listenThread = CreateThread (NULL , 0 , interface4Handler, &listenParams, 0 , NULL );
447
+ if (listenThread == NULL ) {
448
+ std::cout << " Failed to create thread" << std::endl;
449
+ return 1 ;
450
+ }
451
+ std::cout << " Listenr Thread Started" << std::endl;
368
452
369
453
return 1 ;
370
454
}
@@ -375,20 +459,27 @@ int StopConnection()
375
459
{
376
460
if (g_isTracking) {
377
461
g_isTracking = false ;
378
- // Wait for the thread to finish
462
+ g_isListening = false ;
463
+ // Wait for the track thread to finish
379
464
WaitForSingleObject (trackThread, INFINITE);
380
465
TerminateThread (trackThread, 0 );
381
466
CloseHandle (trackThread);
467
+
468
+ // Wait for the listen thread to finish
469
+ WaitForSingleObject (listenThread, INFINITE);
470
+ TerminateThread (listenThread, 0 );
471
+ CloseHandle (listenThread);
382
472
return 1 ;
383
473
}
384
474
else {
385
475
return -1 ;
386
476
}
387
477
}
388
478
479
+ float * q = new float [4 ];
480
+
389
481
float * GetQuaternion ()
390
482
{
391
- float * q = new float [4 ];
392
483
mtx.lock ();
393
484
q[0 ] = qt.array [0 ];
394
485
q[1 ] = qt.array [1 ];
@@ -398,13 +489,26 @@ float* GetQuaternion()
398
489
return q;
399
490
}
400
491
492
+ float * e = new float [3 ];
493
+
401
494
float * GetEuler ()
402
495
{
403
- float * e = new float [ 3 ];
496
+
404
497
mtx.lock ();
405
498
e[0 ] = euler.angle .pitch ;
406
499
e[1 ] = euler.angle .roll ;
407
500
e[2 ] = euler.angle .yaw ;
408
501
mtx.unlock ();
409
502
return e;
410
- }
503
+ }
504
+
505
+
506
+ int GetBrightness ()
507
+ {
508
+ int curBrightness;
509
+ it4.lock ();
510
+ curBrightness = brightness;
511
+ it4.unlock ();
512
+
513
+ return curBrightness;
514
+ }
0 commit comments