-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiiUse.cs
274 lines (247 loc) · 10.3 KB
/
WiiUse.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
namespace TheraWii
{
class WiiUse
{
static IntPtr pWiimoteArray;
static IntPtr[] pWiimote;
static int remoteIdx;
static int nunchuckIdx;
static int balanceIdx;
static bool remoteConnected;
static bool nunchuckConnected;
static bool balanceConnected;
static WiimoteState remoteState;
static NunchuckState nunchuckState;
static BalanceBoardState balanceState;
static bool isConnected = false;
static int numConnected;
const int numWiimotes = 2;
const int timeout = 5;
public static int Connect()
{
if (!isConnected)
{
init_structs();
numConnected = find();
startupPolling();
setupPointers(numConnected);
isConnected = true;
}
return numConnected;
}
private static void init_structs()
{
pWiimoteArray = WiiUseC.wiiuse_init(numWiimotes);
pWiimote = new IntPtr[numWiimotes];
IntPtr current = pWiimoteArray;
for (int i = 0; i < numWiimotes; i++)
{
pWiimote[i] = (IntPtr)Marshal.PtrToStructure(current, typeof(IntPtr));
current = (IntPtr)((long)current + Marshal.SizeOf(current));
}
}
private static int find()
{
int found = WiiUseC.wiiuse_find(pWiimoteArray, numWiimotes, timeout);
WiiUseC.wiiuse_connect(pWiimoteArray, numWiimotes);
WiiUseC.wiiuse_set_leds(pWiimote[0], 0x10);
WiiUseC.wiiuse_set_leds(pWiimote[1], 0x10);
return found;
}
private static void startupPolling()
{
for (int i = 0; i < 40; i++)
{
Poll();
}
}
private static void setupPointers(int num)
{
remoteConnected = false;
nunchuckConnected = false;
balanceConnected = false;
remoteState = new WiimoteState();
nunchuckState = new NunchuckState();
balanceState = new BalanceBoardState();
wiimote_t[] wms = MarshalWiimotes();
for (int i = 0; i < num; i++)
{
if (wms[i].exp.type == WiiExpansion.EXP_NONE || wms[i].exp.type == WiiExpansion.EXP_NUNCHUK)
{
remoteConnected = true;
remoteIdx = i;
remoteState.connected = true;
}
if (wms[i].exp.type == WiiExpansion.EXP_NUNCHUK)
{
nunchuckConnected = true;
nunchuckIdx = i;
nunchuckState.connected = true;
}
if (wms[i].exp.type == WiiExpansion.EXP_BALANCE)
{
balanceConnected = true;
balanceIdx = i;
balanceState.connected = true;
}
}
}
public static void Disconnect()
{
if (isConnected)
{
remoteConnected = false;
nunchuckConnected = false;
balanceConnected = false;
remoteState.connected = false;
nunchuckState.connected = false;
balanceState.connected = false;
isConnected = false;
// This calls wiiuse_disconnect as well
WiiUseC.wiiuse_cleanup(pWiimoteArray, numWiimotes);
}
}
public static bool IsConnected(WiiType type)
{
switch (type)
{
case WiiType.Remote:
return remoteConnected;
case WiiType.Nunchuck:
return nunchuckConnected;
case WiiType.BalanceBoard:
return balanceConnected;
case WiiType.Mouse:
return true;
default:
return false;
}
}
public static wiimote_t[] MarshalWiimotes()
{
wiimote_t[] wms = new wiimote_t[numWiimotes];
for (int i = 0; i < numWiimotes; i++)
{
wms[i] = (wiimote_t)Marshal.PtrToStructure(pWiimote[i], typeof(wiimote_t));
}
return wms;
}
public static WIIUSE_EVENT_TYPE[] Poll()
{
WIIUSE_EVENT_TYPE[] returnEvents = new WIIUSE_EVENT_TYPE[numWiimotes];
for (int i = 0; i < numWiimotes; i++)
{
returnEvents[i] = WIIUSE_EVENT_TYPE.WIIUSE_NONE;
}
if (WiiUseC.wiiuse_poll(pWiimoteArray, numWiimotes) > 0)
{
/*
* This happens if something happened on any wiimote.
* So go through each one and check if anything happened.
*/
wiimote_t[] wms = MarshalWiimotes();
for (int i = 0; i < numWiimotes; i++)
{
returnEvents[i] = wms[i].eventw;
if (remoteConnected && i == remoteIdx)
{
// Create Remote state from wiimote_t struct
updateRemote(wms[i]);
}
if (nunchuckConnected && i == nunchuckIdx)
{
// Create Nunchuck state from wiimote_t struct
updateNunchuck(wms[i]);
}
if (balanceConnected && i == balanceIdx)
{
// Create Balance state from wiimote_t struct
updateBalance(wms[i]);
}
}
}
return returnEvents;
}
private static void updateRemote(wiimote_t w)
{
remoteState.a = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_A) ? ButtonState.Pressed : ButtonState.Released;
remoteState.b = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_B) ? ButtonState.Pressed : ButtonState.Released;
remoteState.dDown = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_DOWN) ? ButtonState.Pressed : ButtonState.Released;
remoteState.dLeft = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_LEFT) ? ButtonState.Pressed : ButtonState.Released;
remoteState.dRight = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_RIGHT) ? ButtonState.Pressed : ButtonState.Released;
remoteState.dUp = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_UP) ? ButtonState.Pressed : ButtonState.Released;
remoteState.home = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_HOME) ? ButtonState.Pressed : ButtonState.Released;
remoteState.minus = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_MINUS) ? ButtonState.Pressed : ButtonState.Released;
remoteState.plus = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_PLUS) ? ButtonState.Pressed : ButtonState.Released;
remoteState.one = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_ONE) ? ButtonState.Pressed : ButtonState.Released;
remoteState.two = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_TWO) ? ButtonState.Pressed : ButtonState.Released;
remoteState.roll = w.orient.roll;
remoteState.pitch = w.orient.pitch;
remoteState.ir.x = MathHelper.Lerp(-1, 1, MathHelper.Clamp((float)w.ir.x / w.ir.vres[0], 0.0f, 1.0f));
remoteState.ir.y = -MathHelper.Lerp(-1, 1, MathHelper.Clamp((float)w.ir.y / w.ir.vres[1], 0.0f, 1.0f));
remoteState.ir.distance = w.ir.z;
remoteState.accel.X = w.gforce.x;
remoteState.accel.Y = w.gforce.y;
remoteState.accel.Z = w.gforce.z;
}
private static void updateNunchuck(wiimote_t w)
{
nunchuckState.c = WiiUseC.IS_PRESSED(w, WiiUseC.NUNCHUK_BUTTON_C) ? ButtonState.Pressed : ButtonState.Released;
nunchuckState.z = WiiUseC.IS_PRESSED(w, WiiUseC.NUNCHUK_BUTTON_Z) ? ButtonState.Pressed : ButtonState.Released;
nunchuckState.roll = w.exp.nunchuk.orient.roll;
nunchuckState.pitch = w.exp.nunchuk.orient.pitch;
nunchuckState.joyAngle = w.exp.nunchuk.js.ang;
nunchuckState.joyMagnitude = w.exp.nunchuk.js.mag;
nunchuckState.accel.X = w.gforce.x;
nunchuckState.accel.Y = w.gforce.y;
nunchuckState.accel.Z = w.gforce.z;
if (float.IsNaN(nunchuckState.joyAngle))
{
nunchuckState.joyAngle = 0;
}
}
private static void updateBalance(wiimote_t w)
{
balanceState.button = WiiUseC.IS_PRESSED(w, WiiUseC.WIIMOTE_BUTTON_A) ? ButtonState.Pressed : ButtonState.Released;
balanceState.frontLeft = w.exp.balance.feet.fl;
balanceState.frontRight = w.exp.balance.feet.fr;
balanceState.backLeft = w.exp.balance.feet.bl;
balanceState.backRight = w.exp.balance.feet.br;
}
public static WiimoteState GetRemoteState()
{
return remoteState.Copy();
}
public static NunchuckState GetNunchuckState()
{
return nunchuckState.Copy();
}
public static BalanceBoardState GetBalanceBoardState()
{
return balanceState.Copy();
}
public static void EnableMotionSensing()
{
WiiUseC.wiiuse_motion_sensing(pWiimote[remoteIdx], 1);
}
public static void DisableMotionSensing()
{
WiiUseC.wiiuse_motion_sensing(pWiimote[remoteIdx], 0);
}
public static void EnableIR()
{
WiiUseC.wiiuse_set_ir(pWiimote[remoteIdx], 1);
}
public static void DisableIR()
{
WiiUseC.wiiuse_set_ir(pWiimote[remoteIdx], 0);
}
}
}