Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 245 lines (198 sloc) 6.002 kB
4fbf5f1 @potatono Wii controller
potatono authored
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
57 osc = new OscP5(this,5600);
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
68
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");
91 }
92
93 void connected(int theValue) {
94 isConnected = (theValue==0) ? false:true;
95 }
96
97
98 void oscEvent(OscMessage theEvent) {
99 // println(theEvent.addrPattern()+"/ "+theEvent.typetag());
100 }
101
102 void acceleration(float theX, float theY, float theZ) {
103 acc.x = theX;
104 acc.y = theY;
105 acc.z = theZ;
106 if(DEBUG) {
107 println("acceleration x:"+acc.x+" y:"+acc.y+" z:"+acc.z);
108 }
109 }
110
111 void orientation(float theRoll, float thePitch) {
112 roll += (theRoll - roll)*0.04;
113 pitch += (thePitch - pitch)*0.04;
114 if(DEBUG) {
115 println("orientation roll:"+roll+" pitch:"+pitch);
116 }
117 }
118
119
120 // darwiinremoteOSC sends 12 floats containing the x,y and size values for
121 // 4 IR spots the wiimote can sense. values are between 0 and 1 for x and y
122 // values for size are 0 and bigger. if the size is 15 or 0, the IR point is not
123 // recognized by the wiimote.
124 void ir(
125 float f10, float f11,float f12,
126 float f20,float f21, float f22,
127 float f30, float f31, float f32,
128 float f40, float f41, float f42
129 ) {
130 ir[0].x = f10;
131 ir[0].y = f11;
132 ir[0].s = f12;
133 ir[1].x = f20;
134 ir[1].y = f21;
135 ir[1].s = f22;
136 ir[2].x = f30;
137 ir[2].y = f31;
138 ir[2].s = f32;
139 ir[3].x = f40;
140 ir[3].y = f41;
141 ir[3].s = f42;
142 }
143
144
145 void joystick(float theX, float theY) {
146 // the origin xy coordinates for the joystick are theX = 125, and theY=129
147 nX = theX;
148 nY = theY;
149 isNunchuck = true;
150 }
151
152 void nunchukAcceleration(float theX, float theY, float theZ) {
153 nAcc.x = theX;
154 nAcc.y = theY;
155 nAcc.z = theZ;
156 }
157
158
159 void nunchukOrientation(float theRoll, float thePitch) {
160 nRoll += (theRoll - nRoll)*0.04;
161 nPitch += (thePitch - nPitch)*0.04;
162 if(DEBUG) {
163 println("NUNCHUCK orientation roll:"+roll+" pitch:"+pitch);
164 }
165 }
166
167 void buttonA(int theValue) {
168 buttonA = (theValue==1) ? true:false;
169 }
170
171 void buttonB(int theValue) {
172 buttonB = (theValue==1) ? true:false;
173 }
174
175 void buttonOne(int theValue) {
176 buttonOne = (theValue==1) ? true:false;
177 }
178
179 void buttonTwo(int theValue) {
180 buttonTwo = (theValue==1) ? true:false;
181 }
182
183 void buttonMinus(int theValue) {
184 buttonMinus = (theValue==1) ? true:
185 false;
186 }
187
188 void buttonPlus(int theValue) {
189 buttonPlus = (theValue==1) ? true:false;
190 }
191
192 void buttonUp(int theValue) {
193 buttonUp = (theValue==1) ? true:false;
194 }
195
196 void buttonDown(int theValue) {
197 buttonDown = (theValue==1) ? true:false;
198 }
199
200 void buttonC(int theValue) {
201 buttonC = (theValue==1) ? true:false;
202 }
203
204 void buttonZ(int theValue) {
205 buttonZ = (theValue==1) ? true:false;
206 }
207
208 void buttonHome(int theValue) {
209 buttonHome = (theValue==1) ? true:false;
210 }
211
212 void batterylevel(float theValue) {
213 println("BatteryLevel: "+theValue);
214 batterylevel = theValue;
215 }
216
217 class Acceleration {
218 float x,y,z;
219 float speedX=0, speedY=0, speedZ=0;
220 }
221
222 class IRdata {
223 float x,y,s;
224 }
225
226
227
228
229 void requestBatterylevel() {
230 osc.send("/wii/batterylevel",new Object[] {},remoteAddress,remotePort);
231 }
232
233 void forcefeedback(boolean theFlag) {
234 int n = 0;
235 if(theFlag) {
236 n = 1;
237 }
238 osc.send("/wii/forcefeedback",new Object[] {new Integer(n) },remoteAddress,remotePort);
239 }
240
241 void led(int[] n) {
242 osc.send("/wii/led",new Object[] { new Integer(n[0]), new Integer(n[1]),new Integer(n[2]),new Integer(n[3])},remoteAddress,remotePort);
243 }
244 }
Something went wrong with that request. Please try again.