| 1 |
//----------------------------------------------------------------------------- |
| 2 |
// name: gametra.ck |
| 3 |
// desc: gametrak boilerplate code; |
| 4 |
// prints 6 axes of the gametrak tethers + foot pedal button; |
| 5 |
// a helpful starting point for mapping gametrak |
| 6 |
// |
| 7 |
// author: Ge Wang ([email protected]) |
| 8 |
// date: summer 2014 |
| 9 |
//----------------------------------------------------------------------------- |
| 10 |
|
| 11 |
// z axis deadzone |
| 12 |
0 => float DEADZONE; |
| 13 |
|
| 14 |
// which joystick |
| 15 |
0 => int device; |
| 16 |
// get from command line |
| 17 |
if( me.args() ) me.arg(0) => Std.atoi => device; |
| 18 |
|
| 19 |
// HID objects |
| 20 |
Hid trak; |
| 21 |
HidMsg msg; |
| 22 |
|
| 23 |
// open joystick 0, exit on fail |
| 24 |
if( !trak.openJoystick( device ) ) me.exit(); |
| 25 |
|
| 26 |
// print |
| 27 |
<<< "joystick '" + trak.name() + "' ready", "" >>>; |
| 28 |
|
| 29 |
// data structure for gametrak |
| 30 |
class GameTrak |
| 31 |
{ |
| 32 |
// timestamps |
| 33 |
time lastTime; |
| 34 |
time currTime; |
| 35 |
|
| 36 |
// previous axis data |
| 37 |
float lastAxis[6]; |
| 38 |
// current axis data |
| 39 |
float axis[6]; |
| 40 |
} |
| 41 |
|
| 42 |
// gametrack |
| 43 |
GameTrak gt; |
| 44 |
|
| 45 |
// spork control |
| 46 |
spork ~ gametrak(); |
| 47 |
|
| 48 |
// main loop |
| 49 |
while( true ) |
| 50 |
{ |
| 51 |
// print 6 continuous axes -- XYZ values for left and right |
| 52 |
<<< "axes:", gt.axis[0],gt.axis[1],gt.axis[2], |
| 53 |
gt.axis[3],gt.axis[4],gt.axis[5] >>>; |
| 54 |
|
| 55 |
// also can map gametrak input to audio parameters around here |
| 56 |
// note: gt.lastAxis[0]...gt.lastAxis[5] hold the previous XYZ values |
| 57 |
|
| 58 |
// advance time |
| 59 |
100::ms => now; |
| 60 |
} |
| 61 |
|
| 62 |
// gametrack handling |
| 63 |
fun void gametrak() |
| 64 |
{ |
| 65 |
while( true ) |
| 66 |
{ |
| 67 |
// wait on HidIn as event |
| 68 |
trak => now; |
| 69 |
|
| 70 |
// messages received |
| 71 |
while( trak.recv( msg ) ) |
| 72 |
{ |
| 73 |
// joystick axis motion |
| 74 |
if( msg.isAxisMotion() ) |
| 75 |
{ |
| 76 |
// check which |
| 77 |
if( msg.which >= 0 && msg.which < 6 ) |
| 78 |
{ |
| 79 |
// check if fresh |
| 80 |
if( now > gt.currTime ) |
| 81 |
{ |
| 82 |
// time stamp |
| 83 |
gt.currTime => gt.lastTime; |
| 84 |
// set |
| 85 |
now => gt.currTime; |
| 86 |
} |
| 87 |
// save last |
| 88 |
gt.axis[msg.which] => gt.lastAxis[msg.which]; |
| 89 |
// the z axes map to [0,1], others map to [-1,1] |
| 90 |
if( msg.which != 2 && msg.which != 5 ) |
| 91 |
{ msg.axisPosition => gt.axis[msg.which]; } |
| 92 |
else |
| 93 |
{ |
| 94 |
1 - ((msg.axisPosition + 1) / 2) - DEADZONE => gt.axis[msg.which]; |
| 95 |
if( gt.axis[msg.which] < 0 ) 0 => gt.axis[msg.which]; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// joystick button down |
| 101 |
else if( msg.isButtonDown() ) |
| 102 |
{ |
| 103 |
<<< "button", msg.which, "down" >>>; |
| 104 |
} |
| 105 |
|
| 106 |
// joystick button up |
| 107 |
else if( msg.isButtonUp() ) |
| 108 |
{ |
| 109 |
<<< "button", msg.which, "up" >>>; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |