Debian Codegraph @

Input/Joysticks/ThrustMaster/Warthog/warthog.nas

raw
1 # Hardware Interface (currently Linux/HIDRAW-only)
2 # see README and http://members.aon.at/mfranz/warthog.html
3
4
5 if (io.stat("/dev/input") != nil and io.stat("/dev/input/hidraw") == nil) {
6 print("warthog: read file $FG_ROOT/Input/Joysticks/ThrustMaster/Warthog/README");
7 print(" for how to enable FlightGear to set backlight and LEDs etc.");
8 }
9
10
11 var device = {
12 new: func(path, bufsize) {
13 var m = { parents: [device] };
14 m.path = path;
15 m.bufsize = bufsize;
16 m.data = bits.buf(m.bufsize);
17 var stat = io.stat(m.path);
18 if (stat == nil or stat[11] != "chr")
19 m.send = m.receive = func {};
20 return m;
21 },
22 send: func {
23 var buf = bits.buf(me.bufsize);
24 forindex (var i; arg)
25 buf[i] = arg[i];
26 var file = io.open(me.path, "wb");
27 io.write(file, buf);
28 io.close(file);
29 },
30 receive: func {
31 var file = io.open(me.path, "rb");
32 io.read(file, me.data, me.bufsize);
33 io.close(file);
34 },
35 };
36
37
38 var joystick = {
39 parents: [device.new("/dev/input/hidraw/Thustmaster_Joystick_-_HOTAS_Warthog", 12)],
40 init: func {
41 me.receive();
42 },
43 };
44
45
46 var throttle = {
47 parents: [device.new("/dev/input/hidraw/Thrustmaster_Throttle_-_HOTAS_Warthog", 36)],
48 init: func {
49 me.receive();
50 me.leds = me.data[26];
51 me.brightness = me.data[27];
52 },
53 set_leds: func(state, which...) { # on/off, list of leds (0: background, 1-5)
54 var leds = me.leds;
55 foreach (var w; which)
56 me.leds = bits.switch(me.leds, me._ledmap[w], state);
57 if (me.leds != leds)
58 me.send(1, 6, me.leds, me.brightness);
59 },
60 toggle_leds: func(which...) {
61 foreach (var w; which)
62 me.leds = bits.toggle(me.leds, me._ledmap[w]);
63 me.send(1, 6, me.leds, me.brightness);
64 },
65 set_brightness: func(v) { # clamped to [0,5], where 0 is off and 5 is bright
66 if (v != me.brightness)
67 me.send(1, 6, me.leds, me.brightness = v < 0 ? 0 : v > 5 ? 5 : v);
68 },
69 brighter: func {
70 me.leds = bits.set(me.leds, me._ledmap[0]);
71 me.set_brightness(me.brightness + 1);
72 },
73 darker: func {
74 me.leds = bits.set(me.leds, me._ledmap[0]);
75 me.set_brightness(me.brightness - 1);
76 },
77 _ledmap: {0: 3, 1: 2, 2: 1, 3: 4, 4: 0, 5: 6},
78 };
79
80
81 joystick.init();
82 throttle.init(); # read configuration
83
84 throttle.set_brightness(1); # LEDs dark (but on)
85 throttle.set_leds(1, 0); # backlight on
86 setlistener("/sim/signals/exit", func throttle.set_leds(0, 1, 2, 3, 4, 5), 1); # other LEDs off (now and at exit)