FlightGear next
ATC-Outputs.cxx
Go to the documentation of this file.
1// ATC-Outputs.hxx -- Translate FGFS properties to ATC hardware outputs.
2//
3// Written by Curtis Olson, started November 2004.
4//
5// Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License as
9// published by the Free Software Foundation; either version 2 of the
10// License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful, but
13// WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15// General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20//
21// $Id$
22
23// TODO FIXME Module still contains lots of "static SGPropertyNode"s below.
24
25#include <config.h>
26
27#include <simgear/compiler.h>
28
29#if defined( unix ) || defined( __CYGWIN__ )
30# include <sys/types.h>
31# include <sys/stat.h>
32# include <fcntl.h>
33# include <stdlib.h>
34# include <unistd.h>
35# include <ostream>
36#endif
37
38#include <errno.h>
39#include <cmath>
40#include <cstdio>
41
42#include <chrono>
43#include <string>
44#include <thread>
45
46#include <simgear/debug/logstream.hxx>
47#include <simgear/misc/sg_path.hxx>
48#include <simgear/props/props_io.hxx>
49
50#include <Main/fg_props.hxx>
51
52#include "ATC-Outputs.hxx"
53
54
55using std::string;
56
57
58// Lock the ATC hardware
59static int ATCLock( int fd ) {
60#if defined( unix ) || defined( __CYGWIN__ )
61 // rewind
62 lseek( fd, 0, SEEK_SET );
63
64 char tmp[2];
65 int result = read( fd, tmp, 1 );
66 if ( result != 1 ) {
67 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
68 }
69
70 return result;
71#else
72 return -1;
73#endif
74}
75
76
77// Release the ATC hardware
78static int ATCRelease( int fd ) {
79#if defined( unix ) || defined( __CYGWIN__ )
80 // rewind
81 lseek( fd, 0, SEEK_SET );
82
83 char tmp[2];
84 tmp[0] = tmp[1] = 0;
85 int result = write( fd, tmp, 1 );
86
87 if ( result != 1 ) {
88 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
89 }
90
91 return result;
92#else
93 return -1;
94#endif
95}
96
97
98// Constructor: The _board parameter specifies which board to
99// reference. Possible values are 0 or 1. The _config_file parameter
100// specifies the location of the output config file (xml)
101FGATCOutput::FGATCOutput( const int _board, const SGPath &_config_file ) :
102 is_open(false),
103 analog_out_node(NULL),
104 lamps_out_node(NULL),
105 radio_display_node(NULL),
106 steppers_node(NULL)
107{
108 board = _board;
109 config = _config_file;
110}
111
112
113// Write analog out data
114static int ATCSetAnalogOut( int fd,
115 unsigned char data[ATC_ANALOG_OUT_CHANNELS*2] )
116{
117#if defined( unix ) || defined( __CYGWIN__ )
118 // rewind
119 lseek( fd, 0, SEEK_SET );
120
121 int result = write( fd, data, ATC_ANALOG_OUT_CHANNELS*2 );
122
123 if ( result != ATC_ANALOG_OUT_CHANNELS*2 ) {
124 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
125 }
126
127 return result;
128#else
129 return -1;
130#endif
131}
132
133
134// Write a radios command
135static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
136#if defined( unix ) || defined( __CYGWIN__ )
137 // rewind
138 lseek( fd, 0, SEEK_SET );
139
140 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
141
142 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
143 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
144 }
145
146 return result;
147#else
148 return -1;
149#endif
150}
151
152
153// Write a stepper command
154static int ATCSetStepper( int fd, unsigned char channel,
155 unsigned char value )
156{
157#if defined( unix ) || defined( __CYGWIN__ )
158 // rewind
159 lseek( fd, 0, SEEK_SET );
160
161 // Write the value
162 unsigned char buf[3];
163 buf[0] = channel;
164 buf[1] = value;
165 buf[2] = 0;
166 int result = write( fd, buf, 2 );
167 if ( result != 2 ) {
168 SG_LOG( SG_IO, SG_INFO, "Write failed" );
169 }
170 SG_LOG( SG_IO, SG_DEBUG,
171 "Sent cmd = " << (int)channel << " value = " << (int)value );
172 return result;
173#else
174 return -1;
175#endif
176}
177
178
179#ifdef ATCFLIGHTSIM_HAVE_COMPASS
180// Read status of last stepper written to
181static unsigned char ATCReadStepper( int fd ) {
182#if defined( unix ) || defined( __CYGWIN__ )
183 int result;
184
185 // rewind
186 lseek( fd, 0, SEEK_SET );
187
188 // Write the value
189 unsigned char buf[2];
190 result = read( fd, buf, 1 );
191 if ( result != 1 ) {
192 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
193 exit( -1 );
194 }
195 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
196
197 return buf[0];
198#else
199 return 0;
200#endif
201}
202#endif
203
204
205// Turn a lamp on or off
206void ATCSetLamp( int fd, int channel, bool value ) {
207#if defined( unix ) || defined( __CYGWIN__ )
208 // lamp channels 0-63 are written to LampPort0, channels 64-127
209 // are written to LampPort1
210
211 // bits 0-6 are the lamp address
212 // bit 7 is the value (on/off)
213
214 int result;
215
216 // Write the value
217 unsigned char buf[3];
218 buf[0] = channel;
219 buf[1] = value;
220 buf[2] = 0;
221 result = write( fd, buf, 2 );
222 if ( result != 2 ) {
223 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
224 exit( -1 );
225 }
226#endif
227}
228
229
230void FGATCOutput::init_config() {
231#if defined( unix ) || defined( __CYGWIN__ )
232 if (!config.isAbsolute()) {
233 SGPath tmp = SGPath::home();
234 tmp.append( ".atcflightsim" );
235 tmp.append( config.utf8Str() );
236 config = tmp;
237 }
238
239 readProperties( config, globals->get_props() );
240#endif
241}
242
243
244// Open and initialize the ATC hardware
245bool FGATCOutput::open( int lock_fd ) {
246 if ( is_open ) {
247 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
248 << board );
249 return false;
250 }
251
252 // This loads the config parameters generated by "simcal"
253 init_config();
254
255 SG_LOG( SG_IO, SG_ALERT,
256 "Initializing ATC output hardware, please wait ..." );
257
258 snprintf( analog_out_file, 256,
259 "/proc/atcflightsim/board%d/analog_out", board );
260 snprintf( lamps_file, 256,
261 "/proc/atcflightsim/board%d/lamps", board );
262 snprintf( radio_display_file, 256,
263 "/proc/atcflightsim/board%d/radios", board );
264 snprintf( stepper_file, 256,
265 "/proc/atcflightsim/board%d/steppers", board );
266
267#if defined( unix ) || defined( __CYGWIN__ )
268
270 // Open the /proc files
272
273 analog_out_fd = ::open( analog_out_file, O_WRONLY );
274 if ( analog_out_fd == -1 ) {
275 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
276 char msg[300];
277 snprintf( msg, 300, "Error opening %s", analog_out_file );
278 perror( msg );
279 exit( -1 );
280 }
281
282 lamps_fd = ::open( lamps_file, O_WRONLY );
283 if ( lamps_fd == -1 ) {
284 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
285 char msg[300];
286 snprintf( msg, 300, "Error opening %s", lamps_file );
287 perror( msg );
288 exit( -1 );
289 }
290
291 radio_display_fd = ::open( radio_display_file, O_RDWR );
292 if ( radio_display_fd == -1 ) {
293 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
294 char msg[300];
295 snprintf( msg, 300, "Error opening %s", radio_display_file );
296 perror( msg );
297 exit( -1 );
298 }
299
300 stepper_fd = ::open( stepper_file, O_RDWR );
301 if ( stepper_fd == -1 ) {
302 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
303 char msg[300];
304 snprintf( msg, 300, "Error opening %s", stepper_file );
305 perror( msg );
306 exit( -1 );
307 }
308
309#endif
310
311#ifdef ATCFLIGHTSIM_HAVE_COMPASS
313 // Home the compass stepper motor
315
316 SG_LOG( SG_IO, SG_ALERT,
317 " - Homing the compass stepper motor" );
318
319 // Lock the hardware, keep trying until we succeed
320 while ( ATCLock( lock_fd ) <= 0 );
321
322 // Send the stepper home command
324
325 // Release the hardware
326 ATCRelease( lock_fd );
327
328 SG_LOG( SG_IO, SG_ALERT,
329 " - Waiting for compass to come home." );
330
331 bool home = false;
332 int timeout = 900; // about 30 seconds
333 timeout = 0;
334 while ( ! home && timeout > 0 ) {
335 if ( timeout % 150 == 0 ) {
336 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
337 } else {
338 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
339 }
340
341 while ( ATCLock( lock_fd ) <= 0 );
342
343 unsigned char result = ATCReadStepper( stepper_fd );
344 if ( result == 0 ) {
345 home = true;
346 }
347
348 ATCRelease( lock_fd );
349
350 {
351 using namespace std::chrono_literals;
352 std::this_thread::sleep_for(std::chrono::milliseconds(33ms));
353 }
354
355 --timeout;
356 }
357
358 compass_position = 0.0;
359#endif
360
361 // Lock the hardware, keep trying until we succeed
362 while ( ATCLock( lock_fd ) <= 0 );
363
365 // Zero the analog outputs
367
368 SG_LOG( SG_IO, SG_ALERT,
369 " - Zeroing Analog Outputs." );
370
371 for ( int channel = 0; channel < ATC_ANALOG_OUT_CHANNELS; ++channel ) {
372 analog_out_data[2*channel] = 0;
373 analog_out_data[2*channel + 1] = 0;
374 }
375 ATCSetAnalogOut( analog_out_fd, analog_out_data );
376
377
379 // Blank the radio display
381
382 SG_LOG( SG_IO, SG_ALERT,
383 " - Clearing the radios displays." );
384
385 // Prepair the data
386 unsigned char value = 0xff;
387 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
388 radio_display_data[channel] = value;
389 }
390 ATCSetRadios( radio_display_fd, radio_display_data );
391
392 ATCRelease( lock_fd );
393
395 // Blank the lamps
397
398 for ( int i = 0; i < 128; ++i ) {
399 ATCSetLamp( lamps_fd, i, false );
400 }
401
403 // Finished initing hardware
405
406 SG_LOG( SG_IO, SG_ALERT,
407 "Done initializing ATC output hardware." );
408
409 is_open = true;
410
412 // Connect up to property values
414
415 char base_name[256];
416
417 snprintf( base_name, 256, "/output/atc-board[%d]/analog-outputs", board );
418 analog_out_node = fgGetNode( base_name );
419
420 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
421 lamps_out_node = fgGetNode( base_name );
422
423 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
424 radio_display_node = fgGetNode( base_name );
425
426 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
427 steppers_node = fgGetNode( base_name );
428
429 return true;
430}
431
432
434// Write the lanalog outputs
436
437bool FGATCOutput::do_analog_out() {
438 if ( analog_out_node != NULL ) {
439 for ( int i = 0; i < analog_out_node->nChildren(); ++i ) {
440 // read the next config entry from the property tree
441
442 SGPropertyNode *child = analog_out_node->getChild(i);
443 string cname = child->getNameString();
444 int index = child->getIndex();
445 string name = "";
446 string type = "";
447 SGPropertyNode *src_prop = NULL;
448 double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
449 if ( cname == "analog-out" ) {
450 SGPropertyNode *prop;
451 prop = child->getChild( "name" );
452 if ( prop != NULL ) {
453 name = prop->getStringValue();
454 }
455 prop = child->getChild( "type" );
456 if ( prop != NULL ) {
457 type = prop->getStringValue();
458 }
459 prop = child->getChild( "prop" );
460 if ( prop != NULL ) {
461 src_prop = fgGetNode( prop->getStringValue(), true );
462 }
463 prop = child->getChild( "value-lo" );
464 if ( prop != NULL ) {
465 x0 = prop->getDoubleValue();
466 }
467 prop = child->getChild( "meter-lo" );
468 if ( prop != NULL ) {
469 y0 = prop->getDoubleValue();
470 }
471 prop = child->getChild( "value-hi" );
472 if ( prop != NULL ) {
473 x1 = prop->getDoubleValue();
474 }
475 prop = child->getChild( "meter-hi" );
476 if ( prop != NULL ) {
477 y1 = prop->getDoubleValue();
478 }
479 // crunch linear interpolation formula
480 double dx = x1 - x0;
481 double dy = y1 - y0;
482 double slope = dy / dx;
483 double value = src_prop->getDoubleValue();
484 int meter = (value - x0) * slope + y0;
485 if ( meter < 0 ) { meter = 0; }
486 if ( meter > 1023 ) { meter = 1023; }
487 analog_out_data[2*index] = meter / 256;
488 analog_out_data[2*index + 1] = meter - analog_out_data[2*index] * 256;
489 } else {
490 SG_LOG( SG_IO, SG_DEBUG,
491 "Input config error, expecting 'analog-out' but found "
492 << cname );
493 }
494 ATCSetAnalogOut( analog_out_fd, analog_out_data );
495 }
496 }
497
498 return true;
499}
500
501
503// Write the lights
505
506bool FGATCOutput::do_lamps() {
507 if ( lamps_out_node != NULL ) {
508 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
509 // read the next config entry from the property tree
510
511 SGPropertyNode *child = lamps_out_node->getChild(i);
512 string cname = child->getNameString();
513 int index = child->getIndex();
514 string name = "";
515 string type = "";
516 SGPropertyNode *src_prop = NULL;
517 if ( cname == "lamp" ) {
518 SGPropertyNode *prop;
519 prop = child->getChild( "name" );
520 if ( prop != NULL ) {
521 name = prop->getStringValue();
522 }
523 prop = child->getChild( "type" );
524 if ( prop != NULL ) {
525 type = prop->getStringValue();
526 }
527 prop = child->getChild( "prop" );
528 if ( prop != NULL ) {
529 src_prop = fgGetNode( prop->getStringValue(), true );
530 }
531 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
532 } else {
533 SG_LOG( SG_IO, SG_DEBUG,
534 "Input config error, expecting 'lamp' but found "
535 << cname );
536 }
537 }
538 }
539
540 return true;
541}
542
543
545// Update the radio display
547
548
549static bool navcom1_has_power() {
550 static SGPropertyNode *navcom1_bus_power
551 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
552 static SGPropertyNode *navcom1_power_btn
553 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
554
555 return (navcom1_bus_power->getDoubleValue() > 1.0)
556 && navcom1_power_btn->getBoolValue();
557}
558
559static bool navcom2_has_power() {
560 static SGPropertyNode *navcom2_bus_power
561 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
562 static SGPropertyNode *navcom2_power_btn
563 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
564
565 return (navcom2_bus_power->getDoubleValue() > 1.0)
566 && navcom2_power_btn->getBoolValue();
567}
568
569static bool dme_has_power() {
570 static SGPropertyNode *dme_bus_power
571 = fgGetNode( "/systems/electrical/outputs/dme", true );
572
573 return (dme_bus_power->getDoubleValue() > 1.0);
574}
575
576static bool adf_has_power() {
577 static SGPropertyNode *adf_bus_power
578 = fgGetNode( "/systems/electrical/outputs/adf", true );
579 static SGPropertyNode *adf_power_btn
580 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
581
582 return (adf_bus_power->getDoubleValue() > 1.0)
583 && adf_power_btn->getBoolValue();
584}
585
586static bool xpdr_has_power() {
587 static SGPropertyNode *xpdr_bus_power
588 = fgGetNode( "/systems/electrical/outputs/transponder", true );
589 static SGPropertyNode *xpdr_func_knob
590 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
591
592 return (xpdr_bus_power->getDoubleValue() > 1.0)
593 && (xpdr_func_knob->getIntValue() > 0);
594}
595
596bool FGATCOutput::do_radio_display() {
597 static SGPropertyNode *dme_serviceable
598 = fgGetNode( "/instrumentation/dme/serviceable", true );
599 static SGPropertyNode *dme_in_range
600 = fgGetNode( "/instrumentation/dme/in-range", true );
601 static SGPropertyNode *dme_min
602 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
603 static SGPropertyNode *dme_kt
604 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
605 static SGPropertyNode *dme_nm
606 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
607
608 static SGPropertyNode *comm1_serviceable
609 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
610 static SGPropertyNode *com1_freq
611 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
612 static SGPropertyNode *com1_stby_freq
613 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
614
615 static SGPropertyNode *comm2_serviceable
616 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
617 static SGPropertyNode *com2_freq
618 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
619 static SGPropertyNode *com2_stby_freq
620 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
621
622 static SGPropertyNode *nav1_serviceable
623 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
624 static SGPropertyNode *nav1_freq
625 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
626 static SGPropertyNode *nav1_stby_freq
627 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
628
629 static SGPropertyNode *nav2_serviceable
630 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
631 static SGPropertyNode *nav2_freq
632 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
633 static SGPropertyNode *nav2_stby_freq
634 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
635
636 static SGPropertyNode *adf_serviceable
637 = fgGetNode( "/instrumentation/adf/serviceable", true );
638 static SGPropertyNode *adf_freq
639 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
640 static SGPropertyNode *adf_stby_freq
641 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
642 static SGPropertyNode *adf_stby_mode
643 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
644 static SGPropertyNode *adf_timer_mode
645 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
646 // static SGPropertyNode *adf_count_mode
647 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
648 static SGPropertyNode *adf_flight_timer
649 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
650 static SGPropertyNode *adf_elapsed_timer
651 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
652
653 static SGPropertyNode *xpdr_serviceable
654 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
655 static SGPropertyNode *xpdr_func_knob
656 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
657 static SGPropertyNode *xpdr_flight_level
658 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
659 static SGPropertyNode *xpdr_id_code
660 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
661
662 char digits[10];
663 int i;
664
665 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
666 if ( dme_in_range->getBoolValue() ) {
667 // DME minutes
668 float minutes = dme_min->getFloatValue();
669 if ( minutes > 999 ) {
670 minutes = 999.0;
671 }
672 snprintf(digits, 7, "%03.0f", minutes);
673 for ( i = 0; i < 6; ++i ) {
674 digits[i] -= '0';
675 }
676 radio_display_data[0] = digits[1] << 4 | digits[2];
677 radio_display_data[1] = 0xf0 | digits[0];
678
679 // DME knots
680 float knots = dme_kt->getFloatValue();
681 if ( knots > 999 ) {
682 knots = 999.0;
683 }
684 snprintf(digits, 7, "%03.0f", knots);
685 for ( i = 0; i < 6; ++i ) {
686 digits[i] -= '0';
687 }
688 radio_display_data[2] = digits[1] << 4 | digits[2];
689 radio_display_data[3] = 0xf0 | digits[0];
690
691 // DME distance (nm)
692 float nm = dme_nm->getFloatValue();
693 if ( nm > 99 ) {
694 nm = 99.0;
695 }
696 snprintf(digits, 7, "%04.1f", nm);
697 for ( i = 0; i < 6; ++i ) {
698 digits[i] -= '0';
699 }
700 radio_display_data[4] = digits[1] << 4 | digits[3];
701 radio_display_data[5] = 0x00 | digits[0];
702 // the 0x00 in the upper nibble of the 6th byte of each
703 // display turns on the decimal point
704 } else {
705 // out of range
706 radio_display_data[0] = 0xbb;
707 radio_display_data[1] = 0xfb;
708 radio_display_data[2] = 0xbb;
709 radio_display_data[3] = 0xfb;
710 radio_display_data[4] = 0xbb;
711 radio_display_data[5] = 0x0b;
712 }
713 } else {
714 // blank dem display
715 for ( i = 0; i < 6; ++i ) {
716 radio_display_data[i] = 0xff;
717 }
718 }
719
720 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
721 // Com1 standby frequency
722 float com1_stby = com1_stby_freq->getFloatValue();
723 if ( fabs(com1_stby) > 999.99 ) {
724 com1_stby = 0.0;
725 }
726 snprintf(digits, 7, "%06.3f", com1_stby);
727 for ( i = 0; i < 6; ++i ) {
728 digits[i] -= '0';
729 }
730 radio_display_data[6] = digits[4] << 4 | digits[5];
731 radio_display_data[7] = digits[1] << 4 | digits[2];
732 radio_display_data[8] = 0xf0 | digits[0];
733
734 // Com1 in use frequency
735 float com1 = com1_freq->getFloatValue();
736 if ( fabs(com1) > 999.99 ) {
737 com1 = 0.0;
738 }
739 snprintf(digits, 7, "%06.3f", com1);
740 for ( i = 0; i < 6; ++i ) {
741 digits[i] -= '0';
742 }
743 radio_display_data[9] = digits[4] << 4 | digits[5];
744 radio_display_data[10] = digits[1] << 4 | digits[2];
745 radio_display_data[11] = 0x00 | digits[0];
746 // the 0x00 in the upper nibble of the 6th byte of each display
747 // turns on the decimal point
748 } else {
749 radio_display_data[6] = 0xff;
750 radio_display_data[7] = 0xff;
751 radio_display_data[8] = 0xff;
752 radio_display_data[9] = 0xff;
753 radio_display_data[10] = 0xff;
754 radio_display_data[11] = 0xff;
755 }
756
757 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
758 // Com2 standby frequency
759 float com2_stby = com2_stby_freq->getFloatValue();
760 if ( fabs(com2_stby) > 999.99 ) {
761 com2_stby = 0.0;
762 }
763 snprintf(digits, 7, "%06.3f", com2_stby);
764 for ( i = 0; i < 6; ++i ) {
765 digits[i] -= '0';
766 }
767 radio_display_data[18] = digits[4] << 4 | digits[5];
768 radio_display_data[19] = digits[1] << 4 | digits[2];
769 radio_display_data[20] = 0xf0 | digits[0];
770
771 // Com2 in use frequency
772 float com2 = com2_freq->getFloatValue();
773 if ( fabs(com2) > 999.99 ) {
774 com2 = 0.0;
775 }
776 snprintf(digits, 7, "%06.3f", com2);
777 for ( i = 0; i < 6; ++i ) {
778 digits[i] -= '0';
779 }
780 radio_display_data[21] = digits[4] << 4 | digits[5];
781 radio_display_data[22] = digits[1] << 4 | digits[2];
782 radio_display_data[23] = 0x00 | digits[0];
783 // the 0x00 in the upper nibble of the 6th byte of each display
784 // turns on the decimal point
785 } else {
786 radio_display_data[18] = 0xff;
787 radio_display_data[19] = 0xff;
788 radio_display_data[20] = 0xff;
789 radio_display_data[21] = 0xff;
790 radio_display_data[22] = 0xff;
791 radio_display_data[23] = 0xff;
792 }
793
794 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
795 // Nav1 standby frequency
796 float nav1_stby = nav1_stby_freq->getFloatValue();
797 if ( fabs(nav1_stby) > 999.99 ) {
798 nav1_stby = 0.0;
799 }
800 snprintf(digits, 7, "%06.2f", nav1_stby);
801 for ( i = 0; i < 6; ++i ) {
802 digits[i] -= '0';
803 }
804 radio_display_data[12] = digits[4] << 4 | digits[5];
805 radio_display_data[13] = digits[1] << 4 | digits[2];
806 radio_display_data[14] = 0xf0 | digits[0];
807
808 // Nav1 in use frequency
809 float nav1 = nav1_freq->getFloatValue();
810 if ( fabs(nav1) > 999.99 ) {
811 nav1 = 0.0;
812 }
813 snprintf(digits, 7, "%06.2f", nav1);
814 for ( i = 0; i < 6; ++i ) {
815 digits[i] -= '0';
816 }
817 radio_display_data[15] = digits[4] << 4 | digits[5];
818 radio_display_data[16] = digits[1] << 4 | digits[2];
819 radio_display_data[17] = 0x00 | digits[0];
820 // the 0x00 in the upper nibble of the 6th byte of each display
821 // turns on the decimal point
822 } else {
823 radio_display_data[12] = 0xff;
824 radio_display_data[13] = 0xff;
825 radio_display_data[14] = 0xff;
826 radio_display_data[15] = 0xff;
827 radio_display_data[16] = 0xff;
828 radio_display_data[17] = 0xff;
829 }
830
831 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
832 // Nav2 standby frequency
833 float nav2_stby = nav2_stby_freq->getFloatValue();
834 if ( fabs(nav2_stby) > 999.99 ) {
835 nav2_stby = 0.0;
836 }
837 snprintf(digits, 7, "%06.2f", nav2_stby);
838 for ( i = 0; i < 6; ++i ) {
839 digits[i] -= '0';
840 }
841 radio_display_data[24] = digits[4] << 4 | digits[5];
842 radio_display_data[25] = digits[1] << 4 | digits[2];
843 radio_display_data[26] = 0xf0 | digits[0];
844
845 // Nav2 in use frequency
846 float nav2 = nav2_freq->getFloatValue();
847 if ( fabs(nav2) > 999.99 ) {
848 nav2 = 0.0;
849 }
850 snprintf(digits, 7, "%06.2f", nav2);
851 for ( i = 0; i < 6; ++i ) {
852 digits[i] -= '0';
853 }
854 radio_display_data[27] = digits[4] << 4 | digits[5];
855 radio_display_data[28] = digits[1] << 4 | digits[2];
856 radio_display_data[29] = 0x00 | digits[0];
857 // the 0x00 in the upper nibble of the 6th byte of each display
858 // turns on the decimal point
859 } else {
860 radio_display_data[24] = 0xff;
861 radio_display_data[25] = 0xff;
862 radio_display_data[26] = 0xff;
863 radio_display_data[27] = 0xff;
864 radio_display_data[28] = 0xff;
865 radio_display_data[29] = 0xff;
866 }
867
868 // ADF standby frequency / timer
869 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
870 if ( adf_stby_mode->getIntValue() == 0 ) {
871 // frequency
872 float adf_stby = adf_stby_freq->getFloatValue();
873 if ( fabs(adf_stby) > 1799 ) {
874 adf_stby = 1799;
875 }
876 snprintf(digits, 7, "%04.0f", adf_stby);
877 for ( i = 0; i < 6; ++i ) {
878 digits[i] -= '0';
879 }
880 radio_display_data[30] = digits[3] << 4 | 0x0f;
881 radio_display_data[31] = digits[1] << 4 | digits[2];
882 if ( digits[0] == 0 ) {
883 radio_display_data[32] = 0xff;
884 } else {
885 radio_display_data[32] = 0xf0 | digits[0];
886 }
887 } else {
888 // timer
889 double time;
890 unsigned hours, min, sec;
891 if ( adf_timer_mode->getIntValue() == 0 ) {
892 time = adf_flight_timer->getDoubleValue();
893 } else {
894 time = adf_elapsed_timer->getDoubleValue();
895 }
896 // cout << time << endl;
897 hours = (int)(time / 3600.0);
898 time -= hours * 3600.00;
899 min = (int)(time / 60.0);
900 time -= min * 60.0;
901 sec = (int)time;
902 unsigned big, little;
903 if ( hours > 0 ) {
904 big = hours;
905 if ( big > 99 ) {
906 big = 99;
907 }
908 little = min;
909 } else {
910 big = min;
911 little = sec;
912 }
913 if ( big > 99 ) {
914 big = 99;
915 }
916 // cout << big << ":" << little << endl;
917 snprintf(digits, 7, "%02d%02d", big, little);
918 for ( i = 0; i < 6; ++i ) {
919 digits[i] -= '0';
920 }
921 radio_display_data[30] = digits[2] << 4 | digits[3];
922 radio_display_data[31] = digits[0] << 4 | digits[1];
923 radio_display_data[32] = 0xff;
924 }
925
926 // ADF in use frequency
927 float adf = adf_freq->getFloatValue();
928 if ( fabs(adf) > 1799 ) {
929 adf = 1799;
930 }
931 snprintf(digits, 7, "%04.0f", adf);
932 for ( i = 0; i < 6; ++i ) {
933 digits[i] -= '0';
934 }
935 radio_display_data[33] = digits[2] << 4 | digits[3];
936 if ( digits[0] == 0 ) {
937 radio_display_data[34] = 0xf0 | digits[1];
938 } else {
939 radio_display_data[34] = digits[0] << 4 | digits[1];
940 }
941 if ( adf_stby_mode->getIntValue() == 0 ) {
942 radio_display_data[35] = 0xff;
943 } else {
944 radio_display_data[35] = 0x0f;
945 }
946 } else {
947 radio_display_data[30] = 0xff;
948 radio_display_data[31] = 0xff;
949 radio_display_data[32] = 0xff;
950 radio_display_data[33] = 0xff;
951 radio_display_data[34] = 0xff;
952 radio_display_data[35] = 0xff;
953 }
954
955 // Transponder code and flight level
956 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
957 if ( xpdr_func_knob->getIntValue() == 2 ) {
958 // test mode
959 radio_display_data[36] = 8 << 4 | 8;
960 radio_display_data[37] = 8 << 4 | 8;
961 radio_display_data[38] = 0xff;
962 radio_display_data[39] = 8 << 4 | 0x0f;
963 radio_display_data[40] = 8 << 4 | 8;
964 } else {
965 // other on modes
966 int id_code = xpdr_id_code->getIntValue();
967 int place = 1000;
968 for ( i = 0; i < 4; ++i ) {
969 digits[i] = id_code / place;
970 id_code -= digits[i] * place;
971 place /= 10;
972 }
973 radio_display_data[36] = digits[2] << 4 | digits[3];
974 radio_display_data[37] = digits[0] << 4 | digits[1];
975 radio_display_data[38] = 0xff;
976
977 if ( xpdr_func_knob->getIntValue() == 3 ||
978 xpdr_func_knob->getIntValue() == 5 )
979 {
980 // do flight level display
981 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
982 for ( i = 0; i < 6; ++i ) {
983 digits[i] -= '0';
984 }
985 radio_display_data[39] = digits[2] << 4 | 0x0f;
986 radio_display_data[40] = digits[0] << 4 | digits[1];
987 } else {
988 // blank flight level display
989 radio_display_data[39] = 0xff;
990 radio_display_data[40] = 0xff;
991 }
992 }
993 } else {
994 // off
995 radio_display_data[36] = 0xff;
996 radio_display_data[37] = 0xff;
997 radio_display_data[38] = 0xff;
998 radio_display_data[39] = 0xff;
999 radio_display_data[40] = 0xff;
1000 }
1001
1002 ATCSetRadios( radio_display_fd, radio_display_data );
1003
1004 return true;
1005}
1006
1007
1009// Drive the stepper motors
1011
1012bool FGATCOutput::do_steppers() {
1013 SGPropertyNode *mag_compass
1014 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
1015 true );
1016
1017 float diff = mag_compass->getFloatValue() - compass_position;
1018 while ( diff < -180.0 ) { diff += 360.0; }
1019 while ( diff > 180.0 ) { diff -= 360.0; }
1020
1021 int steps = (int)(diff * 4);
1022 // cout << "steps = " << steps << endl;
1023 if ( steps > 4 ) { steps = 4; }
1024 if ( steps < -4 ) { steps = -4; }
1025
1026 if ( abs(steps) > 0 ) {
1027 unsigned char cmd = 0x80; // stepper command
1028 if ( steps > 0 ) {
1029 cmd |= 0x20; // go up
1030 } else {
1031 cmd |= 0x00; // go down
1032 }
1033 cmd |= abs(steps);
1034
1035 // sync compass_position with hardware position
1036 compass_position += (float)steps / 4.0;
1037
1038 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1039 }
1040
1041 return true;
1042}
1043
1044
1045// process the hardware outputs. This code assumes the calling layer
1046// will lock the hardware.
1048 if ( !is_open ) {
1049 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
1050 << board );
1051 return false;
1052 }
1053
1054 do_analog_out();
1055 do_lamps();
1056 do_radio_display();
1057#ifdef ATCFLIGHTSIM_HAVE_COMPASS
1058 do_steppers();
1059#endif
1060
1061 return true;
1062}
1063
1064
1066
1067#if defined( unix ) || defined( __CYGWIN__ )
1068
1069 if ( !is_open ) {
1070 return true;
1071 }
1072
1073 int result;
1074
1075 result = ::close( lamps_fd );
1076 if ( result == -1 ) {
1077 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1078 char msg[300];
1079 snprintf( msg, 300, "Error closing %s", lamps_file );
1080 perror( msg );
1081 exit( -1 );
1082 }
1083
1084 result = ::close( radio_display_fd );
1085 if ( result == -1 ) {
1086 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1087 char msg[300];
1088 snprintf( msg, 300, "Error closing %s", radio_display_file );
1089 perror( msg );
1090 exit( -1 );
1091 }
1092
1093 result = ::close( stepper_fd );
1094 if ( result == -1 ) {
1095 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1096 char msg[300];
1097 snprintf( msg, 300, "Error closing %s", stepper_file );
1098 perror( msg );
1099 exit( -1 );
1100 }
1101
1102#endif
1103
1104 return true;
1105}
static int ATCRelease(int fd)
static int ATCSetAnalogOut(int fd, unsigned char data[48 *2])
static bool navcom2_has_power()
static bool xpdr_has_power()
static bool navcom1_has_power()
static int ATCLock(int fd)
static bool adf_has_power()
void ATCSetLamp(int fd, int channel, bool value)
static int ATCSetRadios(int fd, unsigned char data[48])
static int ATCSetStepper(int fd, unsigned char channel, unsigned char value)
static bool dme_has_power()
#define ATC_STEPPER_HOME
#define ATC_RADIO_DISPLAY_BYTES
#define ATC_ANALOG_OUT_CHANNELS
#define ATC_COMPASS_CH
#define min(X, Y)
#define i(x)
bool open(int lock_fd)
FGATCOutput(const int _board, const SGPath &_config_file)
SGPropertyNode * get_props()
Definition globals.hxx:320
const char * name
FGGlobals * globals
Definition globals.cxx:142
const double nm(1852)
float abs(float f)
Definition Airplane.cpp:21
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27