load a keymap in KEYBOARD_Init() based on the contents of "/wiikbd.map"
[libogc.git] / wiiuse / io.c
blob45d1c5120737bc71b46a51d9c06697c393ba04f5
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 "io.h"
11 #include "lwp_wkspace.h"
13 void wiiuse_handshake(struct wiimote_t *wm,ubyte *data,uword len)
15 ubyte *buf = NULL;
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) {
21 case 0:
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);
28 break;
29 case 1:
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);
46 break;
47 default:
48 break;
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))
56 return;
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)
65 int id;
66 ubyte val;
67 ubyte *buf = NULL;
69 switch(wm->expansion_state) {
70 /* These two initialization writes disable the encryption */
71 case 0:
72 wm->expansion_state = 1;
73 val = 0x55;
74 wiiuse_write_data(wm,WM_EXP_MEM_ENABLE1,&val,1,wiiuse_handshake_expansion);
75 break;
76 case 1:
77 wm->expansion_state = 2;
78 val = 0x00;
79 wiiuse_write_data(wm,WM_EXP_MEM_ENABLE2,&val,1,wiiuse_handshake_expansion);
80 break;
81 case 2:
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);
85 break;
86 case 3:
87 if(!data || !len) return;
88 id = BIG_ENDIAN_LONG(*(int*)(&data[220]));
89 switch(id) {
90 case EXP_ID_CODE_NUNCHUK:
91 if(!nunchuk_handshake(wm,&wm->exp.nunchuk,data,len)) return;
92 break;
93 case EXP_ID_CODE_CLASSIC_CONTROLLER:
94 if(!classic_ctrl_handshake(wm,&wm->exp.classic,data,len)) return;
95 break;
96 case EXP_ID_CODE_GUITAR:
97 if(!guitar_hero_3_handshake(wm,&wm->exp.gh3,data,len)) return;
98 break;
99 case EXP_ID_CODE_WIIBOARD:
100 if(!wii_board_handshake(wm,&wm->exp.wb,data,len)) return;
101 break;
102 default:
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);
107 return;
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);
115 break;
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) {
125 case EXP_NUNCHUK:
126 nunchuk_disconnected(&wm->exp.nunchuk);
127 wm->event = WIIUSE_NUNCHUK_REMOVED;
128 break;
129 case EXP_CLASSIC:
130 classic_ctrl_disconnected(&wm->exp.classic);
131 wm->event = WIIUSE_CLASSIC_CTRL_REMOVED;
132 break;
133 case EXP_GUITAR_HERO_3:
134 guitar_hero_3_disconnected(&wm->exp.gh3);
135 wm->event = WIIUSE_GUITAR_HERO_3_CTRL_REMOVED;
136 break;
137 case EXP_WII_BOARD:
138 wii_board_disconnected(&wm->exp.wb);
139 wm->event = WIIUSE_WII_BOARD_REMOVED;
140 break;
141 default:
142 break;
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);