Debian Codegraph @

Aircraft/Instruments-3d/FG1000/Nasal/MFDPages/Surround/SurroundController.nas

raw
1 # Copyright 2018 Stuart Buchanan
2 # This file is part of FlightGear.
3 #
4 # FlightGear is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # FlightGear is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with FlightGear. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Surround Controller
18 var SurroundController =
19 {
20 new : func (page, svg, pfd)
21 {
22 var obj = {
23 parents : [ SurroundController ],
24 _recipient : nil,
25 _page : page,
26 _pfd : pfd,
27 _comselected : 1,
28 _navselected : 1,
29 _com1active : 0.0,
30 _com1standby : 0.0,
31 _com2active : 0.0,
32 _com2standby : 0.0,
33 _commvolume : 0.0,
34 _nav1active : 0.0,
35 _nav1standby : 0.0,
36 _nav1radial : 0.0,
37 _nav1_heading_deg : 0.0,
38 _nav2active : 0.0,
39 _nav2standby : 0.0,
40 _nav2radial : 0.0,
41 _nav2_heading_deg : 0.0,
42 _navvolume : 0.0,
43 _pressure_settings_inhg : 0.0,
44 _selected_alt_ft : 0.0,
45 _heading_bug_deg : 0.0,
46 _heading_deg : 0.0,
47 };
48
49 obj.RegisterWithEmesary();
50 return obj;
51 },
52
53 del : func() {
54 me.DeRegisterWithEmesary();
55 },
56
57 # Helper function to notify the Emesary bridge of a NavComData update.
58 sendNavComDataNotification : func(data) {
59 var notification = notifications.PFDEventNotification.new(
60 "MFD",
61 me._page.mfd.getDeviceID(),
62 notifications.PFDEventNotification.NavComData,
63 data);
64
65 me.transmitter.NotifyAll(notification);
66 },
67
68 # Helper function to notify the Emesary bridge of a FMSData update.
69 sendFMSDataNotification : func(data) {
70 var notification = notifications.PFDEventNotification.new(
71 "MFD",
72 me._page.mfd.getDeviceID(),
73 notifications.PFDEventNotification.FMSData,
74 data);
75
76 me.transmitter.NotifyAll(notification);
77 },
78
79 handleNavComData : func(data) {
80
81 # Store off particularly important data for control.
82 if (data["CommSelected"] != nil) me._comselected = data["CommSelected"];
83 if (data["NavSelected"] != nil) me._navselected = data["NavSelected"];
84
85 if (data["Comm1SelectedFreq"] != nil) me._com1active = data["Comm1SelectedFreq"];
86 if (data["Comm1StandbyFreq"] != nil) me._com1standby = data["Comm1StandbyFreq"];
87 if (data["Comm1Volume"] != nil and me._commvolume != data["Comm1Volume"]) me._commvolume = data["Comm1Volume"];
88 if (data["Comm2SelectedFreq"] != nil) me._com2active = data["Comm2SelectedFreq"];
89 if (data["Comm2StandbyFreq"] != nil) me._com2standby = data["Comm2StandbyFreq"];
90 if (data["Comm2Volume"] != nil and me._commvolume != data["Comm2Volume"]) me._commvolume = data["Comm2Volume"];
91
92 if (data["Nav1SelectedFreq"] != nil) me._nav1active = data["Nav1SelectedFreq"];
93 if (data["Nav1StandbyFreq"] != nil) me._nav1standby = data["Nav1StandbyFreq"];
94 if (data["Nav1RadialDeg"] != nil) me._nav1radial = data["Nav1RadialDeg"];
95 if (data["Nav1HeadingDeg"] != nil) me._nav1_heading_deg = data["Nav1HeadingDeg"];
96 if (data["Nav1Volume"] != nil and me._navvolume != data["Nav1Volume"]) me._navvolume = data["Nav1Volume"];
97
98 if (data["Nav2SelectedFreq"] != nil) me._nav2active = data["Nav2SelectedFreq"];
99 if (data["Nav2StandbyFreq"] != nil) me._nav2standby = data["Nav2StandbyFreq"];
100 if (data["Nav2RadialDeg"] != nil) me._nav2radial = data["Nav2RadialDeg"];
101 if (data["Nav2HeadingDeg"] != nil) me._nav2_heading_deg = data["Nav2HeadingDeg"];
102 if (data["Nav2Volume"] != nil and me._navvolume != data["Nav2Volume"]) me._navvolume = data["Nav2Volume"];
103
104
105 # pass through to the page
106 me._page.handleNavComData(data);
107 return emesary.Transmitter.ReceiptStatus_OK;
108 },
109
110 handleFMSADCData : func(data) {
111 if (data["ADCPressureSettingInHG"] != nil) me._pressure_settings_inhg = data["ADCPressureSettingInHG"];
112 if (data["FMSSelectedAlt"] != nil) me._selected_alt_ft = data["FMSSelectedAlt"];
113 if (data["FMSHeadingBug"] != nil) me._heading_bug_deg = data["FMSHeadingBug"];
114 if (data["ADCHeadingMagneticDeg"] != nil) me._heading_deg = data["ADCHeadingMagneticDeg"];
115
116 # Pass FMS and ADC data straight to the page to display in the header fields
117 me._page.updateHeaderData(data);
118 return emesary.Transmitter.ReceiptStatus_OK;
119 },
120
121 handleSelectPageByID : func(notification) {
122 me._page.goToPage(notification.Group, notification.Page);
123 return emesary.Transmitter.ReceiptStatus_Finished;
124 },
125
126 #
127 # Handle the various COM and NAV controls at the top left and top right of the Fascia
128 #
129 handleNavVol : func (value) {
130 var data={};
131 var incr_or_decr = (value > 0) ? 0.01 : -0.01;
132 var vol = math.max(0,math.min(1,me._navvolume + incr_or_decr));
133 data["Nav1Volume"] = vol;
134 data["Nav2Volume"] = vol;
135 me.sendNavComDataNotification(data);
136 return emesary.Transmitter.ReceiptStatus_Finished;
137 },
138
139 handleNavID : func (value) {
140 var data={};
141
142 if (me._navselected == 1) {
143 data["Nav1AudioID"] = value;
144 } else {
145 data["Nav1AudioID"] = value;
146 }
147
148 me.sendNavComDataNotification(data);
149 return emesary.Transmitter.ReceiptStatus_Finished;
150 },
151
152 # Swap active and standby Nav frequencies. Note that we don't update internal state here - we
153 # instead pass updated NavComData notification data which will be picked up by the underlying
154 # updaters to map to properties, and this controller itself.
155 handleNavFreqTransfer : func (value)
156 {
157 var data={};
158
159 if (me._navselected == 1) {
160 data["Nav1SelectedFreq"] = me._nav1standby;
161 data["Nav1StandbyFreq"] = me._nav1active;
162 } else {
163 data["Nav2SelectedFreq"] = me._nav2standby;
164 data["Nav2StandbyFreq"] = me._nav2active;
165 }
166
167 me.sendNavComDataNotification(data);
168 return emesary.Transmitter.ReceiptStatus_Finished;
169 },
170
171 # Outer Nav dial updates the integer portion of the selected standby
172 # NAV frequency only, and wrap on limits - leaving the fractional part unchanged.
173 handleNavOuter : func (value) {
174 var incr_or_decr = (value > 0) ? 1000.0 : -1000.0;
175 var data={};
176
177 # Determine the new value, wrapping within the limits.
178 var datakey = "";
179 var freq = 0;
180 var old_freq = 0;
181
182 if (me._navselected == 1) {
183 datakey = "Nav1StandbyFreq";
184 old_freq = me._nav1standby;
185 } else {
186 datakey = "Nav2StandbyFreq";
187 old_freq = me._nav2standby;
188 }
189
190 old_freq = math.round(old_freq * 1000);
191 freq = old_freq + incr_or_decr;
192
193 # Wrap if out of bounds
194 if (freq > (fg1000.MAX_NAV_FREQ * 1000)) freq = freq - (fg1000.MAX_NAV_FREQ - fg1000.MIN_NAV_FREQ) * 1000;
195 if (freq < (fg1000.MIN_NAV_FREQ * 1000)) freq = freq + (fg1000.MAX_NAV_FREQ - fg1000.MIN_NAV_FREQ) * 1000;
196
197 # Convert back to a frequency to 3 decimal places.
198 data[datakey] = sprintf("%.3f", freq/1000.0);
199 me.sendNavComDataNotification(data);
200 return emesary.Transmitter.ReceiptStatus_Finished;
201 },
202
203 # Inner Nav dial updates the fractional portion of the selected standby
204 # NAV frequency only - leaving the integer part unchanged. Even if it
205 # increments past 0.975.
206 handleNavInner : func (value) {
207 var incr_or_decr = (value > 0) ? 25 : -25;
208 var data={};
209
210 # Determine the new value, wrapping within the limits.
211 var datakey = "";
212 var freq = 0;
213 var old_freq = 0;
214 if (me._navselected == 1) {
215 datakey = "Nav1StandbyFreq";
216 old_freq = me._nav1standby;
217 } else {
218 datakey = "Nav2StandbyFreq";
219 old_freq = me._nav2standby;
220 }
221
222 old_freq = math.round(old_freq * 1000);
223 freq = old_freq + incr_or_decr;
224
225 # Wrap on decimal by handling case where the integer part has changed
226 if (int(old_freq/1000) < int(freq/1000)) freq = freq - 1000;
227 if (int(old_freq/1000) > int(freq/1000)) freq = freq + 1000;
228
229 data[datakey] = sprintf("%.3f", freq/1000.0);
230 me.sendNavComDataNotification(data);
231 return emesary.Transmitter.ReceiptStatus_Finished;
232 },
233
234 # Switch between Nav1 and Nav2.
235 handleNavToggle : func ()
236 {
237 var data={};
238
239 if (me._navselected == 1) {
240 data["NavSelected"] = 2;
241 } else {
242 data["NavSelected"] = 1;
243 }
244
245 me.sendNavComDataNotification(data);
246 return emesary.Transmitter.ReceiptStatus_Finished;
247 },
248
249 setNav : func(value) {
250 var data={};
251 data["NavSelected"] = value;
252 me.sendNavComDataNotification(data);
253 return emesary.Transmitter.ReceiptStatus_Finished;
254 },
255
256 # Outer COM dial changes the integer value of the standby selected COM
257 # frequency, wrapping on limits. Leaves the fractional part unchanged
258 handleComOuter : func (value) {
259 var incr_or_decr = (value > 0) ? 1000.0 : -1000.0;
260 var data={};
261
262 # Determine the new value, wrapping within the limits.
263 var datakey = "";
264 var freq = 0;
265 var old_freq = 0;
266
267 if (me._comselected == 1) {
268 datakey = "Comm1StandbyFreq";
269 old_freq = me._com1standby;
270 } else {
271 datakey = "Comm2StandbyFreq";
272 old_freq = me._com2standby;
273 }
274
275 old_freq = math.round(old_freq * 1000);
276 freq = old_freq + incr_or_decr;
277
278 # Wrap if out of bounds
279 if (freq > (fg1000.MAX_COM_FREQ * 1000)) freq = freq - (fg1000.MAX_COM_FREQ - fg1000.MIN_COM_FREQ) * 1000;
280 if (freq < (fg1000.MIN_COM_FREQ * 1000)) freq = freq + (fg1000.MAX_COM_FREQ - fg1000.MIN_COM_FREQ) * 1000;
281
282 # Convert back to a frequency to 3 decimal places.
283 data[datakey] = sprintf("%.3f", freq/1000.0);
284 me.sendNavComDataNotification(data);
285 return emesary.Transmitter.ReceiptStatus_Finished;
286 },
287
288 # Inner COM dial changes the fractional part of the standby selected COM frequency,
289 # wrapping on limits and leaving the integer part unchanged.
290 handleComInner : func (value) {
291 var incr_or_decr = (value > 0) ? 1 : -1;
292 var data={};
293
294 var datakey = "";
295 var freq = 0;
296 var old_freq = 0;
297 if (me._comselected == 1) {
298 datakey = "Comm1StandbyFreq";
299 old_freq = me._com1standby;
300 } else {
301 datakey = "Comm2StandbyFreq";
302 old_freq = me._com2standby;
303 }
304
305 old_freq = math.round(old_freq * 1000);
306 var integer_part = int(old_freq / 1000) * 1000;
307 var fractional_part = old_freq - integer_part;
308
309 # 8.33kHz frequencies are complicated - we need to do a lookup to find
310 # the current and next frequencies
311 var idx = 0;
312 for (var i=0; i < size(fg1000.COM_833_SPACING); i = i + 1) {
313 if (math.round(fg1000.COM_833_SPACING[i] * 1000) == fractional_part) {
314 idx = i;
315 break;
316 }
317 }
318
319 idx = math.mod(idx + incr_or_decr, size(fg1000.COM_833_SPACING));
320 freq = integer_part + fg1000.COM_833_SPACING[idx] * 1000;
321 data[datakey] = sprintf("%.3f", freq/1000.0);
322 me.sendNavComDataNotification(data);
323 return emesary.Transmitter.ReceiptStatus_Finished;
324 },
325
326 # Switch between COM1 and COM2
327 handleComToggle : func (value) {
328 var data={};
329
330 if (me._comselected == 1) {
331 data["CommSelected"] = 2;
332 } else {
333 data["CommSelected"] = 1;
334 }
335
336 me.sendNavComDataNotification(data);
337 return emesary.Transmitter.ReceiptStatus_Finished;
338 },
339
340 # Swap active and standby Com frequencies. Note that we don't update internal state here - we
341 # instead pass updated NavComData notification data which will be picked up by the underlying
342 # updaters to map to properties, and this controller itself.
343 handleComFreqTransfer : func (value) {
344 var data={};
345
346 if (me._comselected == 1) {
347 data["Comm1SelectedFreq"] = me._com1standby;
348 data["Comm1StandbyFreq"] = me._com1active;
349 } else {
350 data["Comm2SelectedFreq"] = me._com2standby;
351 data["Comm2StandbyFreq"] = me._com2active;
352 }
353
354 me.sendNavComDataNotification(data);
355 return emesary.Transmitter.ReceiptStatus_Finished;
356 },
357
358 # Auto-tunes the ACTIVE COM channel to 121.2 when pressed for 2 seconds
359 handleComFreqTransferHold : func (value) {
360 var data={};
361
362 if (me._comselected == 1) {
363 data["Comm1SelectedFreq"] = 121.00;
364 } else {
365 data["Comm2SelectedFreq"] = 121.00;
366 }
367
368 me.sendNavComDataNotification(data);
369 return emesary.Transmitter.ReceiptStatus_Finished;
370 },
371
372 handleComVol : func (value) {
373 var data={};
374 var incr_or_decr = (value > 0) ? 0.01 : -0.01;
375 var vol = math.max(0,math.min(1,me._commvolume + incr_or_decr));
376 data["Comm1Volume"] = vol;
377 data["Comm2Volume"] = vol;
378 me.sendNavComDataNotification(data);
379 return emesary.Transmitter.ReceiptStatus_Finished;
380 },
381
382 handleComVolToggle : func (value) {
383 },
384
385 handleBaro : func(value) {
386 var incr_or_decr = (value > 0) ? 1 : -1;
387 var press = me._pressure_settings_inhg + (incr_or_decr * 0.01);
388 var data = {};
389 data["FMSPressureSettingInHG"] = sprintf("%.2f", press);
390 me.sendFMSDataNotification(data);
391 return emesary.Transmitter.ReceiptStatus_Finished;
392 },
393
394 handleCRS : func(value) {
395 var incr_or_decr = (value > 0) ? 1 : -1;
396 var data={};
397
398 if (me._navselected == 1) {
399 data["Nav1RadialDeg"] = math.mod(me._nav1radial + incr_or_decr, 360);
400 } else {
401 data["Nav2RadialDeg"] = math.mod(me._nav2radial + incr_or_decr, 360);
402 }
403
404 me.sendNavComDataNotification(data);
405 return emesary.Transmitter.ReceiptStatus_Finished;
406 },
407
408 handleCRSCenter : func(value) {
409 var data = {};
410 if (me._navselected == 1) {
411 data["Nav1RadialDeg"] = me._nav1_heading_deg;
412 } else {
413 data["Nav2RadialDeg"] = me._nav2_heading_deg;
414 }
415
416 me.sendNavComDataNotification(data);
417 return emesary.Transmitter.ReceiptStatus_Finished;
418 },
419
420 handleAltInner : func(value) {
421 var incr_or_decr = (value > 0) ? 1 : -1;
422 var alt = int(me._selected_alt_ft + incr_or_decr * 100);
423 if (alt < 0) alt = 0;
424 var data = {};
425 data["FMSSelectedAlt"] = alt;
426 me.sendFMSDataNotification(data);
427 return emesary.Transmitter.ReceiptStatus_Finished;
428 },
429
430 handleAltOuter : func(value) {
431 var incr_or_decr = (value > 0) ? 1 : -1;
432 var alt = int(me._selected_alt_ft + incr_or_decr * 1000);
433 if (alt < 0) alt = 0;
434 var data = {};
435 data["FMSSelectedAlt"] = alt;
436 me.sendFMSDataNotification(data);
437 return emesary.Transmitter.ReceiptStatus_Finished;
438 },
439
440 handleHeading : func(value) {
441 var incr_or_decr = (value > 0) ? 1 : -1;
442 var hdg = me._heading_bug_deg + incr_or_decr;
443 hdg = math.mod(hdg, 360);
444 var data = {};
445 data["FMSHeadingBug"] = sprintf("%i", hdg);
446 me.sendFMSDataNotification(data);
447 return emesary.Transmitter.ReceiptStatus_Finished;
448 },
449
450 handleHeadingPress : func(value) {
451 var data = {};
452 data["FMSHeadingBug"] = me._heading_deg;
453 me.sendFMSDataNotification(data);
454 return emesary.Transmitter.ReceiptStatus_Finished;
455 },
456
457 # These methods are slightly unusual in that they are called by other
458 # controllers when the CRSR is not active. Hence they aren't referenced
459 # in the RegisterWithEmesary call below.
460 #
461 handleFMSOuter : func(val)
462 {
463 if (me._pfd) return emesary.Transmitter.ReceiptStatus_NotProcessed;
464 if (me._page.isMenuVisible()) {
465 # Change page group
466 me._page.incrPageGroup(val);
467 }
468 me._page.showMenu();
469 return emesary.Transmitter.ReceiptStatus_Finished;
470 },
471
472 handleFMSInner : func(val)
473 {
474 if (me._pfd) return emesary.Transmitter.ReceiptStatus_NotProcessed;
475 if (me._page.isMenuVisible()) {
476 # Change page group
477 me._page.incrPage(val);
478 }
479 me._page.showMenu();
480 return emesary.Transmitter.ReceiptStatus_Finished;
481 },
482
483 RegisterWithEmesary : func(transmitter = nil){
484 if (transmitter == nil)
485 transmitter = emesary.GlobalTransmitter;
486
487 if (me._recipient == nil){
488 me._recipient = emesary.Recipient.new("SurroundController_" ~ me._page.device.designation);
489 var pfd_obj = me._page.device;
490 var controller = me;
491 me._recipient.Receive = func(notification)
492 {
493 # Note that in general we don't care about the device that the data comes from.
494 if (notification.NotificationType == notifications.PFDEventNotification.DefaultType) {
495
496 if (notification.Event_Id == notifications.PFDEventNotification.NavComData
497 and notification.EventParameter != nil)
498 {
499 return controller.handleNavComData(notification.EventParameter);
500 }
501
502 if (((notification.Event_Id == notifications.PFDEventNotification.FMSData) or
503 (notification.Event_Id == notifications.PFDEventNotification.ADCData) )
504 and notification.EventParameter != nil)
505 {
506 return controller.handleFMSADCData(notification.EventParameter);
507 }
508
509 if (notification.Device_Id == pfd_obj.device_id and
510 notification.Event_Id == notifications.PFDEventNotification.SelectPageById)
511 {
512 return controller.handleSelectPageByID(notification.EventParameter);
513 }
514 }
515 return emesary.Transmitter.ReceiptStatus_NotProcessed;
516 };
517 }
518 transmitter.Register(me._recipient);
519 me.transmitter = transmitter;
520 },
521 DeRegisterWithEmesary : func(transmitter = nil){
522 # remove registration from transmitter; but keep the recipient once it is created.
523 if (me.transmitter != nil)
524 me.transmitter.DeRegister(me._recipient);
525 me.transmitter = nil;
526 },
527
528 # Used by other pages to set the current standby NAV or COM frequency by pressing ENT
529 setStandbyNavComFreq : func(value) {
530 var data={};
531
532 # Determine whether this is NAV or COM based on the frequency itself
533
534 if (value < fg1000.MAX_NAV_FREQ) {
535 # Nav frequency
536 if (value > fg1000.MAX_NAV_FREQ) return;
537 if (value < fg1000.MIN_NAV_FREQ) return;
538
539 # TODO: If we're in approach phase then this should update the Active
540 # frequency
541 if (me._navselected == 1) {
542 data["Nav1StandbyFreq"] = value;
543 } else {
544 data["Nav2StandbyFreq"] = value;
545 }
546 } else {
547 # COM frequency
548 if (value > fg1000.MAX_COM_FREQ) return;
549 if (value < fg1000.MIN_COM_FREQ) return;
550
551 if (me._comselected == 1) {
552 data["Comm1StandbyFreq"] = value;
553 } else {
554 data["Comm2StandbyFreq"] = value;
555 }
556 }
557
558 me.sendNavComDataNotification(data);
559 },
560
561 # Used by other pages to set the current standby NAV frequency by pressing ENT
562 setStandbyNavFreq : func(value) {
563 var data={};
564
565 if (value > fg1000.MAX_NAV_FREQ) return;
566 if (value < fg1000.MIN_NAV_FREQ) return;
567
568 if (me._navselected == 1) {
569 data["Nav1StandbyFreq"] = value;
570 } else {
571 data["Nav2StandbyFreq"] = value;
572 }
573
574 me.sendNavComDataNotification(data);
575 },
576 };