1 // ATC-Outputs.hxx -- Translate FGFS properties to ATC hardware outputs.
3 // Written by Curtis Olson, started November 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <simgear/compiler.h>
30 #if defined( unix ) || defined( __CYGWIN__ )
31 # include <sys/types.h>
32 # include <sys/stat.h>
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/misc/sg_path.hxx>
45 #include <simgear/props/props_io.hxx>
47 #include <Main/fg_props.hxx>
49 #include "ATC-Outputs.hxx"
55 // Lock the ATC hardware
56 static int ATCLock( int fd ) {
57 #if defined( unix ) || defined( __CYGWIN__ )
59 lseek( fd, 0, SEEK_SET );
62 int result = read( fd, tmp, 1 );
64 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
74 // Release the ATC hardware
75 static int ATCRelease( int fd ) {
76 #if defined( unix ) || defined( __CYGWIN__ )
78 lseek( fd, 0, SEEK_SET );
82 int result = write( fd, tmp, 1 );
85 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
95 // Constructor: The _board parameter specifies which board to
96 // reference. Possible values are 0 or 1. The _config_file parameter
97 // specifies the location of the output config file (xml)
98 FGATCOutput::FGATCOutput( const int _board, const SGPath &_config_file ) :
100 analog_out_node(NULL),
101 lamps_out_node(NULL),
102 radio_display_node(NULL),
106 config = _config_file;
110 // Write analog out data
111 static int ATCSetAnalogOut( int fd,
112 unsigned char data[ATC_ANALOG_OUT_CHANNELS*2] )
114 #if defined( unix ) || defined( __CYGWIN__ )
116 lseek( fd, 0, SEEK_SET );
118 int result = write( fd, data, ATC_ANALOG_OUT_CHANNELS*2 );
120 if ( result != ATC_ANALOG_OUT_CHANNELS*2 ) {
121 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
131 // Write a radios command
132 static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
133 #if defined( unix ) || defined( __CYGWIN__ )
135 lseek( fd, 0, SEEK_SET );
137 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
139 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
140 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
150 // Write a stepper command
151 static int ATCSetStepper( int fd, unsigned char channel,
152 unsigned char value )
154 #if defined( unix ) || defined( __CYGWIN__ )
156 lseek( fd, 0, SEEK_SET );
159 unsigned char buf[3];
163 int result = write( fd, buf, 2 );
165 SG_LOG( SG_IO, SG_INFO, "Write failed" );
167 SG_LOG( SG_IO, SG_DEBUG,
168 "Sent cmd = " << (int)channel << " value = " << (int)value );
176 // Read status of last stepper written to
177 static unsigned char ATCReadStepper( int fd ) {
178 #if defined( unix ) || defined( __CYGWIN__ )
182 lseek( fd, 0, SEEK_SET );
185 unsigned char buf[2];
186 result = read( fd, buf, 1 );
188 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
191 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
200 // Turn a lamp on or off
201 void ATCSetLamp( int fd, int channel, bool value ) {
202 #if defined( unix ) || defined( __CYGWIN__ )
203 // lamp channels 0-63 are written to LampPort0, channels 64-127
204 // are written to LampPort1
206 // bits 0-6 are the lamp address
207 // bit 7 is the value (on/off)
212 unsigned char buf[3];
216 result = write( fd, buf, 2 );
218 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
225 void FGATCOutput::init_config() {
226 #if defined( unix ) || defined( __CYGWIN__ )
227 if ( config.str()[0] != '/' ) {
228 // not an absolute path, prepend the standard location
230 char *envp = ::getenv( "HOME" );
231 if ( envp != NULL ) {
233 tmp.append( ".atcflightsim" );
234 tmp.append( config.str() );
238 readProperties( config.str(), globals->get_props() );
243 // Open and initialize the ATC hardware
244 bool FGATCOutput::open( int lock_fd ) {
246 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
251 // This loads the config parameters generated by "simcal"
254 SG_LOG( SG_IO, SG_ALERT,
255 "Initializing ATC output hardware, please wait ..." );
257 snprintf( analog_out_file, 256,
258 "/proc/atcflightsim/board%d/analog_out", board );
259 snprintf( lamps_file, 256,
260 "/proc/atcflightsim/board%d/lamps", board );
261 snprintf( radio_display_file, 256,
262 "/proc/atcflightsim/board%d/radios", board );
263 snprintf( stepper_file, 256,
264 "/proc/atcflightsim/board%d/steppers", board );
266 #if defined( unix ) || defined( __CYGWIN__ )
268 /////////////////////////////////////////////////////////////////////
269 // Open the /proc files
270 /////////////////////////////////////////////////////////////////////
272 analog_out_fd = ::open( analog_out_file, O_WRONLY );
273 if ( analog_out_fd == -1 ) {
274 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
276 snprintf( msg, 256, "Error opening %s", analog_out_file );
281 lamps_fd = ::open( lamps_file, O_WRONLY );
282 if ( lamps_fd == -1 ) {
283 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
285 snprintf( msg, 256, "Error opening %s", lamps_file );
290 radio_display_fd = ::open( radio_display_file, O_RDWR );
291 if ( radio_display_fd == -1 ) {
292 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
294 snprintf( msg, 256, "Error opening %s", radio_display_file );
299 stepper_fd = ::open( stepper_file, O_RDWR );
300 if ( stepper_fd == -1 ) {
301 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
303 snprintf( msg, 256, "Error opening %s", stepper_file );
310 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
311 /////////////////////////////////////////////////////////////////////
312 // Home the compass stepper motor
313 /////////////////////////////////////////////////////////////////////
315 SG_LOG( SG_IO, SG_ALERT,
316 " - Homing the compass stepper motor" );
318 // Lock the hardware, keep trying until we succeed
319 while ( ATCLock( lock_fd ) <= 0 );
321 // Send the stepper home command
322 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
324 // Release the hardware
325 ATCRelease( lock_fd );
327 SG_LOG( SG_IO, SG_ALERT,
328 " - Waiting for compass to come home." );
331 int timeout = 900; // about 30 seconds
333 while ( ! home && timeout > 0 ) {
334 if ( timeout % 150 == 0 ) {
335 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
337 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
340 while ( ATCLock( lock_fd ) <= 0 );
342 unsigned char result = ATCReadStepper( stepper_fd );
347 ATCRelease( lock_fd );
349 #if defined( _MSC_VER )
350 ulMilliSecondSleep(33);
351 #elif defined (WIN32) && !defined(__CYGWIN__)
360 compass_position = 0.0;
363 // Lock the hardware, keep trying until we succeed
364 while ( ATCLock( lock_fd ) <= 0 );
366 /////////////////////////////////////////////////////////////////////
367 // Zero the analog outputs
368 /////////////////////////////////////////////////////////////////////
370 SG_LOG( SG_IO, SG_ALERT,
371 " - Zeroing Analog Outputs." );
373 for ( int channel = 0; channel < ATC_ANALOG_OUT_CHANNELS; ++channel ) {
374 analog_out_data[2*channel] = 0;
375 analog_out_data[2*channel + 1] = 0;
377 ATCSetAnalogOut( analog_out_fd, analog_out_data );
380 /////////////////////////////////////////////////////////////////////
381 // Blank the radio display
382 /////////////////////////////////////////////////////////////////////
384 SG_LOG( SG_IO, SG_ALERT,
385 " - Clearing the radios displays." );
388 unsigned char value = 0xff;
389 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
390 radio_display_data[channel] = value;
392 ATCSetRadios( radio_display_fd, radio_display_data );
394 ATCRelease( lock_fd );
396 /////////////////////////////////////////////////////////////////////
398 /////////////////////////////////////////////////////////////////////
400 for ( int i = 0; i < 128; ++i ) {
401 ATCSetLamp( lamps_fd, i, false );
404 /////////////////////////////////////////////////////////////////////
405 // Finished initing hardware
406 /////////////////////////////////////////////////////////////////////
408 SG_LOG( SG_IO, SG_ALERT,
409 "Done initializing ATC output hardware." );
413 /////////////////////////////////////////////////////////////////////
414 // Connect up to property values
415 /////////////////////////////////////////////////////////////////////
419 snprintf( base_name, 256, "/output/atc-board[%d]/analog-outputs", board );
420 analog_out_node = fgGetNode( base_name );
422 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
423 lamps_out_node = fgGetNode( base_name );
425 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
426 radio_display_node = fgGetNode( base_name );
428 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
429 steppers_node = fgGetNode( base_name );
435 /////////////////////////////////////////////////////////////////////
436 // Write the lanalog outputs
437 /////////////////////////////////////////////////////////////////////
439 bool FGATCOutput::do_analog_out() {
440 if ( analog_out_node != NULL ) {
441 for ( int i = 0; i < analog_out_node->nChildren(); ++i ) {
442 // read the next config entry from the property tree
444 SGPropertyNode *child = analog_out_node->getChild(i);
445 string cname = child->getName();
446 int index = child->getIndex();
449 SGPropertyNode *src_prop = NULL;
450 double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
451 if ( cname == "analog-out" ) {
452 SGPropertyNode *prop;
453 prop = child->getChild( "name" );
454 if ( prop != NULL ) {
455 name = prop->getStringValue();
457 prop = child->getChild( "type" );
458 if ( prop != NULL ) {
459 type = prop->getStringValue();
461 prop = child->getChild( "prop" );
462 if ( prop != NULL ) {
463 src_prop = fgGetNode( prop->getStringValue(), true );
465 prop = child->getChild( "value-lo" );
466 if ( prop != NULL ) {
467 prop = fgGetNode( prop->getStringValue(), true );
468 x0 = prop->getDoubleValue();
470 prop = child->getChild( "meter-lo" );
471 if ( prop != NULL ) {
472 prop = fgGetNode( prop->getStringValue(), true );
473 y0 = prop->getDoubleValue();
475 prop = child->getChild( "value-hi" );
476 if ( prop != NULL ) {
477 prop = fgGetNode( prop->getStringValue(), true );
478 x1 = prop->getDoubleValue();
480 prop = child->getChild( "meter-hi" );
481 if ( prop != NULL ) {
482 prop = fgGetNode( prop->getStringValue(), true );
483 y1 = prop->getDoubleValue();
485 // crunch linear interpolation formula
488 double slope = dy / dx;
489 double value = src_prop->getDoubleValue();
490 int meter = (value - x0) * slope + y0;
491 if ( meter < 0 ) { meter = 0; }
492 if ( meter > 1023 ) { meter = 1023; }
493 analog_out_data[2*index] = meter / 256;
494 analog_out_data[2*index + 1] = meter - analog_out_data[2*index] * 256;
496 SG_LOG( SG_IO, SG_DEBUG,
497 "Input config error, expecting 'analog-out' but found "
500 ATCSetAnalogOut( analog_out_fd, analog_out_data );
508 /////////////////////////////////////////////////////////////////////
510 /////////////////////////////////////////////////////////////////////
512 bool FGATCOutput::do_lamps() {
513 if ( lamps_out_node != NULL ) {
514 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
515 // read the next config entry from the property tree
517 SGPropertyNode *child = lamps_out_node->getChild(i);
518 string cname = child->getName();
519 int index = child->getIndex();
522 SGPropertyNode *src_prop = NULL;
523 if ( cname == "lamp" ) {
524 SGPropertyNode *prop;
525 prop = child->getChild( "name" );
526 if ( prop != NULL ) {
527 name = prop->getStringValue();
529 prop = child->getChild( "type" );
530 if ( prop != NULL ) {
531 type = prop->getStringValue();
533 prop = child->getChild( "prop" );
534 if ( prop != NULL ) {
535 src_prop = fgGetNode( prop->getStringValue(), true );
537 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
539 SG_LOG( SG_IO, SG_DEBUG,
540 "Input config error, expecting 'lamp' but found "
550 /////////////////////////////////////////////////////////////////////
551 // Update the radio display
552 /////////////////////////////////////////////////////////////////////
555 static bool navcom1_has_power() {
556 static SGPropertyNode *navcom1_bus_power
557 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
558 static SGPropertyNode *navcom1_power_btn
559 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
561 return (navcom1_bus_power->getDoubleValue() > 1.0)
562 && navcom1_power_btn->getBoolValue();
565 static bool navcom2_has_power() {
566 static SGPropertyNode *navcom2_bus_power
567 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
568 static SGPropertyNode *navcom2_power_btn
569 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
571 return (navcom2_bus_power->getDoubleValue() > 1.0)
572 && navcom2_power_btn->getBoolValue();
575 static bool dme_has_power() {
576 static SGPropertyNode *dme_bus_power
577 = fgGetNode( "/systems/electrical/outputs/dme", true );
579 return (dme_bus_power->getDoubleValue() > 1.0);
582 static bool adf_has_power() {
583 static SGPropertyNode *adf_bus_power
584 = fgGetNode( "/systems/electrical/outputs/adf", true );
585 static SGPropertyNode *adf_power_btn
586 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
588 return (adf_bus_power->getDoubleValue() > 1.0)
589 && adf_power_btn->getBoolValue();
592 static bool xpdr_has_power() {
593 static SGPropertyNode *xpdr_bus_power
594 = fgGetNode( "/systems/electrical/outputs/transponder", true );
595 static SGPropertyNode *xpdr_func_knob
596 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
598 return (xpdr_bus_power->getDoubleValue() > 1.0)
599 && (xpdr_func_knob->getIntValue() > 0);
602 bool FGATCOutput::do_radio_display() {
603 static SGPropertyNode *dme_serviceable
604 = fgGetNode( "/instrumentation/dme/serviceable", true );
605 static SGPropertyNode *dme_in_range
606 = fgGetNode( "/instrumentation/dme/in-range", true );
607 static SGPropertyNode *dme_min
608 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
609 static SGPropertyNode *dme_kt
610 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
611 static SGPropertyNode *dme_nm
612 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
614 static SGPropertyNode *comm1_serviceable
615 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
616 static SGPropertyNode *com1_freq
617 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
618 static SGPropertyNode *com1_stby_freq
619 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
621 static SGPropertyNode *comm2_serviceable
622 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
623 static SGPropertyNode *com2_freq
624 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
625 static SGPropertyNode *com2_stby_freq
626 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
628 static SGPropertyNode *nav1_serviceable
629 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
630 static SGPropertyNode *nav1_freq
631 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
632 static SGPropertyNode *nav1_stby_freq
633 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
635 static SGPropertyNode *nav2_serviceable
636 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
637 static SGPropertyNode *nav2_freq
638 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
639 static SGPropertyNode *nav2_stby_freq
640 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
642 static SGPropertyNode *adf_serviceable
643 = fgGetNode( "/instrumentation/adf/serviceable", true );
644 static SGPropertyNode *adf_freq
645 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
646 static SGPropertyNode *adf_stby_freq
647 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
648 static SGPropertyNode *adf_stby_mode
649 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
650 static SGPropertyNode *adf_timer_mode
651 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
652 // static SGPropertyNode *adf_count_mode
653 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
654 static SGPropertyNode *adf_flight_timer
655 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
656 static SGPropertyNode *adf_elapsed_timer
657 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
659 static SGPropertyNode *xpdr_serviceable
660 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
661 static SGPropertyNode *xpdr_func_knob
662 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
663 static SGPropertyNode *xpdr_flight_level
664 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
665 static SGPropertyNode *xpdr_id_code
666 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
671 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
672 if ( dme_in_range->getBoolValue() ) {
674 float minutes = dme_min->getFloatValue();
675 if ( minutes > 999 ) {
678 snprintf(digits, 7, "%03.0f", minutes);
679 for ( i = 0; i < 6; ++i ) {
682 radio_display_data[0] = digits[1] << 4 | digits[2];
683 radio_display_data[1] = 0xf0 | digits[0];
686 float knots = dme_kt->getFloatValue();
690 snprintf(digits, 7, "%03.0f", knots);
691 for ( i = 0; i < 6; ++i ) {
694 radio_display_data[2] = digits[1] << 4 | digits[2];
695 radio_display_data[3] = 0xf0 | digits[0];
698 float nm = dme_nm->getFloatValue();
702 snprintf(digits, 7, "%04.1f", nm);
703 for ( i = 0; i < 6; ++i ) {
706 radio_display_data[4] = digits[1] << 4 | digits[3];
707 radio_display_data[5] = 0x00 | digits[0];
708 // the 0x00 in the upper nibble of the 6th byte of each
709 // display turns on the decimal point
712 radio_display_data[0] = 0xbb;
713 radio_display_data[1] = 0xfb;
714 radio_display_data[2] = 0xbb;
715 radio_display_data[3] = 0xfb;
716 radio_display_data[4] = 0xbb;
717 radio_display_data[5] = 0x0b;
721 for ( i = 0; i < 6; ++i ) {
722 radio_display_data[i] = 0xff;
726 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
727 // Com1 standby frequency
728 float com1_stby = com1_stby_freq->getFloatValue();
729 if ( fabs(com1_stby) > 999.99 ) {
732 snprintf(digits, 7, "%06.3f", com1_stby);
733 for ( i = 0; i < 6; ++i ) {
736 radio_display_data[6] = digits[4] << 4 | digits[5];
737 radio_display_data[7] = digits[1] << 4 | digits[2];
738 radio_display_data[8] = 0xf0 | digits[0];
740 // Com1 in use frequency
741 float com1 = com1_freq->getFloatValue();
742 if ( fabs(com1) > 999.99 ) {
745 snprintf(digits, 7, "%06.3f", com1);
746 for ( i = 0; i < 6; ++i ) {
749 radio_display_data[9] = digits[4] << 4 | digits[5];
750 radio_display_data[10] = digits[1] << 4 | digits[2];
751 radio_display_data[11] = 0x00 | digits[0];
752 // the 0x00 in the upper nibble of the 6th byte of each display
753 // turns on the decimal point
755 radio_display_data[6] = 0xff;
756 radio_display_data[7] = 0xff;
757 radio_display_data[8] = 0xff;
758 radio_display_data[9] = 0xff;
759 radio_display_data[10] = 0xff;
760 radio_display_data[11] = 0xff;
763 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
764 // Com2 standby frequency
765 float com2_stby = com2_stby_freq->getFloatValue();
766 if ( fabs(com2_stby) > 999.99 ) {
769 snprintf(digits, 7, "%06.3f", com2_stby);
770 for ( i = 0; i < 6; ++i ) {
773 radio_display_data[18] = digits[4] << 4 | digits[5];
774 radio_display_data[19] = digits[1] << 4 | digits[2];
775 radio_display_data[20] = 0xf0 | digits[0];
777 // Com2 in use frequency
778 float com2 = com2_freq->getFloatValue();
779 if ( fabs(com2) > 999.99 ) {
782 snprintf(digits, 7, "%06.3f", com2);
783 for ( i = 0; i < 6; ++i ) {
786 radio_display_data[21] = digits[4] << 4 | digits[5];
787 radio_display_data[22] = digits[1] << 4 | digits[2];
788 radio_display_data[23] = 0x00 | digits[0];
789 // the 0x00 in the upper nibble of the 6th byte of each display
790 // turns on the decimal point
792 radio_display_data[18] = 0xff;
793 radio_display_data[19] = 0xff;
794 radio_display_data[20] = 0xff;
795 radio_display_data[21] = 0xff;
796 radio_display_data[22] = 0xff;
797 radio_display_data[23] = 0xff;
800 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
801 // Nav1 standby frequency
802 float nav1_stby = nav1_stby_freq->getFloatValue();
803 if ( fabs(nav1_stby) > 999.99 ) {
806 snprintf(digits, 7, "%06.2f", nav1_stby);
807 for ( i = 0; i < 6; ++i ) {
810 radio_display_data[12] = digits[4] << 4 | digits[5];
811 radio_display_data[13] = digits[1] << 4 | digits[2];
812 radio_display_data[14] = 0xf0 | digits[0];
814 // Nav1 in use frequency
815 float nav1 = nav1_freq->getFloatValue();
816 if ( fabs(nav1) > 999.99 ) {
819 snprintf(digits, 7, "%06.2f", nav1);
820 for ( i = 0; i < 6; ++i ) {
823 radio_display_data[15] = digits[4] << 4 | digits[5];
824 radio_display_data[16] = digits[1] << 4 | digits[2];
825 radio_display_data[17] = 0x00 | digits[0];
826 // the 0x00 in the upper nibble of the 6th byte of each display
827 // turns on the decimal point
829 radio_display_data[12] = 0xff;
830 radio_display_data[13] = 0xff;
831 radio_display_data[14] = 0xff;
832 radio_display_data[15] = 0xff;
833 radio_display_data[16] = 0xff;
834 radio_display_data[17] = 0xff;
837 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
838 // Nav2 standby frequency
839 float nav2_stby = nav2_stby_freq->getFloatValue();
840 if ( fabs(nav2_stby) > 999.99 ) {
843 snprintf(digits, 7, "%06.2f", nav2_stby);
844 for ( i = 0; i < 6; ++i ) {
847 radio_display_data[24] = digits[4] << 4 | digits[5];
848 radio_display_data[25] = digits[1] << 4 | digits[2];
849 radio_display_data[26] = 0xf0 | digits[0];
851 // Nav2 in use frequency
852 float nav2 = nav2_freq->getFloatValue();
853 if ( fabs(nav2) > 999.99 ) {
856 snprintf(digits, 7, "%06.2f", nav2);
857 for ( i = 0; i < 6; ++i ) {
860 radio_display_data[27] = digits[4] << 4 | digits[5];
861 radio_display_data[28] = digits[1] << 4 | digits[2];
862 radio_display_data[29] = 0x00 | digits[0];
863 // the 0x00 in the upper nibble of the 6th byte of each display
864 // turns on the decimal point
866 radio_display_data[24] = 0xff;
867 radio_display_data[25] = 0xff;
868 radio_display_data[26] = 0xff;
869 radio_display_data[27] = 0xff;
870 radio_display_data[28] = 0xff;
871 radio_display_data[29] = 0xff;
874 // ADF standby frequency / timer
875 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
876 if ( adf_stby_mode->getIntValue() == 0 ) {
878 float adf_stby = adf_stby_freq->getFloatValue();
879 if ( fabs(adf_stby) > 1799 ) {
882 snprintf(digits, 7, "%04.0f", adf_stby);
883 for ( i = 0; i < 6; ++i ) {
886 radio_display_data[30] = digits[3] << 4 | 0x0f;
887 radio_display_data[31] = digits[1] << 4 | digits[2];
888 if ( digits[0] == 0 ) {
889 radio_display_data[32] = 0xff;
891 radio_display_data[32] = 0xf0 | digits[0];
897 if ( adf_timer_mode->getIntValue() == 0 ) {
898 time = adf_flight_timer->getDoubleValue();
900 time = adf_elapsed_timer->getDoubleValue();
902 // cout << time << endl;
903 hours = (int)(time / 3600.0);
904 time -= hours * 3600.00;
905 min = (int)(time / 60.0);
922 // cout << big << ":" << little << endl;
923 snprintf(digits, 7, "%02d%02d", big, little);
924 for ( i = 0; i < 6; ++i ) {
927 radio_display_data[30] = digits[2] << 4 | digits[3];
928 radio_display_data[31] = digits[0] << 4 | digits[1];
929 radio_display_data[32] = 0xff;
932 // ADF in use frequency
933 float adf = adf_freq->getFloatValue();
934 if ( fabs(adf) > 1799 ) {
937 snprintf(digits, 7, "%04.0f", adf);
938 for ( i = 0; i < 6; ++i ) {
941 radio_display_data[33] = digits[2] << 4 | digits[3];
942 if ( digits[0] == 0 ) {
943 radio_display_data[34] = 0xf0 | digits[1];
945 radio_display_data[34] = digits[0] << 4 | digits[1];
947 if ( adf_stby_mode->getIntValue() == 0 ) {
948 radio_display_data[35] = 0xff;
950 radio_display_data[35] = 0x0f;
953 radio_display_data[30] = 0xff;
954 radio_display_data[31] = 0xff;
955 radio_display_data[32] = 0xff;
956 radio_display_data[33] = 0xff;
957 radio_display_data[34] = 0xff;
958 radio_display_data[35] = 0xff;
961 // Transponder code and flight level
962 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
963 if ( xpdr_func_knob->getIntValue() == 2 ) {
965 radio_display_data[36] = 8 << 4 | 8;
966 radio_display_data[37] = 8 << 4 | 8;
967 radio_display_data[38] = 0xff;
968 radio_display_data[39] = 8 << 4 | 0x0f;
969 radio_display_data[40] = 8 << 4 | 8;
972 int id_code = xpdr_id_code->getIntValue();
974 for ( i = 0; i < 4; ++i ) {
975 digits[i] = id_code / place;
976 id_code -= digits[i] * place;
979 radio_display_data[36] = digits[2] << 4 | digits[3];
980 radio_display_data[37] = digits[0] << 4 | digits[1];
981 radio_display_data[38] = 0xff;
983 if ( xpdr_func_knob->getIntValue() == 3 ||
984 xpdr_func_knob->getIntValue() == 5 )
986 // do flight level display
987 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
988 for ( i = 0; i < 6; ++i ) {
991 radio_display_data[39] = digits[2] << 4 | 0x0f;
992 radio_display_data[40] = digits[0] << 4 | digits[1];
994 // blank flight level display
995 radio_display_data[39] = 0xff;
996 radio_display_data[40] = 0xff;
1001 radio_display_data[36] = 0xff;
1002 radio_display_data[37] = 0xff;
1003 radio_display_data[38] = 0xff;
1004 radio_display_data[39] = 0xff;
1005 radio_display_data[40] = 0xff;
1008 ATCSetRadios( radio_display_fd, radio_display_data );
1014 /////////////////////////////////////////////////////////////////////
1015 // Drive the stepper motors
1016 /////////////////////////////////////////////////////////////////////
1018 bool FGATCOutput::do_steppers() {
1019 SGPropertyNode *mag_compass
1020 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
1023 float diff = mag_compass->getFloatValue() - compass_position;
1024 while ( diff < -180.0 ) { diff += 360.0; }
1025 while ( diff > 180.0 ) { diff -= 360.0; }
1027 int steps = (int)(diff * 4);
1028 // cout << "steps = " << steps << endl;
1029 if ( steps > 4 ) { steps = 4; }
1030 if ( steps < -4 ) { steps = -4; }
1032 if ( abs(steps) > 0 ) {
1033 unsigned char cmd = 0x80; // stepper command
1035 cmd |= 0x20; // go up
1037 cmd |= 0x00; // go down
1041 // sync compass_position with hardware position
1042 compass_position += (float)steps / 4.0;
1044 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1051 // process the hardware outputs. This code assumes the calling layer
1052 // will lock the hardware.
1053 bool FGATCOutput::process() {
1055 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
1063 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
1071 bool FGATCOutput::close() {
1073 #if defined( unix ) || defined( __CYGWIN__ )
1081 result = ::close( lamps_fd );
1082 if ( result == -1 ) {
1083 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1085 snprintf( msg, 256, "Error closing %s", lamps_file );
1090 result = ::close( radio_display_fd );
1091 if ( result == -1 ) {
1092 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1094 snprintf( msg, 256, "Error closing %s", radio_display_file );
1099 result = ::close( stepper_fd );
1100 if ( result == -1 ) {
1101 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1103 snprintf( msg, 256, "Error closing %s", stepper_file );