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 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
177 // Read status of last stepper written to
178 static unsigned char ATCReadStepper( int fd ) {
179 #if defined( unix ) || defined( __CYGWIN__ )
183 lseek( fd, 0, SEEK_SET );
186 unsigned char buf[2];
187 result = read( fd, buf, 1 );
189 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
192 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
202 // Turn a lamp on or off
203 void ATCSetLamp( int fd, int channel, bool value ) {
204 #if defined( unix ) || defined( __CYGWIN__ )
205 // lamp channels 0-63 are written to LampPort0, channels 64-127
206 // are written to LampPort1
208 // bits 0-6 are the lamp address
209 // bit 7 is the value (on/off)
214 unsigned char buf[3];
218 result = write( fd, buf, 2 );
220 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
227 void FGATCOutput::init_config() {
228 #if defined( unix ) || defined( __CYGWIN__ )
229 if ( config.str()[0] != '/' ) {
230 // not an absolute path, prepend the standard location
232 char *envp = ::getenv( "HOME" );
233 if ( envp != NULL ) {
235 tmp.append( ".atcflightsim" );
236 tmp.append( config.str() );
240 readProperties( config.str(), globals->get_props() );
245 // Open and initialize the ATC hardware
246 bool FGATCOutput::open( int lock_fd ) {
248 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
253 // This loads the config parameters generated by "simcal"
256 SG_LOG( SG_IO, SG_ALERT,
257 "Initializing ATC output hardware, please wait ..." );
259 snprintf( analog_out_file, 256,
260 "/proc/atcflightsim/board%d/analog_out", board );
261 snprintf( lamps_file, 256,
262 "/proc/atcflightsim/board%d/lamps", board );
263 snprintf( radio_display_file, 256,
264 "/proc/atcflightsim/board%d/radios", board );
265 snprintf( stepper_file, 256,
266 "/proc/atcflightsim/board%d/steppers", board );
268 #if defined( unix ) || defined( __CYGWIN__ )
270 /////////////////////////////////////////////////////////////////////
271 // Open the /proc files
272 /////////////////////////////////////////////////////////////////////
274 analog_out_fd = ::open( analog_out_file, O_WRONLY );
275 if ( analog_out_fd == -1 ) {
276 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
278 snprintf( msg, 256, "Error opening %s", analog_out_file );
283 lamps_fd = ::open( lamps_file, O_WRONLY );
284 if ( lamps_fd == -1 ) {
285 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
287 snprintf( msg, 256, "Error opening %s", lamps_file );
292 radio_display_fd = ::open( radio_display_file, O_RDWR );
293 if ( radio_display_fd == -1 ) {
294 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
296 snprintf( msg, 256, "Error opening %s", radio_display_file );
301 stepper_fd = ::open( stepper_file, O_RDWR );
302 if ( stepper_fd == -1 ) {
303 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
305 snprintf( msg, 256, "Error opening %s", stepper_file );
312 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
313 /////////////////////////////////////////////////////////////////////
314 // Home the compass stepper motor
315 /////////////////////////////////////////////////////////////////////
317 SG_LOG( SG_IO, SG_ALERT,
318 " - Homing the compass stepper motor" );
320 // Lock the hardware, keep trying until we succeed
321 while ( ATCLock( lock_fd ) <= 0 );
323 // Send the stepper home command
324 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
326 // Release the hardware
327 ATCRelease( lock_fd );
329 SG_LOG( SG_IO, SG_ALERT,
330 " - Waiting for compass to come home." );
333 int timeout = 900; // about 30 seconds
335 while ( ! home && timeout > 0 ) {
336 if ( timeout % 150 == 0 ) {
337 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
339 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
342 while ( ATCLock( lock_fd ) <= 0 );
344 unsigned char result = ATCReadStepper( stepper_fd );
349 ATCRelease( lock_fd );
351 #if defined( _MSC_VER )
352 ulMilliSecondSleep(33);
353 #elif defined (WIN32) && !defined(__CYGWIN__)
362 compass_position = 0.0;
365 // Lock the hardware, keep trying until we succeed
366 while ( ATCLock( lock_fd ) <= 0 );
368 /////////////////////////////////////////////////////////////////////
369 // Zero the analog outputs
370 /////////////////////////////////////////////////////////////////////
372 SG_LOG( SG_IO, SG_ALERT,
373 " - Zeroing Analog Outputs." );
375 for ( int channel = 0; channel < ATC_ANALOG_OUT_CHANNELS; ++channel ) {
376 analog_out_data[2*channel] = 0;
377 analog_out_data[2*channel + 1] = 0;
379 ATCSetAnalogOut( analog_out_fd, analog_out_data );
382 /////////////////////////////////////////////////////////////////////
383 // Blank the radio display
384 /////////////////////////////////////////////////////////////////////
386 SG_LOG( SG_IO, SG_ALERT,
387 " - Clearing the radios displays." );
390 unsigned char value = 0xff;
391 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
392 radio_display_data[channel] = value;
394 ATCSetRadios( radio_display_fd, radio_display_data );
396 ATCRelease( lock_fd );
398 /////////////////////////////////////////////////////////////////////
400 /////////////////////////////////////////////////////////////////////
402 for ( int i = 0; i < 128; ++i ) {
403 ATCSetLamp( lamps_fd, i, false );
406 /////////////////////////////////////////////////////////////////////
407 // Finished initing hardware
408 /////////////////////////////////////////////////////////////////////
410 SG_LOG( SG_IO, SG_ALERT,
411 "Done initializing ATC output hardware." );
415 /////////////////////////////////////////////////////////////////////
416 // Connect up to property values
417 /////////////////////////////////////////////////////////////////////
421 snprintf( base_name, 256, "/output/atc-board[%d]/analog-outputs", board );
422 analog_out_node = fgGetNode( base_name );
424 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
425 lamps_out_node = fgGetNode( base_name );
427 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
428 radio_display_node = fgGetNode( base_name );
430 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
431 steppers_node = fgGetNode( base_name );
437 /////////////////////////////////////////////////////////////////////
438 // Write the lanalog outputs
439 /////////////////////////////////////////////////////////////////////
441 bool FGATCOutput::do_analog_out() {
442 if ( analog_out_node != NULL ) {
443 for ( int i = 0; i < analog_out_node->nChildren(); ++i ) {
444 // read the next config entry from the property tree
446 SGPropertyNode *child = analog_out_node->getChild(i);
447 string cname = child->getName();
448 int index = child->getIndex();
451 SGPropertyNode *src_prop = NULL;
452 double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
453 if ( cname == "analog-out" ) {
454 SGPropertyNode *prop;
455 prop = child->getChild( "name" );
456 if ( prop != NULL ) {
457 name = prop->getStringValue();
459 prop = child->getChild( "type" );
460 if ( prop != NULL ) {
461 type = prop->getStringValue();
463 prop = child->getChild( "prop" );
464 if ( prop != NULL ) {
465 src_prop = fgGetNode( prop->getStringValue(), true );
467 prop = child->getChild( "value-lo" );
468 if ( prop != NULL ) {
469 x0 = prop->getDoubleValue();
471 prop = child->getChild( "meter-lo" );
472 if ( prop != NULL ) {
473 y0 = prop->getDoubleValue();
475 prop = child->getChild( "value-hi" );
476 if ( prop != NULL ) {
477 x1 = prop->getDoubleValue();
479 prop = child->getChild( "meter-hi" );
480 if ( prop != NULL ) {
481 y1 = prop->getDoubleValue();
483 // crunch linear interpolation formula
486 double slope = dy / dx;
487 double value = src_prop->getDoubleValue();
488 int meter = (value - x0) * slope + y0;
489 if ( meter < 0 ) { meter = 0; }
490 if ( meter > 1023 ) { meter = 1023; }
491 analog_out_data[2*index] = meter / 256;
492 analog_out_data[2*index + 1] = meter - analog_out_data[2*index] * 256;
494 SG_LOG( SG_IO, SG_DEBUG,
495 "Input config error, expecting 'analog-out' but found "
498 ATCSetAnalogOut( analog_out_fd, analog_out_data );
506 /////////////////////////////////////////////////////////////////////
508 /////////////////////////////////////////////////////////////////////
510 bool FGATCOutput::do_lamps() {
511 if ( lamps_out_node != NULL ) {
512 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
513 // read the next config entry from the property tree
515 SGPropertyNode *child = lamps_out_node->getChild(i);
516 string cname = child->getName();
517 int index = child->getIndex();
520 SGPropertyNode *src_prop = NULL;
521 if ( cname == "lamp" ) {
522 SGPropertyNode *prop;
523 prop = child->getChild( "name" );
524 if ( prop != NULL ) {
525 name = prop->getStringValue();
527 prop = child->getChild( "type" );
528 if ( prop != NULL ) {
529 type = prop->getStringValue();
531 prop = child->getChild( "prop" );
532 if ( prop != NULL ) {
533 src_prop = fgGetNode( prop->getStringValue(), true );
535 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
537 SG_LOG( SG_IO, SG_DEBUG,
538 "Input config error, expecting 'lamp' but found "
548 /////////////////////////////////////////////////////////////////////
549 // Update the radio display
550 /////////////////////////////////////////////////////////////////////
553 static bool navcom1_has_power() {
554 static SGPropertyNode *navcom1_bus_power
555 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
556 static SGPropertyNode *navcom1_power_btn
557 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
559 return (navcom1_bus_power->getDoubleValue() > 1.0)
560 && navcom1_power_btn->getBoolValue();
563 static bool navcom2_has_power() {
564 static SGPropertyNode *navcom2_bus_power
565 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
566 static SGPropertyNode *navcom2_power_btn
567 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
569 return (navcom2_bus_power->getDoubleValue() > 1.0)
570 && navcom2_power_btn->getBoolValue();
573 static bool dme_has_power() {
574 static SGPropertyNode *dme_bus_power
575 = fgGetNode( "/systems/electrical/outputs/dme", true );
577 return (dme_bus_power->getDoubleValue() > 1.0);
580 static bool adf_has_power() {
581 static SGPropertyNode *adf_bus_power
582 = fgGetNode( "/systems/electrical/outputs/adf", true );
583 static SGPropertyNode *adf_power_btn
584 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
586 return (adf_bus_power->getDoubleValue() > 1.0)
587 && adf_power_btn->getBoolValue();
590 static bool xpdr_has_power() {
591 static SGPropertyNode *xpdr_bus_power
592 = fgGetNode( "/systems/electrical/outputs/transponder", true );
593 static SGPropertyNode *xpdr_func_knob
594 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
596 return (xpdr_bus_power->getDoubleValue() > 1.0)
597 && (xpdr_func_knob->getIntValue() > 0);
600 bool FGATCOutput::do_radio_display() {
601 static SGPropertyNode *dme_serviceable
602 = fgGetNode( "/instrumentation/dme/serviceable", true );
603 static SGPropertyNode *dme_in_range
604 = fgGetNode( "/instrumentation/dme/in-range", true );
605 static SGPropertyNode *dme_min
606 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
607 static SGPropertyNode *dme_kt
608 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
609 static SGPropertyNode *dme_nm
610 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
612 static SGPropertyNode *comm1_serviceable
613 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
614 static SGPropertyNode *com1_freq
615 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
616 static SGPropertyNode *com1_stby_freq
617 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
619 static SGPropertyNode *comm2_serviceable
620 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
621 static SGPropertyNode *com2_freq
622 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
623 static SGPropertyNode *com2_stby_freq
624 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
626 static SGPropertyNode *nav1_serviceable
627 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
628 static SGPropertyNode *nav1_freq
629 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
630 static SGPropertyNode *nav1_stby_freq
631 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
633 static SGPropertyNode *nav2_serviceable
634 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
635 static SGPropertyNode *nav2_freq
636 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
637 static SGPropertyNode *nav2_stby_freq
638 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
640 static SGPropertyNode *adf_serviceable
641 = fgGetNode( "/instrumentation/adf/serviceable", true );
642 static SGPropertyNode *adf_freq
643 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
644 static SGPropertyNode *adf_stby_freq
645 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
646 static SGPropertyNode *adf_stby_mode
647 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
648 static SGPropertyNode *adf_timer_mode
649 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
650 // static SGPropertyNode *adf_count_mode
651 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
652 static SGPropertyNode *adf_flight_timer
653 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
654 static SGPropertyNode *adf_elapsed_timer
655 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
657 static SGPropertyNode *xpdr_serviceable
658 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
659 static SGPropertyNode *xpdr_func_knob
660 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
661 static SGPropertyNode *xpdr_flight_level
662 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
663 static SGPropertyNode *xpdr_id_code
664 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
669 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
670 if ( dme_in_range->getBoolValue() ) {
672 float minutes = dme_min->getFloatValue();
673 if ( minutes > 999 ) {
676 snprintf(digits, 7, "%03.0f", minutes);
677 for ( i = 0; i < 6; ++i ) {
680 radio_display_data[0] = digits[1] << 4 | digits[2];
681 radio_display_data[1] = 0xf0 | digits[0];
684 float knots = dme_kt->getFloatValue();
688 snprintf(digits, 7, "%03.0f", knots);
689 for ( i = 0; i < 6; ++i ) {
692 radio_display_data[2] = digits[1] << 4 | digits[2];
693 radio_display_data[3] = 0xf0 | digits[0];
696 float nm = dme_nm->getFloatValue();
700 snprintf(digits, 7, "%04.1f", nm);
701 for ( i = 0; i < 6; ++i ) {
704 radio_display_data[4] = digits[1] << 4 | digits[3];
705 radio_display_data[5] = 0x00 | digits[0];
706 // the 0x00 in the upper nibble of the 6th byte of each
707 // display turns on the decimal point
710 radio_display_data[0] = 0xbb;
711 radio_display_data[1] = 0xfb;
712 radio_display_data[2] = 0xbb;
713 radio_display_data[3] = 0xfb;
714 radio_display_data[4] = 0xbb;
715 radio_display_data[5] = 0x0b;
719 for ( i = 0; i < 6; ++i ) {
720 radio_display_data[i] = 0xff;
724 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
725 // Com1 standby frequency
726 float com1_stby = com1_stby_freq->getFloatValue();
727 if ( fabs(com1_stby) > 999.99 ) {
730 snprintf(digits, 7, "%06.3f", com1_stby);
731 for ( i = 0; i < 6; ++i ) {
734 radio_display_data[6] = digits[4] << 4 | digits[5];
735 radio_display_data[7] = digits[1] << 4 | digits[2];
736 radio_display_data[8] = 0xf0 | digits[0];
738 // Com1 in use frequency
739 float com1 = com1_freq->getFloatValue();
740 if ( fabs(com1) > 999.99 ) {
743 snprintf(digits, 7, "%06.3f", com1);
744 for ( i = 0; i < 6; ++i ) {
747 radio_display_data[9] = digits[4] << 4 | digits[5];
748 radio_display_data[10] = digits[1] << 4 | digits[2];
749 radio_display_data[11] = 0x00 | digits[0];
750 // the 0x00 in the upper nibble of the 6th byte of each display
751 // turns on the decimal point
753 radio_display_data[6] = 0xff;
754 radio_display_data[7] = 0xff;
755 radio_display_data[8] = 0xff;
756 radio_display_data[9] = 0xff;
757 radio_display_data[10] = 0xff;
758 radio_display_data[11] = 0xff;
761 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
762 // Com2 standby frequency
763 float com2_stby = com2_stby_freq->getFloatValue();
764 if ( fabs(com2_stby) > 999.99 ) {
767 snprintf(digits, 7, "%06.3f", com2_stby);
768 for ( i = 0; i < 6; ++i ) {
771 radio_display_data[18] = digits[4] << 4 | digits[5];
772 radio_display_data[19] = digits[1] << 4 | digits[2];
773 radio_display_data[20] = 0xf0 | digits[0];
775 // Com2 in use frequency
776 float com2 = com2_freq->getFloatValue();
777 if ( fabs(com2) > 999.99 ) {
780 snprintf(digits, 7, "%06.3f", com2);
781 for ( i = 0; i < 6; ++i ) {
784 radio_display_data[21] = digits[4] << 4 | digits[5];
785 radio_display_data[22] = digits[1] << 4 | digits[2];
786 radio_display_data[23] = 0x00 | digits[0];
787 // the 0x00 in the upper nibble of the 6th byte of each display
788 // turns on the decimal point
790 radio_display_data[18] = 0xff;
791 radio_display_data[19] = 0xff;
792 radio_display_data[20] = 0xff;
793 radio_display_data[21] = 0xff;
794 radio_display_data[22] = 0xff;
795 radio_display_data[23] = 0xff;
798 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
799 // Nav1 standby frequency
800 float nav1_stby = nav1_stby_freq->getFloatValue();
801 if ( fabs(nav1_stby) > 999.99 ) {
804 snprintf(digits, 7, "%06.2f", nav1_stby);
805 for ( i = 0; i < 6; ++i ) {
808 radio_display_data[12] = digits[4] << 4 | digits[5];
809 radio_display_data[13] = digits[1] << 4 | digits[2];
810 radio_display_data[14] = 0xf0 | digits[0];
812 // Nav1 in use frequency
813 float nav1 = nav1_freq->getFloatValue();
814 if ( fabs(nav1) > 999.99 ) {
817 snprintf(digits, 7, "%06.2f", nav1);
818 for ( i = 0; i < 6; ++i ) {
821 radio_display_data[15] = digits[4] << 4 | digits[5];
822 radio_display_data[16] = digits[1] << 4 | digits[2];
823 radio_display_data[17] = 0x00 | digits[0];
824 // the 0x00 in the upper nibble of the 6th byte of each display
825 // turns on the decimal point
827 radio_display_data[12] = 0xff;
828 radio_display_data[13] = 0xff;
829 radio_display_data[14] = 0xff;
830 radio_display_data[15] = 0xff;
831 radio_display_data[16] = 0xff;
832 radio_display_data[17] = 0xff;
835 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
836 // Nav2 standby frequency
837 float nav2_stby = nav2_stby_freq->getFloatValue();
838 if ( fabs(nav2_stby) > 999.99 ) {
841 snprintf(digits, 7, "%06.2f", nav2_stby);
842 for ( i = 0; i < 6; ++i ) {
845 radio_display_data[24] = digits[4] << 4 | digits[5];
846 radio_display_data[25] = digits[1] << 4 | digits[2];
847 radio_display_data[26] = 0xf0 | digits[0];
849 // Nav2 in use frequency
850 float nav2 = nav2_freq->getFloatValue();
851 if ( fabs(nav2) > 999.99 ) {
854 snprintf(digits, 7, "%06.2f", nav2);
855 for ( i = 0; i < 6; ++i ) {
858 radio_display_data[27] = digits[4] << 4 | digits[5];
859 radio_display_data[28] = digits[1] << 4 | digits[2];
860 radio_display_data[29] = 0x00 | digits[0];
861 // the 0x00 in the upper nibble of the 6th byte of each display
862 // turns on the decimal point
864 radio_display_data[24] = 0xff;
865 radio_display_data[25] = 0xff;
866 radio_display_data[26] = 0xff;
867 radio_display_data[27] = 0xff;
868 radio_display_data[28] = 0xff;
869 radio_display_data[29] = 0xff;
872 // ADF standby frequency / timer
873 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
874 if ( adf_stby_mode->getIntValue() == 0 ) {
876 float adf_stby = adf_stby_freq->getFloatValue();
877 if ( fabs(adf_stby) > 1799 ) {
880 snprintf(digits, 7, "%04.0f", adf_stby);
881 for ( i = 0; i < 6; ++i ) {
884 radio_display_data[30] = digits[3] << 4 | 0x0f;
885 radio_display_data[31] = digits[1] << 4 | digits[2];
886 if ( digits[0] == 0 ) {
887 radio_display_data[32] = 0xff;
889 radio_display_data[32] = 0xf0 | digits[0];
895 if ( adf_timer_mode->getIntValue() == 0 ) {
896 time = adf_flight_timer->getDoubleValue();
898 time = adf_elapsed_timer->getDoubleValue();
900 // cout << time << endl;
901 hours = (int)(time / 3600.0);
902 time -= hours * 3600.00;
903 min = (int)(time / 60.0);
920 // cout << big << ":" << little << endl;
921 snprintf(digits, 7, "%02d%02d", big, little);
922 for ( i = 0; i < 6; ++i ) {
925 radio_display_data[30] = digits[2] << 4 | digits[3];
926 radio_display_data[31] = digits[0] << 4 | digits[1];
927 radio_display_data[32] = 0xff;
930 // ADF in use frequency
931 float adf = adf_freq->getFloatValue();
932 if ( fabs(adf) > 1799 ) {
935 snprintf(digits, 7, "%04.0f", adf);
936 for ( i = 0; i < 6; ++i ) {
939 radio_display_data[33] = digits[2] << 4 | digits[3];
940 if ( digits[0] == 0 ) {
941 radio_display_data[34] = 0xf0 | digits[1];
943 radio_display_data[34] = digits[0] << 4 | digits[1];
945 if ( adf_stby_mode->getIntValue() == 0 ) {
946 radio_display_data[35] = 0xff;
948 radio_display_data[35] = 0x0f;
951 radio_display_data[30] = 0xff;
952 radio_display_data[31] = 0xff;
953 radio_display_data[32] = 0xff;
954 radio_display_data[33] = 0xff;
955 radio_display_data[34] = 0xff;
956 radio_display_data[35] = 0xff;
959 // Transponder code and flight level
960 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
961 if ( xpdr_func_knob->getIntValue() == 2 ) {
963 radio_display_data[36] = 8 << 4 | 8;
964 radio_display_data[37] = 8 << 4 | 8;
965 radio_display_data[38] = 0xff;
966 radio_display_data[39] = 8 << 4 | 0x0f;
967 radio_display_data[40] = 8 << 4 | 8;
970 int id_code = xpdr_id_code->getIntValue();
972 for ( i = 0; i < 4; ++i ) {
973 digits[i] = id_code / place;
974 id_code -= digits[i] * place;
977 radio_display_data[36] = digits[2] << 4 | digits[3];
978 radio_display_data[37] = digits[0] << 4 | digits[1];
979 radio_display_data[38] = 0xff;
981 if ( xpdr_func_knob->getIntValue() == 3 ||
982 xpdr_func_knob->getIntValue() == 5 )
984 // do flight level display
985 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
986 for ( i = 0; i < 6; ++i ) {
989 radio_display_data[39] = digits[2] << 4 | 0x0f;
990 radio_display_data[40] = digits[0] << 4 | digits[1];
992 // blank flight level display
993 radio_display_data[39] = 0xff;
994 radio_display_data[40] = 0xff;
999 radio_display_data[36] = 0xff;
1000 radio_display_data[37] = 0xff;
1001 radio_display_data[38] = 0xff;
1002 radio_display_data[39] = 0xff;
1003 radio_display_data[40] = 0xff;
1006 ATCSetRadios( radio_display_fd, radio_display_data );
1012 /////////////////////////////////////////////////////////////////////
1013 // Drive the stepper motors
1014 /////////////////////////////////////////////////////////////////////
1016 bool FGATCOutput::do_steppers() {
1017 SGPropertyNode *mag_compass
1018 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
1021 float diff = mag_compass->getFloatValue() - compass_position;
1022 while ( diff < -180.0 ) { diff += 360.0; }
1023 while ( diff > 180.0 ) { diff -= 360.0; }
1025 int steps = (int)(diff * 4);
1026 // cout << "steps = " << steps << endl;
1027 if ( steps > 4 ) { steps = 4; }
1028 if ( steps < -4 ) { steps = -4; }
1030 if ( abs(steps) > 0 ) {
1031 unsigned char cmd = 0x80; // stepper command
1033 cmd |= 0x20; // go up
1035 cmd |= 0x00; // go down
1039 // sync compass_position with hardware position
1040 compass_position += (float)steps / 4.0;
1042 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1049 // process the hardware outputs. This code assumes the calling layer
1050 // will lock the hardware.
1051 bool FGATCOutput::process() {
1053 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
1061 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
1069 bool FGATCOutput::close() {
1071 #if defined( unix ) || defined( __CYGWIN__ )
1079 result = ::close( lamps_fd );
1080 if ( result == -1 ) {
1081 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1083 snprintf( msg, 256, "Error closing %s", lamps_file );
1088 result = ::close( radio_display_fd );
1089 if ( result == -1 ) {
1090 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1092 snprintf( msg, 256, "Error closing %s", radio_display_file );
1097 result = ::close( stepper_fd );
1098 if ( result == -1 ) {
1099 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1101 snprintf( msg, 256, "Error closing %s", stepper_file );