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.isAbsolute()) {
233 SGPath tmp = SGPath::home();
234 tmp.append( ".atcflightsim" );
235 tmp.append( config.utf8Str() );
239 readProperties( config, globals->get_props() );
244 // Open and initialize the ATC hardware
245 bool FGATCOutput::open( int lock_fd ) {
247 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
252 // This loads the config parameters generated by "simcal"
255 SG_LOG( SG_IO, SG_ALERT,
256 "Initializing ATC output hardware, please wait ..." );
258 snprintf( analog_out_file, 256,
259 "/proc/atcflightsim/board%d/analog_out", board );
260 snprintf( lamps_file, 256,
261 "/proc/atcflightsim/board%d/lamps", board );
262 snprintf( radio_display_file, 256,
263 "/proc/atcflightsim/board%d/radios", board );
264 snprintf( stepper_file, 256,
265 "/proc/atcflightsim/board%d/steppers", board );
267 #if defined( unix ) || defined( __CYGWIN__ )
269 /////////////////////////////////////////////////////////////////////
270 // Open the /proc files
271 /////////////////////////////////////////////////////////////////////
273 analog_out_fd = ::open( analog_out_file, O_WRONLY );
274 if ( analog_out_fd == -1 ) {
275 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
277 snprintf( msg, 256, "Error opening %s", analog_out_file );
282 lamps_fd = ::open( lamps_file, O_WRONLY );
283 if ( lamps_fd == -1 ) {
284 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
286 snprintf( msg, 256, "Error opening %s", lamps_file );
291 radio_display_fd = ::open( radio_display_file, O_RDWR );
292 if ( radio_display_fd == -1 ) {
293 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
295 snprintf( msg, 256, "Error opening %s", radio_display_file );
300 stepper_fd = ::open( stepper_file, O_RDWR );
301 if ( stepper_fd == -1 ) {
302 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
304 snprintf( msg, 256, "Error opening %s", stepper_file );
311 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
312 /////////////////////////////////////////////////////////////////////
313 // Home the compass stepper motor
314 /////////////////////////////////////////////////////////////////////
316 SG_LOG( SG_IO, SG_ALERT,
317 " - Homing the compass stepper motor" );
319 // Lock the hardware, keep trying until we succeed
320 while ( ATCLock( lock_fd ) <= 0 );
322 // Send the stepper home command
323 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
325 // Release the hardware
326 ATCRelease( lock_fd );
328 SG_LOG( SG_IO, SG_ALERT,
329 " - Waiting for compass to come home." );
332 int timeout = 900; // about 30 seconds
334 while ( ! home && timeout > 0 ) {
335 if ( timeout % 150 == 0 ) {
336 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
338 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
341 while ( ATCLock( lock_fd ) <= 0 );
343 unsigned char result = ATCReadStepper( stepper_fd );
348 ATCRelease( lock_fd );
359 compass_position = 0.0;
362 // Lock the hardware, keep trying until we succeed
363 while ( ATCLock( lock_fd ) <= 0 );
365 /////////////////////////////////////////////////////////////////////
366 // Zero the analog outputs
367 /////////////////////////////////////////////////////////////////////
369 SG_LOG( SG_IO, SG_ALERT,
370 " - Zeroing Analog Outputs." );
372 for ( int channel = 0; channel < ATC_ANALOG_OUT_CHANNELS; ++channel ) {
373 analog_out_data[2*channel] = 0;
374 analog_out_data[2*channel + 1] = 0;
376 ATCSetAnalogOut( analog_out_fd, analog_out_data );
379 /////////////////////////////////////////////////////////////////////
380 // Blank the radio display
381 /////////////////////////////////////////////////////////////////////
383 SG_LOG( SG_IO, SG_ALERT,
384 " - Clearing the radios displays." );
387 unsigned char value = 0xff;
388 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
389 radio_display_data[channel] = value;
391 ATCSetRadios( radio_display_fd, radio_display_data );
393 ATCRelease( lock_fd );
395 /////////////////////////////////////////////////////////////////////
397 /////////////////////////////////////////////////////////////////////
399 for ( int i = 0; i < 128; ++i ) {
400 ATCSetLamp( lamps_fd, i, false );
403 /////////////////////////////////////////////////////////////////////
404 // Finished initing hardware
405 /////////////////////////////////////////////////////////////////////
407 SG_LOG( SG_IO, SG_ALERT,
408 "Done initializing ATC output hardware." );
412 /////////////////////////////////////////////////////////////////////
413 // Connect up to property values
414 /////////////////////////////////////////////////////////////////////
418 snprintf( base_name, 256, "/output/atc-board[%d]/analog-outputs", board );
419 analog_out_node = fgGetNode( base_name );
421 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
422 lamps_out_node = fgGetNode( base_name );
424 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
425 radio_display_node = fgGetNode( base_name );
427 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
428 steppers_node = fgGetNode( base_name );
434 /////////////////////////////////////////////////////////////////////
435 // Write the lanalog outputs
436 /////////////////////////////////////////////////////////////////////
438 bool FGATCOutput::do_analog_out() {
439 if ( analog_out_node != NULL ) {
440 for ( int i = 0; i < analog_out_node->nChildren(); ++i ) {
441 // read the next config entry from the property tree
443 SGPropertyNode *child = analog_out_node->getChild(i);
444 string cname = child->getName();
445 int index = child->getIndex();
448 SGPropertyNode *src_prop = NULL;
449 double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
450 if ( cname == "analog-out" ) {
451 SGPropertyNode *prop;
452 prop = child->getChild( "name" );
453 if ( prop != NULL ) {
454 name = prop->getStringValue();
456 prop = child->getChild( "type" );
457 if ( prop != NULL ) {
458 type = prop->getStringValue();
460 prop = child->getChild( "prop" );
461 if ( prop != NULL ) {
462 src_prop = fgGetNode( prop->getStringValue(), true );
464 prop = child->getChild( "value-lo" );
465 if ( prop != NULL ) {
466 x0 = prop->getDoubleValue();
468 prop = child->getChild( "meter-lo" );
469 if ( prop != NULL ) {
470 y0 = prop->getDoubleValue();
472 prop = child->getChild( "value-hi" );
473 if ( prop != NULL ) {
474 x1 = prop->getDoubleValue();
476 prop = child->getChild( "meter-hi" );
477 if ( prop != NULL ) {
478 y1 = prop->getDoubleValue();
480 // crunch linear interpolation formula
483 double slope = dy / dx;
484 double value = src_prop->getDoubleValue();
485 int meter = (value - x0) * slope + y0;
486 if ( meter < 0 ) { meter = 0; }
487 if ( meter > 1023 ) { meter = 1023; }
488 analog_out_data[2*index] = meter / 256;
489 analog_out_data[2*index + 1] = meter - analog_out_data[2*index] * 256;
491 SG_LOG( SG_IO, SG_DEBUG,
492 "Input config error, expecting 'analog-out' but found "
495 ATCSetAnalogOut( analog_out_fd, analog_out_data );
503 /////////////////////////////////////////////////////////////////////
505 /////////////////////////////////////////////////////////////////////
507 bool FGATCOutput::do_lamps() {
508 if ( lamps_out_node != NULL ) {
509 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
510 // read the next config entry from the property tree
512 SGPropertyNode *child = lamps_out_node->getChild(i);
513 string cname = child->getName();
514 int index = child->getIndex();
517 SGPropertyNode *src_prop = NULL;
518 if ( cname == "lamp" ) {
519 SGPropertyNode *prop;
520 prop = child->getChild( "name" );
521 if ( prop != NULL ) {
522 name = prop->getStringValue();
524 prop = child->getChild( "type" );
525 if ( prop != NULL ) {
526 type = prop->getStringValue();
528 prop = child->getChild( "prop" );
529 if ( prop != NULL ) {
530 src_prop = fgGetNode( prop->getStringValue(), true );
532 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
534 SG_LOG( SG_IO, SG_DEBUG,
535 "Input config error, expecting 'lamp' but found "
545 /////////////////////////////////////////////////////////////////////
546 // Update the radio display
547 /////////////////////////////////////////////////////////////////////
550 static bool navcom1_has_power() {
551 static SGPropertyNode *navcom1_bus_power
552 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
553 static SGPropertyNode *navcom1_power_btn
554 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
556 return (navcom1_bus_power->getDoubleValue() > 1.0)
557 && navcom1_power_btn->getBoolValue();
560 static bool navcom2_has_power() {
561 static SGPropertyNode *navcom2_bus_power
562 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
563 static SGPropertyNode *navcom2_power_btn
564 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
566 return (navcom2_bus_power->getDoubleValue() > 1.0)
567 && navcom2_power_btn->getBoolValue();
570 static bool dme_has_power() {
571 static SGPropertyNode *dme_bus_power
572 = fgGetNode( "/systems/electrical/outputs/dme", true );
574 return (dme_bus_power->getDoubleValue() > 1.0);
577 static bool adf_has_power() {
578 static SGPropertyNode *adf_bus_power
579 = fgGetNode( "/systems/electrical/outputs/adf", true );
580 static SGPropertyNode *adf_power_btn
581 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
583 return (adf_bus_power->getDoubleValue() > 1.0)
584 && adf_power_btn->getBoolValue();
587 static bool xpdr_has_power() {
588 static SGPropertyNode *xpdr_bus_power
589 = fgGetNode( "/systems/electrical/outputs/transponder", true );
590 static SGPropertyNode *xpdr_func_knob
591 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
593 return (xpdr_bus_power->getDoubleValue() > 1.0)
594 && (xpdr_func_knob->getIntValue() > 0);
597 bool FGATCOutput::do_radio_display() {
598 static SGPropertyNode *dme_serviceable
599 = fgGetNode( "/instrumentation/dme/serviceable", true );
600 static SGPropertyNode *dme_in_range
601 = fgGetNode( "/instrumentation/dme/in-range", true );
602 static SGPropertyNode *dme_min
603 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
604 static SGPropertyNode *dme_kt
605 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
606 static SGPropertyNode *dme_nm
607 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
609 static SGPropertyNode *comm1_serviceable
610 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
611 static SGPropertyNode *com1_freq
612 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
613 static SGPropertyNode *com1_stby_freq
614 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
616 static SGPropertyNode *comm2_serviceable
617 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
618 static SGPropertyNode *com2_freq
619 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
620 static SGPropertyNode *com2_stby_freq
621 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
623 static SGPropertyNode *nav1_serviceable
624 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
625 static SGPropertyNode *nav1_freq
626 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
627 static SGPropertyNode *nav1_stby_freq
628 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
630 static SGPropertyNode *nav2_serviceable
631 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
632 static SGPropertyNode *nav2_freq
633 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
634 static SGPropertyNode *nav2_stby_freq
635 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
637 static SGPropertyNode *adf_serviceable
638 = fgGetNode( "/instrumentation/adf/serviceable", true );
639 static SGPropertyNode *adf_freq
640 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
641 static SGPropertyNode *adf_stby_freq
642 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
643 static SGPropertyNode *adf_stby_mode
644 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
645 static SGPropertyNode *adf_timer_mode
646 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
647 // static SGPropertyNode *adf_count_mode
648 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
649 static SGPropertyNode *adf_flight_timer
650 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
651 static SGPropertyNode *adf_elapsed_timer
652 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
654 static SGPropertyNode *xpdr_serviceable
655 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
656 static SGPropertyNode *xpdr_func_knob
657 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
658 static SGPropertyNode *xpdr_flight_level
659 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
660 static SGPropertyNode *xpdr_id_code
661 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
666 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
667 if ( dme_in_range->getBoolValue() ) {
669 float minutes = dme_min->getFloatValue();
670 if ( minutes > 999 ) {
673 snprintf(digits, 7, "%03.0f", minutes);
674 for ( i = 0; i < 6; ++i ) {
677 radio_display_data[0] = digits[1] << 4 | digits[2];
678 radio_display_data[1] = 0xf0 | digits[0];
681 float knots = dme_kt->getFloatValue();
685 snprintf(digits, 7, "%03.0f", knots);
686 for ( i = 0; i < 6; ++i ) {
689 radio_display_data[2] = digits[1] << 4 | digits[2];
690 radio_display_data[3] = 0xf0 | digits[0];
693 float nm = dme_nm->getFloatValue();
697 snprintf(digits, 7, "%04.1f", nm);
698 for ( i = 0; i < 6; ++i ) {
701 radio_display_data[4] = digits[1] << 4 | digits[3];
702 radio_display_data[5] = 0x00 | digits[0];
703 // the 0x00 in the upper nibble of the 6th byte of each
704 // display turns on the decimal point
707 radio_display_data[0] = 0xbb;
708 radio_display_data[1] = 0xfb;
709 radio_display_data[2] = 0xbb;
710 radio_display_data[3] = 0xfb;
711 radio_display_data[4] = 0xbb;
712 radio_display_data[5] = 0x0b;
716 for ( i = 0; i < 6; ++i ) {
717 radio_display_data[i] = 0xff;
721 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
722 // Com1 standby frequency
723 float com1_stby = com1_stby_freq->getFloatValue();
724 if ( fabs(com1_stby) > 999.99 ) {
727 snprintf(digits, 7, "%06.3f", com1_stby);
728 for ( i = 0; i < 6; ++i ) {
731 radio_display_data[6] = digits[4] << 4 | digits[5];
732 radio_display_data[7] = digits[1] << 4 | digits[2];
733 radio_display_data[8] = 0xf0 | digits[0];
735 // Com1 in use frequency
736 float com1 = com1_freq->getFloatValue();
737 if ( fabs(com1) > 999.99 ) {
740 snprintf(digits, 7, "%06.3f", com1);
741 for ( i = 0; i < 6; ++i ) {
744 radio_display_data[9] = digits[4] << 4 | digits[5];
745 radio_display_data[10] = digits[1] << 4 | digits[2];
746 radio_display_data[11] = 0x00 | digits[0];
747 // the 0x00 in the upper nibble of the 6th byte of each display
748 // turns on the decimal point
750 radio_display_data[6] = 0xff;
751 radio_display_data[7] = 0xff;
752 radio_display_data[8] = 0xff;
753 radio_display_data[9] = 0xff;
754 radio_display_data[10] = 0xff;
755 radio_display_data[11] = 0xff;
758 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
759 // Com2 standby frequency
760 float com2_stby = com2_stby_freq->getFloatValue();
761 if ( fabs(com2_stby) > 999.99 ) {
764 snprintf(digits, 7, "%06.3f", com2_stby);
765 for ( i = 0; i < 6; ++i ) {
768 radio_display_data[18] = digits[4] << 4 | digits[5];
769 radio_display_data[19] = digits[1] << 4 | digits[2];
770 radio_display_data[20] = 0xf0 | digits[0];
772 // Com2 in use frequency
773 float com2 = com2_freq->getFloatValue();
774 if ( fabs(com2) > 999.99 ) {
777 snprintf(digits, 7, "%06.3f", com2);
778 for ( i = 0; i < 6; ++i ) {
781 radio_display_data[21] = digits[4] << 4 | digits[5];
782 radio_display_data[22] = digits[1] << 4 | digits[2];
783 radio_display_data[23] = 0x00 | digits[0];
784 // the 0x00 in the upper nibble of the 6th byte of each display
785 // turns on the decimal point
787 radio_display_data[18] = 0xff;
788 radio_display_data[19] = 0xff;
789 radio_display_data[20] = 0xff;
790 radio_display_data[21] = 0xff;
791 radio_display_data[22] = 0xff;
792 radio_display_data[23] = 0xff;
795 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
796 // Nav1 standby frequency
797 float nav1_stby = nav1_stby_freq->getFloatValue();
798 if ( fabs(nav1_stby) > 999.99 ) {
801 snprintf(digits, 7, "%06.2f", nav1_stby);
802 for ( i = 0; i < 6; ++i ) {
805 radio_display_data[12] = digits[4] << 4 | digits[5];
806 radio_display_data[13] = digits[1] << 4 | digits[2];
807 radio_display_data[14] = 0xf0 | digits[0];
809 // Nav1 in use frequency
810 float nav1 = nav1_freq->getFloatValue();
811 if ( fabs(nav1) > 999.99 ) {
814 snprintf(digits, 7, "%06.2f", nav1);
815 for ( i = 0; i < 6; ++i ) {
818 radio_display_data[15] = digits[4] << 4 | digits[5];
819 radio_display_data[16] = digits[1] << 4 | digits[2];
820 radio_display_data[17] = 0x00 | digits[0];
821 // the 0x00 in the upper nibble of the 6th byte of each display
822 // turns on the decimal point
824 radio_display_data[12] = 0xff;
825 radio_display_data[13] = 0xff;
826 radio_display_data[14] = 0xff;
827 radio_display_data[15] = 0xff;
828 radio_display_data[16] = 0xff;
829 radio_display_data[17] = 0xff;
832 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
833 // Nav2 standby frequency
834 float nav2_stby = nav2_stby_freq->getFloatValue();
835 if ( fabs(nav2_stby) > 999.99 ) {
838 snprintf(digits, 7, "%06.2f", nav2_stby);
839 for ( i = 0; i < 6; ++i ) {
842 radio_display_data[24] = digits[4] << 4 | digits[5];
843 radio_display_data[25] = digits[1] << 4 | digits[2];
844 radio_display_data[26] = 0xf0 | digits[0];
846 // Nav2 in use frequency
847 float nav2 = nav2_freq->getFloatValue();
848 if ( fabs(nav2) > 999.99 ) {
851 snprintf(digits, 7, "%06.2f", nav2);
852 for ( i = 0; i < 6; ++i ) {
855 radio_display_data[27] = digits[4] << 4 | digits[5];
856 radio_display_data[28] = digits[1] << 4 | digits[2];
857 radio_display_data[29] = 0x00 | digits[0];
858 // the 0x00 in the upper nibble of the 6th byte of each display
859 // turns on the decimal point
861 radio_display_data[24] = 0xff;
862 radio_display_data[25] = 0xff;
863 radio_display_data[26] = 0xff;
864 radio_display_data[27] = 0xff;
865 radio_display_data[28] = 0xff;
866 radio_display_data[29] = 0xff;
869 // ADF standby frequency / timer
870 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
871 if ( adf_stby_mode->getIntValue() == 0 ) {
873 float adf_stby = adf_stby_freq->getFloatValue();
874 if ( fabs(adf_stby) > 1799 ) {
877 snprintf(digits, 7, "%04.0f", adf_stby);
878 for ( i = 0; i < 6; ++i ) {
881 radio_display_data[30] = digits[3] << 4 | 0x0f;
882 radio_display_data[31] = digits[1] << 4 | digits[2];
883 if ( digits[0] == 0 ) {
884 radio_display_data[32] = 0xff;
886 radio_display_data[32] = 0xf0 | digits[0];
892 if ( adf_timer_mode->getIntValue() == 0 ) {
893 time = adf_flight_timer->getDoubleValue();
895 time = adf_elapsed_timer->getDoubleValue();
897 // cout << time << endl;
898 hours = (int)(time / 3600.0);
899 time -= hours * 3600.00;
900 min = (int)(time / 60.0);
917 // cout << big << ":" << little << endl;
918 snprintf(digits, 7, "%02d%02d", big, little);
919 for ( i = 0; i < 6; ++i ) {
922 radio_display_data[30] = digits[2] << 4 | digits[3];
923 radio_display_data[31] = digits[0] << 4 | digits[1];
924 radio_display_data[32] = 0xff;
927 // ADF in use frequency
928 float adf = adf_freq->getFloatValue();
929 if ( fabs(adf) > 1799 ) {
932 snprintf(digits, 7, "%04.0f", adf);
933 for ( i = 0; i < 6; ++i ) {
936 radio_display_data[33] = digits[2] << 4 | digits[3];
937 if ( digits[0] == 0 ) {
938 radio_display_data[34] = 0xf0 | digits[1];
940 radio_display_data[34] = digits[0] << 4 | digits[1];
942 if ( adf_stby_mode->getIntValue() == 0 ) {
943 radio_display_data[35] = 0xff;
945 radio_display_data[35] = 0x0f;
948 radio_display_data[30] = 0xff;
949 radio_display_data[31] = 0xff;
950 radio_display_data[32] = 0xff;
951 radio_display_data[33] = 0xff;
952 radio_display_data[34] = 0xff;
953 radio_display_data[35] = 0xff;
956 // Transponder code and flight level
957 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
958 if ( xpdr_func_knob->getIntValue() == 2 ) {
960 radio_display_data[36] = 8 << 4 | 8;
961 radio_display_data[37] = 8 << 4 | 8;
962 radio_display_data[38] = 0xff;
963 radio_display_data[39] = 8 << 4 | 0x0f;
964 radio_display_data[40] = 8 << 4 | 8;
967 int id_code = xpdr_id_code->getIntValue();
969 for ( i = 0; i < 4; ++i ) {
970 digits[i] = id_code / place;
971 id_code -= digits[i] * place;
974 radio_display_data[36] = digits[2] << 4 | digits[3];
975 radio_display_data[37] = digits[0] << 4 | digits[1];
976 radio_display_data[38] = 0xff;
978 if ( xpdr_func_knob->getIntValue() == 3 ||
979 xpdr_func_knob->getIntValue() == 5 )
981 // do flight level display
982 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
983 for ( i = 0; i < 6; ++i ) {
986 radio_display_data[39] = digits[2] << 4 | 0x0f;
987 radio_display_data[40] = digits[0] << 4 | digits[1];
989 // blank flight level display
990 radio_display_data[39] = 0xff;
991 radio_display_data[40] = 0xff;
996 radio_display_data[36] = 0xff;
997 radio_display_data[37] = 0xff;
998 radio_display_data[38] = 0xff;
999 radio_display_data[39] = 0xff;
1000 radio_display_data[40] = 0xff;
1003 ATCSetRadios( radio_display_fd, radio_display_data );
1009 /////////////////////////////////////////////////////////////////////
1010 // Drive the stepper motors
1011 /////////////////////////////////////////////////////////////////////
1013 bool FGATCOutput::do_steppers() {
1014 SGPropertyNode *mag_compass
1015 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
1018 float diff = mag_compass->getFloatValue() - compass_position;
1019 while ( diff < -180.0 ) { diff += 360.0; }
1020 while ( diff > 180.0 ) { diff -= 360.0; }
1022 int steps = (int)(diff * 4);
1023 // cout << "steps = " << steps << endl;
1024 if ( steps > 4 ) { steps = 4; }
1025 if ( steps < -4 ) { steps = -4; }
1027 if ( abs(steps) > 0 ) {
1028 unsigned char cmd = 0x80; // stepper command
1030 cmd |= 0x20; // go up
1032 cmd |= 0x00; // go down
1036 // sync compass_position with hardware position
1037 compass_position += (float)steps / 4.0;
1039 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1046 // process the hardware outputs. This code assumes the calling layer
1047 // will lock the hardware.
1048 bool FGATCOutput::process() {
1050 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
1058 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
1066 bool FGATCOutput::close() {
1068 #if defined( unix ) || defined( __CYGWIN__ )
1076 result = ::close( lamps_fd );
1077 if ( result == -1 ) {
1078 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1080 snprintf( msg, 256, "Error closing %s", lamps_file );
1085 result = ::close( radio_display_fd );
1086 if ( result == -1 ) {
1087 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1089 snprintf( msg, 256, "Error closing %s", radio_display_file );
1094 result = ::close( stepper_fd );
1095 if ( result == -1 ) {
1096 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1098 snprintf( msg, 256, "Error closing %s", stepper_file );