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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/compiler.h>
30 #if defined( unix ) || defined( __CYGWIN__ )
31 # include <sys/types.h>
32 # include <sys/stat.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/misc/sg_path.hxx>
44 #include <Main/fg_props.hxx>
46 #include "ATC-Outputs.hxx"
52 // Lock the ATC hardware
53 static int ATCLock( int fd ) {
54 #if defined( unix ) || defined( __CYGWIN__ )
56 lseek( fd, 0, SEEK_SET );
59 int result = read( fd, tmp, 1 );
61 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
71 // Release the ATC hardware
72 static int ATCRelease( int fd ) {
73 #if defined( unix ) || defined( __CYGWIN__ )
75 lseek( fd, 0, SEEK_SET );
79 int result = write( fd, tmp, 1 );
82 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
92 // Constructor: The _board parameter specifies which board to
93 // reference. Possible values are 0 or 1. The _config_file parameter
94 // specifies the location of the output config file (xml)
95 FGATCOutput::FGATCOutput( const int _board, const SGPath &_config_file ) :
98 radio_display_node(NULL),
102 config = _config_file;
106 // Write a radios command
107 static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
108 #if defined( unix ) || defined( __CYGWIN__ )
110 lseek( fd, 0, SEEK_SET );
112 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
114 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
115 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
125 // Write a stepper command
126 static int ATCSetStepper( int fd, unsigned char channel,
127 unsigned char value )
129 #if defined( unix ) || defined( __CYGWIN__ )
131 lseek( fd, 0, SEEK_SET );
134 unsigned char buf[3];
138 int result = write( fd, buf, 2 );
140 SG_LOG( SG_IO, SG_INFO, "Write failed" );
142 SG_LOG( SG_IO, SG_DEBUG,
143 "Sent cmd = " << (int)channel << " value = " << (int)value );
151 // Read status of last stepper written to
152 static unsigned char ATCReadStepper( int fd ) {
153 #if defined( unix ) || defined( __CYGWIN__ )
157 lseek( fd, 0, SEEK_SET );
160 unsigned char buf[2];
161 result = read( fd, buf, 1 );
163 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
166 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
175 // Turn a lamp on or off
176 void ATCSetLamp( int fd, int channel, bool value ) {
177 #if defined( unix ) || defined( __CYGWIN__ )
178 // lamp channels 0-63 are written to LampPort0, channels 64-127
179 // are written to LampPort1
181 // bits 0-6 are the lamp address
182 // bit 7 is the value (on/off)
187 unsigned char buf[3];
191 result = write( fd, buf, 2 );
193 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
200 void FGATCOutput::init_config() {
201 #if defined( unix ) || defined( __CYGWIN__ )
202 if ( config.str()[0] != '/' ) {
203 // not an absolute path, prepend the standard location
205 char *envp = ::getenv( "HOME" );
206 if ( envp != NULL ) {
208 tmp.append( ".atcflightsim" );
209 tmp.append( config.str() );
213 readProperties( config.str(), globals->get_props() );
218 // Open and initialize the ATC hardware
219 bool FGATCOutput::open( int lock_fd ) {
221 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
226 // This loads the config parameters generated by "simcal"
229 SG_LOG( SG_IO, SG_ALERT,
230 "Initializing ATC output hardware, please wait ..." );
232 snprintf( lamps_file, 256,
233 "/proc/atcflightsim/board%d/lamps", board );
234 snprintf( radio_display_file, 256,
235 "/proc/atcflightsim/board%d/radios", board );
236 snprintf( stepper_file, 256,
237 "/proc/atcflightsim/board%d/steppers", board );
239 #if defined( unix ) || defined( __CYGWIN__ )
241 /////////////////////////////////////////////////////////////////////
242 // Open the /proc files
243 /////////////////////////////////////////////////////////////////////
245 lamps_fd = ::open( lamps_file, O_WRONLY );
246 if ( lamps_fd == -1 ) {
247 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
249 snprintf( msg, 256, "Error opening %s", lamps_file );
254 radio_display_fd = ::open( radio_display_file, O_RDWR );
255 if ( radio_display_fd == -1 ) {
256 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
258 snprintf( msg, 256, "Error opening %s", radio_display_file );
263 stepper_fd = ::open( stepper_file, O_RDWR );
264 if ( stepper_fd == -1 ) {
265 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
267 snprintf( msg, 256, "Error opening %s", stepper_file );
274 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
275 /////////////////////////////////////////////////////////////////////
276 // Home the compass stepper motor
277 /////////////////////////////////////////////////////////////////////
279 SG_LOG( SG_IO, SG_ALERT,
280 " - Homing the compass stepper motor" );
282 // Lock the hardware, keep trying until we succeed
283 while ( ATCLock( lock_fd ) <= 0 );
285 // Send the stepper home command
286 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
288 // Release the hardware
289 ATCRelease( lock_fd );
291 SG_LOG( SG_IO, SG_ALERT,
292 " - Waiting for compass to come home." );
295 int timeout = 900; // about 30 seconds
297 while ( ! home && timeout > 0 ) {
298 if ( timeout % 150 == 0 ) {
299 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
301 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
304 while ( ATCLock( lock_fd ) <= 0 );
306 unsigned char result = ATCReadStepper( stepper_fd );
311 ATCRelease( lock_fd );
313 #if defined( _MSC_VER )
314 ulMilliSecondSleep(33);
315 #elif defined (WIN32) && !defined(__CYGWIN__)
324 compass_position = 0.0;
327 /////////////////////////////////////////////////////////////////////
328 // Blank the radio display
329 /////////////////////////////////////////////////////////////////////
331 SG_LOG( SG_IO, SG_ALERT,
332 " - Clearing the radios displays." );
335 unsigned char value = 0xff;
336 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
337 radio_display_data[channel] = value;
340 // Lock the hardware, keep trying until we succeed
341 while ( ATCLock( lock_fd ) <= 0 );
344 ATCSetRadios( radio_display_fd, radio_display_data );
346 ATCRelease( lock_fd );
348 /////////////////////////////////////////////////////////////////////
350 /////////////////////////////////////////////////////////////////////
352 for ( int i = 0; i < 128; ++i ) {
353 ATCSetLamp( lamps_fd, i, false );
356 /////////////////////////////////////////////////////////////////////
357 // Finished initing hardware
358 /////////////////////////////////////////////////////////////////////
360 SG_LOG( SG_IO, SG_ALERT,
361 "Done initializing ATC output hardware." );
365 /////////////////////////////////////////////////////////////////////
366 // Connect up to property values
367 /////////////////////////////////////////////////////////////////////
371 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
372 lamps_out_node = fgGetNode( base_name );
374 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
375 radio_display_node = fgGetNode( base_name );
377 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
378 steppers_node = fgGetNode( base_name );
384 /////////////////////////////////////////////////////////////////////
386 /////////////////////////////////////////////////////////////////////
388 bool FGATCOutput::do_lamps() {
389 if ( lamps_out_node != NULL ) {
390 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
391 // read the next config entry from the property tree
393 SGPropertyNode *child = lamps_out_node->getChild(i);
394 string cname = child->getName();
395 int index = child->getIndex();
398 SGPropertyNode *src_prop = NULL;
399 if ( cname == "lamp" ) {
400 SGPropertyNode *prop;
401 prop = child->getChild( "name" );
402 if ( prop != NULL ) {
403 name = prop->getStringValue();
405 prop = child->getChild( "type" );
406 if ( prop != NULL ) {
407 type = prop->getStringValue();
409 prop = child->getChild( "prop" );
410 if ( prop != NULL ) {
411 src_prop = fgGetNode( prop->getStringValue(), true );
413 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
415 SG_LOG( SG_IO, SG_DEBUG,
416 "Input config error, expecting 'lamp' but found "
426 /////////////////////////////////////////////////////////////////////
427 // Update the radio display
428 /////////////////////////////////////////////////////////////////////
431 static bool navcom1_has_power() {
432 static SGPropertyNode *navcom1_bus_power
433 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
434 static SGPropertyNode *navcom1_power_btn
435 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
437 return (navcom1_bus_power->getDoubleValue() > 1.0)
438 && navcom1_power_btn->getBoolValue();
441 static bool navcom2_has_power() {
442 static SGPropertyNode *navcom2_bus_power
443 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
444 static SGPropertyNode *navcom2_power_btn
445 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
447 return (navcom2_bus_power->getDoubleValue() > 1.0)
448 && navcom2_power_btn->getBoolValue();
451 static bool dme_has_power() {
452 static SGPropertyNode *dme_bus_power
453 = fgGetNode( "/systems/electrical/outputs/dme", true );
455 return (dme_bus_power->getDoubleValue() > 1.0);
458 static bool adf_has_power() {
459 static SGPropertyNode *adf_bus_power
460 = fgGetNode( "/systems/electrical/outputs/adf", true );
461 static SGPropertyNode *adf_power_btn
462 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
464 return (adf_bus_power->getDoubleValue() > 1.0)
465 && adf_power_btn->getBoolValue();
468 static bool xpdr_has_power() {
469 static SGPropertyNode *xpdr_bus_power
470 = fgGetNode( "/systems/electrical/outputs/transponder", true );
471 static SGPropertyNode *xpdr_func_knob
472 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
474 return (xpdr_bus_power->getDoubleValue() > 1.0)
475 && (xpdr_func_knob->getIntValue() > 0);
478 bool FGATCOutput::do_radio_display() {
479 static SGPropertyNode *dme_serviceable
480 = fgGetNode( "/instrumentation/dme/serviceable", true );
481 static SGPropertyNode *dme_in_range
482 = fgGetNode( "/instrumentation/dme/in-range", true );
483 static SGPropertyNode *dme_min
484 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
485 static SGPropertyNode *dme_kt
486 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
487 static SGPropertyNode *dme_nm
488 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
490 static SGPropertyNode *comm1_serviceable
491 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
492 static SGPropertyNode *com1_freq
493 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
494 static SGPropertyNode *com1_stby_freq
495 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
497 static SGPropertyNode *comm2_serviceable
498 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
499 static SGPropertyNode *com2_freq
500 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
501 static SGPropertyNode *com2_stby_freq
502 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
504 static SGPropertyNode *nav1_serviceable
505 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
506 static SGPropertyNode *nav1_freq
507 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
508 static SGPropertyNode *nav1_stby_freq
509 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
511 static SGPropertyNode *nav2_serviceable
512 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
513 static SGPropertyNode *nav2_freq
514 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
515 static SGPropertyNode *nav2_stby_freq
516 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
518 static SGPropertyNode *adf_serviceable
519 = fgGetNode( "/instrumentation/adf/serviceable", true );
520 static SGPropertyNode *adf_freq
521 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
522 static SGPropertyNode *adf_stby_freq
523 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
524 static SGPropertyNode *adf_stby_mode
525 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
526 static SGPropertyNode *adf_timer_mode
527 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
528 // static SGPropertyNode *adf_count_mode
529 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
530 static SGPropertyNode *adf_flight_timer
531 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
532 static SGPropertyNode *adf_elapsed_timer
533 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
535 static SGPropertyNode *xpdr_serviceable
536 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
537 static SGPropertyNode *xpdr_func_knob
538 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
539 static SGPropertyNode *xpdr_flight_level
540 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
541 static SGPropertyNode *xpdr_id_code
542 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
547 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
548 if ( dme_in_range->getBoolValue() ) {
550 float minutes = dme_min->getFloatValue();
551 if ( minutes > 999 ) {
554 snprintf(digits, 7, "%03.0f", minutes);
555 for ( i = 0; i < 6; ++i ) {
558 radio_display_data[0] = digits[1] << 4 | digits[2];
559 radio_display_data[1] = 0xf0 | digits[0];
562 float knots = dme_kt->getFloatValue();
566 snprintf(digits, 7, "%03.0f", knots);
567 for ( i = 0; i < 6; ++i ) {
570 radio_display_data[2] = digits[1] << 4 | digits[2];
571 radio_display_data[3] = 0xf0 | digits[0];
574 float nm = dme_nm->getFloatValue();
578 snprintf(digits, 7, "%04.1f", nm);
579 for ( i = 0; i < 6; ++i ) {
582 radio_display_data[4] = digits[1] << 4 | digits[3];
583 radio_display_data[5] = 0x00 | digits[0];
584 // the 0x00 in the upper nibble of the 6th byte of each
585 // display turns on the decimal point
588 radio_display_data[0] = 0xbb;
589 radio_display_data[1] = 0xfb;
590 radio_display_data[2] = 0xbb;
591 radio_display_data[3] = 0xfb;
592 radio_display_data[4] = 0xbb;
593 radio_display_data[5] = 0x0b;
597 for ( i = 0; i < 6; ++i ) {
598 radio_display_data[i] = 0xff;
602 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
603 // Com1 standby frequency
604 float com1_stby = com1_stby_freq->getFloatValue();
605 if ( fabs(com1_stby) > 999.99 ) {
608 snprintf(digits, 7, "%06.3f", com1_stby);
609 for ( i = 0; i < 6; ++i ) {
612 radio_display_data[6] = digits[4] << 4 | digits[5];
613 radio_display_data[7] = digits[1] << 4 | digits[2];
614 radio_display_data[8] = 0xf0 | digits[0];
616 // Com1 in use frequency
617 float com1 = com1_freq->getFloatValue();
618 if ( fabs(com1) > 999.99 ) {
621 snprintf(digits, 7, "%06.3f", com1);
622 for ( i = 0; i < 6; ++i ) {
625 radio_display_data[9] = digits[4] << 4 | digits[5];
626 radio_display_data[10] = digits[1] << 4 | digits[2];
627 radio_display_data[11] = 0x00 | digits[0];
628 // the 0x00 in the upper nibble of the 6th byte of each display
629 // turns on the decimal point
631 radio_display_data[6] = 0xff;
632 radio_display_data[7] = 0xff;
633 radio_display_data[8] = 0xff;
634 radio_display_data[9] = 0xff;
635 radio_display_data[10] = 0xff;
636 radio_display_data[11] = 0xff;
639 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
640 // Com2 standby frequency
641 float com2_stby = com2_stby_freq->getFloatValue();
642 if ( fabs(com2_stby) > 999.99 ) {
645 snprintf(digits, 7, "%06.3f", com2_stby);
646 for ( i = 0; i < 6; ++i ) {
649 radio_display_data[18] = digits[4] << 4 | digits[5];
650 radio_display_data[19] = digits[1] << 4 | digits[2];
651 radio_display_data[20] = 0xf0 | digits[0];
653 // Com2 in use frequency
654 float com2 = com2_freq->getFloatValue();
655 if ( fabs(com2) > 999.99 ) {
658 snprintf(digits, 7, "%06.3f", com2);
659 for ( i = 0; i < 6; ++i ) {
662 radio_display_data[21] = digits[4] << 4 | digits[5];
663 radio_display_data[22] = digits[1] << 4 | digits[2];
664 radio_display_data[23] = 0x00 | digits[0];
665 // the 0x00 in the upper nibble of the 6th byte of each display
666 // turns on the decimal point
668 radio_display_data[18] = 0xff;
669 radio_display_data[19] = 0xff;
670 radio_display_data[20] = 0xff;
671 radio_display_data[21] = 0xff;
672 radio_display_data[22] = 0xff;
673 radio_display_data[23] = 0xff;
676 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
677 // Nav1 standby frequency
678 float nav1_stby = nav1_stby_freq->getFloatValue();
679 if ( fabs(nav1_stby) > 999.99 ) {
682 snprintf(digits, 7, "%06.2f", nav1_stby);
683 for ( i = 0; i < 6; ++i ) {
686 radio_display_data[12] = digits[4] << 4 | digits[5];
687 radio_display_data[13] = digits[1] << 4 | digits[2];
688 radio_display_data[14] = 0xf0 | digits[0];
690 // Nav1 in use frequency
691 float nav1 = nav1_freq->getFloatValue();
692 if ( fabs(nav1) > 999.99 ) {
695 snprintf(digits, 7, "%06.2f", nav1);
696 for ( i = 0; i < 6; ++i ) {
699 radio_display_data[15] = digits[4] << 4 | digits[5];
700 radio_display_data[16] = digits[1] << 4 | digits[2];
701 radio_display_data[17] = 0x00 | digits[0];
702 // the 0x00 in the upper nibble of the 6th byte of each display
703 // turns on the decimal point
705 radio_display_data[12] = 0xff;
706 radio_display_data[13] = 0xff;
707 radio_display_data[14] = 0xff;
708 radio_display_data[15] = 0xff;
709 radio_display_data[16] = 0xff;
710 radio_display_data[17] = 0xff;
713 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
714 // Nav2 standby frequency
715 float nav2_stby = nav2_stby_freq->getFloatValue();
716 if ( fabs(nav2_stby) > 999.99 ) {
719 snprintf(digits, 7, "%06.2f", nav2_stby);
720 for ( i = 0; i < 6; ++i ) {
723 radio_display_data[24] = digits[4] << 4 | digits[5];
724 radio_display_data[25] = digits[1] << 4 | digits[2];
725 radio_display_data[26] = 0xf0 | digits[0];
727 // Nav2 in use frequency
728 float nav2 = nav2_freq->getFloatValue();
729 if ( fabs(nav2) > 999.99 ) {
732 snprintf(digits, 7, "%06.2f", nav2);
733 for ( i = 0; i < 6; ++i ) {
736 radio_display_data[27] = digits[4] << 4 | digits[5];
737 radio_display_data[28] = digits[1] << 4 | digits[2];
738 radio_display_data[29] = 0x00 | digits[0];
739 // the 0x00 in the upper nibble of the 6th byte of each display
740 // turns on the decimal point
742 radio_display_data[24] = 0xff;
743 radio_display_data[25] = 0xff;
744 radio_display_data[26] = 0xff;
745 radio_display_data[27] = 0xff;
746 radio_display_data[28] = 0xff;
747 radio_display_data[29] = 0xff;
750 // ADF standby frequency / timer
751 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
752 if ( adf_stby_mode->getIntValue() == 0 ) {
754 float adf_stby = adf_stby_freq->getFloatValue();
755 if ( fabs(adf_stby) > 1799 ) {
758 snprintf(digits, 7, "%04.0f", adf_stby);
759 for ( i = 0; i < 6; ++i ) {
762 radio_display_data[30] = digits[3] << 4 | 0x0f;
763 radio_display_data[31] = digits[1] << 4 | digits[2];
764 if ( digits[0] == 0 ) {
765 radio_display_data[32] = 0xff;
767 radio_display_data[32] = 0xf0 | digits[0];
773 if ( adf_timer_mode->getIntValue() == 0 ) {
774 time = adf_flight_timer->getDoubleValue();
776 time = adf_elapsed_timer->getDoubleValue();
778 // cout << time << endl;
779 hours = (int)(time / 3600.0);
780 time -= hours * 3600.00;
781 min = (int)(time / 60.0);
798 // cout << big << ":" << little << endl;
799 snprintf(digits, 7, "%02d%02d", big, little);
800 for ( i = 0; i < 6; ++i ) {
803 radio_display_data[30] = digits[2] << 4 | digits[3];
804 radio_display_data[31] = digits[0] << 4 | digits[1];
805 radio_display_data[32] = 0xff;
808 // ADF in use frequency
809 float adf = adf_freq->getFloatValue();
810 if ( fabs(adf) > 1799 ) {
813 snprintf(digits, 7, "%04.0f", adf);
814 for ( i = 0; i < 6; ++i ) {
817 radio_display_data[33] = digits[2] << 4 | digits[3];
818 if ( digits[0] == 0 ) {
819 radio_display_data[34] = 0xf0 | digits[1];
821 radio_display_data[34] = digits[0] << 4 | digits[1];
823 if ( adf_stby_mode->getIntValue() == 0 ) {
824 radio_display_data[35] = 0xff;
826 radio_display_data[35] = 0x0f;
829 radio_display_data[30] = 0xff;
830 radio_display_data[31] = 0xff;
831 radio_display_data[32] = 0xff;
832 radio_display_data[33] = 0xff;
833 radio_display_data[34] = 0xff;
834 radio_display_data[35] = 0xff;
837 // Transponder code and flight level
838 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
839 if ( xpdr_func_knob->getIntValue() == 2 ) {
841 radio_display_data[36] = 8 << 4 | 8;
842 radio_display_data[37] = 8 << 4 | 8;
843 radio_display_data[38] = 0xff;
844 radio_display_data[39] = 8 << 4 | 0x0f;
845 radio_display_data[40] = 8 << 4 | 8;
848 int id_code = xpdr_id_code->getIntValue();
850 for ( i = 0; i < 4; ++i ) {
851 digits[i] = id_code / place;
852 id_code -= digits[i] * place;
855 radio_display_data[36] = digits[2] << 4 | digits[3];
856 radio_display_data[37] = digits[0] << 4 | digits[1];
857 radio_display_data[38] = 0xff;
859 if ( xpdr_func_knob->getIntValue() == 3 ||
860 xpdr_func_knob->getIntValue() == 5 )
862 // do flight level display
863 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
864 for ( i = 0; i < 6; ++i ) {
867 radio_display_data[39] = digits[2] << 4 | 0x0f;
868 radio_display_data[40] = digits[0] << 4 | digits[1];
870 // blank flight level display
871 radio_display_data[39] = 0xff;
872 radio_display_data[40] = 0xff;
877 radio_display_data[36] = 0xff;
878 radio_display_data[37] = 0xff;
879 radio_display_data[38] = 0xff;
880 radio_display_data[39] = 0xff;
881 radio_display_data[40] = 0xff;
884 ATCSetRadios( radio_display_fd, radio_display_data );
890 /////////////////////////////////////////////////////////////////////
891 // Drive the stepper motors
892 /////////////////////////////////////////////////////////////////////
894 bool FGATCOutput::do_steppers() {
895 SGPropertyNode *mag_compass
896 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
899 float diff = mag_compass->getFloatValue() - compass_position;
900 while ( diff < -180.0 ) { diff += 360.0; }
901 while ( diff > 180.0 ) { diff -= 360.0; }
903 int steps = (int)(diff * 4);
904 // cout << "steps = " << steps << endl;
905 if ( steps > 4 ) { steps = 4; }
906 if ( steps < -4 ) { steps = -4; }
908 if ( abs(steps) > 0 ) {
909 unsigned char cmd = 0x80; // stepper command
911 cmd |= 0x20; // go up
913 cmd |= 0x00; // go down
917 // sync compass_position with hardware position
918 compass_position += (float)steps / 4.0;
920 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
927 // process the hardware outputs. This code assumes the calling layer
928 // will lock the hardware.
929 bool FGATCOutput::process() {
931 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
938 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
946 bool FGATCOutput::close() {
948 #if defined( unix ) || defined( __CYGWIN__ )
956 result = ::close( lamps_fd );
957 if ( result == -1 ) {
958 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
960 snprintf( msg, 256, "Error closing %s", lamps_file );
965 result = ::close( radio_display_fd );
966 if ( result == -1 ) {
967 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
969 snprintf( msg, 256, "Error closing %s", radio_display_file );
974 result = ::close( stepper_fd );
975 if ( result == -1 ) {
976 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
978 snprintf( msg, 256, "Error closing %s", stepper_file );