2011-10-07 12:20:30 +00:00
|
|
|
# Hardware Interface (currently Linux/HIDRAW-only)
|
|
|
|
# see README and http://members.aon.at/mfranz/warthog.html
|
|
|
|
|
|
|
|
|
|
|
|
if (io.stat("/dev/input") != nil and io.stat("/dev/input/hidraw") == nil) {
|
|
|
|
print("warthog: read file $FG_ROOT/Input/Joysticks/ThrustMaster/Warthog/README");
|
|
|
|
print(" for how to enable FlightGear to set backlight and LEDs etc.");
|
|
|
|
}
|
|
|
|
|
2011-10-01 16:34:28 +00:00
|
|
|
|
|
|
|
var device = {
|
|
|
|
new: func(path, bufsize) {
|
|
|
|
var m = { parents: [device] };
|
|
|
|
m.path = path;
|
|
|
|
m.bufsize = bufsize;
|
2011-10-07 12:20:30 +00:00
|
|
|
m.data = bits.buf(m.bufsize);
|
2011-10-01 16:34:28 +00:00
|
|
|
var stat = io.stat(m.path);
|
|
|
|
if (stat == nil or stat[11] != "chr")
|
2011-10-07 12:20:30 +00:00
|
|
|
m.send = m.receive = func {};
|
2011-10-01 16:34:28 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
send: func {
|
|
|
|
var buf = bits.buf(me.bufsize);
|
|
|
|
forindex (var i; arg)
|
2011-10-07 12:20:30 +00:00
|
|
|
buf[i] = arg[i];
|
2011-10-01 16:34:28 +00:00
|
|
|
var file = io.open(me.path, "wb");
|
|
|
|
io.write(file, buf);
|
|
|
|
io.close(file);
|
|
|
|
},
|
2011-10-07 12:20:30 +00:00
|
|
|
receive: func {
|
|
|
|
var file = io.open(me.path, "rb");
|
|
|
|
io.read(file, me.data, me.bufsize);
|
|
|
|
io.close(file);
|
|
|
|
},
|
2011-10-02 20:19:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var joystick = {
|
|
|
|
parents: [device.new("/dev/input/hidraw/Thustmaster_Joystick_-_HOTAS_Warthog", 12)],
|
2011-10-07 12:20:30 +00:00
|
|
|
init: func {
|
|
|
|
me.receive();
|
|
|
|
},
|
2011-10-02 20:19:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var throttle = {
|
|
|
|
parents: [device.new("/dev/input/hidraw/Thrustmaster_Throttle_-_HOTAS_Warthog", 36)],
|
2011-10-07 12:20:30 +00:00
|
|
|
init: func {
|
|
|
|
me.receive();
|
|
|
|
me.leds = me.data[26];
|
|
|
|
me.brightness = me.data[27];
|
|
|
|
},
|
2011-10-02 20:19:24 +00:00
|
|
|
set_leds: func(state, which...) { # on/off, list of leds (0: background, 1-5)
|
2011-10-07 12:20:30 +00:00
|
|
|
var leds = me.leds;
|
2011-10-01 16:34:28 +00:00
|
|
|
foreach (var w; which)
|
2011-10-02 20:19:24 +00:00
|
|
|
me.leds = bits.switch(me.leds, me._ledmap[w], state);
|
2011-10-07 12:20:30 +00:00
|
|
|
if (me.leds != leds)
|
|
|
|
me.send(1, 6, me.leds, me.brightness);
|
2011-10-01 16:34:28 +00:00
|
|
|
},
|
|
|
|
toggle_leds: func(which...) {
|
|
|
|
foreach (var w; which)
|
|
|
|
me.leds = bits.toggle(me.leds, me._ledmap[w]);
|
2011-10-07 12:20:30 +00:00
|
|
|
me.send(1, 6, me.leds, me.brightness);
|
2011-10-01 16:34:28 +00:00
|
|
|
},
|
2011-10-02 20:19:24 +00:00
|
|
|
set_brightness: func(v) { # clamped to [0,5], where 0 is off and 5 is bright
|
2011-10-07 12:20:30 +00:00
|
|
|
if (v != me.brightness)
|
|
|
|
me.send(1, 6, me.leds, me.brightness = v < 0 ? 0 : v > 5 ? 5 : v);
|
2011-10-01 16:34:28 +00:00
|
|
|
},
|
|
|
|
brighter: func {
|
|
|
|
me.leds = bits.set(me.leds, me._ledmap[0]);
|
2011-10-07 12:20:30 +00:00
|
|
|
me.set_brightness(me.brightness + 1);
|
2011-10-01 16:34:28 +00:00
|
|
|
},
|
|
|
|
darker: func {
|
|
|
|
me.leds = bits.set(me.leds, me._ledmap[0]);
|
2011-10-07 12:20:30 +00:00
|
|
|
me.set_brightness(me.brightness - 1);
|
2011-10-01 16:34:28 +00:00
|
|
|
},
|
|
|
|
_ledmap: {0: 3, 1: 2, 2: 1, 3: 4, 4: 0, 5: 6},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-10-07 12:20:30 +00:00
|
|
|
joystick.init();
|
|
|
|
throttle.init(); # read configuration
|
|
|
|
|
|
|
|
throttle.set_brightness(1); # LEDs dark (but on)
|
|
|
|
throttle.set_leds(1, 0); # backlight on
|
2011-10-02 20:19:24 +00:00
|
|
|
setlistener("/sim/signals/exit", func throttle.set_leds(0, 1, 2, 3, 4, 5), 1); # other LEDs off (now and at exit)
|