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 );
360 compass_position = 0.0;
363 // Lock the hardware, keep trying until we succeed
364 while ( ATCLock( lock_fd ) <= 0 );
366 /////////////////////////////////////////////////////////////////////
367 // Zero the analog outputs
368 /////////////////////////////////////////////////////////////////////
370 SG_LOG( SG_IO, SG_ALERT,
371 " - Zeroing Analog Outputs." );
373 for ( int channel = 0; channel < ATC_ANALOG_OUT_CHANNELS; ++channel ) {
374 analog_out_data[2*channel] = 0;
375 analog_out_data[2*channel + 1] = 0;
377 ATCSetAnalogOut( analog_out_fd, analog_out_data );
380 /////////////////////////////////////////////////////////////////////
381 // Blank the radio display
382 /////////////////////////////////////////////////////////////////////
384 SG_LOG( SG_IO, SG_ALERT,
385 " - Clearing the radios displays." );
388 unsigned char value = 0xff;
389 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
390 radio_display_data[channel] = value;
392 ATCSetRadios( radio_display_fd, radio_display_data );
394 ATCRelease( lock_fd );
396 /////////////////////////////////////////////////////////////////////
398 /////////////////////////////////////////////////////////////////////
400 for ( int i = 0; i < 128; ++i ) {
401 ATCSetLamp( lamps_fd, i, false );
404 /////////////////////////////////////////////////////////////////////
405 // Finished initing hardware
406 /////////////////////////////////////////////////////////////////////
408 SG_LOG( SG_IO, SG_ALERT,
409 "Done initializing ATC output hardware." );
413 /////////////////////////////////////////////////////////////////////
414 // Connect up to property values
415 /////////////////////////////////////////////////////////////////////
419 snprintf( base_name, 256, "/output/atc-board[%d]/analog-outputs", board );
420 analog_out_node = fgGetNode( base_name );
422 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
423 lamps_out_node = fgGetNode( base_name );
425 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
426 radio_display_node = fgGetNode( base_name );
428 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
429 steppers_node = fgGetNode( base_name );
435 /////////////////////////////////////////////////////////////////////
436 // Write the lanalog outputs
437 /////////////////////////////////////////////////////////////////////
439 bool FGATCOutput::do_analog_out() {
440 if ( analog_out_node != NULL ) {
441 for ( int i = 0; i < analog_out_node->nChildren(); ++i ) {
442 // read the next config entry from the property tree
444 SGPropertyNode *child = analog_out_node->getChild(i);
445 string cname = child->getName();
446 int index = child->getIndex();
449 SGPropertyNode *src_prop = NULL;
450 double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
451 if ( cname == "analog-out" ) {
452 SGPropertyNode *prop;
453 prop = child->getChild( "name" );
454 if ( prop != NULL ) {
455 name = prop->getStringValue();
457 prop = child->getChild( "type" );
458 if ( prop != NULL ) {
459 type = prop->getStringValue();
461 prop = child->getChild( "prop" );
462 if ( prop != NULL ) {
463 src_prop = fgGetNode( prop->getStringValue(), true );
465 prop = child->getChild( "value-lo" );
466 if ( prop != NULL ) {
467 x0 = prop->getDoubleValue();
469 prop = child->getChild( "meter-lo" );
470 if ( prop != NULL ) {
471 y0 = prop->getDoubleValue();
473 prop = child->getChild( "value-hi" );
474 if ( prop != NULL ) {
475 x1 = prop->getDoubleValue();
477 prop = child->getChild( "meter-hi" );
478 if ( prop != NULL ) {
479 y1 = prop->getDoubleValue();
481 // crunch linear interpolation formula
484 double slope = dy / dx;
485 double value = src_prop->getDoubleValue();
486 int meter = (value - x0) * slope + y0;
487 if ( meter < 0 ) { meter = 0; }
488 if ( meter > 1023 ) { meter = 1023; }
489 analog_out_data[2*index] = meter / 256;
490 analog_out_data[2*index + 1] = meter - analog_out_data[2*index] * 256;
492 SG_LOG( SG_IO, SG_DEBUG,
493 "Input config error, expecting 'analog-out' but found "
496 ATCSetAnalogOut( analog_out_fd, analog_out_data );
504 /////////////////////////////////////////////////////////////////////
506 /////////////////////////////////////////////////////////////////////
508 bool FGATCOutput::do_lamps() {
509 if ( lamps_out_node != NULL ) {
510 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
511 // read the next config entry from the property tree
513 SGPropertyNode *child = lamps_out_node->getChild(i);
514 string cname = child->getName();
515 int index = child->getIndex();
518 SGPropertyNode *src_prop = NULL;
519 if ( cname == "lamp" ) {
520 SGPropertyNode *prop;
521 prop = child->getChild( "name" );
522 if ( prop != NULL ) {
523 name = prop->getStringValue();
525 prop = child->getChild( "type" );
526 if ( prop != NULL ) {
527 type = prop->getStringValue();
529 prop = child->getChild( "prop" );
530 if ( prop != NULL ) {
531 src_prop = fgGetNode( prop->getStringValue(), true );
533 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
535 SG_LOG( SG_IO, SG_DEBUG,
536 "Input config error, expecting 'lamp' but found "
546 /////////////////////////////////////////////////////////////////////
547 // Update the radio display
548 /////////////////////////////////////////////////////////////////////
551 static bool navcom1_has_power() {
552 static SGPropertyNode *navcom1_bus_power
553 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
554 static SGPropertyNode *navcom1_power_btn
555 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
557 return (navcom1_bus_power->getDoubleValue() > 1.0)
558 && navcom1_power_btn->getBoolValue();
561 static bool navcom2_has_power() {
562 static SGPropertyNode *navcom2_bus_power
563 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
564 static SGPropertyNode *navcom2_power_btn
565 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
567 return (navcom2_bus_power->getDoubleValue() > 1.0)
568 && navcom2_power_btn->getBoolValue();
571 static bool dme_has_power() {
572 static SGPropertyNode *dme_bus_power
573 = fgGetNode( "/systems/electrical/outputs/dme", true );
575 return (dme_bus_power->getDoubleValue() > 1.0);
578 static bool adf_has_power() {
579 static SGPropertyNode *adf_bus_power
580 = fgGetNode( "/systems/electrical/outputs/adf", true );
581 static SGPropertyNode *adf_power_btn
582 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
584 return (adf_bus_power->getDoubleValue() > 1.0)
585 && adf_power_btn->getBoolValue();
588 static bool xpdr_has_power() {
589 static SGPropertyNode *xpdr_bus_power
590 = fgGetNode( "/systems/electrical/outputs/transponder", true );
591 static SGPropertyNode *xpdr_func_knob
592 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
594 return (xpdr_bus_power->getDoubleValue() > 1.0)
595 && (xpdr_func_knob->getIntValue() > 0);
598 bool FGATCOutput::do_radio_display() {
599 static SGPropertyNode *dme_serviceable
600 = fgGetNode( "/instrumentation/dme/serviceable", true );
601 static SGPropertyNode *dme_in_range
602 = fgGetNode( "/instrumentation/dme/in-range", true );
603 static SGPropertyNode *dme_min
604 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
605 static SGPropertyNode *dme_kt
606 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
607 static SGPropertyNode *dme_nm
608 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
610 static SGPropertyNode *comm1_serviceable
611 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
612 static SGPropertyNode *com1_freq
613 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
614 static SGPropertyNode *com1_stby_freq
615 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
617 static SGPropertyNode *comm2_serviceable
618 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
619 static SGPropertyNode *com2_freq
620 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
621 static SGPropertyNode *com2_stby_freq
622 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
624 static SGPropertyNode *nav1_serviceable
625 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
626 static SGPropertyNode *nav1_freq
627 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
628 static SGPropertyNode *nav1_stby_freq
629 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
631 static SGPropertyNode *nav2_serviceable
632 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
633 static SGPropertyNode *nav2_freq
634 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
635 static SGPropertyNode *nav2_stby_freq
636 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
638 static SGPropertyNode *adf_serviceable
639 = fgGetNode( "/instrumentation/adf/serviceable", true );
640 static SGPropertyNode *adf_freq
641 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
642 static SGPropertyNode *adf_stby_freq
643 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
644 static SGPropertyNode *adf_stby_mode
645 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
646 static SGPropertyNode *adf_timer_mode
647 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
648 // static SGPropertyNode *adf_count_mode
649 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
650 static SGPropertyNode *adf_flight_timer
651 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
652 static SGPropertyNode *adf_elapsed_timer
653 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
655 static SGPropertyNode *xpdr_serviceable
656 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
657 static SGPropertyNode *xpdr_func_knob
658 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
659 static SGPropertyNode *xpdr_flight_level
660 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
661 static SGPropertyNode *xpdr_id_code
662 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
667 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
668 if ( dme_in_range->getBoolValue() ) {
670 float minutes = dme_min->getFloatValue();
671 if ( minutes > 999 ) {
674 snprintf(digits, 7, "%03.0f", minutes);
675 for ( i = 0; i < 6; ++i ) {
678 radio_display_data[0] = digits[1] << 4 | digits[2];
679 radio_display_data[1] = 0xf0 | digits[0];
682 float knots = dme_kt->getFloatValue();
686 snprintf(digits, 7, "%03.0f", knots);
687 for ( i = 0; i < 6; ++i ) {
690 radio_display_data[2] = digits[1] << 4 | digits[2];
691 radio_display_data[3] = 0xf0 | digits[0];
694 float nm = dme_nm->getFloatValue();
698 snprintf(digits, 7, "%04.1f", nm);
699 for ( i = 0; i < 6; ++i ) {
702 radio_display_data[4] = digits[1] << 4 | digits[3];
703 radio_display_data[5] = 0x00 | digits[0];
704 // the 0x00 in the upper nibble of the 6th byte of each
705 // display turns on the decimal point
708 radio_display_data[0] = 0xbb;
709 radio_display_data[1] = 0xfb;
710 radio_display_data[2] = 0xbb;
711 radio_display_data[3] = 0xfb;
712 radio_display_data[4] = 0xbb;
713 radio_display_data[5] = 0x0b;
717 for ( i = 0; i < 6; ++i ) {
718 radio_display_data[i] = 0xff;
722 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
723 // Com1 standby frequency
724 float com1_stby = com1_stby_freq->getFloatValue();
725 if ( fabs(com1_stby) > 999.99 ) {
728 snprintf(digits, 7, "%06.3f", com1_stby);
729 for ( i = 0; i < 6; ++i ) {
732 radio_display_data[6] = digits[4] << 4 | digits[5];
733 radio_display_data[7] = digits[1] << 4 | digits[2];
734 radio_display_data[8] = 0xf0 | digits[0];
736 // Com1 in use frequency
737 float com1 = com1_freq->getFloatValue();
738 if ( fabs(com1) > 999.99 ) {
741 snprintf(digits, 7, "%06.3f", com1);
742 for ( i = 0; i < 6; ++i ) {
745 radio_display_data[9] = digits[4] << 4 | digits[5];
746 radio_display_data[10] = digits[1] << 4 | digits[2];
747 radio_display_data[11] = 0x00 | digits[0];
748 // the 0x00 in the upper nibble of the 6th byte of each display
749 // turns on the decimal point
751 radio_display_data[6] = 0xff;
752 radio_display_data[7] = 0xff;
753 radio_display_data[8] = 0xff;
754 radio_display_data[9] = 0xff;
755 radio_display_data[10] = 0xff;
756 radio_display_data[11] = 0xff;
759 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
760 // Com2 standby frequency
761 float com2_stby = com2_stby_freq->getFloatValue();
762 if ( fabs(com2_stby) > 999.99 ) {
765 snprintf(digits, 7, "%06.3f", com2_stby);
766 for ( i = 0; i < 6; ++i ) {
769 radio_display_data[18] = digits[4] << 4 | digits[5];
770 radio_display_data[19] = digits[1] << 4 | digits[2];
771 radio_display_data[20] = 0xf0 | digits[0];
773 // Com2 in use frequency
774 float com2 = com2_freq->getFloatValue();
775 if ( fabs(com2) > 999.99 ) {
778 snprintf(digits, 7, "%06.3f", com2);
779 for ( i = 0; i < 6; ++i ) {
782 radio_display_data[21] = digits[4] << 4 | digits[5];
783 radio_display_data[22] = digits[1] << 4 | digits[2];
784 radio_display_data[23] = 0x00 | digits[0];
785 // the 0x00 in the upper nibble of the 6th byte of each display
786 // turns on the decimal point
788 radio_display_data[18] = 0xff;
789 radio_display_data[19] = 0xff;
790 radio_display_data[20] = 0xff;
791 radio_display_data[21] = 0xff;
792 radio_display_data[22] = 0xff;
793 radio_display_data[23] = 0xff;
796 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
797 // Nav1 standby frequency
798 float nav1_stby = nav1_stby_freq->getFloatValue();
799 if ( fabs(nav1_stby) > 999.99 ) {
802 snprintf(digits, 7, "%06.2f", nav1_stby);
803 for ( i = 0; i < 6; ++i ) {
806 radio_display_data[12] = digits[4] << 4 | digits[5];
807 radio_display_data[13] = digits[1] << 4 | digits[2];
808 radio_display_data[14] = 0xf0 | digits[0];
810 // Nav1 in use frequency
811 float nav1 = nav1_freq->getFloatValue();
812 if ( fabs(nav1) > 999.99 ) {
815 snprintf(digits, 7, "%06.2f", nav1);
816 for ( i = 0; i < 6; ++i ) {
819 radio_display_data[15] = digits[4] << 4 | digits[5];
820 radio_display_data[16] = digits[1] << 4 | digits[2];
821 radio_display_data[17] = 0x00 | digits[0];
822 // the 0x00 in the upper nibble of the 6th byte of each display
823 // turns on the decimal point
825 radio_display_data[12] = 0xff;
826 radio_display_data[13] = 0xff;
827 radio_display_data[14] = 0xff;
828 radio_display_data[15] = 0xff;
829 radio_display_data[16] = 0xff;
830 radio_display_data[17] = 0xff;
833 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
834 // Nav2 standby frequency
835 float nav2_stby = nav2_stby_freq->getFloatValue();
836 if ( fabs(nav2_stby) > 999.99 ) {
839 snprintf(digits, 7, "%06.2f", nav2_stby);
840 for ( i = 0; i < 6; ++i ) {
843 radio_display_data[24] = digits[4] << 4 | digits[5];
844 radio_display_data[25] = digits[1] << 4 | digits[2];
845 radio_display_data[26] = 0xf0 | digits[0];
847 // Nav2 in use frequency
848 float nav2 = nav2_freq->getFloatValue();
849 if ( fabs(nav2) > 999.99 ) {
852 snprintf(digits, 7, "%06.2f", nav2);
853 for ( i = 0; i < 6; ++i ) {
856 radio_display_data[27] = digits[4] << 4 | digits[5];
857 radio_display_data[28] = digits[1] << 4 | digits[2];
858 radio_display_data[29] = 0x00 | digits[0];
859 // the 0x00 in the upper nibble of the 6th byte of each display
860 // turns on the decimal point
862 radio_display_data[24] = 0xff;
863 radio_display_data[25] = 0xff;
864 radio_display_data[26] = 0xff;
865 radio_display_data[27] = 0xff;
866 radio_display_data[28] = 0xff;
867 radio_display_data[29] = 0xff;
870 // ADF standby frequency / timer
871 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
872 if ( adf_stby_mode->getIntValue() == 0 ) {
874 float adf_stby = adf_stby_freq->getFloatValue();
875 if ( fabs(adf_stby) > 1799 ) {
878 snprintf(digits, 7, "%04.0f", adf_stby);
879 for ( i = 0; i < 6; ++i ) {
882 radio_display_data[30] = digits[3] << 4 | 0x0f;
883 radio_display_data[31] = digits[1] << 4 | digits[2];
884 if ( digits[0] == 0 ) {
885 radio_display_data[32] = 0xff;
887 radio_display_data[32] = 0xf0 | digits[0];
893 if ( adf_timer_mode->getIntValue() == 0 ) {
894 time = adf_flight_timer->getDoubleValue();
896 time = adf_elapsed_timer->getDoubleValue();
898 // cout << time << endl;
899 hours = (int)(time / 3600.0);
900 time -= hours * 3600.00;
901 min = (int)(time / 60.0);
918 // cout << big << ":" << little << endl;
919 snprintf(digits, 7, "%02d%02d", big, little);
920 for ( i = 0; i < 6; ++i ) {
923 radio_display_data[30] = digits[2] << 4 | digits[3];
924 radio_display_data[31] = digits[0] << 4 | digits[1];
925 radio_display_data[32] = 0xff;
928 // ADF in use frequency
929 float adf = adf_freq->getFloatValue();
930 if ( fabs(adf) > 1799 ) {
933 snprintf(digits, 7, "%04.0f", adf);
934 for ( i = 0; i < 6; ++i ) {
937 radio_display_data[33] = digits[2] << 4 | digits[3];
938 if ( digits[0] == 0 ) {
939 radio_display_data[34] = 0xf0 | digits[1];
941 radio_display_data[34] = digits[0] << 4 | digits[1];
943 if ( adf_stby_mode->getIntValue() == 0 ) {
944 radio_display_data[35] = 0xff;
946 radio_display_data[35] = 0x0f;
949 radio_display_data[30] = 0xff;
950 radio_display_data[31] = 0xff;
951 radio_display_data[32] = 0xff;
952 radio_display_data[33] = 0xff;
953 radio_display_data[34] = 0xff;
954 radio_display_data[35] = 0xff;
957 // Transponder code and flight level
958 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
959 if ( xpdr_func_knob->getIntValue() == 2 ) {
961 radio_display_data[36] = 8 << 4 | 8;
962 radio_display_data[37] = 8 << 4 | 8;
963 radio_display_data[38] = 0xff;
964 radio_display_data[39] = 8 << 4 | 0x0f;
965 radio_display_data[40] = 8 << 4 | 8;
968 int id_code = xpdr_id_code->getIntValue();
970 for ( i = 0; i < 4; ++i ) {
971 digits[i] = id_code / place;
972 id_code -= digits[i] * place;
975 radio_display_data[36] = digits[2] << 4 | digits[3];
976 radio_display_data[37] = digits[0] << 4 | digits[1];
977 radio_display_data[38] = 0xff;
979 if ( xpdr_func_knob->getIntValue() == 3 ||
980 xpdr_func_knob->getIntValue() == 5 )
982 // do flight level display
983 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
984 for ( i = 0; i < 6; ++i ) {
987 radio_display_data[39] = digits[2] << 4 | 0x0f;
988 radio_display_data[40] = digits[0] << 4 | digits[1];
990 // blank flight level display
991 radio_display_data[39] = 0xff;
992 radio_display_data[40] = 0xff;
997 radio_display_data[36] = 0xff;
998 radio_display_data[37] = 0xff;
999 radio_display_data[38] = 0xff;
1000 radio_display_data[39] = 0xff;
1001 radio_display_data[40] = 0xff;
1004 ATCSetRadios( radio_display_fd, radio_display_data );
1010 /////////////////////////////////////////////////////////////////////
1011 // Drive the stepper motors
1012 /////////////////////////////////////////////////////////////////////
1014 bool FGATCOutput::do_steppers() {
1015 SGPropertyNode *mag_compass
1016 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
1019 float diff = mag_compass->getFloatValue() - compass_position;
1020 while ( diff < -180.0 ) { diff += 360.0; }
1021 while ( diff > 180.0 ) { diff -= 360.0; }
1023 int steps = (int)(diff * 4);
1024 // cout << "steps = " << steps << endl;
1025 if ( steps > 4 ) { steps = 4; }
1026 if ( steps < -4 ) { steps = -4; }
1028 if ( abs(steps) > 0 ) {
1029 unsigned char cmd = 0x80; // stepper command
1031 cmd |= 0x20; // go up
1033 cmd |= 0x00; // go down
1037 // sync compass_position with hardware position
1038 compass_position += (float)steps / 4.0;
1040 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1047 // process the hardware outputs. This code assumes the calling layer
1048 // will lock the hardware.
1049 bool FGATCOutput::process() {
1051 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
1059 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
1067 bool FGATCOutput::close() {
1069 #if defined( unix ) || defined( __CYGWIN__ )
1077 result = ::close( lamps_fd );
1078 if ( result == -1 ) {
1079 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1081 snprintf( msg, 256, "Error closing %s", lamps_file );
1086 result = ::close( radio_display_fd );
1087 if ( result == -1 ) {
1088 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1090 snprintf( msg, 256, "Error closing %s", radio_display_file );
1095 result = ::close( stepper_fd );
1096 if ( result == -1 ) {
1097 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
1099 snprintf( msg, 256, "Error closing %s", stepper_file );