Debian Codegraph @

Nasal/modules/canvas_efis/efis.nas

raw
1 #-------------------------------------------------------------------------------
2 # efis.nas
3 # author: jsb
4 # created: 12/2017
5 #-------------------------------------------------------------------------------
6
7 # class EFIS
8 # manage cockpit displays (=outputs) and sources (image generators for PFD, MFD, EICAS...)
9 # allow redirection of sources to alternate displays (allow for simulated display fault)
10 var EFIS = {
11 #-- static members
12 _instances: [],
13 unload: func() {
14 foreach (var instance; EFIS._instances) {
15 instance.del();
16 }
17 EFIS._instances = [];
18 },
19 NO_SRC: -1,
20
21 defaultcanvas_settings: {
22 "name": "EFIS_display",
23 "size": [1024,1024],
24 "view": [1024,1024],
25 "mipmapping": 1
26 },
27
28 window_size: [450,450],
29
30 colors: canvas.colors,
31
32 del: func() {
33 },
34
35 # create EFIS object
36 # display_names: vector of display names, one DisplayUnit per entry will be
37 # created
38 # object_names: vector of same size and order as display_names, containing
39 # 3D object names for canvas placement of the DisplayUnits
40 new: func(display_names, object_names, canvas_settings=nil) {
41 if (!isvec(display_names)) {
42 logprint(DEV_ALERT, "EFIS.new: 'display_names' not a vector!");
43 return;
44 }
45 var obj = {
46 parents: [me],
47 id: 0,
48 display_units: [],
49 sources: [], # vector of EFISCanvas instances
50 display_names: display_names,
51 controls: {},
52 source_records: [], # stores infos about each source
53 active_sources: [],
54 powerN: nil,
55 minimum_power: 0,
56 isPowered: 0,
57 };
58 if (object_names != nil and isvec(object_names)
59 and size(display_names) == size(object_names))
60 {
61 foreach (var i; display_names) {
62 append(obj.active_sources, EFIS.NO_SRC);
63 }
64 var settings = obj.defaultcanvas_settings;
65 if (canvas_settings != nil and ishash(canvas_settings)) {
66 foreach (var key; keys(canvas_settings)) {
67 settings[key] = canvas_settings[key];
68 }
69 }
70 setsize(obj.display_units, size(display_names));
71 forindex (var id; display_names)
72 {
73 obj.display_units[id] = DisplayUnit.new(obj.display_names[id],
74 obj.defaultcanvas_settings, object_names[id]);
75 }
76 }
77 append(EFIS._instances, obj);
78 return obj;
79 }, #new
80
81 #-- private methods ----------------------
82
83 # _setDisplaySource - switch display unit du_id to source source_id
84 # count how often a source is displayed, sources not displayed stop updating themselves
85 _setDisplaySource: func(du_id, source_id)
86 {
87 var prev_source = me.active_sources[du_id];
88 #print("setDisplaySource unit "~du_id~" src "~source_id~" prev "~prev_source);
89 if (prev_source >= 0) {
90 if (me.source_records[prev_source] == nil)
91 logprint(LOG_ALERT, "_setDisplaySource error: prev: "~prev_source~" #"~size(me.source_records));
92 var n = me.source_records[prev_source].visibleN;
93 n.setValue(n.getValue() - 1);
94 }
95 var path = "";
96 if (source_id >= 0) {
97 path = me.sources[source_id].getPath();
98 }
99 me.display_units[du_id].setSource(path);
100 me.active_sources[du_id] = source_id;
101 var n = me.source_records[source_id].visibleN;
102 n.setValue(n.getValue() + 1);
103 },
104
105 # mapping can be either:
106 # - vector of source ids, size must equal size(display_units)
107 # values nil = do nothing, 0..N select source, -1 no source
108 # - hash {<unit_name>: source_id}
109 _activateRouting: func(mapping)
110 {
111 if (isvec(mapping)) {
112 forindex (var unit_id; me.display_units)
113 {
114 if (mapping[unit_id] != nil)
115 me._setDisplaySource(unit_id, mapping[unit_id]);
116 }
117 }
118 elsif (ishash(mapping)) {
119 foreach (var unit_name; keys(mapping))
120 {
121 forindex (var unit_id; me.display_names) {
122 if (me.display_names[unit_id] == unit_name) {
123 me._setDisplaySource(unit_id, mapping[unit_name]);
124 }
125 }
126 }
127 }
128 },
129
130 # Start/stop updates on all sources
131 _powerOnOff: func(power) {
132 logprint(LOG_DEBUG, "EFIS power: "~power~", "~me.isPowered);
133 if (power >= me.minimum_power and !me.isPowered) {
134 logprint(LOG_INFO, "EFIS power on");
135 me.isPowered = 1;
136 foreach (var src; me.sources) {
137 src.startUpdates();
138 }
139 }
140 else if (power < me.minimum_power) {
141 logprint(LOG_INFO, "EFIS power off");
142 me.isPowered = 0;
143 foreach (var src; me.sources) {
144 src.stopUpdates();
145 }
146 }
147 },
148
149 #-- public methods -----------------------
150 # set power prop and add listener to start/stop all registered update functions
151 # e.g. power up will start updates, loss of power will stop updates
152 setPowerProp: func(path, minimum_power = 1) {
153 me.powerN = props.getNode(path,1);
154 me.minimum_power = minimum_power;
155 logprint(LOG_INFO, "EFIS power switch "~me.powerN.getPath()~" "~me.powerN.getValue());
156 setlistener(me.powerN, func(n) {
157 me._powerOnOff(n.getValue());
158 }, 1, 0);
159 },
160
161 setWindowSize: func(window_size) {
162 if (window_size != nil and isvec(window_size)) {
163 me.window_size = window_size;
164 }
165 else {
166 logprint(DEV_ALERT, "EFIS.setWindowSize(): Error, argument is not a vector.");
167 }
168 },
169
170 boot: func() {
171 me._powerOnOff(me.powerN.getValue());
172 },
173
174 setDUPowerProps: func(power_props, minimum_power=0) {
175 if (power_props != nil and isvec(power_props)) {
176 forindex (var i; me.display_names) {
177 me.display_units[i].setPowerSource(power_props[i], minimum_power);
178 }
179 }
180 else logprint(DEV_ALERT, "EFIS.setDUPowerProps(): Error, argument is not a vector.");
181 },
182
183 # add a EFISCanvas instance as display source
184 # EFIS controls updating by tracking how often source is used
185 # returns source ID that can be used in mappings
186 addSource: func(efis_canvas) {
187 append(me.sources, efis_canvas);
188 var srcID = size(me.sources) - 1;
189 var visibleN = EFIS_root_node.getNode("update/visible"~srcID,1);
190 visibleN.setIntValue(0);
191 efis_canvas.setUpdateN(visibleN);
192 append(me.source_records, {visibleN: visibleN});
193 return srcID;
194 },
195
196 # ctrl: property path to integer prop
197 # mappings: vector of display mappings
198 # callback: optional function that will be called with current ctrl value
199 addDisplaySwapControl: func(ctrl, mappings, callback=nil)
200 {
201 if (me.controls[ctrl] != nil) return;
202 ctrlN = props.getNode(ctrl,1);
203 if (!isvec(mappings)) {
204 logprint(DEV_ALERT, "EFIS addDisplayControl: mappings must be a vector.");
205 return;
206 }
207 var listener = func(p) {
208 var ctlValue = p.getValue();
209 if (ctlValue >= 0 and ctlValue < size(me.controls[ctrl].mappings))
210 me._activateRouting(me.controls[ctrl].mappings[ctlValue]);
211 else debug.warn("Invalid value for display selector "~ctrl~": "~ctlValue);
212 if (callback != nil) {
213 call(callback, [ctlValue], nil, nil, var err = []);
214 debug.printerror(err);
215 }
216 }
217 #print("addDisplayControl "~ctrl);
218 me.controls[ctrl] = {L: setlistener(ctrlN, listener, 0, 0), mappings: mappings};
219 },
220
221 # selected: property (node or path) containing source number (integer)
222 # target: contains the DU number to which the source will be mapped
223 # sources: optional vector, selected -> source ID (as returned by addSource)
224 # defaults to all registered sources
225 addSourceSelector: func(selected, target, sources=nil){
226 if (isscalar(selected)) {
227 selected = props.getNode(selected,1);
228 }
229 if (isscalar(target)) {
230 target = props.getNode(target,1);
231 }
232 if (selected.getValue() == nil)
233 selected.setIntValue(0);
234 if (sources == nil) {
235 for (var i = 0; i < size(me.sources); i += 1)
236 append(sources, i);
237 }
238 setlistener(selected, func(n){
239 var src = n.getValue();
240 var destination = target.getValue();
241 if (src >= 0 and src < size(sources))
242 me._setDisplaySource(destination, sources[src]);
243 }, 0, 0);
244 },
245
246 setDefaultMapping: func(mapping) {
247 if (mapping != nil and (isvec(mapping) or ishash(mapping))) {
248 me.default_mapping = mapping;
249 me._activateRouting(me.default_mapping);
250 }
251 },
252
253 getDU: func(i) {return me.display_units[i]},
254
255 #getSources: func() { return me.source_records; },
256
257 getDisplayName: func(id) {
258 id = num(id);
259 if (id != nil and id >=0 and id < size(me.display_names))
260 return me.display_names[id];
261 else return "Invalid display ID.";
262 },
263
264 getDisplayID: func(name) {
265 for (var id = 0; id < size(me.display_names); id += 1) {
266 if (me.display_names[id] == name) return id;
267 }
268 return -1;
269 },
270
271 #open a canvas window for display unit <id>
272 displayWindow: func(id)
273 {
274 id = num(id);
275 if (id < 0 or id >= size(me.display_units))
276 {
277 debug.warn("EFIS.displayWindow: invalid id");
278 return;
279 }
280 return me.display_units[id].asWindow(me.window_size);
281 },
282 };