4 #include "definitions.h"
5 #include "wiiuse_internal.h"
8 #include "guitar_hero_3.h"
10 #include "motion_plus.h"
12 #include "lwp_wkspace.h"
14 void wiiuse_handshake(struct wiimote_t
*wm
,ubyte
*data
,uword len
)
17 struct accel_t
*accel
= &wm
->accel_calib
;
19 //printf("wiiuse_handshake(%d,%p,%d)\n",wm->handshake_state,data,len);
21 switch(wm
->handshake_state
) {
23 wm
->handshake_state
++;
25 wiiuse_set_leds(wm
,WIIMOTE_LED_NONE
,NULL
);
27 buf
= __lwp_wkspace_allocate(sizeof(ubyte
)*8);
28 wiiuse_read_data(wm
,buf
,WM_MEM_OFFSET_CALIBRATION
,7,wiiuse_handshake
);
31 wm
->handshake_state
++;
33 accel
->cal_zero
.x
= ((data
[0]<<2)|((data
[3]>>4)&3));
34 accel
->cal_zero
.y
= ((data
[1]<<2)|((data
[3]>>2)&3));
35 accel
->cal_zero
.z
= ((data
[2]<<2)|(data
[3]&3));
37 accel
->cal_g
.x
= (((data
[4]<<2)|((data
[7]>>4)&3)) - accel
->cal_zero
.x
);
38 accel
->cal_g
.y
= (((data
[5]<<2)|((data
[7]>>2)&3)) - accel
->cal_zero
.y
);
39 accel
->cal_g
.z
= (((data
[6]<<2)|(data
[7]&3)) - accel
->cal_zero
.z
);
40 __lwp_wkspace_free(data
);
42 WIIMOTE_DISABLE_STATE(wm
, WIIMOTE_STATE_HANDSHAKE
);
43 WIIMOTE_ENABLE_STATE(wm
, WIIMOTE_STATE_HANDSHAKE_COMPLETE
);
45 wm
->event
= WIIUSE_CONNECT
;
46 wiiuse_status(wm
,NULL
);
54 void wiiuse_handshake_expansion_start(struct wiimote_t
*wm
)
56 if(WIIMOTE_IS_SET(wm
,WIIMOTE_STATE_EXP
) || WIIMOTE_IS_SET(wm
,WIIMOTE_STATE_EXP_FAILED
) || WIIMOTE_IS_SET(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
))
59 wm
->expansion_state
= 0;
60 WIIMOTE_ENABLE_STATE(wm
, WIIMOTE_STATE_EXP_HANDSHAKE
);
61 wiiuse_handshake_expansion(wm
, NULL
, 0);
64 void wiiuse_handshake_expansion(struct wiimote_t
*wm
,ubyte
*data
,uword len
)
70 switch(wm
->expansion_state
) {
71 /* These two initialization writes disable the encryption */
73 wm
->expansion_state
= 1;
75 wiiuse_write_data(wm
,WM_EXP_MEM_ENABLE1
,&val
,1,wiiuse_handshake_expansion
);
78 wm
->expansion_state
= 2;
80 wiiuse_write_data(wm
,WM_EXP_MEM_ENABLE2
,&val
,1,wiiuse_handshake_expansion
);
83 wm
->expansion_state
= 3;
84 buf
= __lwp_wkspace_allocate(sizeof(ubyte
)*EXP_HANDSHAKE_LEN
);
85 wiiuse_read_data(wm
,buf
,WM_EXP_MEM_CALIBR
,EXP_HANDSHAKE_LEN
,wiiuse_handshake_expansion
);
88 if(!data
|| !len
) return;
89 id
= BIG_ENDIAN_LONG(*(int*)(&data
[220]));
92 case EXP_ID_CODE_NUNCHUK
:
93 if(!nunchuk_handshake(wm
,&wm
->exp
.nunchuk
,data
,len
)) return;
95 case EXP_ID_CODE_CLASSIC_CONTROLLER
:
96 case EXP_ID_CODE_CLASSIC_CONTROLLER_NYKOWING
:
97 case EXP_ID_CODE_CLASSIC_CONTROLLER_NYKOWING2
:
98 case EXP_ID_CODE_CLASSIC_CONTROLLER_NYKOWING3
:
99 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC
:
100 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC2
:
101 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC3
:
102 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC4
:
103 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC5
:
104 if(!classic_ctrl_handshake(wm
,&wm
->exp
.classic
,data
,len
)) return;
106 case EXP_ID_CODE_GUITAR
:
107 if(!guitar_hero_3_handshake(wm
,&wm
->exp
.gh3
,data
,len
)) return;
109 case EXP_ID_CODE_WIIBOARD
:
110 if(!wii_board_handshake(wm
,&wm
->exp
.wb
,data
,len
)) return;
113 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
);
114 WIIMOTE_ENABLE_STATE(wm
,WIIMOTE_STATE_EXP_FAILED
);
115 __lwp_wkspace_free(data
);
116 wiiuse_status(wm
,NULL
);
119 __lwp_wkspace_free(data
);
121 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
);
122 WIIMOTE_ENABLE_STATE(wm
,WIIMOTE_STATE_EXP
);
123 wiiuse_set_ir_mode(wm
);
124 wiiuse_status(wm
,NULL
);
129 void wiiuse_disable_expansion(struct wiimote_t
*wm
)
131 if(!WIIMOTE_IS_SET(wm
, WIIMOTE_STATE_EXP
)) return;
133 /* tell the associated module the expansion was removed */
134 switch(wm
->exp
.type
) {
136 nunchuk_disconnected(&wm
->exp
.nunchuk
);
137 wm
->event
= WIIUSE_NUNCHUK_REMOVED
;
140 classic_ctrl_disconnected(&wm
->exp
.classic
);
141 wm
->event
= WIIUSE_CLASSIC_CTRL_REMOVED
;
143 case EXP_GUITAR_HERO_3
:
144 guitar_hero_3_disconnected(&wm
->exp
.gh3
);
145 wm
->event
= WIIUSE_GUITAR_HERO_3_CTRL_REMOVED
;
148 wii_board_disconnected(&wm
->exp
.wb
);
149 wm
->event
= WIIUSE_WII_BOARD_REMOVED
;
151 case EXP_MOTION_PLUS
:
152 motion_plus_disconnected(&wm
->exp
.mp
);
153 wm
->event
= WIIUSE_MOTION_PLUS_REMOVED
;
160 WIIMOTE_DISABLE_STATE(wm
, WIIMOTE_STATE_EXP
);
161 wm
->exp
.type
= EXP_NONE
;
163 wiiuse_set_ir_mode(wm
);
164 wiiuse_status(wm
,NULL
);