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>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/props/props_io.hxx>
48 #include <Main/fg_props.hxx>
50 #include "ATC-Outputs.hxx"
56 // Lock the ATC hardware
57 static int ATCLock( int fd ) {
58 #if defined( unix ) || defined( __CYGWIN__ )
60 lseek( fd, 0, SEEK_SET );
63 int result = read( fd, tmp, 1 );
65 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
75 // Release the ATC hardware
76 static int ATCRelease( int fd ) {
77 #if defined( unix ) || defined( __CYGWIN__ )
79 lseek( fd, 0, SEEK_SET );
83 int result = write( fd, tmp, 1 );
86 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
96 // Constructor: The _board parameter specifies which board to
97 // reference. Possible values are 0 or 1. The _config_file parameter
98 // specifies the location of the output config file (xml)
99 FGATCOutput::FGATCOutput( const int _board, const SGPath &_config_file ) :
101 analog_out_node(NULL),
102 lamps_out_node(NULL),
103 radio_display_node(NULL),
107 config = _config_file;
111 // Write analog out data
112 static int ATCSetAnalogOut( int fd,
113 unsigned char data[ATC_ANALOG_OUT_CHANNELS*2] )
115 #if defined( unix ) || defined( __CYGWIN__ )
117 lseek( fd, 0, SEEK_SET );
119 int result = write( fd, data, ATC_ANALOG_OUT_CHANNELS*2 );
121 if ( result != ATC_ANALOG_OUT_CHANNELS*2 ) {
122 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
132 // Write a radios command
133 static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
134 #if defined( unix ) || defined( __CYGWIN__ )
136 lseek( fd, 0, SEEK_SET );
138 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
140 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
141 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
151 // Write a stepper command
152 static int ATCSetStepper( int fd, unsigned char channel,
153 unsigned char value )
155 #if defined( unix ) || defined( __CYGWIN__ )
157 lseek( fd, 0, SEEK_SET );
160 unsigned char buf[3];
164 int result = write( fd, buf, 2 );
166 SG_LOG( SG_IO, SG_INFO, "Write failed" );
168 SG_LOG( SG_IO, SG_DEBUG,
169 "Sent cmd = " << (int)channel << " value = " << (int)value );
177 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
178 // Read status of last stepper written to
179 static unsigned char ATCReadStepper( int fd ) {
180 #if defined( unix ) || defined( __CYGWIN__ )
184 lseek( fd, 0, SEEK_SET );
187 unsigned char buf[2];
188 result = read( fd, buf, 1 );
190 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
193 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
203 // Turn a lamp on or off
204 void ATCSetLamp( int fd, int channel, bool value ) {
205 #if defined( unix ) || defined( __CYGWIN__ )
206 // lamp channels 0-63 are written to LampPort0, channels 64-127
207 // are written to LampPort1
209 // bits 0-6 are the lamp address
210 // bit 7 is the value (on/off)
215 unsigned char buf[3];
219 result = write( fd, buf, 2 );
221 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
228 void FGATCOutput::init_config() {
229 #if defined( unix ) || defined( __CYGWIN__ )
230 if ( config.str()[0] != '/' ) {
231 // not an absolute path, prepend the standard location
233 char *envp = ::getenv( "HOME" );
234 if ( envp != NULL ) {
236 tmp.append( ".atcflightsim" );
237 tmp.append( config.str() );
241 readProperties( config.str(), globals->get_props() );
246 // Open and initialize the ATC hardware
247 bool FGATCOutput::open( int lock_fd ) {
249 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
254 // This loads the config parameters generated by "simcal"
257 SG_LOG( SG_IO, SG_ALERT,
258 "Initializing ATC output hardware, please wait ..." );
260 snprintf( analog_out_file, 256,
261 "/proc/atcflightsim/board%d/analog_out", board );
262 snprintf( lamps_file, 256,
263 "/proc/atcflightsim/board%d/lamps", board );
264 snprintf( radio_display_file, 256,
265 "/proc/atcflightsim/board%d/radios", board );
266 snprintf( stepper_file, 256,
267 "/proc/atcflightsim/board%d/steppers", board );
269 #if defined( unix ) || defined( __CYGWIN__ )
271 /////////////////////////////////////////////////////////////////////
272 // Open the /proc files
273 /////////////////////////////////////////////////////////////////////
275 analog_out_fd = ::open( analog_out_file, O_WRONLY );
276 if ( analog_out_fd == -1 ) {
277 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
279 snprintf( msg, 256, "Error opening %s", analog_out_file );
284 lamps_fd = ::open( lamps_file, O_WRONLY );
285 if ( lamps_fd == -1 ) {
286 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
288 snprintf( msg, 256, "Error opening %s", lamps_file );
293 radio_display_fd = ::open( radio_display_file, O_RDWR );
294 if ( radio_display_fd == -1 ) {
295 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
297 snprintf( msg, 256, "Error opening %s", radio_display_file );
302 stepper_fd = ::open( stepper_file, O_RDWR );
303 if ( stepper_fd == -1 ) {
304 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
306 snprintf( msg, 256, "Error opening %s", stepper_file );
313 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
314 /////////////////////////////////////////////////////////////////////
315 // Home the compass stepper motor
316 /////////////////////////////////////////////////////////////////////
318 SG_LOG( SG_IO, SG_ALERT,
319 " - Homing the compass stepper motor" );
321 // Lock the hardware, keep trying until we succeed
322 while ( ATCLock( lock_fd ) <= 0 );
324 // Send the stepper home command
325 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
327 // Release the hardware
328 ATCRelease( lock_fd );
330 SG_LOG( SG_IO, SG_ALERT,
331 " - Waiting for compass to come home." );
334 int timeout = 900; // about 30 seconds
336 while ( ! home && timeout > 0 ) {
337 if ( timeout % 150 == 0 ) {
338 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
340 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
343 while ( ATCLock( lock_fd ) <= 0 );
345 unsigned char result = ATCReadStepper( stepper_fd );
350 ATCRelease( lock_fd );
361 compass_position = 0.0;
364 // Lock the hardware, keep trying until we succeed
365 while ( ATCLock( lock_fd ) <= 0 );
367 /////////////////////////////////////////////////////////////////////
368 // Zero the analog outputs
369 /////////////////////////////////////////////////////////////////////
371 SG_LOG( SG_IO, SG_ALERT,
372 " - Zeroing Analog Outputs." );
374 for ( int channel = 0; channel < ATC_ANALOG_OUT_CHANNELS; ++channel ) {
375 analog_out_data[2*channel] = 0;
376 analog_out_data[2*channel + 1] = 0;
378 ATCSetAnalogOut( analog_out_fd, analog_out_data );
381 /////////////////////////////////////////////////////////////////////
382 // Blank the radio display
383 /////////////////////////////////////////////////////////////////////
385 SG_LOG( SG_IO, SG_ALERT,
386 " - Clearing the radios displays." );
389 unsigned char value = 0xff;
390 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
391 radio_display_data[channel] = value;
393 ATCSetRadios( radio_display_fd, radio_display_data );
395 ATCRelease( lock_fd );
397 /////////////////////////////////////////////////////////////////////
399 /////////////////////////////////////////////////////////////////////
401 for ( int i = 0; i < 128; ++i ) {
402 ATCSetLamp( lamps_fd, i, false );
405 /////////////////////////////////////////////////////////////////////
406 // Finished initing hardware
407 /////////////////////////////////////////////////////////////////////
409 SG_LOG( SG_IO, SG_ALERT,
410 "Done initializing ATC output hardware." );
414 /////////////////////////////////////////////////////////////////////
415 // Connect up to property values
416 /////////////////////////////////////////////////////////////////////
420 snprintf( base_name, 256, "/output/atc-board[%d]/analog-outputs", board );
421 analog_out_node = fgGetNode( base_name );
423 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
424 lamps_out_node = fgGetNode( base_name );
426 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
427 radio_display_node = fgGetNode( base_name );
429 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
430 steppers_node = fgGetNode( base_name );
436 /////////////////////////////////////////////////////////////////////
437 // Write the lanalog outputs
438 /////////////////////////////////////////////////////////////////////
440 bool FGATCOutput::do_analog_out() {
441 if ( analog_out_node != NULL ) {
442 for ( int i = 0; i < analog_out_node->nChildren(); ++i ) {
443 // read the next config entry from the property tree
445 SGPropertyNode *child = analog_out_node->getChild(i);
446 string cname = child->getName();
447 int index = child->getIndex();
450 SGPropertyNode *src_prop = NULL;
451 double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
452 if ( cname == "analog-out" ) {
453 SGPropertyNode *prop;
454 prop = child->getChild( "name" );
455 if ( prop != NULL ) {
456 name = prop->getStringValue();
458 prop = child->getChild( "type" );
459 if ( prop != NULL ) {
460 type = prop->getStringValue();
462 prop = child->getChild( "prop" );
463 if ( prop != NULL ) {
464 src_prop = fgGetNode( prop->getStringValue(), true );
466 prop = child->getChild( "value-lo" );
467 if ( prop != NULL ) {
468 x0 = prop->getDoubleValue();
470 prop = child->getChild( "meter-lo" );
471 if ( prop != NULL ) {
472 y0 = prop->getDoubleValue();
474 prop = child->getChild( "value-hi" );
475 if ( prop != NULL ) {
476 x1 = prop->getDoubleValue();
478 prop = child->getChild( "meter-hi" );
479 if ( prop != NULL ) {
480 y1 = prop->getDoubleValue();
482 // crunch linear interpolation formula
485 double slope = dy / dx;
486 double value = src_prop->getDoubleValue();
487 int meter = (value - x0) * slope + y0;
488 if ( meter < 0 ) { meter = 0; }
489 if ( meter > 1023 ) { meter = 1023; }
490 analog_out_data[2*index] = meter / 256;
491 analog_out_data[2*index + 1] = meter - analog_out_data[2*index] * 256;
493 SG_LOG( SG_IO, SG_DEBUG,
494 "Input config error, expecting 'analog-out' but found "
497 ATCSetAnalogOut( analog_out_fd, analog_out_data );
505 /////////////////////////////////////////////////////////////////////
507 /////////////////////////////////////////////////////////////////////
509 bool FGATCOutput::do_lamps() {
510 if ( lamps_out_node != NULL ) {
511 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
512 // read the next config entry from the property tree
514 SGPropertyNode *child = lamps_out_node->getChild(i);
515 string cname = child->getName();
516 int index = child->getIndex();
519 SGPropertyNode *src_prop = NULL;
520 if ( cname == "lamp" ) {
521 SGPropertyNode *prop;
522 prop = child->getChild( "name" );
523 if ( prop != NULL ) {
524 name = prop->getStringValue();
526 prop = child->getChild( "type" );
527 if ( prop != NULL ) {
528 type = prop->getStringValue();
530 prop = child->getChild( "prop" );
531 if ( prop != NULL ) {
532 src_prop = fgGetNode( prop->getStringValue(), true );
534 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
536 SG_LOG( SG_IO, SG_DEBUG,
537 "Input config error, expecting 'lamp' but found "
547 /////////////////////////////////////////////////////////////////////
548 // Update the radio display
549 /////////////////////////////////////////////////////////////////////
552 static bool navcom1_has_power() {
553 static SGPropertyNode *navcom1_bus_power
554 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
555 static SGPropertyNode *navcom1_power_btn
556 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
558 return (navcom1_bus_power->getDoubleValue() > 1.0)
559 && navcom1_power_btn->getBoolValue();
562 static bool navcom2_has_power() {
563 static SGPropertyNode *navcom2_bus_power
564 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
565 static SGPropertyNode *navcom2_power_btn
566 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
568 return (navcom2_bus_power->getDoubleValue() > 1.0)
569 && navcom2_power_btn->getBoolValue();
572 static bool dme_has_power() {
573 static SGPropertyNode *dme_bus_power
574 = fgGetNode( "/systems/electrical/outputs/dme", true );
576 return (dme_bus_power->getDoubleValue() > 1.0);
579 static bool adf_has_power() {
580 static SGPropertyNode *adf_bus_power
581 = fgGetNode( "/systems/electrical/outputs/adf", true );
582 static SGPropertyNode *adf_power_btn
583 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
585 return (adf_bus_power->getDoubleValue() > 1.0)
586 && adf_power_btn->getBoolValue();
589 static bool xpdr_has_power() {
590 static SGPropertyNode *xpdr_bus_power
591 = fgGetNode( "/systems/electrical/outputs/transponder", true );
592 static SGPropertyNode *xpdr_func_knob
593 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
595 return (xpdr_bus_power->getDoubleValue() > 1.0)
596 && (xpdr_func_knob->getIntValue() > 0);
599 bool FGATCOutput::do_radio_display() {
600 static SGPropertyNode *dme_serviceable
601 = fgGetNode( "/instrumentation/dme/serviceable", true );
602 static SGPropertyNode *dme_in_range
603 = fgGetNode( "/instrumentation/dme/in-range", true );
604 static SGPropertyNode *dme_min
605 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
606 static SGPropertyNode *dme_kt
607 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
608 static SGPropertyNode *dme_nm
609 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
611 static SGPropertyNode *comm1_serviceable
612 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
613 static SGPropertyNode *com1_freq
614 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
615 static SGPropertyNode *com1_stby_freq
616 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
618 static SGPropertyNode *comm2_serviceable
619 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
620 static SGPropertyNode *com2_freq
621 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
622 static SGPropertyNode *com2_stby_freq
623 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
625 static SGPropertyNode *nav1_serviceable
626 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
627 static SGPropertyNode *nav1_freq
628 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
629 static SGPropertyNode *nav1_stby_freq
630 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
632 static SGPropertyNode *nav2_serviceable
633 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
634 static SGPropertyNode *nav2_freq
635 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
636 static SGPropertyNode *nav2_stby_freq
637 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
639 static SGPropertyNode *adf_serviceable
640 = fgGetNode( "/instrumentation/adf/serviceable", true );
641 static SGPropertyNode *adf_freq
642 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
643 static SGPropertyNode *adf_stby_freq
644 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
645 static SGPropertyNode *adf_stby_mode
646 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
647 static SGPropertyNode *adf_timer_mode
648 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
649 // static SGPropertyNode *adf_count_mode
650 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
651 static SGPropertyNode *adf_flight_timer
652 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
653 static SGPropertyNode *adf_elapsed_timer
654 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
656 static SGPropertyNode *xpdr_serviceable
657 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
658 static SGPropertyNode *xpdr_func_knob
659 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
660 static SGPropertyNode *xpdr_flight_level
661 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
662 static SGPropertyNode *xpdr_id_code
663 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
668 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
669 if ( dme_in_range->getBoolValue() ) {
671 float minutes = dme_min->getFloatValue();
672 if ( minutes > 999 ) {
675 snprintf(digits, 7, "%03.0f", minutes);
676 for ( i = 0; i < 6; ++i ) {
679 radio_display_data[0] = digits[1] << 4 | digits[2];
680 radio_display_data[1] = 0xf0 | digits[0];
683 float knots = dme_kt->getFloatValue();
687 snprintf(digits, 7, "%03.0f", knots);
688 for ( i = 0; i < 6; ++i ) {
691 radio_display_data[2] = digits[1] << 4 | digits[2];
692 radio_display_data[3] = 0xf0 | digits[0];
695 float nm = dme_nm->getFloatValue();
699 snprintf(digits, 7, "%04.1f", nm);
700 for ( i = 0; i < 6; ++i ) {
703 radio_display_data[4] = digits[1] << 4 | digits[3];
704 radio_display_data[5] = 0x00 | digits[0];
705 // the 0x00 in the upper nibble of the 6th byte of each
706 // display turns on the decimal point
709 radio_display_data[0] = 0xbb;
710 radio_display_data[1] = 0xfb;
711 radio_display_data[2] = 0xbb;
712 radio_display_data[3] = 0xfb;
713 radio_display_data[4] = 0xbb;
714 radio_display_data[5] = 0x0b;
718 for ( i = 0; i < 6; ++i ) {
719 radio_display_data[i] = 0xff;
723 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
724 // Com1 standby frequency
725 float com1_stby = com1_stby_freq->getFloatValue();
726 if ( fabs(com1_stby) > 999.99 ) {
729 snprintf(digits, 7, "%06.3f", com1_stby);
730 for ( i = 0; i < 6; ++i ) {
733 radio_display_data[6] = digits[4] << 4 | digits[5];
734 radio_display_data[7] = digits[1] << 4 | digits[2];
735 radio_display_data[8] = 0xf0 | digits[0];
737 // Com1 in use frequency
738 float com1 = com1_freq->getFloatValue();
739 if ( fabs(com1) > 999.99 ) {
742 snprintf(digits, 7, "%06.3f", com1);
743 for ( i = 0; i < 6; ++i ) {
746 radio_display_data[9] = digits[4] << 4 | digits[5];
747 radio_display_data[10] = digits[1] << 4 | digits[2];
748 radio_display_data[11] = 0x00 | digits[0];
749 // the 0x00 in the upper nibble of the 6th byte of each display
750 // turns on the decimal point
752 radio_display_data[6] = 0xff;
753 radio_display_data[7] = 0xff;
754 radio_display_data[8] = 0xff;
755 radio_display_data[9] = 0xff;
756 radio_display_data[10] = 0xff;
757 radio_display_data[11] = 0xff;
760 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
761 // Com2 standby frequency
762 float com2_stby = com2_stby_freq->getFloatValue();
763 if ( fabs(com2_stby) > 999.99 ) {
766 snprintf(digits, 7, "%06.3f", com2_stby);
767 for ( i = 0; i < 6; ++i ) {
770 radio_display_data[18] = digits[4] << 4 | digits[5];
771 radio_display_data[19] = digits[1] << 4 | digits[2];
772 radio_display_data[20] = 0xf0 | digits[0];
774 // Com2 in use frequency
775 float com2 = com2_freq->getFloatValue();
776 if ( fabs(com2) > 999.99 ) {
779 snprintf(digits, 7, "%06.3f", com2);
780 for ( i = 0; i < 6; ++i ) {
783 radio_display_data[21] = digits[4] << 4 | digits[5];
784 radio_display_data[22] = digits[1] << 4 | digits[2];
785 radio_display_data[23] = 0x00 | digits[0];
786 // the 0x00 in the upper nibble of the 6th byte of each display
787 // turns on the decimal point
789 radio_display_data[18] = 0xff;
790 radio_display_data[19] = 0xff;
791 radio_display_data[20] = 0xff;
792 radio_display_data[21] = 0xff;
793 radio_display_data[22] = 0xff;
794 radio_display_data[23] = 0xff;
797 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
798 // Nav1 standby frequency
799 float nav1_stby = nav1_stby_freq->getFloatValue();
800 if ( fabs(nav1_stby) > 999.99 ) {
803 snprintf(digits, 7, "%06.2f", nav1_stby);
804 for ( i = 0; i < 6; ++i ) {
807 radio_display_data[12] = digits[4] << 4 | digits[5];
808 radio_display_data[13] = digits[1] << 4 | digits[2];
809 radio_display_data[14] = 0xf0 | digits[0];
811 // Nav1 in use frequency
812 float nav1 = nav1_freq->getFloatValue();
813 if ( fabs(nav1) > 999.99 ) {
816 snprintf(digits, 7, "%06.2f", nav1);
817 for ( i = 0; i < 6; ++i ) {
820 radio_display_data[15] = digits[4] << 4 | digits[5];
821 radio_display_data[16] = digits[1] << 4 | digits[2];
822 radio_display_data[17] = 0x00 | digits[0];
823 // the 0x00 in the upper nibble of the 6th byte of each display
824 // turns on the decimal point
826 radio_display_data[12] = 0xff;
827 radio_display_data[13] = 0xff;
828 radio_display_data[14] = 0xff;
829 radio_display_data[15] = 0xff;
830 radio_display_data[16] = 0xff;
831 radio_display_data[17] = 0xff;
834 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
835 // Nav2 standby frequency
836 float nav2_stby = nav2_stby_freq->getFloatValue();
837 if ( fabs(nav2_stby) > 999.99 ) {
840 snprintf(digits, 7, "%06.2f", nav2_stby);
841 for ( i = 0; i < 6; ++i ) {
844 radio_display_data[24] = digits[4] << 4 | digits[5];
845 radio_display_data[25] = digits[1] << 4 | digits[2];
846 radio_display_data[26] = 0xf0 | digits[0];
848 // Nav2 in use frequency
849 float nav2 = nav2_freq->getFloatValue();
850 if ( fabs(nav2) > 999.99 ) {
853 snprintf(digits, 7, "%06.2f", nav2);
854 for ( i = 0; i < 6; ++i ) {
857 radio_display_data[27] = digits[4] << 4 | digits[5];
858 radio_display_data[28] = digits[1] << 4 | digits[2];
859 radio_display_data[29] = 0x00 | digits[0];
860 // the 0x00 in the upper nibble of the 6th byte of each display
861 // turns on the decimal point
863 radio_display_data[24] = 0xff;
864 radio_display_data[25] = 0xff;
865 radio_display_data[26] = 0xff;
866 radio_display_data[27] = 0xff;
867 radio_display_data[28] = 0xff;
868 radio_display_data[29] = 0xff;
871 // ADF standby frequency / timer
872 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
873 if ( adf_stby_mode->getIntValue() == 0 ) {
875 float adf_stby = adf_stby_freq->getFloatValue();
876 if ( fabs(adf_stby) > 1799 ) {
879 snprintf(digits, 7, "%04.0f", adf_stby);
880 for ( i = 0; i < 6; ++i ) {
883 radio_display_data[30] = digits[3] << 4 | 0x0f;
884 radio_display_data[31] = digits[1] << 4 | digits[2];
885 if ( digits[0] == 0 ) {
886 radio_display_data[32] = 0xff;
888 radio_display_data[32] = 0xf0 | digits[0];
894 if ( adf_timer_mode->getIntValue() == 0 ) {
895 time = adf_flight_timer->getDoubleValue();
897 time = adf_elapsed_timer->getDoubleValue();
899 // cout << time << endl;
900 hours = (int)(time / 3600.0);
901 time -= hours * 3600.00;
902 min = (int)(time / 60.0);
919 // cout << big << ":" << little << endl;
920 snprintf(digits, 7, "%02d%02d", big, little);
921 for ( i = 0; i < 6; ++i ) {
924 radio_display_data[30] = digits[2] << 4 | digits[3];
925 radio_display_data[31] = digits[0] << 4 | digits[1];
926 radio_display_data[32] = 0xff;
929 // ADF in use frequency
930 float adf = adf_freq->getFloatValue();
931 if ( fabs(adf) > 1799 ) {
934 snprintf(digits, 7, "%04.0f", adf);
935 for ( i = 0; i < 6; ++i ) {
938 radio_display_data[33] = digits[2] << 4 | digits[3];
939 if ( digits[0] == 0 ) {
940 radio_display_data[34] = 0xf0 | digits[1];
942 radio_display_data[34] = digits[0] << 4 | digits[1];
944 if ( adf_stby_mode->getIntValue() == 0 ) {
945 radio_display_data[35] = 0xff;
947 radio_display_data[35] = 0x0f;
950 radio_display_data[30] = 0xff;
951 radio_display_data[31] = 0xff;
952 radio_display_data[32] = 0xff;
953 radio_display_data[33] = 0xff;
954 radio_display_data[34] = 0xff;
955 radio_display_data[35] = 0xff;
958 // Transponder code and flight level
959 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
960 if ( xpdr_func_knob->getIntValue() == 2 ) {
962 radio_display_data[36] = 8 << 4 | 8;
963 radio_display_data[37] = 8 << 4 | 8;
964 radio_display_data[38] = 0xff;
965 radio_display_data[39] = 8 << 4 | 0x0f;
966 radio_display_data[40] = 8 << 4 | 8;
969 int id_code = xpdr_id_code->getIntValue();
971 for ( i = 0; i < 4; ++i ) {
972 digits[i] = id_code / place;
973 id_code -= digits[i] * place;
976 radio_display_data[36] = digits[2] << 4 | digits[3];
977 radio_display_data[37] = digits[0] << 4 | digits[1];
978 radio_display_data[38] = 0xff;
980 if ( xpdr_func_knob->getIntValue() == 3 ||
981 xpdr_func_knob->getIntValue() == 5 )
983 // do flight level display
984 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
985 for ( i = 0; i < 6; ++i ) {
988 radio_display_data[39] = digits[2] << 4 | 0x0f;
989 radio_display_data[40] = digits[0] << 4 | digits[1];
991 // blank flight level display
992 radio_display_data[39] = 0xff;
993 radio_display_data[40] = 0xff;
998 radio_display_data[36] = 0xff;
999 radio_display_data[37] = 0xff;
1000 radio_display_data[38] = 0xff;
1001 radio_display_data[39] = 0xff;
1002 radio_display_data[40] = 0xff;
1005 ATCSetRadios( radio_display_fd, radio_display_data );
1011 /////////////////////////////////////////////////////////////////////
1012 // Drive the stepper motors
1013 /////////////////////////////////////////////////////////////////////
1015 bool FGATCOutput::do_steppers() {
1016 SGPropertyNode *mag_compass
1017 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
1020 float diff = mag_compass->getFloatValue() - compass_position;
1021 while ( diff < -180.0 ) { diff += 360.0; }
1022 while ( diff > 180.0 ) { diff -= 360.0; }
1024 int steps = (int)(diff * 4);
1025 // cout << "steps = " << steps << endl;
1026 if ( steps > 4 ) { steps = 4; }
1027 if ( steps < -4 ) { steps = -4; }
1029 if ( abs(steps) > 0 ) {
1030 unsigned char cmd = 0x80; // stepper command
1032 cmd |= 0x20; // go up
1034 cmd |= 0x00; // go down
1038 // sync compass_position with hardware position
1039 compass_position += (float)steps / 4.0;
1041 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1048 // process the hardware outputs. This code assumes the calling layer
1049 // will lock the hardware.
1050 bool FGATCOutput::process() {
1052 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
1060 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
1068 bool FGATCOutput::close() {
1070 #if defined( unix ) || defined( __CYGWIN__ )
1078 result = ::close( lamps_fd );
1079 if ( result == -1 ) {
1080 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1082 snprintf( msg, 256, "Error closing %s", lamps_file );
1087 result = ::close( radio_display_fd );
1088 if ( result == -1 ) {
1089 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1091 snprintf( msg, 256, "Error closing %s", radio_display_file );
1096 result = ::close( stepper_fd );
1097 if ( result == -1 ) {
1098 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1100 snprintf( msg, 256, "Error closing %s", stepper_file );