-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiiRemote.m
1633 lines (1344 loc) · 49.9 KB
/
WiiRemote.m
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// WiiRemote.m
// DarwiinRemote
//
// Created by KIMURA Hiroaki on 06/12/04.
// Copyright 2006 KIMURA Hiroaki. All rights reserved.
//
#import "WiiRemote.h"
static WiiJoyStickCalibData kWiiNullJoystickCalibData = {0, 0, 0, 0, 0, 0};
static WiiAccCalibData kWiiNullAccCalibData = {0, 0, 0, 0, 0, 0};
// define some constants
#define kWiiIRPixelsWidth 1024.0
#define kWiiIRPixelsHeight 768.0
#define WII_DECRYPT(data) (((data ^ 0x17) + 0x17) & 0xFF)
#define BIT_2x8_16(dp1, dp2) ((dp1 << 8) | dp2)
#define PRINT_BB_GRID(grid, value) printf("===Grid %fkg===\nTR %f\nBR %f\nTL %f\nTR %f\n", value, grid.tr, grid.br, grid.tl, grid.tr)
#define WIR_HALFRANGE 256.0
#define WIR_INTERVAL 10.0
// Notification strings
NSString * WiiRemoteExpansionPortChangedNotification = @"WiiRemoteExpansionPortChangedNotification";
// this type is used a lot (data array):
typedef unsigned char darr[];
typedef enum {
kWiiRemoteTwoButton = 0x0001,
kWiiRemoteOneButton = 0x0002,
kWiiRemoteBButton = 0x0004,
kWiiRemoteAButton = 0x0008,
kWiiRemoteMinusButton = 0x0010,
kWiiRemoteHomeButton = 0x0080,
kWiiRemoteLeftButton = 0x0100,
kWiiRemoteRightButton = 0x0200,
kWiiRemoteDownButton = 0x0400,
kWiiRemoteUpButton = 0x0800,
kWiiRemotePlusButton = 0x1000,
kWiiNunchukZButton = 0x0001,
kWiiNunchukCButton = 0x0002,
kWiiClassicControllerUpButton = 0x0001,
kWiiClassicControllerLeftButton = 0x0002,
kWiiClassicControllerZRButton = 0x0004,
kWiiClassicControllerXButton = 0x0008,
kWiiClassicControllerAButton = 0x0010,
kWiiClassicControllerYButton = 0x0020,
kWiiClassicControllerBButton = 0x0040,
kWiiClassicControllerZLButton = 0x0080,
kWiiClassicControllerUnUsedButton = 0x0100,
kWiiClassicControllerRButton = 0x0200,
kWiiClassicControllerPlusButton = 0x0400,
kWiiClassicControllerHomeButton = 0x0800,
kWiiClassicControllerMinusButton = 0x1000,
kWiiClassicControllerLButton = 0x2000,
kWiiClassicControllerDownButton = 0x4000,
kWiiClassicControllerRightButton = 0x8000
} WiiButtonBitMask;
@interface WiiRemote (Private)
- (IOBluetoothL2CAPChannel *) openL2CAPChannelWithPSM:(BluetoothL2CAPPSM) psm delegate:(id) delegate;
@end
@implementation WiiRemote
- (id) init
{
self = [super init];
NSLogDebug (@"Wii instantiated");
if (self != nil) {
#ifdef DEBUG
/* Allow full protocol logging */
_dump = TRUE;
#endif
accX = 0x10;
accY = 0x10;
accZ = 0x10;
buttonData = 0;
leftPoint = -1;
_batteryLevel = 0;
_warningBatteryLevel = 0.05;
_delegate = nil;
_shouldUpdateReportMode = NO;
_shouldReadExpansionCalibration = NO;
_shouldSetInitialConfiguration = YES; //Used as dirty hack to make sure the Initial configuration is set _after_ device has be indentified
_wiiDevice = nil;
_opened = NO;
_ichan = nil;
_cchan = nil;
_isIRSensorEnabled = NO;
_isMotionSensorEnabled = NO;
_isVibrationEnabled = NO;
_isExpansionPortEnabled = NO;
_isExpansionPortAttached = NO;
wiiIRMode = kWiiIRModeExtended;
expType = WiiExpUknown;
}
return self;
}
- (void) dealloc
{
NSLogDebug (@"Wii released");
[super dealloc];
}
- (void) setDelegate:(id) delegate
{
_delegate = delegate;
}
- (BOOL) available
{
if ((_wiiDevice != nil) && (_cchan != nil) && (_ichan != nil))
return YES;
return NO;
}
- (IOReturn) connectTo:(IOBluetoothDevice *) device
{
_wiiDevice = device;
if (_wiiDevice == nil)
return kIOReturnBadArgument;
IOReturn ret = kIOReturnSuccess;
// it seems like it is not needed to call openConnection in order to open L2CAP channels ...
_cchan = [self openL2CAPChannelWithPSM:kBluetoothL2CAPPSMHIDControl delegate:self];
if (!_cchan)
return kIOReturnNotOpen;
usleep (20000);
_ichan = [self openL2CAPChannelWithPSM:kBluetoothL2CAPPSMHIDInterrupt delegate:self];
if (!_ichan)
return kIOReturnNotOpen;
NSLogDebug(@"Allow bluetooth stack to 'settle', wait few milliseconds");
usleep (20000);
//statusTimer = [[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(getCurrentStatus:) userInfo:nil repeats:YES] retain];
//Initial polling - find out of status of current device
ret = [self getCurrentStatus:nil];
/* Allow recoginition of device, BB for example is kind of slow in startup ;-)
* Value determined by experimenting:
* *20000 is causing timeouts
* *1000 causes WiiBalanceBoard not be be detected
*/
usleep (10000);
/* Moved initial intitalization after device recognising scheme, as Balanance Board dieds while polling the IRSensor
* Find a more clever way to detect we are actually talking to the Wii Device
*/
if ((ret == kIOReturnSuccess) && [self available]) {
disconnectNotification = [_wiiDevice registerForDisconnectNotification:self selector:@selector(disconnected:fromDevice:)];
_opened = YES;
} else {
_opened = NO;
[self closeConnection];
}
return ret;
}
- (void) disconnected:(IOBluetoothUserNotification*) note fromDevice:(IOBluetoothDevice*) device
{
NSLogDebug (@"Disconnected.");
if (device == _wiiDevice) {
// _cchan = nil;
// _ichan = nil;
[self closeConnection];
}
}
- (IOReturn) sendCommand:(const unsigned char *) data length:(size_t) length
{
unsigned char buf[40];
memset (buf, 0, 40);
buf[0] = 0x52;
memcpy (buf+1, data, length);
if (buf[1] == 0x16) length = 23;
else length++;
#ifdef DEBUG
if (_dump) {
int i;
printf ("channel:%i - send%3u:", [_cchan getPSM], (unsigned int)length);
for(i=0 ; i<length ; i++) {
printf(" %02X", buf[i]);
}
printf("\n");
}
#endif
IOReturn ret = kIOReturnSuccess;
int i;
// cam: i think there is no need to do the loop many times in order to see there is an error
// if there's an error it must be managed right away
for (i=0; i<10 ; i++) {
ret = [_cchan writeSync:buf length:length];
if (ret != kIOReturnSuccess) {
NSLogDebug(@"Write Error for command 0x%x:", buf[1], ret);
LogIOReturn (ret);
// [self closeConnection];
usleep (10000);
}
else
break;
}
return ret;
}
- (double) batteryLevel
{
return _batteryLevel;
}
- (NSString *) address
{
return [_wiiDevice getAddressString];
}
- (void) setMotionSensorEnabled:(BOOL) enabled
{
if (enabled) {
NSLogDebug (@"Set motion sensor enabled");
} else {
NSLogDebug (@"Set motion sensor disabled");
}
// this variable indicate a desire, and should be updated regardless of the sucess of sending the command
_isMotionSensorEnabled = enabled;
[self updateReportMode];
}
- (void) setForceFeedbackEnabled:(BOOL) enabled
{
// this variable indicate a desire, and should be updated regardless of the sucess of sending the command
_isVibrationEnabled = enabled;
[self updateReportMode];
}
- (void) setLEDEnabled1:(BOOL) enabled1 enabled2:(BOOL) enabled2 enabled3:(BOOL) enabled3 enabled4:(BOOL) enabled4
{
unsigned char cmd[] = {0x11, 0x00};
if (_isVibrationEnabled) cmd[1] |= 0x01;
if (enabled1) cmd[1] |= 0x10;
if (enabled2) cmd[1] |= 0x20;
if (enabled3) cmd[1] |= 0x40;
if (enabled4) cmd[1] |= 0x80;
_isLED1Illuminated = enabled1;
_isLED2Illuminated = enabled2;
_isLED3Illuminated = enabled3;
_isLED4Illuminated = enabled4;
IOReturn ret = [self sendCommand:cmd length:2];
LogIOReturn (ret);
}
- (void) updateReportMode
{
_shouldUpdateReportMode = YES;
if (!_cchan)
[self closeConnection];
}
- (IOReturn) doUpdateReportMode
{
if (!_shouldUpdateReportMode)
return kIOReturnSuccess;
_shouldUpdateReportMode = NO;
NSLogDebug (@"Updating Report Mode");
// Set the report type the Wiimote should send.
unsigned char cmd[] = {0x12, 0x02, 0x30}; // Just buttons.
/*
There are numerous status report types that can be requested.
The IR reports must be matched with the data format set when initializing the IR camera:
0x36, 0x37 - 10 IR bytes go with Basic mode
0x33 - 12 IR bytes go with Extended mode
0x3e/0x3f - 36 IR bytes go with Full mode
The Nunchuk and Classic controller use 6 bytes to report their state, so the reports that
give more extension bytes don't provide any more info.
Buttons | Accelerometer | IR | Extension
--------------------+-------------------+-----------+-------------
0x30: Core Buttons | | |
0x31: Core Buttons | Accelerometer | |
0x32: Core Buttons | | | 8 bytes
0x33: Core Buttons | Accelerometer | 12 bytes |
0x34: Core Buttons | | | 19 bytes
0x35: Core Buttons | Accelerometer | | 16 bytes
0x36: Core Buttons | | 10 bytes | 9 bytes
0x37: Core Buttons | Accelerometer | 10 bytes | 6 bytes
?? 0x38: Core Buttons and Accelerometer with 16 IR bytes ??
0x3d: | | | 21 bytes
0x3e / 0x3f: Interleaved Core Buttons and Accelerometer with 16/36 IR bytes
*/
if (_isIRSensorEnabled) {
cmd[2] = _isExpansionPortEnabled ? 0x36 : 0x33; // Buttons, 10 IR Bytes, 9 Extension Bytes
wiiIRMode = _isExpansionPortEnabled ? kWiiIRModeBasic : kWiiIRModeExtended;
// Set IR Mode
NSLogDebug (@"Setting IR Mode to finish initialization.");
// I don't think it should be here ...
[self writeData:(darr){ wiiIRMode } at:0x04B00033 length:1];
usleep(10000);
} else {
cmd[2] = _isExpansionPortEnabled ? 0x34 : 0x30; // Buttons, 19 Extension Bytes
}
if (_isVibrationEnabled)
cmd[1] |= 0x01;
if (_isMotionSensorEnabled)
cmd[2] |= 0x01; // Add Accelerometer
usleep(10000);
return [self sendCommand:cmd length:3];
} // updateReportMode
- (void) setExpansionPortEnabled:(BOOL) enabled
{
IOReturn ret = kIOReturnSuccess;
if (enabled)
NSLogDebug (@"Enabling expansion port.");
else
NSLogDebug (@"Disabling expansion port.");
if (_isExpansionPortAttached) {
_isExpansionPortEnabled = enabled;
// get expansion device calibration data
_shouldReadExpansionCalibration = YES;
/* Calibration of WiiRemote takes up only 16 bytes, BalanceBoard however uses 32 bytes
* http://wiibrew.org/wiki/Wii_Balance_Board#Calibration_Data as second set of bytes on WiiRemote
* are zero http://wiibrew.org/wiki/Wiimote#Extension_Controller no harm of fetching the full 32 bytes :-)
*/
NSLogDebug (@"Requesting expansion calibration data");
ret = [self readData:0x04A40020 length: 32];
LogIOReturn (ret);
}
[self updateReportMode];
}
//based on Ian's codes. thanks!
- (void) setIRSensorEnabled:(BOOL) enabled
{
_isIRSensorEnabled = enabled;
// ir enable 1
IOReturn ret = kIOReturnSuccess;
unsigned char cmd[] = {0x13, 0x00};
if (_isVibrationEnabled) cmd[1] |= 0x01;
if (_isIRSensorEnabled) cmd[1] |= 0x04;
ret = [self sendCommand:cmd length:2];
LogIOReturn (ret);
usleep(10000);
// set register 0x1a (ir enable 2)
unsigned char cmd2[] = {0x1a, 0x00};
if (_isIRSensorEnabled) cmd2[1] |= 0x04;
ret = [self sendCommand:cmd2 length:2];
LogIOReturn (ret);
usleep(10000);
if (_isIRSensorEnabled) {
NSLogDebug (@"Enabling IR Sensor");
// based on marcan's method, found on wiili wiki:
// tweaked to include some aspects of cliff's setup procedure in the hopes
// of it actually turning on 100% of the time (was seeing 30-40% failure rate before)
// the sleeps help it it seems
// start initializing camera
ret = [self writeData:(darr){0x01} at:0x04B00030 length:1];
LogIOReturn (ret);
usleep(10000);
// set sensitivity block 1
ret = [self writeData:(darr){0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0xC0} at:0x04B00000 length:9];
LogIOReturn (ret);
usleep(10000);
// set sensitivity block 2
ret = [self writeData:(darr){0x40, 0x00} at:0x04B0001A length:2];
LogIOReturn (ret);
usleep(10000);
// finish initializing camera
ret = [self writeData:(darr){0x08} at:0x04B00030 length:1];
LogIOReturn (ret);
usleep(10000);
if (ret != kIOReturnSuccess) {
NSLogDebug (@"Set IR Enabled failed, closing connection");
// [self closeConnection];
_isIRSensorEnabled = NO;
return;
}
}
[self updateReportMode];
}
- (IOReturn) writeData:(const unsigned char*) data at:(unsigned long) address length:(size_t) length
{
unsigned char cmd [22];
if (length > 16)
NSLog (@"Error! Trying to write more than 16 bytes of data (length=%i)", length);
memset (cmd, 0, 22);
memcpy (cmd + 6, data, length);
// register write header
cmd[0] = 0x16;
// write address
cmd[1] = (address >> 24) & 0xFF;
cmd[2] = (address >> 16) & 0xFF;
cmd[3] = (address >> 8) & 0xFF;
cmd[4] = (address >> 0) & 0xFF;
// data length
cmd[5] = length;
// and of course the vibration flag, as usual
if (_isVibrationEnabled)
cmd[1] |= 0x01;
return [self sendCommand:cmd length:22];
}
- (IOReturn) readData:(unsigned long) address length:(unsigned short) length
{
unsigned char cmd[7];
unsigned long addr = address;
unsigned short len = length;
cmd[0] = 0x17; // read memory
// address
cmd[1] = (addr >> 24) & 0xFF; // bit 2 being set indicates read from control registers, 0 indicates EEPROM Memory
cmd[2] = (addr >> 16) & 0xFF;
cmd[3] = (addr >> 8) & 0xFF;
cmd[4] = (addr >> 0) & 0xFF;
// Number of bytes to read
cmd[5] = (len >> 8) & 0xFF;
cmd[6] = (len >> 0) & 0xFF;
if (_isVibrationEnabled)
cmd[1] |= 0x01;
return [self sendCommand:cmd length:7];
}
- (IOReturn) closeConnection
{
IOReturn ret = 0;
_opened = NO;
[disconnectNotification unregister];
disconnectNotification = nil;
[_delegate wiiRemoteDisconnected:_wiiDevice];
_delegate = nil;
// cam: set delegate to nil
[_cchan setDelegate:nil];
ret = [_cchan closeChannel];
_cchan = nil;
LogIOReturn (ret);
[_ichan setDelegate:nil];
ret = [_ichan closeChannel];
_ichan = nil;
LogIOReturn (ret);
ret = [_wiiDevice closeConnection];
_wiiDevice = nil;
LogIOReturn (ret);
// no longer a delegate
[statusTimer invalidate];
[statusTimer release];
statusTimer = nil;
return ret;
}
- (void) setInitialConfiguration
{
if (_shouldSetInitialConfiguration) {
if (expType != WiiBalanceBoard) {
NSLogDebug(@"Setting default wiimote values");
IOReturn ret = [self readData:0x0020 length:7]; // Get Accelerometer calibration data
if (ret == kIOReturnSuccess) {
[self setMotionSensorEnabled:YES];
[self setIRSensorEnabled:NO];
[self setForceFeedbackEnabled:NO];
[self setLEDEnabled1:YES enabled2:NO enabled3:NO enabled4:NO];
[self updateReportMode];
//ret = [self doUpdateReportMode];
}
}
_shouldSetInitialConfiguration = NO;
}
}
- (void) handleWriteResponse:(unsigned char *) dp length:(size_t) dataLength
{
NSLogDebug (@"Write data response: %00x %00x %00x %00x", dp[2], dp[3], dp[4], dp[5]);
}
/** Also see: http://wiibrew.org/wiki/Wiimote#Reading_and_Writing
* Handle report 0x21 (Read Data) from wiimote.
* dp[0] = Bluetooth header
* dp[1] = (0x21) Report/Channel ID
* dp[2] = Wiimote Buttons
* dp[3] = Wiimote Buttons
* dp[4] = High 4 bits = payload size; Low 4 bits = Error flag (0 = all good)
* dp[5] = Offset of memory read
* dp[6] = Offset of memory read
* dp[7+] = the Data.
**/
- (void) handleRAMData:(unsigned char *) dp length:(size_t) dataLength
{
/**
if (dp[1] == 0x21){
int i;
printf ("ack%3d:", dataLength);
for(i=0 ; i<dataLength ; i++) {
printf(" %02X", dp[i]);
}
printf("\n");
}**/
unsigned short addr = (dp[5] * 256) + dp[6];
NSLogDebug (@"handleRAMData (0x21) addr=0x%x", addr);
//mii data
if ((dataLength >= 20) && (addr >= WIIMOTE_MII_DATA_BEGIN_ADDR) && (addr <= WIIMOTE_MII_CHECKSUM1_ADDR)) {
if ([_delegate respondsToSelector:@selector (gotMiidata:at:)]) {
int slot = MII_SLOT(addr);
int len = dataLength - 7;
memcpy (&mii_data_buf[mii_data_offset], &dp[7], len);
mii_data_offset += len;
if (mii_data_offset >= WIIMOTE_MII_DATA_BYTES_PER_SLOT)
[_delegate gotMiiData: (Mii *)mii_data_buf at: slot];
}
}
// specify attached expasion device
if (addr == 0x00F0) {
NSLogDebug (@"Expansion device connected.");
switch (WII_DECRYPT(dp[21])) {
case 0x00:
NSLogDebug (@"Nunchuk connected.");
if (expType != WiiNunchuk) {
expType = WiiNunchuk;
[[NSNotificationCenter defaultCenter] postNotificationName:WiiRemoteExpansionPortChangedNotification object:self];
}
break;
case 0x01:
NSLogDebug (@"Classic controller connected.");
if (expType != WiiClassicController) {
expType = WiiClassicController;
[[NSNotificationCenter defaultCenter] postNotificationName:WiiRemoteExpansionPortChangedNotification object:self];
}
break;
case 0x2a:
NSLogDebug (@"Balance Board connected.");
if (expType != WiiBalanceBoard) {
expType = WiiBalanceBoard;
[[NSNotificationCenter defaultCenter] postNotificationName:WiiRemoteExpansionPortChangedNotification object:self];
}
break;
case 0x2e:
NSLogDebug(@"No Expansion detected.");
if (expType != WiiExpNotAttached) {
expType = WiiExpNotAttached;
[[NSNotificationCenter defaultCenter] postNotificationName:WiiRemoteExpansionPortChangedNotification object:self];
}
break;
default:
NSLogDebug (@"Unknown device connected (0x%x). ", WII_DECRYPT(dp[21]));
expType = WiiExpUknown;
break;
}
[self setInitialConfiguration];
return;
}
// wiimote calibration data
if (!_shouldReadExpansionCalibration && (addr == 0x0020)) {
NSLogDebug (@"Read Wii calibration");
wiiCalibData.accX_zero = dp[7] << 1;
wiiCalibData.accY_zero = dp[8] << 1;
wiiCalibData.accZ_zero = dp[9] << 1;
//dp[10] - unknown/unused
wiiCalibData.accX_1g = dp[11] << 1;
wiiCalibData.accY_1g = dp[12] << 1;
wiiCalibData.accZ_1g = dp[13] << 1;
return;
}
// expansion device calibration data.
if (_shouldReadExpansionCalibration && (addr == 0x0020)) {
if (expType == WiiNunchuk) {
NSLogDebug (@"Read nunchuk calibration");
//nunchuk calibration data
nunchukCalibData.accX_zero = WII_DECRYPT(dp[7]);
nunchukCalibData.accY_zero = WII_DECRYPT(dp[8]);
nunchukCalibData.accZ_zero = WII_DECRYPT(dp[9]);
nunchukCalibData.accX_1g = WII_DECRYPT(dp[11]);
nunchukCalibData.accY_1g = WII_DECRYPT(dp[12]);
nunchukCalibData.accZ_1g = WII_DECRYPT(dp[13]);
nunchukJoyStickCalibData.x_max = WII_DECRYPT(dp[15]);
nunchukJoyStickCalibData.x_min = WII_DECRYPT(dp[16]);
nunchukJoyStickCalibData.x_center = WII_DECRYPT(dp[17]);
nunchukJoyStickCalibData.y_max = WII_DECRYPT(dp[18]);
nunchukJoyStickCalibData.y_min = WII_DECRYPT(dp[19]);
nunchukJoyStickCalibData.y_center = WII_DECRYPT(dp[20]);
_shouldReadExpansionCalibration = NO;
return;
} else if (expType == WiiBalanceBoard) {
NSLogDebug (@"Read Balance Board calibration");
/* Format found at http://wiibrew.org/wiki/Wii_Balance_Board#Calibration_Data
* in 24 bytes from 0xa40024 to 0xa4003a
*/
/* First 4 values 0xa40020 - 0xa40023 unknown */
balanceBoardCalibData.kg0.tr = BIT_2x8_16(dp[11], dp[12]);
balanceBoardCalibData.kg0.br = BIT_2x8_16(dp[13], dp[14]);
balanceBoardCalibData.kg0.tl = BIT_2x8_16(dp[15], dp[16]);
balanceBoardCalibData.kg0.bl = BIT_2x8_16(dp[17], dp[18]);
balanceBoardCalibData.kg17.tr = BIT_2x8_16(dp[19], dp[20]);
balanceBoardCalibData.kg17.br = BIT_2x8_16(dp[21], dp[22]);
/* Not yet fully configured, so keep the _shouldReadExpansionCalibration set */
return;
} else if (expType == WiiClassicController) {
//classic controller calibration data (probably)
}
} else if (_shouldReadExpansionCalibration && (addr == 0x0030)) {
if (expType == WiiBalanceBoard) {
balanceBoardCalibData.kg17.tl = BIT_2x8_16(dp[7], dp[8]);
balanceBoardCalibData.kg17.bl = BIT_2x8_16(dp[9], dp[10]);
balanceBoardCalibData.kg34.tr = BIT_2x8_16(dp[11], dp[12]);
balanceBoardCalibData.kg34.br = BIT_2x8_16(dp[13], dp[14]);
balanceBoardCalibData.kg34.tl = BIT_2x8_16(dp[15], dp[16]);
balanceBoardCalibData.kg34.bl = BIT_2x8_16(dp[17], dp[18]);
/* Usage of last 4 values also unkown */
PRINT_BB_GRID(balanceBoardCalibData.kg0, (float)0);
PRINT_BB_GRID(balanceBoardCalibData.kg17, (float)17);
PRINT_BB_GRID(balanceBoardCalibData.kg34, (float)34);
/* All data read, as 0x0020 data is presented to us already */
_shouldReadExpansionCalibration = NO;
}
} // expansion device calibration data
// wiimote buttons
buttonData = ((short)dp[2] << 8) + dp[3];
[self sendWiiRemoteButtonEvent:buttonData];
} // handleRAMData
- (void) handleStatusReport:(unsigned char *) dp length:(size_t) dataLength
{
NSLogDebug (@"Status Report (0x%x)", dp[4]);
/* sample: A1 20 00 00 02 00 00 AB */
double level = (double) dp[7];
level /= (double) 0xC0; // C0 = fully charged.
if (level != _batteryLevel) {
_batteryLevel = level;
if ([_delegate respondsToSelector:@selector (batteryLevelChanged:)])
[_delegate batteryLevelChanged:_batteryLevel];
}
if (_batteryLevel < _warningBatteryLevel)
[[NSNotificationCenter defaultCenter] postNotificationName:@"WiiRemoteBatteryLowNotification" object:self];
IOReturn ret = kIOReturnSuccess;
if (dp[4] & 0x02) { //some device attached to Wiimote
NSLogDebug (@"Expantion device Attached");
if (!_isExpansionPortAttached) {
/* Initialize the device, reason unknown see for example
* http://www.wiili.org/index.php/Wiimote/Extension_Controllers/Nunchuk#Communication
*/
ret = [self writeData:(darr){0x00} at:(unsigned long)0x04A40040 length:1];
usleep (10000);
if (ret != kIOReturnSuccess) {
NSLogDebug (@"Problem occured while initializing the expansion port.");
LogIOReturn (ret);
return;
}
/* Purpose unkown, but only succesfull when controller attached */
IOReturn ret = [self readData:0x04A400F0 length:16]; // read expansion device type
LogIOReturn (ret);
_isExpansionPortAttached = (ret == kIOReturnSuccess);
if (ret == kIOReturnSuccess) {
NSLogDebug (@"Expansion Device initialized");
} else
NSLogDebug (@"Failed to initialize Expansion Device");
return;
}
} else { // unplugged
if (_isExpansionPortAttached) {
NSLogDebug (@"Device Detached");
_isExpansionPortAttached = NO;
expType = WiiExpNotAttached;
[[NSNotificationCenter defaultCenter] postNotificationName:WiiRemoteExpansionPortChangedNotification object:self];
}
}
[self setInitialConfiguration];
_isLED1Illuminated = (dp[4] & 0x10);
_isLED2Illuminated = (dp[4] & 0x20);
_isLED3Illuminated = (dp[4] & 0x40);
_isLED4Illuminated = (dp[4] & 0x80);
} // handleStatusReport
- (float) bbPressure2kg:(float) value pkg0:(float) pkg0 pkg17:(float) pkg17 pkg34:(float) pkg34
{
/* Convert to Kilograms
* 0kg=0; value=20; 17kg=100
* eachKG = high - low / 17
* startKG = 0
* result = startKG + (value - low) /result;
*/
if (value < pkg0) {
/* Lower than 0kg should never happen..., but make it 0 anyway */
return 0;
} else if (value < pkg17) {
return 0 + (value - pkg0) / ((pkg17 - pkg0) / 17);
} else if (value < pkg34) {
return 17 + (value - pkg17) / ((pkg34 - pkg17) / 17);
} else {
return 34 + (value - pkg34) / ((pkg34 - pkg0) / 34);
}
}
- (void) handleExtensionData:(unsigned char *) dp length:(size_t) dataLength
{
unsigned char startByte;
switch (dp[1]) {
case 0x34 :
startByte = 4;
break;
case 0x35 :
startByte = 7;
break;
case 0x36 :
startByte = 14;
break;
case 0x37 :
startByte = 17;
break;
default:
NSLogDebug (@"Unsupported Report mode for extension data.");
return; // This shouldn't ever happen.
}
switch (expType) {
case WiiNunchuk:
nStickX = WII_DECRYPT(dp[startByte +0]);
nStickY = WII_DECRYPT(dp[startByte +1]);
nAccX = WII_DECRYPT(dp[startByte +2]);
nAccY = WII_DECRYPT(dp[startByte +3]);
nAccZ = WII_DECRYPT(dp[startByte +4]);
nButtonData = WII_DECRYPT(dp[startByte +5]);
[self sendWiiNunchukButtonEvent:nButtonData];
if ([_delegate respondsToSelector:@selector (accelerationChanged:accX:accY:accZ:)])
[_delegate accelerationChanged:WiiNunchukAccelerationSensor accX:nAccX accY:nAccY accZ:nAccZ];
if ([_delegate respondsToSelector:@selector (joyStickChanged:tiltX:tiltY:)])
[_delegate joyStickChanged:WiiNunchukJoyStick tiltX:nStickX tiltY:nStickY];
break;
case WiiClassicController:
cButtonData = (unsigned short)((WII_DECRYPT(dp[startByte+4]) << 8) + WII_DECRYPT(dp[startByte+5]));
cStickX1 = WII_DECRYPT(dp[startByte +0]) & 0x3F;
cStickY1 = WII_DECRYPT(dp[startByte +1]) & 0x3F;
cStickX2 =
(((WII_DECRYPT(dp[startByte +0]) & 0xC0) >> 3) |
((WII_DECRYPT(dp[startByte +1]) & 0xC0) >> 5) |
((WII_DECRYPT(dp[startByte +2]) & 0x80) >> 7)) & 0x1F;
cStickY2 = WII_DECRYPT(dp[startByte +2]) & 0x1F;
cAnalogL =
(((WII_DECRYPT(dp[startByte +2]) & 0x60) >> 2) |
((WII_DECRYPT(dp[startByte +3]) & 0xE0) >> 5)) & 0x1F;
cAnalogR = WII_DECRYPT(dp[startByte +3]) & 0x1F;
[self sendWiiClassicControllerButtonEvent:cButtonData];
if ([_delegate respondsToSelector:@selector (joyStickChanged:tiltX:tiltY:)]) {
[_delegate joyStickChanged:WiiClassicControllerLeftJoyStick tiltX:cStickX1 tiltY:cStickY1];
[_delegate joyStickChanged:WiiClassicControllerRightJoyStick tiltX:cStickX2 tiltY:cStickY2];
}
if ([_delegate respondsToSelector:@selector (analogButtonChanged:amount:)]) {
[_delegate analogButtonChanged:WiiClassicControllerLButton amount:cAnalogL];
[_delegate analogButtonChanged:WiiClassicControllerRButton amount:cAnalogR];
}
break;
case WiiBalanceBoard:
/* http://www.wiili.org/index.php/Wii_Balance_Board_PC_Drivers
* 34 00 00 AA AA BB BB CC CC DD DD? ? ? ? EE..
* AA AA right-top (L)
* BB BB right-bottom (S)
* CC CC left-top (S)
* DD DD left-bottom (L)
*
* (AA AA... are BigEndian)
*
* It seems that some value is also in EE, but the usage is unclear. There are four sensors.
* It seems that they are placed at each of the 4 feet of the balance board itself.
*
* +--------------------+
* | C0C1 (S) A0A1 (L) | bPressureTL | bPressureTR // b(alance board) Pressure (sensor) [T(top)|B(bottom)][L(eft)|R(ight)]
* | D0D1 (L) B0B1 (S) | bPressureBL | bPressureBR
* | POWER |
* +--------------------+
*/
bPressure.tr = BIT_2x8_16(dp[startByte +0], dp[startByte +1]);
bPressure.br = BIT_2x8_16(dp[startByte +2], dp[startByte +3]);
bPressure.tl = BIT_2x8_16(dp[startByte +4], dp[startByte +5]);
bPressure.bl = BIT_2x8_16(dp[startByte +6], dp[startByte +7]);
bKg.tr = [self bbPressure2kg:bPressure.tr pkg0:balanceBoardCalibData.kg0.tr pkg17:balanceBoardCalibData.kg17.tr pkg34:balanceBoardCalibData.kg34.tr];
bKg.br = [self bbPressure2kg:bPressure.br pkg0:balanceBoardCalibData.kg0.br pkg17:balanceBoardCalibData.kg17.br pkg34:balanceBoardCalibData.kg34.br];
bKg.tl = [self bbPressure2kg:bPressure.tl pkg0:balanceBoardCalibData.kg0.tl pkg17:balanceBoardCalibData.kg17.tl pkg34:balanceBoardCalibData.kg34.tl];
bKg.bl = [self bbPressure2kg:bPressure.bl pkg0:balanceBoardCalibData.kg0.bl pkg17:balanceBoardCalibData.kg17.bl pkg34:balanceBoardCalibData.kg34.bl];
if ([_delegate respondsToSelector:@selector (pressureChanged:pressureTR:pressureBR:pressureTL:pressureBL:)])
/* Don't use raw values, but use KG for the time beeing */
//[_delegate pressureChanged:WiiBalanceBoardPressureSensor pressureTR:bPressure.tr pressureBR:bPressure.br pressureTL:bPressure.tl pressureBL:bPressure.bl];
[_delegate pressureChanged:WiiBalanceBoardPressureSensor pressureTR:bKg.tr pressureBR:bKg.br pressureTL:bKg.tl pressureBL:bKg.bl];
if ([_delegate respondsToSelector:@selector (rawPressureChanged:)])
[_delegate rawPressureChanged:bPressure];
if ([_delegate respondsToSelector:@selector (allPressureChanged:bbData:bbDataInKg:)])
[_delegate allPressureChanged:WiiBalanceBoardPressureSensor bbData:bPressure bbDataInKg:bKg];
break;
}
} // handleExtensionData
- (void) handleIRData:(unsigned char *) dp length:(size_t) dataLength
{
/* Set all IR data to array, based on input format */
// NSLog(@"Handling IR Data for 0x%00x", dp[1]);
int i = 0;
if (dp[1] == 0x33) { // 12 IR bytes
int startByte = 0;
for(i=0 ; i < 4 ; i++) {
startByte = 7 + 3 * i;
irData[i].x = (dp[startByte +0] | ((dp[startByte +2] & 0x30) << 4)) & 0x3FF;
irData[i].y = (dp[startByte +1] | ((dp[startByte +2] & 0xC0) << 2)) & 0x3FF;
irData[i].s = dp[startByte +2] & 0x0F;
}
} else { // 10 IR bytes
int shift = (dp[1] == 0x36) ? 4 : 7;
int startByte = 0;
int i;
for (i=0; i < 2; i++) {
startByte = shift + 5 * i;
irData[2*i].x = (dp[startByte +0] | ((dp[startByte +2] & 0x30) << 4)) & 0x3FF;
irData[2*i].y = (dp[startByte +1] | ((dp[startByte +2] & 0xC0) << 2)) & 0x3FF;
irData[2*i].s = ((irData[2*i].x == irData[2*i].y) && (irData[2*i].x == 0x3FF)) ? 0x0F : 0x05; // No size is given in 10 byte report.
irData[(2*i)+1].x = (dp[startByte +3] | ((dp[startByte +2] & 0x03) << 8)) & 0x3FF;
irData[(2*i)+1].y = (dp[startByte +4] | ((dp[startByte +2] & 0x0C) << 6)) & 0x3FF;
irData[(2*i)+1].s = ((irData[(2*i)+1].x == irData[(2*i)+1].y) && (irData[(2*i)+1].x == 0x3FF)) ? 0x0F : 0x05; // No size is given in 10 byte report.
}
}
// NSLogDebug (@"IR Data (%i, %i, %i) (%i, %i, %i) (%i, %i, %i) (%i, %i, %i)",
// irData[0].x, irData[0].y, irData[0].s,
// irData[1].x, irData[1].y, irData[1].s,
// irData[2].x, irData[2].y, irData[2].s,
// irData[3].x, irData[3].y, irData[3].s);
/* Determine 2 out of 4 points to be used for position calculations */
int p1 = -1;
int p2 = -1;
// we should modify this loop to take the points with the lowest s (the brightest ones)
for (i=0 ; i<4 ; i++) {
if (p1 == -1) {
if (irData [i].s < 0x0F)
p1 = i;
} else {
if (irData [i].s < 0x0F) {
p2 = i;
break;
}
}
}
// NSLogDebug (@"p1=%i ; p2=%i", p1, p2);
double ox, oy;
/* Verify wether there exists two points allowing us to do proper tracking */
if ((p1 > -1) && (p2 > -1)) {
/* Determine left and right point??? */
int l = leftPoint;
if (leftPoint == -1) {
switch (orientation) {
case 0: l = (irData[p1].x < irData[p2].x) ? 0 : 1; break;
case 1: l = (irData[p1].y > irData[p2].y) ? 0 : 1; break;
case 2: l = (irData[p1].x > irData[p2].x) ? 0 : 1; break;
case 3: l = (irData[p1].y < irData[p2].y) ? 0 : 1; break;
}
leftPoint = l;
}
int r = 1-l;
/* Calculate space between point L,R */
double dx = irData[r].x - irData[l].x;
double dy = irData[r].y - irData[l].y;
/* http://en.wikipedia.org/wiki/Atan2#The_hypot_function */
double d = hypot (dx, dy);
/* Normalize distances */
dx /= d;
dy /= d;
/* RvdZ: What is happening over here? */
double cx = (irData[l].x + irData[r].x)/kWiiIRPixelsWidth - 1;
double cy = (irData[l].y + irData[r].y)/kWiiIRPixelsHeight - 1;
/* RvdZ: What is happening over here? */
ox = -dy*cy-dx*cx;