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
|
/*
* "Logitech Rumble Gamepad" event class
*/
public class RumbleEvent extends GenEvent {
static string @__axisToName[]; /* pseudo-private */
static string @__buttonToName[]; /* pseudo-private */
/*
* Should be "Generic X-Box pad"
* Can be changed by constructor since __hid_loop() starts only
* when constructing shred passes time
*/
0 => int device;
fun void
__hid_loop() /* pseudo-private */
{
Hid hid;
if (!hid.openJoystick(device)) {
cherr <= "Cannot open joystick device " <= device
<= IO.newline();
me.exit();
}
chout <= "Opened joystick device " <= hid.num()
<= " (" <= hid.name() <= ")" <= IO.newline();
while (hid => now) {
while (HidMsg msg => hid.recv) {
if (msg.isAxisMotion()) {
__axisToName[msg.which] => control;
/* normalize value [-1, 1] to [0, 1] */
(msg.axisPosition+1)/2 => value;
} else if (msg.isButtonDown() || msg.isButtonUp()) {
__buttonToName[msg.which] => control;
msg.isButtonDown() => value;
}
if (control == "") {
cherr <= "Unknown joystick controller " <= msg.which
<= " (isAxisMotion=" <= msg.isAxisMotion() <= ")"
<= IO.newline();
} else {
broadcast();
/*
* ensure that shreds waiting on the event
* process it before it is overwritten
* by the next message in the queue
*/
me.yield();
}
}
}
/* never reached */
}
spork ~ __hid_loop();
}
/* static initialization */
["joystickLeftX", "joystickLeftY", "axisButtonLeft",
"joystickRightX", "joystickRightY", "axisButtonRight",
"cursorX", "cursorY"] @=> RumbleEvent.__axisToName;
["buttonA", "buttonB", "buttonX", "buttonY",
"buttonLeft", "buttonRight",
"buttonStart", "buttonLogitech",
"buttonJoystickLeft", "buttonJoystickRight",
"buttonBack"] @=> RumbleEvent.__buttonToName;
|