summaryrefslogtreecommitdiff
path: root/wibble.c
blob: f1cdc100b91ea43e41d607b3dc85a455a6766e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (C) 2011 by Daniel Friesel <derf@finalrewind.org>
 * License: WTFPL <http://sam.zoy.org/wtfpl>
 *   0. You just DO WHAT THE FUCK YOU WANT TO.
 */

/*
 * wibble: interactive wiimote blinkenlights with wobble.
 *
 * Usage: start wibble, put wiimote in discoverable mode, see blinkenlights
 *
 * Controls:
 *    D-Pad Up     toggle auto / manual mode
 *    D-Pad Left   previous animation
 *    D-Pad Right  next animation
 *    D-Pad Down   toggle rumble
 *
 *    -            decrease animation speed
 *    +            increase animation speed
 *
 *    Home         quit
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <bluetooth/bluetooth.h>
#include <cwiid.h>

#define MAX_BRIGHTNESS 10
#define X_MAXP 19
#define X_MAXS 20
#define X_MAXB 36
#define X_MAX 36

cwiid_mesg_callback_t cwiid_callback;

volatile char rumble = 0;
volatile char auto_rumble = 1;

volatile char auto_mode = 1;
volatile int  cur_mode = 0;

volatile int8_t cnt_max = 7;
volatile uint8_t f_led[4][X_MAX];
volatile uint8_t x_max = X_MAXP;

struct acc_cal wm_cal;

const uint8_t stevens_io[X_MAX] = {
	0, 0, 1, 1, 1, 3, 5, 7, 9, 10, 9, 7, 5, 3, 1, 1, 1, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

const uint8_t invb[X_MAX] = {
	0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

void set_led_fun(int new_mode)
{
	const int max_current = 5;
	int i;

	cur_mode = new_mode;
	if (cur_mode < 0)
		cur_mode = max_current;
	else if (cur_mode > max_current)
		cur_mode = 0;

	switch (cur_mode) {
	case 0:
		x_max = X_MAXP;
		for (i = 0; i < x_max; i++) {
			f_led[0][i] = floor( 5 * ( cos( (float) (i +  0) / 3 ) + 1 ) );
			f_led[1][i] = floor( 5 * ( cos( (float) (i +  3) / 3 ) + 1 ) );
			f_led[2][i] = floor( 5 * ( cos( (float) (i +  6) / 3 ) + 1 ) );
			f_led[3][i] = floor( 5 * ( cos( (float) (i +  9) / 3 ) + 1 ) );
		}
		break;
	case 1:
		x_max = X_MAXP;
		for (i = 0; i < x_max; i++) {
			f_led[0][i] = floor( 5 * ( cos( (float) (i +  9) / 3 ) + 1 ) );
			f_led[1][i] = floor( 5 * ( cos( (float) (i +  6) / 3 ) + 1 ) );
			f_led[2][i] = floor( 5 * ( cos( (float) (i +  3) / 3 ) + 1 ) );
			f_led[3][i] = floor( 5 * ( cos( (float) (i +  0) / 3 ) + 1 ) );
		}
		break;
	case 2:
		x_max = X_MAXP;
		for (i = 0; i < x_max; i++)
			f_led[0][i] = f_led[1][i] = f_led[2][i] = f_led[3][i]
				= stevens_io[i];
		break;
	case 3:
		x_max = X_MAXB;
		for (i = 0; i < x_max; i++) {
			f_led[0][i] = f_led[1][i] = f_led[2][i] = f_led[3][i] = 0;
			if ((i%2) && (i<6))
				f_led[0][i] = MAX_BRIGHTNESS;
			if ((i%2) && (((i>=6) && (i<12)) || (i>=30)))
				f_led[1][i] = MAX_BRIGHTNESS;
			if ((i%2) && (((i>=12) && (i<18)) || ((i>=24) && (i<30))))
				f_led[2][i] = MAX_BRIGHTNESS;
			if ((i%2) && (i>=18) && (i<24))
				f_led[3][i] = MAX_BRIGHTNESS;
		}
		break;
	case 4:
		x_max = X_MAXS;
		for (i = 0; i < x_max; i++) {
			f_led[0][i] = invb[(i + 15) % x_max];
			f_led[1][i] = invb[(i + 10) % x_max];
			f_led[2][i] = invb[(i +  5) % x_max];
			f_led[3][i] = invb[i];
		}
		break;
	case 5:
		x_max = X_MAXS;
		for (i = 0; i < x_max; i++)
			f_led[0][i] = f_led[1][i] = f_led[2][i] = f_led[3][i]
				= (i % 2) * 10;
		break;
	}
}

int main()
{
	cwiid_wiimote_t *wiimote = NULL;

	struct cwiid_state state;

	uint8_t ledstate = 0x0f;

	uint8_t cnt = 0;
	uint8_t led[4] = {0, 0, 0, 0};

	uint8_t step = 0;
	uint8_t x = 0;

	uint8_t i;

	uint8_t next_mode = 0;

	if ((wiimote = cwiid_open(BDADDR_ANY, 0)) == NULL) {
		fputs("Unable to connect\n", stderr);
		return EXIT_FAILURE;
	}
	fputs("connected\n", stdout);

	sleep(2);

	set_led_fun(0);

	if (cwiid_get_acc_cal(wiimote, CWIID_EXT_NONE, &wm_cal))
		fputs("unable to retrieve accelerometer calibration\n", stderr);

	if (!cwiid_get_state(wiimote, &state))
		printf("battery at %d%%\n",
			(int)(100.0 * state.battery / CWIID_BATTERY_MAX));

	if (cwiid_set_mesg_callback(wiimote, cwiid_callback))
		fputs("cannot set callback. buttons won't work.\n", stderr);
	
/*	cwiid_enable(wiimote, CWIID_FLAG_MOTIONPLUS);
*/
	if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC))
		fputs("cannot enable callback. buttons won't work.\n", stderr);

	if (cwiid_set_rpt_mode(wiimote,
			CWIID_RPT_BTN | CWIID_RPT_ACC | CWIID_RPT_STATUS | CWIID_RPT_EXT))
		fputs("cannot set report mode. buttons won't work.\n", stderr);

	while (1) {

		if (++cnt >= cnt_max) {
			cnt = 0;
			
			if (++x == x_max) {
				x = 0;

				if (!auto_mode && (++next_mode == 42)) {
					set_led_fun(cur_mode + 1);
					next_mode = 0;
				}
			}

			for (i = 0; i < 4; i++)
				led[i] = f_led[i][x];
		}

		step = cnt % MAX_BRIGHTNESS;

		if (step == 0)
			ledstate = 0x0f;

		for (i = 0; i < 4; i++)
			if (step == led[i])
				ledstate &= ~(1 << i);

		if (cwiid_set_led(wiimote, ledstate))
			fputs("Error setting LED state\n", stderr);
	}

	return EXIT_SUCCESS;
}

/* 97 .. 122 .. 150 */
/* mp: normal 8200 .. 8250? */

void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
	union cwiid_mesg mesg[], struct timespec *ts)
{
	static double ff_start, ff_diff;
	static double fp_start, fp_diff;
	double a_x, a_y, a_z, accel;
	struct cwiid_acc_mesg *am;

	for (int i = 0; i < mesg_count; i++) {
		if (mesg[i].type == CWIID_MESG_BTN) {

			if (mesg[i].btn_mesg.buttons & CWIID_BTN_LEFT)
				set_led_fun(cur_mode - 1);
			if (mesg[i].btn_mesg.buttons & CWIID_BTN_RIGHT)
				set_led_fun(cur_mode + 1);
			if (mesg[i].btn_mesg.buttons & CWIID_BTN_PLUS)
				cnt_max -= (cnt_max > 1 ? 1 : 0);
			if (mesg[i].btn_mesg.buttons & CWIID_BTN_MINUS)
				cnt_max += 1;
			if (mesg[i].btn_mesg.buttons & CWIID_BTN_UP) {
				auto_mode = !auto_mode;
				if (auto_mode)
					x_max = X_MAXP;
			}

			if (mesg[i].btn_mesg.buttons & CWIID_BTN_DOWN) {
				if (auto_mode)
					auto_rumble = !auto_rumble;
				else if (!rumble)
					cwiid_set_rumble(wiimote, (rumble = 1));
			}
			else if (rumble && !auto_mode)
				cwiid_set_rumble(wiimote, (rumble = 0));

			if (mesg[i].btn_mesg.buttons & CWIID_BTN_HOME) {
				exit(0);
			}

		}
/*		else if ((mesg[i].type == CWIID_MESG_MOTIONPLUS) && auto_mode ) {

			if ((mesg[i].motionplus_mesg.angle_rate[2] < 8000) && !fp_start)
				fp_start = ((uint64_t)ts->tv_sec * 1000000000) + ts->tv_nsec;
			else if ((mesg[i].motionplus_mesg.angle_rate[2] > 8200) && fp_start) {
				fp_diff = ((((uint64_t)ts->tv_sec * 1000000000)
					+ ts->tv_nsec
					- ff_start) / 1000000000);

				printf("mpdel_t %.3fs - Fell approx. %.2fm\n", ff_diff,
					(double)((9.81 * (double)(fp_diff) * (double)(fp_diff)) / (double)2));
				fp_start = 0;
			}
		}
*/		else if ((mesg[i].type == CWIID_MESG_ACC) && auto_mode) {

			am = &mesg[i].acc_mesg;

			a_x = ((double)am->acc[CWIID_X] - wm_cal.zero[CWIID_X]) /
				(wm_cal.one[CWIID_X] - wm_cal.zero[CWIID_X]);
			a_y = ((double)am->acc[CWIID_Y] - wm_cal.zero[CWIID_Y]) /
				(wm_cal.one[CWIID_Y] - wm_cal.zero[CWIID_Y]);
			a_z = ((double)am->acc[CWIID_Z] - wm_cal.zero[CWIID_Z]) /
				(wm_cal.one[CWIID_Z] - wm_cal.zero[CWIID_Z]);
			accel = sqrt(pow(a_x,2)+pow(a_y,2)+pow(a_z,2));

			if ((accel < 0.07) && !ff_start)
				ff_start = ((uint64_t)ts->tv_sec * 1000000000) + ts->tv_nsec;
			else if ((accel > 1.0) && ff_start) {
				ff_diff = ((((uint64_t)ts->tv_sec * 1000000000)
					+ ts->tv_nsec
					- ff_start) / 1000000000);

				printf("delta_t %.3fs - Fell approx. %.2fm\n", ff_diff,
					(double)((9.81 * (double)(ff_diff) * (double)(ff_diff)) / (double)2));
				ff_start = 0;
			}

			if (auto_rumble && (accel > 1.5)) {
				if (!rumble)
					cwiid_set_rumble(wiimote, (rumble = 1));
			}
			else {
				if (rumble)
					cwiid_set_rumble(wiimote, (rumble = 0));
			}

			if (mesg[i].acc_mesg.acc[CWIID_X] < 123) {

				if (cur_mode != 0)
					set_led_fun(0);

				cnt_max = (mesg[i].acc_mesg.acc[CWIID_X] - 95) / 2;
				if (cnt_max < 2)
					cnt_max = 2;
			}
			else {

				if (cur_mode != 1)
					set_led_fun(1);

				cnt_max = (150 - mesg[i].acc_mesg.acc[CWIID_X]) / 2;
				if (cnt_max < 2)
					cnt_max = 2;
			}
		}
	}
}