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_GENERIC
:
99 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC2
:
100 if(!classic_ctrl_handshake(wm
,&wm
->exp
.classic
,data
,len
)) return;
102 case EXP_ID_CODE_GUITAR
:
103 if(!guitar_hero_3_handshake(wm
,&wm
->exp
.gh3
,data
,len
)) return;
105 case EXP_ID_CODE_WIIBOARD
:
106 if(!wii_board_handshake(wm
,&wm
->exp
.wb
,data
,len
)) return;
109 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
);
110 WIIMOTE_ENABLE_STATE(wm
,WIIMOTE_STATE_EXP_FAILED
);
111 __lwp_wkspace_free(data
);
112 wiiuse_status(wm
,NULL
);
115 __lwp_wkspace_free(data
);
117 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
);
118 WIIMOTE_ENABLE_STATE(wm
,WIIMOTE_STATE_EXP
);
119 wiiuse_set_ir_mode(wm
);
120 wiiuse_status(wm
,NULL
);
125 void wiiuse_disable_expansion(struct wiimote_t
*wm
)
127 if(!WIIMOTE_IS_SET(wm
, WIIMOTE_STATE_EXP
)) return;
129 /* tell the associated module the expansion was removed */
130 switch(wm
->exp
.type
) {
132 nunchuk_disconnected(&wm
->exp
.nunchuk
);
133 wm
->event
= WIIUSE_NUNCHUK_REMOVED
;
136 classic_ctrl_disconnected(&wm
->exp
.classic
);
137 wm
->event
= WIIUSE_CLASSIC_CTRL_REMOVED
;
139 case EXP_GUITAR_HERO_3
:
140 guitar_hero_3_disconnected(&wm
->exp
.gh3
);
141 wm
->event
= WIIUSE_GUITAR_HERO_3_CTRL_REMOVED
;
144 wii_board_disconnected(&wm
->exp
.wb
);
145 wm
->event
= WIIUSE_WII_BOARD_REMOVED
;
147 case EXP_MOTION_PLUS
:
148 motion_plus_disconnected(&wm
->exp
.mp
);
149 wm
->event
= WIIUSE_MOTION_PLUS_REMOVED
;
156 WIIMOTE_DISABLE_STATE(wm
, WIIMOTE_STATE_EXP
);
157 wm
->exp
.type
= EXP_NONE
;
159 wiiuse_set_ir_mode(wm
);
160 wiiuse_status(wm
,NULL
);