1 | /* |
---|
2 | * nunchuck.c |
---|
3 | * |
---|
4 | * This file is part of WiiC, written by: |
---|
5 | * Gabriele Randelli |
---|
6 | * Email: randelli@dis.uniroma1.it |
---|
7 | * |
---|
8 | * Copyright 2010 |
---|
9 | * |
---|
10 | * This file is based on Wiiuse, written By: |
---|
11 | * Michael Laforest < para > |
---|
12 | * Email: < thepara (--AT--) g m a i l [--DOT--] com > |
---|
13 | * |
---|
14 | * Copyright 2006-2007 |
---|
15 | * |
---|
16 | * This program is free software; you can redistribute it and/or modify |
---|
17 | * it under the terms of the GNU General Public License as published by |
---|
18 | * the Free Software Foundation; either version 3 of the License, or |
---|
19 | * (at your option) any later version. |
---|
20 | * |
---|
21 | * This program is distributed in the hope that it will be useful, |
---|
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
24 | * GNU General Public License for more details. |
---|
25 | * |
---|
26 | * You should have received a copy of the GNU General Public License |
---|
27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
28 | * |
---|
29 | * $Header$ |
---|
30 | */ |
---|
31 | |
---|
32 | /** |
---|
33 | * @file |
---|
34 | * @brief Nunchuk expansion device. |
---|
35 | */ |
---|
36 | |
---|
37 | #include <stdio.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <math.h> |
---|
40 | |
---|
41 | #include "definitions.h" |
---|
42 | #include "wiic_internal.h" |
---|
43 | #include "dynamics.h" |
---|
44 | #include "events.h" |
---|
45 | #include "nunchuk.h" |
---|
46 | |
---|
47 | static void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now); |
---|
48 | |
---|
49 | /** |
---|
50 | * @brief Handle the handshake data from the nunchuk. |
---|
51 | * |
---|
52 | * @param nc A pointer to a nunchuk_t structure. |
---|
53 | * @param data The data read in from the device. |
---|
54 | * @param len The length of the data block, in bytes. |
---|
55 | * |
---|
56 | * @return Returns 1 if handshake was successful, 0 if not. |
---|
57 | */ |
---|
58 | int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, unsigned short len) { |
---|
59 | int i; |
---|
60 | int offset = 0; |
---|
61 | |
---|
62 | nc->btns = 0; |
---|
63 | nc->btns_held = 0; |
---|
64 | nc->btns_released = 0; |
---|
65 | |
---|
66 | /* set the smoothing to the same as the wiimote */ |
---|
67 | nc->flags = &wm->flags; |
---|
68 | nc->accel_calib.st_alpha = wm->accel_calib.st_alpha; |
---|
69 | |
---|
70 | /* decrypt data */ |
---|
71 | for (i = 0; i < len; ++i) |
---|
72 | data[i] = (data[i] ^ 0x17) + 0x17; |
---|
73 | |
---|
74 | if (data[offset] == 0xFF) { |
---|
75 | /* |
---|
76 | * Sometimes the data returned here is not correct. |
---|
77 | * This might happen because the wiimote is lagging |
---|
78 | * behind our initialization sequence. |
---|
79 | * To fix this just request the handshake again. |
---|
80 | * |
---|
81 | * Other times it's just the first 16 bytes are 0xFF, |
---|
82 | * but since the next 16 bytes are the same, just use |
---|
83 | * those. |
---|
84 | */ |
---|
85 | if (data[offset + 16] == 0xFF) { |
---|
86 | /* get the calibration data */ |
---|
87 | byte* handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte)); |
---|
88 | |
---|
89 | WIIC_DEBUG("Nunchuk handshake appears invalid, trying again."); |
---|
90 | wiic_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); |
---|
91 | |
---|
92 | return 0; |
---|
93 | } else |
---|
94 | offset += 16; |
---|
95 | } |
---|
96 | |
---|
97 | nc->accel_calib.cal_zero.x = data[offset + 0]; |
---|
98 | nc->accel_calib.cal_zero.y = data[offset + 1]; |
---|
99 | nc->accel_calib.cal_zero.z = data[offset + 2]; |
---|
100 | nc->accel_calib.cal_g.x = data[offset + 4]; |
---|
101 | nc->accel_calib.cal_g.y = data[offset + 5]; |
---|
102 | nc->accel_calib.cal_g.z = data[offset + 6]; |
---|
103 | nc->js.max.x = data[offset + 8]; |
---|
104 | nc->js.min.x = data[offset + 9]; |
---|
105 | nc->js.center.x = data[offset + 10]; |
---|
106 | nc->js.max.y = data[offset + 11]; |
---|
107 | nc->js.min.y = data[offset + 12]; |
---|
108 | nc->js.center.y = data[offset + 13]; |
---|
109 | |
---|
110 | /* default the thresholds to the same as the wiimote */ |
---|
111 | nc->orient_threshold = wm->orient_threshold; |
---|
112 | nc->accel_threshold = wm->accel_threshold; |
---|
113 | |
---|
114 | /* handshake done */ |
---|
115 | wm->exp.type = EXP_NUNCHUK; |
---|
116 | |
---|
117 | return 1; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | * @brief The nunchuk disconnected. |
---|
123 | * |
---|
124 | * @param nc A pointer to a nunchuk_t structure. |
---|
125 | */ |
---|
126 | void nunchuk_disconnected(struct nunchuk_t* nc) { |
---|
127 | memset(nc, 0, sizeof(struct nunchuk_t)); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | * @brief Handle nunchuk event. |
---|
134 | * |
---|
135 | * @param nc A pointer to a nunchuk_t structure. |
---|
136 | * @param msg The message specified in the event packet. |
---|
137 | */ |
---|
138 | void nunchuk_event(struct nunchuk_t* nc, byte* msg) { |
---|
139 | int i; |
---|
140 | |
---|
141 | /* decrypt data */ |
---|
142 | for (i = 0; i < 6; ++i) |
---|
143 | msg[i] = (msg[i] ^ 0x17) + 0x17; |
---|
144 | |
---|
145 | /* get button states */ |
---|
146 | nunchuk_pressed_buttons(nc, msg[5]); |
---|
147 | |
---|
148 | /* calculate joystick state */ |
---|
149 | calc_joystick_state(&nc->js, msg[0], msg[1]); |
---|
150 | |
---|
151 | /* calculate orientation */ |
---|
152 | nc->accel.x = msg[2]; |
---|
153 | nc->accel.y = msg[3]; |
---|
154 | nc->accel.z = msg[4]; |
---|
155 | |
---|
156 | calculate_gforce(&nc->accel_calib, &nc->accel, &nc->gforce, NUNCHUK_IS_FLAG_SET(nc, WIIC_SMOOTHING)); |
---|
157 | calculate_orientation(&nc->gforce.vec, &nc->orient.angle); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | /** |
---|
162 | * @brief Find what buttons are pressed. |
---|
163 | * |
---|
164 | * @param nc Pointer to a nunchuk_t structure. |
---|
165 | * @param msg The message byte specified in the event packet. |
---|
166 | */ |
---|
167 | static void nunchuk_pressed_buttons(struct nunchuk_t* nc, byte now) { |
---|
168 | /* message is inverted (0 is active, 1 is inactive) */ |
---|
169 | now = ~now & NUNCHUK_BUTTON_ALL; |
---|
170 | |
---|
171 | /* pressed now & were pressed, then held */ |
---|
172 | nc->btns_held = (now & nc->btns); |
---|
173 | |
---|
174 | /* were pressed or were held & not pressed now, then released */ |
---|
175 | nc->btns_released = ((nc->btns | nc->btns_held) & ~now); |
---|
176 | |
---|
177 | /* buttons pressed now */ |
---|
178 | nc->btns = now; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | /** |
---|
183 | * @brief Set the orientation event threshold for the nunchuk. |
---|
184 | * |
---|
185 | * @param wm Pointer to a wiimote_t structure with a nunchuk attached. |
---|
186 | * @param threshold The decimal place that should be considered a significant change. |
---|
187 | * |
---|
188 | * See wiic_set_orient_threshold() for details. |
---|
189 | */ |
---|
190 | void wiic_set_nunchuk_orient_threshold(struct wiimote_t* wm, float threshold) { |
---|
191 | if (!wm) return; |
---|
192 | |
---|
193 | wm->exp.nunchuk.orient_threshold = threshold; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | /** |
---|
198 | * @brief Set the accelerometer event threshold for the nunchuk. |
---|
199 | * |
---|
200 | * @param wm Pointer to a wiimote_t structure with a nunchuk attached. |
---|
201 | * @param threshold The decimal place that should be considered a significant change. |
---|
202 | * |
---|
203 | * See wiic_set_orient_threshold() for details. |
---|
204 | */ |
---|
205 | void wiic_set_nunchuk_accel_threshold(struct wiimote_t* wm, int threshold) { |
---|
206 | if (!wm) return; |
---|
207 | |
---|
208 | wm->exp.nunchuk.accel_threshold = threshold; |
---|
209 | } |
---|