4 #include "definitions.h"
5 #include "wiiuse_internal.h"
8 #include "guitar_hero_3.h"
11 #include "lwp_wkspace.h"
13 void wiiuse_handshake(struct wiimote_t
*wm
,ubyte
*data
,uword len
)
16 struct accel_t
*accel
= &wm
->accel_calib
;
18 //printf("wiiuse_handshake(%d,%p,%d)\n",wm->handshake_state,data,len);
20 switch(wm
->handshake_state
) {
22 wm
->handshake_state
++;
24 wiiuse_set_leds(wm
,WIIMOTE_LED_NONE
,NULL
);
26 buf
= __lwp_wkspace_allocate(sizeof(ubyte
)*8);
27 wiiuse_read_data(wm
,buf
,WM_MEM_OFFSET_CALIBRATION
,7,wiiuse_handshake
);
30 wm
->handshake_state
++;
32 accel
->cal_zero
.x
= ((data
[0]<<2)|((data
[3]>>4)&3));
33 accel
->cal_zero
.y
= ((data
[1]<<2)|((data
[3]>>2)&3));
34 accel
->cal_zero
.z
= ((data
[2]<<2)|(data
[3]&3));
36 accel
->cal_g
.x
= (((data
[4]<<2)|((data
[7]>>4)&3)) - accel
->cal_zero
.x
);
37 accel
->cal_g
.y
= (((data
[5]<<2)|((data
[7]>>2)&3)) - accel
->cal_zero
.y
);
38 accel
->cal_g
.z
= (((data
[6]<<2)|(data
[7]&3)) - accel
->cal_zero
.z
);
39 __lwp_wkspace_free(data
);
41 WIIMOTE_DISABLE_STATE(wm
, WIIMOTE_STATE_HANDSHAKE
);
42 WIIMOTE_ENABLE_STATE(wm
, WIIMOTE_STATE_HANDSHAKE_COMPLETE
);
44 wm
->event
= WIIUSE_CONNECT
;
45 wiiuse_status(wm
,NULL
);
53 void wiiuse_handshake_expansion_start(struct wiimote_t
*wm
)
55 if(WIIMOTE_IS_SET(wm
,WIIMOTE_STATE_EXP
) || WIIMOTE_IS_SET(wm
,WIIMOTE_STATE_EXP_FAILED
) || WIIMOTE_IS_SET(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
))
58 wm
->expansion_state
= 0;
59 WIIMOTE_ENABLE_STATE(wm
, WIIMOTE_STATE_EXP_HANDSHAKE
);
60 wiiuse_handshake_expansion(wm
, NULL
, 0);
63 void wiiuse_handshake_expansion(struct wiimote_t
*wm
,ubyte
*data
,uword len
)
69 switch(wm
->expansion_state
) {
70 /* These two initialization writes disable the encryption */
72 wm
->expansion_state
= 1;
74 wiiuse_write_data(wm
,WM_EXP_MEM_ENABLE1
,&val
,1,wiiuse_handshake_expansion
);
77 wm
->expansion_state
= 2;
79 wiiuse_write_data(wm
,WM_EXP_MEM_ENABLE2
,&val
,1,wiiuse_handshake_expansion
);
82 wm
->expansion_state
= 3;
83 buf
= __lwp_wkspace_allocate(sizeof(ubyte
)*EXP_HANDSHAKE_LEN
);
84 wiiuse_read_data(wm
,buf
,WM_EXP_MEM_CALIBR
,EXP_HANDSHAKE_LEN
,wiiuse_handshake_expansion
);
87 if(!data
|| !len
) return;
88 id
= BIG_ENDIAN_LONG(*(int*)(&data
[220]));
90 case EXP_ID_CODE_NUNCHUK
:
91 if(!nunchuk_handshake(wm
,&wm
->exp
.nunchuk
,data
,len
)) return;
93 case EXP_ID_CODE_CLASSIC_CONTROLLER
:
94 if(!classic_ctrl_handshake(wm
,&wm
->exp
.classic
,data
,len
)) return;
96 case EXP_ID_CODE_GUITAR
:
97 if(!guitar_hero_3_handshake(wm
,&wm
->exp
.gh3
,data
,len
)) return;
99 case EXP_ID_CODE_WIIBOARD
:
100 if(!wii_board_handshake(wm
,&wm
->exp
.wb
,data
,len
)) return;
103 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
);
104 WIIMOTE_ENABLE_STATE(wm
,WIIMOTE_STATE_EXP_FAILED
);
105 __lwp_wkspace_free(data
);
106 wiiuse_status(wm
,NULL
);
109 __lwp_wkspace_free(data
);
111 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_EXP_HANDSHAKE
);
112 WIIMOTE_ENABLE_STATE(wm
,WIIMOTE_STATE_EXP
);
113 wiiuse_set_ir_mode(wm
);
114 wiiuse_status(wm
,NULL
);
119 void wiiuse_disable_expansion(struct wiimote_t
*wm
)
121 if(!WIIMOTE_IS_SET(wm
, WIIMOTE_STATE_EXP
)) return;
123 /* tell the associated module the expansion was removed */
124 switch(wm
->exp
.type
) {
126 nunchuk_disconnected(&wm
->exp
.nunchuk
);
127 wm
->event
= WIIUSE_NUNCHUK_REMOVED
;
130 classic_ctrl_disconnected(&wm
->exp
.classic
);
131 wm
->event
= WIIUSE_CLASSIC_CTRL_REMOVED
;
133 case EXP_GUITAR_HERO_3
:
134 guitar_hero_3_disconnected(&wm
->exp
.gh3
);
135 wm
->event
= WIIUSE_GUITAR_HERO_3_CTRL_REMOVED
;
138 wii_board_disconnected(&wm
->exp
.wb
);
139 wm
->event
= WIIUSE_WII_BOARD_REMOVED
;
145 WIIMOTE_DISABLE_STATE(wm
, WIIMOTE_STATE_EXP
);
146 wm
->exp
.type
= EXP_NONE
;
148 wiiuse_set_ir_mode(wm
);
149 wiiuse_status(wm
,NULL
);