/
UniMoveController.cs
574 lines (466 loc) · 17.3 KB
/
UniMoveController.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/**
* UniMove API - A Unity plugin for the PlayStation Move motion controller
* Copyright (C) 2012, 2013, Copenhagen Game Collective (http://www.cphgc.org)
* Patrick Jarnfelt
* Douglas Wilson (http://www.doougle.net)
*
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
/**
* IMPORTANT NOTES!
*
* -- This API has been compiled for Mac OSX (10.7 and later) specifically.
*
* -- This API assumes that the controller has already been paired and connected by Bluetooth beforehand.
* To pair a controller(s), use the Pairing Utility provided by the PS Move API http://thp.io/2010/psmove/.
* To connect a controller by Bluetooth, just press the PS button after pairing it.
* You can also use the controllers by USB, but with limited functionality (see below).
*
* -- Features include:
*
* - Setting the RGB LED color and rumble intensity (USB and Bluetooth)
* - Read the status of the digital buttons (Bluetooth only)
* - Read the status of the analog trigger (Bluetooth only)
* - Read values for the internal sensors (Bluetooth only):
* - accelorometer
* - gyroscope
* - magnetometer
* - temperature
* - battery level
*
* Please see the README for more information!
**/
using System;
using UnityEngine;
using System.Runtime.InteropServices;
#region enums and structs
/// <summary>
/// The Move controller can be connected by USB and/or Bluetooth.
/// </summary>
public enum PSMoveConnectionType
{
Bluetooth,
USB,
Unknown,
};
// Not entirely sure why some of these buttons (R3/L3) are exposed...
public enum PSMoveButton
{
L2 = 1 << 0x00,
R2 = 1 << 0x01,
L1 = 1 << 0x02,
R1 = 1 << 0x03,
Triangle = 1 << 0x04,
Circle = 1 << 0x05,
Cross = 1 << 0x06,
Square = 1 << 0x07,
Select = 1 << 0x08,
L3 = 1 << 0x09,
R3 = 1 << 0x0A,
Start = 1 << 0x0B,
Up = 1 << 0x0C,
Right = 1 << 0x0D,
Down = 1 << 0x0E,
Left = 1 << 0x0F,
PS = 1 << 0x10,
Move = 1 << 0x13,
Trigger = 1 << 0x14, /* We can use this value with IsButtonDown() (or the events) to get
* a binary yes/no answer about if the trigger button is down at all.
* For the full integer/analog value of the trigger, see the corresponding property below.
*/
};
// Used by psmove_get_battery().
public enum PSMove_Battery_Level {
Batt_MIN = 0x00, /*!< Battery is almost empty (< 20%) */
Batt_20Percent = 0x01, /*!< Battery has at least 20% remaining */
Batt_40Percent = 0x02, /*!< Battery has at least 40% remaining */
Batt_60Percent = 0x03, /*!< Battery has at least 60% remaining */
Batt_80Percent = 0x04, /*!< Battery has at least 80% remaining */
Batt_MAX = 0x05, /*!< Battery is fully charged (not on charger) */
Batt_CHARGING = 0xEE, /*!< Battery is currently being charged */
Batt_CHARGING_DONE = 0xEF, /*!< Battery is fully charged (on charger) */
};
public enum PSMove_Frame {
Frame_FirstHalf = 0, /*!< The older frame */
Frame_SecondHalf, /*!< The most recent frame */
};
public class UniMoveButtonEventArgs : EventArgs
{
public readonly PSMoveButton button;
public UniMoveButtonEventArgs(PSMoveButton button)
{
this.button = button;
}
}
#endregion
public class UniMoveController : MonoBehaviour
{
#region private instance variables
/// <summary>
/// The handle for this controller. This pointer is what the psmove library uses for reading data via the hid library.
/// </summary>
private IntPtr handle;
private bool disconnected = false;
private float timeElapsed = 0.0f;
private float updateRate = 0.05f; // The default update rate is 50 milliseconds
private static float MIN_UPDATE_RATE = 0.02f; // You probably don't want to update the controller more frequently than every 20 milliseconds
private float trigger = 0f;
private uint currentButtons = 0;
private uint prevButtons = 0;
private Vector3 rawAccel = Vector3.down;
private Vector3 accel = Vector3.down;
private Vector3 magnet = Vector3.zero;
private Vector3 rawGyro = Vector3.zero;
private Vector3 gyro = Vector3.zero;
// TODO: These values still need to be implemented, so we don't expose them publicly
private PSMove_Battery_Level battery = PSMove_Battery_Level.Batt_20Percent;
private float temperature = 0f;
/// <summary>
/// Event fired when the controller disconnects unexpectedly (i.e. on going out of range).
/// </summary>
public event EventHandler OnControllerDisconnected;
#endregion
/// <summary>
/// Returns whether the connecting succeeded or not.
///
/// NOTE! This function does NOT pair the controller by Bluetooth.
/// If the controller is not already paired, it can only be connected by USB.
/// See README for more information.
/// </summary>
public bool Init(int index)
{
handle = psmove_connect_by_id(index);
// Error check the result!
if (handle == IntPtr.Zero) return false;
// Make sure the connection is actually sending data. If not, this is probably a controller
// you need to remove manually from the OSX Bluetooth Control Panel, then re-connect.
return (psmove_update_leds(handle) != 0);
}
/// <summary>
/// Static function that returns the number of *all* controller connections.
/// This count will tally both USB and Bluetooth connections.
/// Note that one physical controller, then, might register multiple connections.
/// To discern between different connection types, see the ConnectionType property below.
/// </summary>
public static int GetNumConnected()
{
return psmove_count_connected();
}
/// <summary>
/// The amount of time, in seconds, between update calls.
/// The faster this rate, the more responsive the controllers will be.
/// However, update too fast and your computer won't be able to keep up (see below).
/// You almost certainly don't want to make this faster than 20 milliseconds (0.02f).
///
/// NOTE! We find that slower/older computers can have trouble keeping up with a fast update rate,
/// especially the more controllers that are connected. See the README for more information.
/// </summary>
public float UpdateRate
{
get { return this.updateRate; }
set { updateRate = Math.Max(value, MIN_UPDATE_RATE); } // Clamp negative values up to 0
}
void Update()
{
if (disconnected) return;
// we want to update the previous buttons outside the update restriction so,
// we only get one button event pr. unity update frame
prevButtons = currentButtons;
timeElapsed += Time.deltaTime;
// Here we manually enforce updates only every updateRate amount of time
// The reason we don't just do this in FixedUpdate is so the main program's FixedUpdate rate
// can be set independently of the controllers' update rate.
if (timeElapsed < updateRate) return;
else timeElapsed = 0.0f;
uint buttons = 0;
// NOTE! There is potentially data waiting in queue.
// We need to poll *all* of it by calling psmove_poll() until the queue is empty. Otherwise, data might begin to build up.
while (psmove_poll(handle) > 0)
{
// We are interested in every button press between the last update and this one:
buttons = buttons | psmove_get_buttons(handle);
// The events are not really working from the PS Move Api. So we do our own with the prevButtons
//psmove_get_button_events(handle, ref pressed, ref released);
currentButtons = buttons;
}
// For acceleration, gyroscope, and magnetometer values, we look at only the last value in the queue.
// We could in theory average all the acceleration (and other) values in the queue for a "smoothing" effect, but we've chosen not to.
ProcessData();
// Send a report to the controller to update the LEDs and rumble.
if (psmove_update_leds(handle) == 0)
{
// If it returns zero, the controller must have disconnected (i.e. out of battery or out of range),
// so we should fire off any events and disconnect it.
OnControllerDisconnected(this, new EventArgs());
Disconnect();
}
}
void OnApplicationQuit()
{
Disconnect();
}
/// <summary>
/// Returns true if "button" is currently down.
/// </summary
public bool GetButton(PSMoveButton b)
{
if (disconnected) return false;
return ((currentButtons & (uint)b) != 0);
}
/// <summary>
/// Returns true if "button" is pressed down this instant.
/// </summary
public bool GetButtonDown(PSMoveButton b)
{
if (disconnected) return false;
return ((prevButtons & (uint)b) == 0) && ((currentButtons & (uint)b) != 0);
}
/// <summary>
/// Returns true if "button" is released this instant.
/// </summary
public bool GetButtonUp(PSMoveButton b)
{
if (disconnected) return false;
return ((prevButtons & (uint)b) != 0) && ((currentButtons & (uint)b) == 0);
}
/// <summary>
/// Disconnect the controller
/// </summary>
public void Disconnect()
{
disconnected = true;
SetLED(0,0,0);
SetRumble(0);
psmove_disconnect(handle);
}
/// <summary>
/// Whether or not the controller has been disconnected
/// </summary
public bool Disconnected
{
get { return disconnected; }
}
/// <summary>
/// Sets the amount of rumble
/// </summary>
/// <param name="rumble">the rumble amount (0-1)</param>
public void SetRumble(float rumble)
{
if (disconnected) return;
// Clamp value between 0 and 1:
byte rumbleByte = (byte) (Math.Min(Math.Max(rumble, 0f), 1f) * 255);
psmove_set_rumble(handle, (char)rumbleByte);
}
/// <summary>
/// Sets the LED color
/// </summary>
/// <param name="color">Unity's Color type</param>
public void SetLED(Color color)
{
if (disconnected) return;
psmove_set_leds(handle, (char)(color.r * 255), (char)(color.g * 255), (char)(color.b * 255));
}
/// <summary>
/// Sets the LED color
/// </summary>
/// <param name="r">Red value of the LED color (0-255)</param>
/// <param name="g">Green value of the LED color (0-255)</param>
/// <param name="b">Blue value of the LED color (0-255)</param>
public void SetLED(byte r, byte g, byte b)
{
if (disconnected) return;
psmove_set_leds(handle, (char)r, (char)g, (char)b);
}
/// <summary>
/// Value of the analog trigger button (between 0 and 1)
/// </summary
public float Trigger
{
get { return trigger; }
}
/// <summary>
/// The 3-axis acceleration values.
/// </summary>
public Vector3 RawAcceleration
{
get { return rawAccel; }
}
/// <summary>
/// The 3-axis acceleration values, roughly scaled between -3g to 3g (where 1g is Earth's gravity).
/// </summary>
public Vector3 Acceleration
{
get { return accel; }
}
/// <summary>
/// The raw values of the 3-axis gyroscope.
/// </summary>
public Vector3 RawGyroscope
{
get { return rawGyro; }
}
/// <summary>
/// The raw values of the 3-axis gyroscope.
/// </summary>
public Vector3 Gyro
{
get { return gyro; }
}
/// <summary>
/// The raw values of the 3-axis magnetometer.
/// To be honest, we don't fully understand what the magnetometer does.
/// The C API on which this code is based warns that this isn't fully tested.
/// </summary>
public Vector3 Magnetometer
{
get { return magnet; }
}
/// <summary>
/// The battery level
/// </summary>
public PSMove_Battery_Level Battery
{
get { return battery; }
}
/// <summary>
/// The temperature in Celcius
/// </summary>
public float Temperature
{
get { return temperature; }
}
/// <summary>
/// The serial number of the controller (Bluetooth address)
/// </summary>
public string Serial
{
get { return psmove_get_serial(handle); }
}
/* TODO: These two values still need to be implemented, so we don't expose them publicly... yet!
public float Battery
{
get { return this.battery; }
}
public float Temperature
{
get { return this.temperature; }
}
*/
public PSMoveConnectionType ConnectionType
{
get { return psmove_connection_type(handle); }
}
#region private methods
/// <summary>
/// Process all the raw data on the Playstation Move controller
/// </summary>
private void ProcessData()
{
trigger = ((int)psmove_get_trigger(handle)) / 255f;
int x = 0, y = 0, z = 0;
psmove_get_accelerometer(handle, ref x, ref y, ref z);
rawAccel.x = x;
rawAccel.y = y;
rawAccel.z = z;
float ax = 0, ay = 0, az = 0;
psmove_get_accelerometer_frame(handle, PSMove_Frame.Frame_SecondHalf, ref ax, ref ay, ref az);
accel.x = ax;
accel.y = ay;
accel.z = az;
psmove_get_gyroscope(handle, ref x, ref y, ref z );
rawGyro.x = x;
rawGyro.y = y;
rawGyro.z = z;
float gx = 0, gy = 0, gz = 0;
psmove_get_gyroscope_frame(handle, PSMove_Frame.Frame_SecondHalf, ref gx, ref gy, ref gz);
gyro.x = gx;
gyro.y = gy;
gyro.z = gz;
psmove_get_magnetometer(handle, ref x, ref y, ref z );
// TODO: Should these values be converted into a more human-understandable range?
magnet.x = x;
magnet.y = y;
magnet.z = z;
battery = psmove_get_battery(handle);
temperature = psmove_get_temperature(handle);
}
#endregion
#region importfunctions
/* The following functions are bindings to Thomas Perl's C API for the PlayStation Move (http://thp.io/2010/psmove/)
* See README for more details.
*
* NOTE! We have included bindings for the psmove_pair() function, even though we don't use it here
* See README and Pairing Utility code for more about pairing.
*
* TODO: Expose hooks to psmove_get_btaddr() and psmove_set_btadd()
* These functions are already called by psmove_pair(), so unless you need to do something special, you won't need them.
*/
[DllImport("libpsmoveapi")]
private static extern int psmove_count_connected();
[DllImport("libpsmoveapi")]
private static extern IntPtr psmove_connect();
[DllImport("libpsmoveapi")]
private static extern IntPtr psmove_connect_by_id(int id);
[DllImport("libpsmoveapi")]
private static extern int psmove_pair(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern PSMoveConnectionType psmove_connection_type(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern int psmove_has_calibration(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern void psmove_set_leds(IntPtr move, char r, char g, char b);
[DllImport("libpsmoveapi")]
private static extern int psmove_update_leds(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern void psmove_set_rumble(IntPtr move, char rumble);
[DllImport("libpsmoveapi")]
private static extern uint psmove_poll(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern uint psmove_get_buttons(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern uint psmove_get_button_events(IntPtr move, ref uint pressed, ref uint released);
[DllImport("libpsmoveapi")]
private static extern char psmove_get_trigger(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern float psmove_get_temperature(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern PSMove_Battery_Level psmove_get_battery(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern void psmove_get_accelerometer(IntPtr move, ref int ax, ref int ay, ref int az);
[DllImport("libpsmoveapi")]
private static extern void psmove_get_accelerometer_frame(IntPtr move,PSMove_Frame frame, ref float ax, ref float ay, ref float az);
[DllImport("libpsmoveapi")]
private static extern void psmove_get_gyroscope(IntPtr move, ref int gx, ref int gy, ref int gz);
[DllImport("libpsmoveapi")]
private static extern void psmove_get_gyroscope_frame(IntPtr move,PSMove_Frame frame, ref float gx, ref float gy, ref float gz);
[DllImport("libpsmoveapi")]
private static extern void psmove_get_magnetometer(IntPtr move, ref int mx, ref int my, ref int mz);
[DllImport("libpsmoveapi")]
private static extern void psmove_disconnect(IntPtr move);
[DllImport("libpsmoveapi")]
private static extern string psmove_get_serial(IntPtr move);
#endregion
}