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 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC3
:
101 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC4
:
102 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC5
:
103 if(!classic_ctrl_handshake(wm
,&wm
->exp
.classic
,data
,len
)) return;
105 case EXP_ID_CODE_GUITAR
:
106 if(!guitar_hero_3_handshake(wm
,&wm
->exp
.gh3
,data
,len
)) return;
108 case EXP_ID_CODE_WIIBOARD
:
109 if(!wii_board_handshake(wm
,&wm
->exp
.wb
,data
,len
)) return;
112 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
);
113 WIIMOTE_ENABLE_STATE(wm
,WIIMOTE_STATE_EXP_FAILED
);
114 __lwp_wkspace_free(data
);
115 wiiuse_status(wm
,NULL
);
118 __lwp_wkspace_free(data
);
120 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
);
121 WIIMOTE_ENABLE_STATE(wm
,WIIMOTE_STATE_EXP
);
122 wiiuse_set_ir_mode(wm
);
123 wiiuse_status(wm
,NULL
);
128 void wiiuse_disable_expansion(struct wiimote_t
*wm
)
130 if(!WIIMOTE_IS_SET(wm
, WIIMOTE_STATE_EXP
)) return;
132 /* tell the associated module the expansion was removed */
133 switch(wm
->exp
.type
) {
135 nunchuk_disconnected(&wm
->exp
.nunchuk
);
136 wm
->event
= WIIUSE_NUNCHUK_REMOVED
;
139 classic_ctrl_disconnected(&wm
->exp
.classic
);
140 wm
->event
= WIIUSE_CLASSIC_CTRL_REMOVED
;
142 case EXP_GUITAR_HERO_3
:
143 guitar_hero_3_disconnected(&wm
->exp
.gh3
);
144 wm
->event
= WIIUSE_GUITAR_HERO_3_CTRL_REMOVED
;
147 wii_board_disconnected(&wm
->exp
.wb
);
148 wm
->event
= WIIUSE_WII_BOARD_REMOVED
;
150 case EXP_MOTION_PLUS
:
151 motion_plus_disconnected(&wm
->exp
.mp
);
152 wm
->event
= WIIUSE_MOTION_PLUS_REMOVED
;
159 WIIMOTE_DISABLE_STATE(wm
, WIIMOTE_STATE_EXP
);
160 wm
->exp
.type
= EXP_NONE
;
162 wiiuse_set_ir_mode(wm
);
163 wiiuse_status(wm
,NULL
);