summaryrefslogtreecommitdiff
path: root/neverball/tilt_wii.patch
blob: 3a096bf56ab2569de58a9aa72b1660df30c3747e (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
diff --git a/Makefile b/Makefile
index 169b9f8..b6a1878 100644
--- a/Makefile
+++ b/Makefile
@@ -108,7 +108,7 @@ endif
 INTL_LIBS :=
 
 ifeq ($(ENABLE_TILT),wii)
-    TILT_LIBS := -lcwiimote -lbluetooth
+    TILT_LIBS := -lcwiid -lbluetooth
 else
 ifeq ($(ENABLE_TILT),loop)
     TILT_LIBS := -lusb-1.0 -lfreespace
diff --git a/share/tilt_wii.c b/share/tilt_wii.c
index 44eaf73..5427540 100644
--- a/share/tilt_wii.c
+++ b/share/tilt_wii.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003 Robert Kooima
+ * Copyright (C) 2011 Daniel Friesel
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
  * it under the  terms of the GNU General  Public License as published
@@ -15,6 +16,7 @@
 #include <SDL.h>
 #include <SDL_thread.h>
 #include <math.h>
+#include <unistd.h>
 #include <stdio.h>
 
 #include "config.h"
@@ -22,8 +24,10 @@
 /*---------------------------------------------------------------------------*/
 
 #define _ENABLE_TILT
-#include <libcwiimote/wiimote.h>
-#include <libcwiimote/wiimote_api.h>
+#include <bluetooth/bluetooth.h>
+#include <cwiid.h>
+
+struct balance_cal balance_cal;
 
 /*
  * This data structure tracks button changes, counting transitions so that
@@ -43,6 +47,23 @@ struct button_state
     unsigned char dnc;
 };
 
+
+/* not having and setting a callback causes problems with SDL's mutex
+ * ("Mesg pipe is full")
+ */
+static void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
+	union cwiid_mesg mesg[], struct timespec *ts)
+{
+}
+
+static double weight(unsigned short reading, unsigned short cal[3])
+{
+	if (reading < cal[1])
+		return ((double)reading - cal[0]) / (cal[1] - cal[0]) * 17.0;
+
+	return (((double)reading - cal[1]) / (cal[2] - cal[1]) * 17.0) + 17.0;
+}
+
 static void set_button(struct button_state *B, int s)
 {
     if ((B->curr == 0) != (s == 0))
@@ -106,59 +127,96 @@ static SDL_Thread       *thread = NULL;
 
 static int tilt_func(void *data)
 {
-    wiimote_t   wiimote = WIIMOTE_INIT;
-    const char *address = config_get_s(CONFIG_WIIMOTE_ADDR);
+    cwiid_wiimote_t *wiimote = NULL;
+    cwiid_wiimote_t *remote = NULL;
 
-    if (strlen(address) > 0)
-    {
-        if (wiimote_connect(&wiimote, address) < 0)
-            fprintf(stderr, "%s\n", wiimote_get_error());
-        else
-        {
-            int running = 1;
+    struct cwiid_state wiistate;
+    struct cwiid_state remstate;
+
+    bdaddr_t addr_board;
+    bdaddr_t addr_remote;
+
+    str2ba("00:26:59:62:A6:8F", &addr_remote);
+    str2ba("00:26:59:82:16:C0", &addr_board);
 
-            wiimote.mode.bits = WIIMOTE_MODE_ACC;
-            wiimote.led.one   = 1;
+    double wlt, wrt, wlb, wrb, bal_x, bal_y;
+
+	remote = cwiid_open(&addr_remote, 0);
+	cwiid_set_led(remote, 2);
+	wiimote = cwiid_open(&addr_board, 0);
+	cwiid_set_led(wiimote, 1);
+
+	if ((wiimote == NULL) || (remote == NULL))
+		fprintf(stderr, "Unable to connect to bboard\n");
+	else
+	{
+		int running = 1;
+
+		cwiid_get_balance_cal(wiimote, &balance_cal);
+		cwiid_set_mesg_callback(wiimote, cwiid_callback);
+		cwiid_set_mesg_callback(remote, cwiid_callback);
+		cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC);
+		cwiid_enable(remote, CWIID_FLAG_MESG_IFC);
+		cwiid_set_rpt_mode(wiimote, CWIID_RPT_STATUS | CWIID_RPT_EXT);
+		cwiid_set_rpt_mode(remote, CWIID_RPT_STATUS | CWIID_RPT_BTN);
 
             SDL_mutexP(mutex);
             state.status = running;
             SDL_mutexV(mutex);
 
-            while (mutex && running && wiimote_is_open(&wiimote))
+            while (mutex && running)
             {
-                if (wiimote_update(&wiimote) < 0)
-                    break;
 
                 SDL_mutexP(mutex);
                 {
                     running = state.status;
 
-                    set_button(&state.A,     wiimote.keys.a);
-                    set_button(&state.B,     wiimote.keys.b);
-                    set_button(&state.plus,  wiimote.keys.plus);
-                    set_button(&state.minus, wiimote.keys.minus);
-                    set_button(&state.home,  wiimote.keys.home);
-                    set_button(&state.L,     wiimote.keys.left);
-                    set_button(&state.R,     wiimote.keys.right);
-                    set_button(&state.U,     wiimote.keys.up);
-                    set_button(&state.D,     wiimote.keys.down);
-
-                    if (isnormal(wiimote.tilt.y))
+				cwiid_get_state(wiimote, &wiistate);
+				cwiid_get_state(remote, &remstate);
+
+                    set_button(&state.A,     remstate.buttons & CWIID_BTN_A);
+                    set_button(&state.B,     remstate.buttons & CWIID_BTN_B);
+                    set_button(&state.plus,  remstate.buttons & CWIID_BTN_PLUS);
+                    set_button(&state.minus, remstate.buttons & CWIID_BTN_MINUS);
+                    set_button(&state.home,  remstate.buttons & CWIID_BTN_HOME);
+                    set_button(&state.L,     remstate.buttons & CWIID_BTN_LEFT);
+                    set_button(&state.R,     remstate.buttons & CWIID_BTN_RIGHT);
+                    set_button(&state.U,     remstate.buttons & CWIID_BTN_UP);
+                    set_button(&state.D,     remstate.buttons & CWIID_BTN_DOWN);
+
+				wlt = weight(wiistate.ext.balance.left_top, balance_cal.left_top);
+				wrt = weight(wiistate.ext.balance.right_top, balance_cal.right_top);
+				wlb = weight(wiistate.ext.balance.left_bottom, balance_cal.left_bottom);
+				wrb = weight(wiistate.ext.balance.right_bottom, balance_cal.right_bottom);
+
+				bal_x = (wrt + wrb) / (wlt + wlb);
+				if (bal_x > 1)
+					bal_x = ((wlt + wlb) / (wrt + wrb) * (-1.0)) + 1.0;
+				else
+					bal_x -= 1;
+
+				bal_y = (wlt + wrt) / (wlb + wrb);
+				if (bal_y > 1)
+					bal_y = ((wlb + wrb) / (wlt + wrt) * (-1.0)) + 1.0;
+				else
+					bal_y -= 1;
+
+                    if ((bal_y >= -1) && (bal_y <= 1))
                     {
+					bal_y *= 20;
                         state.x = (state.x * (FILTER - 1) +
-                                   wiimote.tilt.y) / FILTER;
+                                   bal_y) / FILTER;
                     }
-                    if (isnormal(wiimote.tilt.x))
+                    if ((bal_x >= -1) && (bal_x <= 1))
                     {
+					bal_x *= 20;
                         state.z = (state.z * (FILTER - 1) +
-                                   wiimote.tilt.x) / FILTER;
+                                   bal_x) / FILTER;
                     }
                 }
                 SDL_mutexV(mutex);
             }
 
-            wiimote_disconnect(&wiimote);
-        }
     }
     return 0;
 }
@@ -295,3 +353,4 @@ int tilt_stat(void)
 }
 
 /*---------------------------------------------------------------------------*/
+