revert more changes
[libogc.git] / wiiuse / io.c
blob10ed4b4793cf9463c40a3e880df510a84123ddc4
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "definitions.h"
5 #include "wiiuse_internal.h"
6 #include "nunchuk.h"
7 #include "classic.h"
8 #include "guitar_hero_3.h"
9 #include "wiiboard.h"
10 #include "motion_plus.h"
11 #include "io.h"
12 #include "lwp_wkspace.h"
14 void wiiuse_handshake(struct wiimote_t *wm,ubyte *data,uword len)
16 ubyte *buf = NULL;
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) {
22 case 0:
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);
29 break;
30 case 1:
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);
47 break;
48 default:
49 break;
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))
57 return;
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)
66 int id;
67 ubyte val;
68 ubyte *buf = NULL;
70 switch(wm->expansion_state) {
71 /* These two initialization writes disable the encryption */
72 case 0:
73 wm->expansion_state = 1;
74 val = 0x55;
75 wiiuse_write_data(wm,WM_EXP_MEM_ENABLE1,&val,1,wiiuse_handshake_expansion);
76 break;
77 case 1:
78 wm->expansion_state = 2;
79 val = 0x00;
80 wiiuse_write_data(wm,WM_EXP_MEM_ENABLE2,&val,1,wiiuse_handshake_expansion);
81 break;
82 case 2:
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);
86 break;
87 case 3:
88 if(!data || !len) return;
89 id = BIG_ENDIAN_LONG(*(int*)(&data[220]));
91 switch(id) {
92 case EXP_ID_CODE_NUNCHUK:
93 if(!nunchuk_handshake(wm,&wm->exp.nunchuk,data,len)) return;
94 break;
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;
105 break;
106 case EXP_ID_CODE_GUITAR:
107 if(!guitar_hero_3_handshake(wm,&wm->exp.gh3,data,len)) return;
108 break;
109 case EXP_ID_CODE_WIIBOARD:
110 if(!wii_board_handshake(wm,&wm->exp.wb,data,len)) return;
111 break;
112 default:
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);
117 return;
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);
125 break;
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) {
135 case EXP_NUNCHUK:
136 nunchuk_disconnected(&wm->exp.nunchuk);
137 wm->event = WIIUSE_NUNCHUK_REMOVED;
138 break;
139 case EXP_CLASSIC:
140 classic_ctrl_disconnected(&wm->exp.classic);
141 wm->event = WIIUSE_CLASSIC_CTRL_REMOVED;
142 break;
143 case EXP_GUITAR_HERO_3:
144 guitar_hero_3_disconnected(&wm->exp.gh3);
145 wm->event = WIIUSE_GUITAR_HERO_3_CTRL_REMOVED;
146 break;
147 case EXP_WII_BOARD:
148 wii_board_disconnected(&wm->exp.wb);
149 wm->event = WIIUSE_WII_BOARD_REMOVED;
150 break;
151 case EXP_MOTION_PLUS:
152 motion_plus_disconnected(&wm->exp.mp);
153 wm->event = WIIUSE_MOTION_PLUS_REMOVED;
154 break;
156 default:
157 break;
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);