Skip to content
Newer
Older
100644 318 lines (263 sloc) 8.87 KB
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
1 import oscP5.*;
2 import netP5.*;
3
4 /** darwiinremoteOSC address space
5 /wii/connected , i
6 /wii/mousemode , i
7 /wii/button/a , i
8 /wii/button/b , i
9 /wii/button/up , i
10 /wii/button/down , i
11 /wii/button/left , i
12 /wii/button/right , i
13 /wii/button/minus , i
14 /wii/button/plus , i
15 /wii/button/home , i
16 /wii/button/one , i
17 /wii/button/two , i
18 /wii/acc , fff
19 /wii/orientation , ff
20 /wii/irdata , ffffffffffff
21 /wii/batterylevel , f
22 /nunchuk/joystick , ff
23 /nunchuk/button/z , i
24 /nunchuk/button/c , i
25 /nunchuk/acc , fff
26 /nunchuk/orientation , ff
27 */
28
29
30 class WiiController {
31 WiiController singleton = null;
32
33 OscP5 osc;
34 boolean buttonA, buttonB, buttonUp, buttonDown, buttonLeft, buttonRight;
35 boolean buttonOne, buttonTwo, buttonMinus, buttonPlus, buttonHome,buttonC,buttonZ;
36 boolean isConnected;
37 float roll, pitch;
38 float nRoll, nPitch;
39 Acceleration acc;
40 float x,y;
41 float nX, nY;
42 float pNx, pNy;
43 Acceleration nAcc;
44 boolean isNunchuck = false;
45
46 float batterylevel;
47 boolean DEBUG = false;
48
49 IRdata[] ir;
50
51 String remoteAddress;
52 int remotePort;
53
54
55 WiiController() {
56 // by default darwiinremoteOSC sends OSC messages to port 5600
e69eaeb @cibomahto Add speed and brightness controls to the wii
cibomahto authored Sep 30, 2012
57 osc = new OscP5(this,5600);
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
58
59 // the address and the port of darwiinremoteOSC
60 remoteAddress = "127.0.0.1";
61 remotePort = 5601;
62
63
64 acc = new Acceleration();
65 nAcc = new Acceleration();
66 ir = new IRdata[4];
67
3bbc380 @cibomahto Add support for wiimote under linux
cibomahto authored May 25, 2012
68 // Darwiin messages
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
69 osc.plug(this,"connected","/wii/connected");// i
70 osc.plug(this,"mousemode","/wii/mousemode");// i
71 osc.plug(this,"buttonA","/wii/button/a");// i
72 osc.plug(this,"buttonB","/wii/button/b");// i
73 osc.plug(this,"buttonUp","/wii/button/up");// i
74 osc.plug(this,"buttonDown","/wii/button/down");// i
75 osc.plug(this,"buttonLeft","/wii/button/left");// i
76 osc.plug(this,"buttonRight","/wii/button/right");// i
77 osc.plug(this,"buttonMinus","/wii/button/minus");// i
78 osc.plug(this,"buttonPlus","/wii/button/plus");// i
79 osc.plug(this,"buttonHome","/wii/button/home");// i
80 osc.plug(this,"buttonOne","/wii/button/one");// i
81 osc.plug(this,"buttonTwo","/wii/button/two");// i
82 osc.plug(this,"acceleration","/wii/acc");
83 osc.plug(this,"orientation","/wii/orientation");
84 osc.plug(this,"irdata","/wii/irdata");
85 osc.plug(this,"batterylevel","/wii/batterylevel");
86 osc.plug(this,"joystick","/nunchuk/joystick");
87 osc.plug(this,"buttonZ","/nunchuk/button/z");
88 osc.plug(this,"buttonC","/nunchuk/button/c");
89 osc.plug(this,"nunchukAcceleration","/nunchuk/acc");
90 osc.plug(this,"nunchukOrientation","/nunchuk/orientation");
3bbc380 @cibomahto Add support for wiimote under linux
cibomahto authored May 26, 2012
91
92 // Wiiosc messages
93 osc.plug(this,"wiiosc_buttonA", "/wii/keys/a");
94 osc.plug(this,"wiiosc_buttonB", "/wii/keys/b");
95 osc.plug(this,"wiiosc_buttonUp", "/wii/keys/up");// i
96 osc.plug(this,"wiiosc_buttonDown", "/wii/keys/down");// i
97 osc.plug(this,"wiiosc_buttonLeft", "/wii/keys/left");// i
98 osc.plug(this,"wiiosc_buttonRight", "/wii/keys/right");// i
99 osc.plug(this,"wiiosc_buttonMinus", "/wii/keys/minus");// i
100 osc.plug(this,"wiiosc_buttonPlus", "/wii/keys/plus");// i
101 osc.plug(this,"wiiosc_buttonHome", "/wii/keys/home");// i
102 osc.plug(this,"wiiosc_buttonOne", "/wii/keys/one");// i
103 osc.plug(this,"wiiosc_buttonTwo", "/wii/keys/two");// i
5ecc1c4 Add broken pitch/roll
justin authored May 27, 2012
104 osc.plug(this,"wiiosc_accX", "/wii/acc/x");// i
105 osc.plug(this,"wiiosc_accY", "/wii/acc/y");// i
106 osc.plug(this,"wiiosc_accZ", "/wii/acc/z");// i
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
107 }
dafd339 @cibomahto Change things, fix up sketches to work on dome.
cibomahto authored May 26, 2012
108
5ecc1c4 Add broken pitch/roll
justin authored May 27, 2012
109 void wiiosc_accX(int id, float value) {
0dfc24d @potatono mah
potatono authored May 27, 2012
110 // value goes from .4 (-90) to .6 (90)
111 acc.x = (value-.5)*10;
5ecc1c4 Add broken pitch/roll
justin authored May 27, 2012
112 }
113
114 void wiiosc_accY(int id, float value) {
0dfc24d @potatono mah
potatono authored May 28, 2012
115 acc.y = (value-.5)*10;
5ecc1c4 Add broken pitch/roll
justin authored May 27, 2012
116 }
117
118 void wiiosc_accZ(int id, float value) {
0dfc24d @potatono mah
potatono authored May 28, 2012
119 acc.z = (value-.5)*10;
5ecc1c4 Add broken pitch/roll
justin authored May 27, 2012
120
121 pitch = (float) Math.atan2(acc.y, acc.z)*(180/3.14);
122 roll = (float) Math.atan2(acc.x, acc.z)*(180/3.14);
123 }
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
124
125 void connected(int theValue) {
126 isConnected = (theValue==0) ? false:true;
127 }
128
129
2495fc5 @cibomahto Add input osc ports for brightness, framerate
cibomahto authored Aug 29, 2012
130 void oscEvent(OscMessage theOscMessage) {
131 if(theOscMessage.checkAddrPattern("/framerate")==true) {
132 if(theOscMessage.checkTypetag("i")) {
133 // println("Update framerate to " + theOscMessage.get(0).intValue());
134 frameRate(theOscMessage.get(0).intValue());
135 }
136 }
137 if(theOscMessage.checkAddrPattern("/brightness")==true) {
138 if(theOscMessage.checkTypetag("f")) {
139 // println("Update brigthness to " + theOscMessage.get(0).floatValue());
ce1845d Refactored to use Config class
Justin Day authored Jun 16, 2014
140 Config.BRIGHTNESS = theOscMessage.get(0).floatValue();
2495fc5 @cibomahto Add input osc ports for brightness, framerate
cibomahto authored Aug 29, 2012
141 }
142 }
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
143 }
144
2495fc5 @cibomahto Add input osc ports for brightness, framerate
cibomahto authored Aug 29, 2012
145
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
146 void acceleration(float theX, float theY, float theZ) {
147 acc.x = theX;
148 acc.y = theY;
149 acc.z = theZ;
150 if(DEBUG) {
151 println("acceleration x:"+acc.x+" y:"+acc.y+" z:"+acc.z);
152 }
153 }
154
155 void orientation(float theRoll, float thePitch) {
156 roll += (theRoll - roll)*0.04;
157 pitch += (thePitch - pitch)*0.04;
158 if(DEBUG) {
159 println("orientation roll:"+roll+" pitch:"+pitch);
160 }
161 }
162
163
164 // darwiinremoteOSC sends 12 floats containing the x,y and size values for
165 // 4 IR spots the wiimote can sense. values are between 0 and 1 for x and y
166 // values for size are 0 and bigger. if the size is 15 or 0, the IR point is not
167 // recognized by the wiimote.
168 void ir(
169 float f10, float f11,float f12,
170 float f20,float f21, float f22,
171 float f30, float f31, float f32,
172 float f40, float f41, float f42
173 ) {
174 ir[0].x = f10;
175 ir[0].y = f11;
176 ir[0].s = f12;
177 ir[1].x = f20;
178 ir[1].y = f21;
179 ir[1].s = f22;
180 ir[2].x = f30;
181 ir[2].y = f31;
182 ir[2].s = f32;
183 ir[3].x = f40;
184 ir[3].y = f41;
185 ir[3].s = f42;
186 }
187
188
189 void joystick(float theX, float theY) {
190 // the origin xy coordinates for the joystick are theX = 125, and theY=129
191 nX = theX;
192 nY = theY;
193 isNunchuck = true;
194 }
195
196 void nunchukAcceleration(float theX, float theY, float theZ) {
197 nAcc.x = theX;
198 nAcc.y = theY;
199 nAcc.z = theZ;
200 }
201
202
203 void nunchukOrientation(float theRoll, float thePitch) {
204 nRoll += (theRoll - nRoll)*0.04;
205 nPitch += (thePitch - nPitch)*0.04;
206 if(DEBUG) {
207 println("NUNCHUCK orientation roll:"+roll+" pitch:"+pitch);
208 }
209 }
3bbc380 @cibomahto Add support for wiimote under linux
cibomahto authored May 26, 2012
210
211 void wiiosc_buttonA (int id, int theValue) {buttonA (theValue);}
212 void wiiosc_buttonB (int id, int theValue) {buttonB (theValue);}
213 void wiiosc_buttonOne (int id, int theValue) {buttonOne (theValue);}
214 void wiiosc_buttonTwo (int id, int theValue) {buttonTwo (theValue);}
215 void wiiosc_buttonMinus(int id, int theValue) {buttonMinus(theValue);}
216 void wiiosc_buttonPlus (int id, int theValue) {buttonPlus (theValue);}
217 void wiiosc_buttonUp (int id, int theValue) {buttonUp (theValue);}
218 void wiiosc_buttonDown (int id, int theValue) {buttonDown (theValue);}
219 void wiiosc_buttonLeft (int id, int theValue) {buttonLeft (theValue);}
220 void wiiosc_buttonRight(int id, int theValue) {buttonRight(theValue);}
221 void wiiosc_buttonHome (int id, int theValue) {buttonHome (theValue);}
9c18f7f @cibomahto Rename things that were confusing
cibomahto authored May 27, 2012
222
223 // void wiiosc_accX (int id, int theValue) {acc.x = theX;}
224 // void wiiosc_accY (int id, int theValue) {}
225 // void wiiosc_accZ (int id, int theValue) {buttonHome (theValue);}
3bbc380 @cibomahto Add support for wiimote under linux
cibomahto authored May 26, 2012
226
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
227 void buttonA(int theValue) {
228 buttonA = (theValue==1) ? true:false;
229 }
230
231 void buttonB(int theValue) {
232 buttonB = (theValue==1) ? true:false;
233 }
234
235 void buttonOne(int theValue) {
236 buttonOne = (theValue==1) ? true:false;
237 }
238
239 void buttonTwo(int theValue) {
240 buttonTwo = (theValue==1) ? true:false;
241 }
242
243 void buttonMinus(int theValue) {
3bbc380 @cibomahto Add support for wiimote under linux
cibomahto authored May 26, 2012
244 buttonMinus = (theValue==1) ? true:false;
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
245 }
246
247 void buttonPlus(int theValue) {
248 buttonPlus = (theValue==1) ? true:false;
249 }
250
251 void buttonUp(int theValue) {
252 buttonUp = (theValue==1) ? true:false;
e69eaeb @cibomahto Add speed and brightness controls to the wii
cibomahto authored Sep 30, 2012
253
254 if(buttonUp) {
ce1845d Refactored to use Config class
Justin Day authored Jun 17, 2014
255 Config.BRIGHTNESS = min(1,Config.BRIGHTNESS + .1);
e69eaeb @cibomahto Add speed and brightness controls to the wii
cibomahto authored Sep 30, 2012
256 }
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
257 }
258
259 void buttonDown(int theValue) {
260 buttonDown = (theValue==1) ? true:false;
e69eaeb @cibomahto Add speed and brightness controls to the wii
cibomahto authored Sep 30, 2012
261
262 if(buttonDown) {
ce1845d Refactored to use Config class
Justin Day authored Jun 17, 2014
263 Config.BRIGHTNESS = max(.1,Config.BRIGHTNESS - .1);
e69eaeb @cibomahto Add speed and brightness controls to the wii
cibomahto authored Sep 30, 2012
264 }
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
265 }
3bbc380 @cibomahto Add support for wiimote under linux
cibomahto authored May 26, 2012
266
267 void buttonLeft(int theValue) {
268 buttonLeft = (theValue==1) ? true:false;
e69eaeb @cibomahto Add speed and brightness controls to the wii
cibomahto authored Sep 30, 2012
269
270 if (buttonLeft) {
271 frameRate(max(15,frameRate - 5));
272 }
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
273 }
274
3bbc380 @cibomahto Add support for wiimote under linux
cibomahto authored May 26, 2012
275 void buttonRight(int theValue) {
276 buttonRight = (theValue==1) ? true:false;
e69eaeb @cibomahto Add speed and brightness controls to the wii
cibomahto authored Sep 30, 2012
277
278 // make faster
279 if (buttonRight) {
280 frameRate(min(150,frameRate + 5));
281 }
4fbf5f1 @potatono Wii controller
potatono authored May 19, 2012
282 }
283
284 void buttonHome(int theValue) {
285 buttonHome = (theValue==1) ? true:false;
286 }
287
288 void batterylevel(float theValue) {
289 println("BatteryLevel: "+theValue);
290 batterylevel = theValue;
291 }
292
293 class Acceleration {
294 float x,y,z;
295 float speedX=0, speedY=0, speedZ=0;
296 }
297
298 class IRdata {
299 float x,y,s;
300 }
301
302 void requestBatterylevel() {
303 osc.send("/wii/batterylevel",new Object[] {},remoteAddress,remotePort);
304 }
305
306 void forcefeedback(boolean theFlag) {
307 int n = 0;
308 if(theFlag) {
309 n = 1;
310 }
311 osc.send("/wii/forcefeedback",new Object[] {new Integer(n) },remoteAddress,remotePort);
312 }
313
314 void led(int[] n) {
315 osc.send("/wii/led",new Object[] { new Integer(n[0]), new Integer(n[1]),new Integer(n[2]),new Integer(n[3])},remoteAddress,remotePort);
316 }
317 }
Something went wrong with that request. Please try again.