2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
16 #include <SDL_thread.h>
22 /*---------------------------------------------------------------------------*/
26 #include <libcwiimote/wiimote.h>
27 #include <libcwiimote/wiimote_api.h>
30 * This data structure tracks button changes, counting transitions so that
31 * none are missed if the event handling thread falls significantly behind
32 * the device IO thread.
47 static void set_button(struct button_state *B, int s)
49 if ((B->curr == 0) != (s == 0))
64 static int get_button(struct button_state *B)
68 if (B->last == 1 && B->upc > 0)
74 else if (B->last == 0 && B->dnc > 0)
84 /*---------------------------------------------------------------------------*/
91 struct button_state A;
92 struct button_state B;
93 struct button_state plus;
94 struct button_state minus;
95 struct button_state home;
96 struct button_state L;
97 struct button_state R;
98 struct button_state U;
99 struct button_state D;
102 static struct tilt_state state;
103 static SDL_mutex *mutex = NULL;
104 static SDL_Thread *thread = NULL;
108 static int tilt_func(void *data)
110 wiimote_t wiimote = WIIMOTE_INIT;
111 char address[MAXSTR];
113 config_get_s(CONFIG_WIIMOTE_ADDR, address, MAXSTR);
115 if (strlen(address) > 0)
117 if (wiimote_connect(&wiimote, address) < 0)
118 fprintf(stderr, "%s\n", wiimote_get_error());
123 wiimote.mode.bits = WIIMOTE_MODE_ACC;
127 state.status = running;
130 while (mutex && running && wiimote_is_open(&wiimote))
132 if (wiimote_update(&wiimote) < 0)
137 running = state.status;
139 set_button(&state.A, wiimote.keys.a);
140 set_button(&state.B, wiimote.keys.b);
141 set_button(&state.plus, wiimote.keys.plus);
142 set_button(&state.minus, wiimote.keys.minus);
143 set_button(&state.home, wiimote.keys.home);
144 set_button(&state.L, wiimote.keys.left);
145 set_button(&state.R, wiimote.keys.right);
146 set_button(&state.U, wiimote.keys.up);
147 set_button(&state.D, wiimote.keys.down);
149 if (isnormal(wiimote.tilt.y))
151 state.x = (state.x * (FILTER - 1) +
152 wiimote.tilt.y) / FILTER;
154 if (isnormal(wiimote.tilt.x))
156 state.z = (state.z * (FILTER - 1) +
157 wiimote.tilt.x) / FILTER;
163 wiimote_disconnect(&wiimote);
171 memset(&state, 0, sizeof (struct tilt_state));
173 mutex = SDL_CreateMutex();
174 thread = SDL_CreateThread(tilt_func, NULL);
183 /* Get/set the status of the tilt sensor thread. */
190 /* Kill the thread and destroy the mutex. */
192 SDL_KillThread(thread);
193 SDL_DestroyMutex(mutex);
200 int tilt_get_button(int *b, int *s)
208 if ((ch = get_button(&state.A)))
210 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_A);
211 *s = (ch == BUTTON_DN);
213 else if ((ch = get_button(&state.B)))
215 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_B);
216 *s = (ch == BUTTON_DN);
218 else if ((ch = get_button(&state.plus)))
220 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_R);
221 *s = (ch == BUTTON_DN);
223 else if ((ch = get_button(&state.minus)))
225 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_L);
226 *s = (ch == BUTTON_DN);
228 else if ((ch = get_button(&state.home)))
230 *b = config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT);
231 *s = (ch == BUTTON_DN);
233 else if ((ch = get_button(&state.L)))
235 *b = config_get_d(CONFIG_JOYSTICK_DPAD_L);
236 *s = (ch == BUTTON_DN);
238 else if ((ch = get_button(&state.R)))
240 *b = config_get_d(CONFIG_JOYSTICK_DPAD_R);
241 *s = (ch == BUTTON_DN);
243 else if ((ch = get_button(&state.U)))
245 *b = config_get_d(CONFIG_JOYSTICK_DPAD_U);
246 *s = (ch == BUTTON_DN);
248 else if ((ch = get_button(&state.D)))
250 *b = config_get_d(CONFIG_JOYSTICK_DPAD_D);
251 *s = (ch == BUTTON_DN);
259 float tilt_get_x(void)
273 float tilt_get_z(void)
301 /*---------------------------------------------------------------------------*/
317 int tilt_get_button(int *b, int *s)
322 void tilt_get_direct(int *x, int *y)
328 float tilt_get_x(void)
333 float tilt_get_z(void)
339 /*---------------------------------------------------------------------------*/