Integrates existing soaring instruments with the soaring sdk
[fg:toms-fgdata.git] / Aircraft / Instruments-3d / glider / vario / ilec-sc7 / ilec-sc7.nas
1 io.include("Aircraft/Generic/soaring-instrumentation-sdk.nas");
2
3 # Initialize exported properties
4 setprop("/instrumentation/ilec-sc7/volume", 0.8);
5 setprop("/instrumentation/ilec-sc7/audio", 2);
6 setprop("/instrumentation/ilec-sc7/mode", 1);
7 setprop("/instrumentation/ilec-sc7/sensitivity", 3);
8 setprop("/instrumentation/ilec-sc7/lcd-digits-abs", 0);
9 setprop("/instrumentation/ilec-sc7/lcd-digits-sgn", 0);
10 setprop("/instrumentation/ilec-sc7/te-reading-mps", 0);
11 setprop("/instrumentation/variometer/te-reading-mps", 0);
12
13 # Helper function for updating lcd display
14 var update_lcd_props = func(value) {
15         setprop("/instrumentation/ilec-sc7/lcd-digits-abs", math.abs(value));
16         setprop("/instrumentation/ilec-sc7/lcd-digits-sgn", (value < 0) ? 0 : 1);
17 };
18
19 # Instrument setup:
20
21 # One TE probe feeds two vario needles and a 25s averager.
22 # LCD digits are controlled by the.. um.. lcd_controller
23 # that switches between battery level, temperature and averager
24 # depending on mode switch posiion.
25
26 # Why a second needle? A digital vario is usually installed together
27 # with a mechanical one, so now we are at it, why not provide a bonus
28 # TE reading for it and avoid loading an extra script?
29
30 var probe = TotalEnergyProbe.new();
31
32 var sc7_needle = Dampener.new(
33         input: probe,
34         dampening: 3,
35         on_update: update_prop("/instrumentation/ilec-sc7/te-reading-mps"));
36
37 var extra_needle = Dampener.new(
38         input: probe,
39         dampening: 2.7,
40         on_update: update_prop("/instrumentation/variometer/te-reading-mps"));
41
42 var averager = Averager.new(
43         input: probe,
44         buffer_size: 25);
45
46 var battery_level = { output: 9.9 };
47
48 var temperature = PropertyReader.new(
49         property: "environment/temperature-degc",
50         scale: 0.1);
51
52 var lcd_controller = InputSwitcher.new(
53         inputs: [battery_level, averager, temperature],
54         active_input: 1,
55         on_update: update_lcd_props);
56
57 # Subscribe property listeners for instrument switches
58 setlistener("instrumentation/ilec-sc7/mode",
59         func(n) { lcd_controller.select_input(n.getValue()) }, 0, 0);
60
61 setlistener("instrumentation/ilec-sc7/sensitivity",
62         func(n) { sc7_needle.dampening = n.getValue() }, 0, 0);
63
64 # Wrap everything together into an instrument
65 var fast_instruments = Instrument.new(
66         update_period: 0,
67         components: [probe, sc7_needle, extra_needle],
68         enable: 1);
69
70 var slow_instruments = Instrument.new(
71         update_period: 1,
72         components: [averager, temperature, lcd_controller],
73         enable: 1);