File: nunchuk_kb.c

package info (click to toggle)
cwiid 0.6.00%2Bsvn201-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,372 kB
  • sloc: ansic: 7,572; makefile: 281; python: 255; lex: 176; yacc: 73; awk: 16
file content (116 lines) | stat: -rw-r--r-- 3,209 bytes parent folder | download | duplicates (8)
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
/* Copyright (C) 2007 L. Donnie Smith <cwiid@abstrakraft.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <math.h>

#include "wmplugin.h"

/* Button flags */
#define STICK_KEY_UP		0x0001
#define STICK_KEY_DOWN		0x0002
#define STICK_KEY_RIGHT		0x0004
#define STICK_KEY_LEFT		0x0008
#define STICK_MID_VAL		128
#define STICK_NEUTRAL		20

static unsigned char info_init = 0;
static struct wmplugin_info info;
static struct wmplugin_data data;

static cwiid_wiimote_t *wiimote;

static struct acc_cal acc_cal;
static int plugin_id;

wmplugin_info_t wmplugin_info;
wmplugin_init_t wmplugin_init;
wmplugin_exec_t wmplugin_exec;
static void process_nunchuk(struct cwiid_nunchuk_mesg *mesg);

static float Roll_Scale = 1.0;
static float Pitch_Scale = 1.0;
static float X_Scale = 1.0;
static float Y_Scale = 1.0;

struct wmplugin_info *wmplugin_info() {
	if (!info_init) {
		info.button_count = 4;
		info.button_info[0].name = "Up";
		info.button_info[1].name = "Down";
		info.button_info[2].name = "Right";
		info.button_info[3].name = "Left";
		info.axis_count = 0;
		info_init = 1;
	}
	return &info;
}

int wmplugin_init(int id, cwiid_wiimote_t *arg_wiimote)
{
	plugin_id = id;
	wiimote = arg_wiimote;
	data.buttons = 0;
	data.axes[0].valid = 1;
	data.axes[1].valid = 1;
	if (wmplugin_set_rpt_mode(id, CWIID_RPT_STATUS | CWIID_RPT_NUNCHUK)) {
		return -1;
	}

	return 0;
}

struct wmplugin_data *wmplugin_exec(int mesg_count, union cwiid_mesg mesg[])
{
	int i;
	enum cwiid_ext_type ext_type = CWIID_EXT_NONE;
	struct wmplugin_data *ret = NULL;

	for (i=0; i < mesg_count; i++) {
		switch (mesg[i].type) {
		case CWIID_MESG_STATUS:
			if ((mesg[i].status_mesg.ext_type == CWIID_EXT_NUNCHUK) &&
			  (ext_type != CWIID_EXT_NUNCHUK)) {
				if (cwiid_get_acc_cal(wiimote, CWIID_EXT_NUNCHUK, &acc_cal)) {
					wmplugin_err(plugin_id, "calibration error");
				}
			}
			ext_type = mesg[i].status_mesg.ext_type;
			break;
		case CWIID_MESG_NUNCHUK:
			process_nunchuk(&mesg[i].nunchuk_mesg);
			ret = &data;
			break;
		default:
			break;
		}
	}

	return ret;
}

static void process_nunchuk(struct cwiid_nunchuk_mesg *mesg)
{
	double stx=(double)mesg->stick[CWIID_X];
	double sty=(double)mesg->stick[CWIID_Y];
	data.buttons=0;
	if (sty > STICK_MID_VAL+STICK_NEUTRAL) data.buttons |= STICK_KEY_UP;
	if (sty < STICK_MID_VAL-STICK_NEUTRAL) data.buttons |= STICK_KEY_DOWN;
	if (stx > STICK_MID_VAL+STICK_NEUTRAL) data.buttons |= STICK_KEY_RIGHT;
	if (stx < STICK_MID_VAL-STICK_NEUTRAL) data.buttons |= STICK_KEY_LEFT;
}