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.
23 // TODO FIXME Module still contains lots of "static SGPropertyNode"s below.
29 #include <simgear/compiler.h>
31 #if defined( unix ) || defined( __CYGWIN__ )
32 # include <sys/types.h>
33 # include <sys/stat.h>
46 #include <simgear/debug/logstream.hxx>
47 #include <simgear/misc/sg_path.hxx>
48 #include <simgear/props/props_io.hxx>
50 #include <Main/fg_props.hxx>
52 #include "ATC-Outputs.hxx"
58 // Lock the ATC hardware
59 static int ATCLock( int fd ) {
60 #if defined( unix ) || defined( __CYGWIN__ )
62 lseek( fd, 0, SEEK_SET );
65 int result = read( fd, tmp, 1 );
67 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
77 // Release the ATC hardware
78 static int ATCRelease( int fd ) {
79 #if defined( unix ) || defined( __CYGWIN__ )
81 lseek( fd, 0, SEEK_SET );
85 int result = write( fd, tmp, 1 );
88 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
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)
101 FGATCOutput::FGATCOutput( const int _board, const SGPath &_config_file ) :
103 analog_out_node(NULL),
104 lamps_out_node(NULL),
105 radio_display_node(NULL),
109 config = _config_file;
113 // Write analog out data
114 static int ATCSetAnalogOut( int fd,
115 unsigned char data[ATC_ANALOG_OUT_CHANNELS*2] )
117 #if defined( unix ) || defined( __CYGWIN__ )
119 lseek( fd, 0, SEEK_SET );
121 int result = write( fd, data, ATC_ANALOG_OUT_CHANNELS*2 );
123 if ( result != ATC_ANALOG_OUT_CHANNELS*2 ) {
124 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
134 // Write a radios command
135 static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
136 #if defined( unix ) || defined( __CYGWIN__ )
138 lseek( fd, 0, SEEK_SET );
140 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
142 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
143 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
153 // Write a stepper command
154 static int ATCSetStepper( int fd, unsigned char channel,
155 unsigned char value )
157 #if defined( unix ) || defined( __CYGWIN__ )
159 lseek( fd, 0, SEEK_SET );
162 unsigned char buf[3];
166 int result = write( fd, buf, 2 );
168 SG_LOG( SG_IO, SG_INFO, "Write failed" );
170 SG_LOG( SG_IO, SG_DEBUG,
171 "Sent cmd = " << (int)channel << " value = " << (int)value );
179 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
180 // Read status of last stepper written to
181 static unsigned char ATCReadStepper( int fd ) {
182 #if defined( unix ) || defined( __CYGWIN__ )
186 lseek( fd, 0, SEEK_SET );
189 unsigned char buf[2];
190 result = read( fd, buf, 1 );
192 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
195 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
205 // Turn a lamp on or off
206 void 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
211 // bits 0-6 are the lamp address
212 // bit 7 is the value (on/off)
217 unsigned char buf[3];
221 result = write( fd, buf, 2 );
223 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
230 void FGATCOutput::init_config() {
231 #if defined( unix ) || defined( __CYGWIN__ )
232 if ( config.str()[0] != '/' ) {
233 // not an absolute path, prepend the standard location
235 char *envp = ::getenv( "HOME" );
236 if ( envp != NULL ) {
238 tmp.append( ".atcflightsim" );
239 tmp.append( config.str() );
243 readProperties( config.str(), globals->get_props() );
248 // Open and initialize the ATC hardware
249 bool FGATCOutput::open( int lock_fd ) {
251 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
256 // This loads the config parameters generated by "simcal"
259 SG_LOG( SG_IO, SG_ALERT,
260 "Initializing ATC output hardware, please wait ..." );
262 snprintf( analog_out_file, 256,
263 "/proc/atcflightsim/board%d/analog_out", board );
264 snprintf( lamps_file, 256,
265 "/proc/atcflightsim/board%d/lamps", board );
266 snprintf( radio_display_file, 256,
267 "/proc/atcflightsim/board%d/radios", board );
268 snprintf( stepper_file, 256,
269 "/proc/atcflightsim/board%d/steppers", board );
271 #if defined( unix ) || defined( __CYGWIN__ )
273 /////////////////////////////////////////////////////////////////////
274 // Open the /proc files
275 /////////////////////////////////////////////////////////////////////
277 analog_out_fd = ::open( analog_out_file, O_WRONLY );
278 if ( analog_out_fd == -1 ) {
279 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
281 snprintf( msg, 256, "Error opening %s", analog_out_file );
286 lamps_fd = ::open( lamps_file, O_WRONLY );
287 if ( lamps_fd == -1 ) {
288 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
290 snprintf( msg, 256, "Error opening %s", lamps_file );
295 radio_display_fd = ::open( radio_display_file, O_RDWR );
296 if ( radio_display_fd == -1 ) {
297 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
299 snprintf( msg, 256, "Error opening %s", radio_display_file );
304 stepper_fd = ::open( stepper_file, O_RDWR );
305 if ( stepper_fd == -1 ) {
306 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
308 snprintf( msg, 256, "Error opening %s", stepper_file );
315 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
316 /////////////////////////////////////////////////////////////////////
317 // Home the compass stepper motor
318 /////////////////////////////////////////////////////////////////////
320 SG_LOG( SG_IO, SG_ALERT,
321 " - Homing the compass stepper motor" );
323 // Lock the hardware, keep trying until we succeed
324 while ( ATCLock( lock_fd ) <= 0 );
326 // Send the stepper home command
327 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
329 // Release the hardware
330 ATCRelease( lock_fd );
332 SG_LOG( SG_IO, SG_ALERT,
333 " - Waiting for compass to come home." );
336 int timeout = 900; // about 30 seconds
338 while ( ! home && timeout > 0 ) {
339 if ( timeout % 150 == 0 ) {
340 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
342 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
345 while ( ATCLock( lock_fd ) <= 0 );
347 unsigned char result = ATCReadStepper( stepper_fd );
352 ATCRelease( lock_fd );
363 compass_position = 0.0;
366 // Lock the hardware, keep trying until we succeed
367 while ( ATCLock( lock_fd ) <= 0 );
369 /////////////////////////////////////////////////////////////////////
370 // Zero the analog outputs
371 /////////////////////////////////////////////////////////////////////
373 SG_LOG( SG_IO, SG_ALERT,
374 " - Zeroing Analog Outputs." );
376 for ( int channel = 0; channel < ATC_ANALOG_OUT_CHANNELS; ++channel ) {
377 analog_out_data[2*channel] = 0;
378 analog_out_data[2*channel + 1] = 0;
380 ATCSetAnalogOut( analog_out_fd, analog_out_data );
383 /////////////////////////////////////////////////////////////////////
384 // Blank the radio display
385 /////////////////////////////////////////////////////////////////////
387 SG_LOG( SG_IO, SG_ALERT,
388 " - Clearing the radios displays." );
391 unsigned char value = 0xff;
392 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
393 radio_display_data[channel] = value;
395 ATCSetRadios( radio_display_fd, radio_display_data );
397 ATCRelease( lock_fd );
399 /////////////////////////////////////////////////////////////////////
401 /////////////////////////////////////////////////////////////////////
403 for ( int i = 0; i < 128; ++i ) {
404 ATCSetLamp( lamps_fd, i, false );
407 /////////////////////////////////////////////////////////////////////
408 // Finished initing hardware
409 /////////////////////////////////////////////////////////////////////
411 SG_LOG( SG_IO, SG_ALERT,
412 "Done initializing ATC output hardware." );
416 /////////////////////////////////////////////////////////////////////
417 // Connect up to property values
418 /////////////////////////////////////////////////////////////////////
422 snprintf( base_name, 256, "/output/atc-board[%d]/analog-outputs", board );
423 analog_out_node = fgGetNode( base_name );
425 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
426 lamps_out_node = fgGetNode( base_name );
428 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
429 radio_display_node = fgGetNode( base_name );
431 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
432 steppers_node = fgGetNode( base_name );
438 /////////////////////////////////////////////////////////////////////
439 // Write the lanalog outputs
440 /////////////////////////////////////////////////////////////////////
442 bool FGATCOutput::do_analog_out() {
443 if ( analog_out_node != NULL ) {
444 for ( int i = 0; i < analog_out_node->nChildren(); ++i ) {
445 // read the next config entry from the property tree
447 SGPropertyNode *child = analog_out_node->getChild(i);
448 string cname = child->getName();
449 int index = child->getIndex();
452 SGPropertyNode *src_prop = NULL;
453 double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
454 if ( cname == "analog-out" ) {
455 SGPropertyNode *prop;
456 prop = child->getChild( "name" );
457 if ( prop != NULL ) {
458 name = prop->getStringValue();
460 prop = child->getChild( "type" );
461 if ( prop != NULL ) {
462 type = prop->getStringValue();
464 prop = child->getChild( "prop" );
465 if ( prop != NULL ) {
466 src_prop = fgGetNode( prop->getStringValue(), true );
468 prop = child->getChild( "value-lo" );
469 if ( prop != NULL ) {
470 x0 = prop->getDoubleValue();
472 prop = child->getChild( "meter-lo" );
473 if ( prop != NULL ) {
474 y0 = prop->getDoubleValue();
476 prop = child->getChild( "value-hi" );
477 if ( prop != NULL ) {
478 x1 = prop->getDoubleValue();
480 prop = child->getChild( "meter-hi" );
481 if ( prop != NULL ) {
482 y1 = prop->getDoubleValue();
484 // crunch linear interpolation formula
487 double slope = dy / dx;
488 double value = src_prop->getDoubleValue();
489 int meter = (value - x0) * slope + y0;
490 if ( meter < 0 ) { meter = 0; }
491 if ( meter > 1023 ) { meter = 1023; }
492 analog_out_data[2*index] = meter / 256;
493 analog_out_data[2*index + 1] = meter - analog_out_data[2*index] * 256;
495 SG_LOG( SG_IO, SG_DEBUG,
496 "Input config error, expecting 'analog-out' but found "
499 ATCSetAnalogOut( analog_out_fd, analog_out_data );
507 /////////////////////////////////////////////////////////////////////
509 /////////////////////////////////////////////////////////////////////
511 bool FGATCOutput::do_lamps() {
512 if ( lamps_out_node != NULL ) {
513 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
514 // read the next config entry from the property tree
516 SGPropertyNode *child = lamps_out_node->getChild(i);
517 string cname = child->getName();
518 int index = child->getIndex();
521 SGPropertyNode *src_prop = NULL;
522 if ( cname == "lamp" ) {
523 SGPropertyNode *prop;
524 prop = child->getChild( "name" );
525 if ( prop != NULL ) {
526 name = prop->getStringValue();
528 prop = child->getChild( "type" );
529 if ( prop != NULL ) {
530 type = prop->getStringValue();
532 prop = child->getChild( "prop" );
533 if ( prop != NULL ) {
534 src_prop = fgGetNode( prop->getStringValue(), true );
536 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
538 SG_LOG( SG_IO, SG_DEBUG,
539 "Input config error, expecting 'lamp' but found "
549 /////////////////////////////////////////////////////////////////////
550 // Update the radio display
551 /////////////////////////////////////////////////////////////////////
554 static bool navcom1_has_power() {
555 static SGPropertyNode *navcom1_bus_power
556 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
557 static SGPropertyNode *navcom1_power_btn
558 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
560 return (navcom1_bus_power->getDoubleValue() > 1.0)
561 && navcom1_power_btn->getBoolValue();
564 static bool navcom2_has_power() {
565 static SGPropertyNode *navcom2_bus_power
566 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
567 static SGPropertyNode *navcom2_power_btn
568 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
570 return (navcom2_bus_power->getDoubleValue() > 1.0)
571 && navcom2_power_btn->getBoolValue();
574 static bool dme_has_power() {
575 static SGPropertyNode *dme_bus_power
576 = fgGetNode( "/systems/electrical/outputs/dme", true );
578 return (dme_bus_power->getDoubleValue() > 1.0);
581 static bool adf_has_power() {
582 static SGPropertyNode *adf_bus_power
583 = fgGetNode( "/systems/electrical/outputs/adf", true );
584 static SGPropertyNode *adf_power_btn
585 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
587 return (adf_bus_power->getDoubleValue() > 1.0)
588 && adf_power_btn->getBoolValue();
591 static bool xpdr_has_power() {
592 static SGPropertyNode *xpdr_bus_power
593 = fgGetNode( "/systems/electrical/outputs/transponder", true );
594 static SGPropertyNode *xpdr_func_knob
595 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
597 return (xpdr_bus_power->getDoubleValue() > 1.0)
598 && (xpdr_func_knob->getIntValue() > 0);
601 bool FGATCOutput::do_radio_display() {
602 static SGPropertyNode *dme_serviceable
603 = fgGetNode( "/instrumentation/dme/serviceable", true );
604 static SGPropertyNode *dme_in_range
605 = fgGetNode( "/instrumentation/dme/in-range", true );
606 static SGPropertyNode *dme_min
607 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
608 static SGPropertyNode *dme_kt
609 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
610 static SGPropertyNode *dme_nm
611 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
613 static SGPropertyNode *comm1_serviceable
614 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
615 static SGPropertyNode *com1_freq
616 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
617 static SGPropertyNode *com1_stby_freq
618 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
620 static SGPropertyNode *comm2_serviceable
621 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
622 static SGPropertyNode *com2_freq
623 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
624 static SGPropertyNode *com2_stby_freq
625 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
627 static SGPropertyNode *nav1_serviceable
628 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
629 static SGPropertyNode *nav1_freq
630 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
631 static SGPropertyNode *nav1_stby_freq
632 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
634 static SGPropertyNode *nav2_serviceable
635 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
636 static SGPropertyNode *nav2_freq
637 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
638 static SGPropertyNode *nav2_stby_freq
639 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
641 static SGPropertyNode *adf_serviceable
642 = fgGetNode( "/instrumentation/adf/serviceable", true );
643 static SGPropertyNode *adf_freq
644 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
645 static SGPropertyNode *adf_stby_freq
646 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
647 static SGPropertyNode *adf_stby_mode
648 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
649 static SGPropertyNode *adf_timer_mode
650 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
651 // static SGPropertyNode *adf_count_mode
652 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
653 static SGPropertyNode *adf_flight_timer
654 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
655 static SGPropertyNode *adf_elapsed_timer
656 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
658 static SGPropertyNode *xpdr_serviceable
659 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
660 static SGPropertyNode *xpdr_func_knob
661 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
662 static SGPropertyNode *xpdr_flight_level
663 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
664 static SGPropertyNode *xpdr_id_code
665 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
670 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
671 if ( dme_in_range->getBoolValue() ) {
673 float minutes = dme_min->getFloatValue();
674 if ( minutes > 999 ) {
677 snprintf(digits, 7, "%03.0f", minutes);
678 for ( i = 0; i < 6; ++i ) {
681 radio_display_data[0] = digits[1] << 4 | digits[2];
682 radio_display_data[1] = 0xf0 | digits[0];
685 float knots = dme_kt->getFloatValue();
689 snprintf(digits, 7, "%03.0f", knots);
690 for ( i = 0; i < 6; ++i ) {
693 radio_display_data[2] = digits[1] << 4 | digits[2];
694 radio_display_data[3] = 0xf0 | digits[0];
697 float nm = dme_nm->getFloatValue();
701 snprintf(digits, 7, "%04.1f", nm);
702 for ( i = 0; i < 6; ++i ) {
705 radio_display_data[4] = digits[1] << 4 | digits[3];
706 radio_display_data[5] = 0x00 | digits[0];
707 // the 0x00 in the upper nibble of the 6th byte of each
708 // display turns on the decimal point
711 radio_display_data[0] = 0xbb;
712 radio_display_data[1] = 0xfb;
713 radio_display_data[2] = 0xbb;
714 radio_display_data[3] = 0xfb;
715 radio_display_data[4] = 0xbb;
716 radio_display_data[5] = 0x0b;
720 for ( i = 0; i < 6; ++i ) {
721 radio_display_data[i] = 0xff;
725 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
726 // Com1 standby frequency
727 float com1_stby = com1_stby_freq->getFloatValue();
728 if ( fabs(com1_stby) > 999.99 ) {
731 snprintf(digits, 7, "%06.3f", com1_stby);
732 for ( i = 0; i < 6; ++i ) {
735 radio_display_data[6] = digits[4] << 4 | digits[5];
736 radio_display_data[7] = digits[1] << 4 | digits[2];
737 radio_display_data[8] = 0xf0 | digits[0];
739 // Com1 in use frequency
740 float com1 = com1_freq->getFloatValue();
741 if ( fabs(com1) > 999.99 ) {
744 snprintf(digits, 7, "%06.3f", com1);
745 for ( i = 0; i < 6; ++i ) {
748 radio_display_data[9] = digits[4] << 4 | digits[5];
749 radio_display_data[10] = digits[1] << 4 | digits[2];
750 radio_display_data[11] = 0x00 | digits[0];
751 // the 0x00 in the upper nibble of the 6th byte of each display
752 // turns on the decimal point
754 radio_display_data[6] = 0xff;
755 radio_display_data[7] = 0xff;
756 radio_display_data[8] = 0xff;
757 radio_display_data[9] = 0xff;
758 radio_display_data[10] = 0xff;
759 radio_display_data[11] = 0xff;
762 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
763 // Com2 standby frequency
764 float com2_stby = com2_stby_freq->getFloatValue();
765 if ( fabs(com2_stby) > 999.99 ) {
768 snprintf(digits, 7, "%06.3f", com2_stby);
769 for ( i = 0; i < 6; ++i ) {
772 radio_display_data[18] = digits[4] << 4 | digits[5];
773 radio_display_data[19] = digits[1] << 4 | digits[2];
774 radio_display_data[20] = 0xf0 | digits[0];
776 // Com2 in use frequency
777 float com2 = com2_freq->getFloatValue();
778 if ( fabs(com2) > 999.99 ) {
781 snprintf(digits, 7, "%06.3f", com2);
782 for ( i = 0; i < 6; ++i ) {
785 radio_display_data[21] = digits[4] << 4 | digits[5];
786 radio_display_data[22] = digits[1] << 4 | digits[2];
787 radio_display_data[23] = 0x00 | digits[0];
788 // the 0x00 in the upper nibble of the 6th byte of each display
789 // turns on the decimal point
791 radio_display_data[18] = 0xff;
792 radio_display_data[19] = 0xff;
793 radio_display_data[20] = 0xff;
794 radio_display_data[21] = 0xff;
795 radio_display_data[22] = 0xff;
796 radio_display_data[23] = 0xff;
799 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
800 // Nav1 standby frequency
801 float nav1_stby = nav1_stby_freq->getFloatValue();
802 if ( fabs(nav1_stby) > 999.99 ) {
805 snprintf(digits, 7, "%06.2f", nav1_stby);
806 for ( i = 0; i < 6; ++i ) {
809 radio_display_data[12] = digits[4] << 4 | digits[5];
810 radio_display_data[13] = digits[1] << 4 | digits[2];
811 radio_display_data[14] = 0xf0 | digits[0];
813 // Nav1 in use frequency
814 float nav1 = nav1_freq->getFloatValue();
815 if ( fabs(nav1) > 999.99 ) {
818 snprintf(digits, 7, "%06.2f", nav1);
819 for ( i = 0; i < 6; ++i ) {
822 radio_display_data[15] = digits[4] << 4 | digits[5];
823 radio_display_data[16] = digits[1] << 4 | digits[2];
824 radio_display_data[17] = 0x00 | digits[0];
825 // the 0x00 in the upper nibble of the 6th byte of each display
826 // turns on the decimal point
828 radio_display_data[12] = 0xff;
829 radio_display_data[13] = 0xff;
830 radio_display_data[14] = 0xff;
831 radio_display_data[15] = 0xff;
832 radio_display_data[16] = 0xff;
833 radio_display_data[17] = 0xff;
836 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
837 // Nav2 standby frequency
838 float nav2_stby = nav2_stby_freq->getFloatValue();
839 if ( fabs(nav2_stby) > 999.99 ) {
842 snprintf(digits, 7, "%06.2f", nav2_stby);
843 for ( i = 0; i < 6; ++i ) {
846 radio_display_data[24] = digits[4] << 4 | digits[5];
847 radio_display_data[25] = digits[1] << 4 | digits[2];
848 radio_display_data[26] = 0xf0 | digits[0];
850 // Nav2 in use frequency
851 float nav2 = nav2_freq->getFloatValue();
852 if ( fabs(nav2) > 999.99 ) {
855 snprintf(digits, 7, "%06.2f", nav2);
856 for ( i = 0; i < 6; ++i ) {
859 radio_display_data[27] = digits[4] << 4 | digits[5];
860 radio_display_data[28] = digits[1] << 4 | digits[2];
861 radio_display_data[29] = 0x00 | digits[0];
862 // the 0x00 in the upper nibble of the 6th byte of each display
863 // turns on the decimal point
865 radio_display_data[24] = 0xff;
866 radio_display_data[25] = 0xff;
867 radio_display_data[26] = 0xff;
868 radio_display_data[27] = 0xff;
869 radio_display_data[28] = 0xff;
870 radio_display_data[29] = 0xff;
873 // ADF standby frequency / timer
874 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
875 if ( adf_stby_mode->getIntValue() == 0 ) {
877 float adf_stby = adf_stby_freq->getFloatValue();
878 if ( fabs(adf_stby) > 1799 ) {
881 snprintf(digits, 7, "%04.0f", adf_stby);
882 for ( i = 0; i < 6; ++i ) {
885 radio_display_data[30] = digits[3] << 4 | 0x0f;
886 radio_display_data[31] = digits[1] << 4 | digits[2];
887 if ( digits[0] == 0 ) {
888 radio_display_data[32] = 0xff;
890 radio_display_data[32] = 0xf0 | digits[0];
896 if ( adf_timer_mode->getIntValue() == 0 ) {
897 time = adf_flight_timer->getDoubleValue();
899 time = adf_elapsed_timer->getDoubleValue();
901 // cout << time << endl;
902 hours = (int)(time / 3600.0);
903 time -= hours * 3600.00;
904 min = (int)(time / 60.0);
921 // cout << big << ":" << little << endl;
922 snprintf(digits, 7, "%02d%02d", big, little);
923 for ( i = 0; i < 6; ++i ) {
926 radio_display_data[30] = digits[2] << 4 | digits[3];
927 radio_display_data[31] = digits[0] << 4 | digits[1];
928 radio_display_data[32] = 0xff;
931 // ADF in use frequency
932 float adf = adf_freq->getFloatValue();
933 if ( fabs(adf) > 1799 ) {
936 snprintf(digits, 7, "%04.0f", adf);
937 for ( i = 0; i < 6; ++i ) {
940 radio_display_data[33] = digits[2] << 4 | digits[3];
941 if ( digits[0] == 0 ) {
942 radio_display_data[34] = 0xf0 | digits[1];
944 radio_display_data[34] = digits[0] << 4 | digits[1];
946 if ( adf_stby_mode->getIntValue() == 0 ) {
947 radio_display_data[35] = 0xff;
949 radio_display_data[35] = 0x0f;
952 radio_display_data[30] = 0xff;
953 radio_display_data[31] = 0xff;
954 radio_display_data[32] = 0xff;
955 radio_display_data[33] = 0xff;
956 radio_display_data[34] = 0xff;
957 radio_display_data[35] = 0xff;
960 // Transponder code and flight level
961 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
962 if ( xpdr_func_knob->getIntValue() == 2 ) {
964 radio_display_data[36] = 8 << 4 | 8;
965 radio_display_data[37] = 8 << 4 | 8;
966 radio_display_data[38] = 0xff;
967 radio_display_data[39] = 8 << 4 | 0x0f;
968 radio_display_data[40] = 8 << 4 | 8;
971 int id_code = xpdr_id_code->getIntValue();
973 for ( i = 0; i < 4; ++i ) {
974 digits[i] = id_code / place;
975 id_code -= digits[i] * place;
978 radio_display_data[36] = digits[2] << 4 | digits[3];
979 radio_display_data[37] = digits[0] << 4 | digits[1];
980 radio_display_data[38] = 0xff;
982 if ( xpdr_func_knob->getIntValue() == 3 ||
983 xpdr_func_knob->getIntValue() == 5 )
985 // do flight level display
986 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
987 for ( i = 0; i < 6; ++i ) {
990 radio_display_data[39] = digits[2] << 4 | 0x0f;
991 radio_display_data[40] = digits[0] << 4 | digits[1];
993 // blank flight level display
994 radio_display_data[39] = 0xff;
995 radio_display_data[40] = 0xff;
1000 radio_display_data[36] = 0xff;
1001 radio_display_data[37] = 0xff;
1002 radio_display_data[38] = 0xff;
1003 radio_display_data[39] = 0xff;
1004 radio_display_data[40] = 0xff;
1007 ATCSetRadios( radio_display_fd, radio_display_data );
1013 /////////////////////////////////////////////////////////////////////
1014 // Drive the stepper motors
1015 /////////////////////////////////////////////////////////////////////
1017 bool FGATCOutput::do_steppers() {
1018 SGPropertyNode *mag_compass
1019 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
1022 float diff = mag_compass->getFloatValue() - compass_position;
1023 while ( diff < -180.0 ) { diff += 360.0; }
1024 while ( diff > 180.0 ) { diff -= 360.0; }
1026 int steps = (int)(diff * 4);
1027 // cout << "steps = " << steps << endl;
1028 if ( steps > 4 ) { steps = 4; }
1029 if ( steps < -4 ) { steps = -4; }
1031 if ( abs(steps) > 0 ) {
1032 unsigned char cmd = 0x80; // stepper command
1034 cmd |= 0x20; // go up
1036 cmd |= 0x00; // go down
1040 // sync compass_position with hardware position
1041 compass_position += (float)steps / 4.0;
1043 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1050 // process the hardware outputs. This code assumes the calling layer
1051 // will lock the hardware.
1052 bool FGATCOutput::process() {
1054 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
1062 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
1070 bool FGATCOutput::close() {
1072 #if defined( unix ) || defined( __CYGWIN__ )
1080 result = ::close( lamps_fd );
1081 if ( result == -1 ) {
1082 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1084 snprintf( msg, 256, "Error closing %s", lamps_file );
1089 result = ::close( radio_display_fd );
1090 if ( result == -1 ) {
1091 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1093 snprintf( msg, 256, "Error closing %s", radio_display_file );
1098 result = ::close( stepper_fd );
1099 if ( result == -1 ) {
1100 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1102 snprintf( msg, 256, "Error closing %s", stepper_file );