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>
46 #include <Main/fg_props.hxx>
48 #include "ATC-Outputs.hxx"
54 // Lock the ATC hardware
55 static int ATCLock( int fd ) {
56 #if defined( unix ) || defined( __CYGWIN__ )
58 lseek( fd, 0, SEEK_SET );
61 int result = read( fd, tmp, 1 );
63 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
73 // Release the ATC hardware
74 static int ATCRelease( int fd ) {
75 #if defined( unix ) || defined( __CYGWIN__ )
77 lseek( fd, 0, SEEK_SET );
81 int result = write( fd, tmp, 1 );
84 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
94 // Constructor: The _board parameter specifies which board to
95 // reference. Possible values are 0 or 1. The _config_file parameter
96 // specifies the location of the output config file (xml)
97 FGATCOutput::FGATCOutput( const int _board, const SGPath &_config_file ) :
100 radio_display_node(NULL),
104 config = _config_file;
108 // Write a radios command
109 static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
110 #if defined( unix ) || defined( __CYGWIN__ )
112 lseek( fd, 0, SEEK_SET );
114 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
116 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
117 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
127 // Write a stepper command
128 static int ATCSetStepper( int fd, unsigned char channel,
129 unsigned char value )
131 #if defined( unix ) || defined( __CYGWIN__ )
133 lseek( fd, 0, SEEK_SET );
136 unsigned char buf[3];
140 int result = write( fd, buf, 2 );
142 SG_LOG( SG_IO, SG_INFO, "Write failed" );
144 SG_LOG( SG_IO, SG_DEBUG,
145 "Sent cmd = " << (int)channel << " value = " << (int)value );
153 // Read status of last stepper written to
154 static unsigned char ATCReadStepper( int fd ) {
155 #if defined( unix ) || defined( __CYGWIN__ )
159 lseek( fd, 0, SEEK_SET );
162 unsigned char buf[2];
163 result = read( fd, buf, 1 );
165 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
168 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
177 // Turn a lamp on or off
178 void ATCSetLamp( int fd, int channel, bool value ) {
179 #if defined( unix ) || defined( __CYGWIN__ )
180 // lamp channels 0-63 are written to LampPort0, channels 64-127
181 // are written to LampPort1
183 // bits 0-6 are the lamp address
184 // bit 7 is the value (on/off)
189 unsigned char buf[3];
193 result = write( fd, buf, 2 );
195 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
202 void FGATCOutput::init_config() {
203 #if defined( unix ) || defined( __CYGWIN__ )
204 if ( config.str()[0] != '/' ) {
205 // not an absolute path, prepend the standard location
207 char *envp = ::getenv( "HOME" );
208 if ( envp != NULL ) {
210 tmp.append( ".atcflightsim" );
211 tmp.append( config.str() );
215 readProperties( config.str(), globals->get_props() );
220 // Open and initialize the ATC hardware
221 bool FGATCOutput::open( int lock_fd ) {
223 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
228 // This loads the config parameters generated by "simcal"
231 SG_LOG( SG_IO, SG_ALERT,
232 "Initializing ATC output hardware, please wait ..." );
234 snprintf( lamps_file, 256,
235 "/proc/atcflightsim/board%d/lamps", board );
236 snprintf( radio_display_file, 256,
237 "/proc/atcflightsim/board%d/radios", board );
238 snprintf( stepper_file, 256,
239 "/proc/atcflightsim/board%d/steppers", board );
241 #if defined( unix ) || defined( __CYGWIN__ )
243 /////////////////////////////////////////////////////////////////////
244 // Open the /proc files
245 /////////////////////////////////////////////////////////////////////
247 lamps_fd = ::open( lamps_file, O_WRONLY );
248 if ( lamps_fd == -1 ) {
249 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
251 snprintf( msg, 256, "Error opening %s", lamps_file );
256 radio_display_fd = ::open( radio_display_file, O_RDWR );
257 if ( radio_display_fd == -1 ) {
258 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
260 snprintf( msg, 256, "Error opening %s", radio_display_file );
265 stepper_fd = ::open( stepper_file, O_RDWR );
266 if ( stepper_fd == -1 ) {
267 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
269 snprintf( msg, 256, "Error opening %s", stepper_file );
276 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
277 /////////////////////////////////////////////////////////////////////
278 // Home the compass stepper motor
279 /////////////////////////////////////////////////////////////////////
281 SG_LOG( SG_IO, SG_ALERT,
282 " - Homing the compass stepper motor" );
284 // Lock the hardware, keep trying until we succeed
285 while ( ATCLock( lock_fd ) <= 0 );
287 // Send the stepper home command
288 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
290 // Release the hardware
291 ATCRelease( lock_fd );
293 SG_LOG( SG_IO, SG_ALERT,
294 " - Waiting for compass to come home." );
297 int timeout = 900; // about 30 seconds
299 while ( ! home && timeout > 0 ) {
300 if ( timeout % 150 == 0 ) {
301 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
303 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
306 while ( ATCLock( lock_fd ) <= 0 );
308 unsigned char result = ATCReadStepper( stepper_fd );
313 ATCRelease( lock_fd );
315 #if defined( _MSC_VER )
316 ulMilliSecondSleep(33);
317 #elif defined (WIN32) && !defined(__CYGWIN__)
326 compass_position = 0.0;
329 /////////////////////////////////////////////////////////////////////
330 // Blank the radio display
331 /////////////////////////////////////////////////////////////////////
333 SG_LOG( SG_IO, SG_ALERT,
334 " - Clearing the radios displays." );
337 unsigned char value = 0xff;
338 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
339 radio_display_data[channel] = value;
342 // Lock the hardware, keep trying until we succeed
343 while ( ATCLock( lock_fd ) <= 0 );
346 ATCSetRadios( radio_display_fd, radio_display_data );
348 ATCRelease( lock_fd );
350 /////////////////////////////////////////////////////////////////////
352 /////////////////////////////////////////////////////////////////////
354 for ( int i = 0; i < 128; ++i ) {
355 ATCSetLamp( lamps_fd, i, false );
358 /////////////////////////////////////////////////////////////////////
359 // Finished initing hardware
360 /////////////////////////////////////////////////////////////////////
362 SG_LOG( SG_IO, SG_ALERT,
363 "Done initializing ATC output hardware." );
367 /////////////////////////////////////////////////////////////////////
368 // Connect up to property values
369 /////////////////////////////////////////////////////////////////////
373 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
374 lamps_out_node = fgGetNode( base_name );
376 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
377 radio_display_node = fgGetNode( base_name );
379 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
380 steppers_node = fgGetNode( base_name );
386 /////////////////////////////////////////////////////////////////////
388 /////////////////////////////////////////////////////////////////////
390 bool FGATCOutput::do_lamps() {
391 if ( lamps_out_node != NULL ) {
392 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
393 // read the next config entry from the property tree
395 SGPropertyNode *child = lamps_out_node->getChild(i);
396 string cname = child->getName();
397 int index = child->getIndex();
400 SGPropertyNode *src_prop = NULL;
401 if ( cname == "lamp" ) {
402 SGPropertyNode *prop;
403 prop = child->getChild( "name" );
404 if ( prop != NULL ) {
405 name = prop->getStringValue();
407 prop = child->getChild( "type" );
408 if ( prop != NULL ) {
409 type = prop->getStringValue();
411 prop = child->getChild( "prop" );
412 if ( prop != NULL ) {
413 src_prop = fgGetNode( prop->getStringValue(), true );
415 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
417 SG_LOG( SG_IO, SG_DEBUG,
418 "Input config error, expecting 'lamp' but found "
428 /////////////////////////////////////////////////////////////////////
429 // Update the radio display
430 /////////////////////////////////////////////////////////////////////
433 static bool navcom1_has_power() {
434 static SGPropertyNode *navcom1_bus_power
435 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
436 static SGPropertyNode *navcom1_power_btn
437 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
439 return (navcom1_bus_power->getDoubleValue() > 1.0)
440 && navcom1_power_btn->getBoolValue();
443 static bool navcom2_has_power() {
444 static SGPropertyNode *navcom2_bus_power
445 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
446 static SGPropertyNode *navcom2_power_btn
447 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
449 return (navcom2_bus_power->getDoubleValue() > 1.0)
450 && navcom2_power_btn->getBoolValue();
453 static bool dme_has_power() {
454 static SGPropertyNode *dme_bus_power
455 = fgGetNode( "/systems/electrical/outputs/dme", true );
457 return (dme_bus_power->getDoubleValue() > 1.0);
460 static bool adf_has_power() {
461 static SGPropertyNode *adf_bus_power
462 = fgGetNode( "/systems/electrical/outputs/adf", true );
463 static SGPropertyNode *adf_power_btn
464 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
466 return (adf_bus_power->getDoubleValue() > 1.0)
467 && adf_power_btn->getBoolValue();
470 static bool xpdr_has_power() {
471 static SGPropertyNode *xpdr_bus_power
472 = fgGetNode( "/systems/electrical/outputs/transponder", true );
473 static SGPropertyNode *xpdr_func_knob
474 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
476 return (xpdr_bus_power->getDoubleValue() > 1.0)
477 && (xpdr_func_knob->getIntValue() > 0);
480 bool FGATCOutput::do_radio_display() {
481 static SGPropertyNode *dme_serviceable
482 = fgGetNode( "/instrumentation/dme/serviceable", true );
483 static SGPropertyNode *dme_in_range
484 = fgGetNode( "/instrumentation/dme/in-range", true );
485 static SGPropertyNode *dme_min
486 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
487 static SGPropertyNode *dme_kt
488 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
489 static SGPropertyNode *dme_nm
490 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
492 static SGPropertyNode *comm1_serviceable
493 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
494 static SGPropertyNode *com1_freq
495 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
496 static SGPropertyNode *com1_stby_freq
497 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
499 static SGPropertyNode *comm2_serviceable
500 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
501 static SGPropertyNode *com2_freq
502 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
503 static SGPropertyNode *com2_stby_freq
504 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
506 static SGPropertyNode *nav1_serviceable
507 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
508 static SGPropertyNode *nav1_freq
509 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
510 static SGPropertyNode *nav1_stby_freq
511 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
513 static SGPropertyNode *nav2_serviceable
514 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
515 static SGPropertyNode *nav2_freq
516 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
517 static SGPropertyNode *nav2_stby_freq
518 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
520 static SGPropertyNode *adf_serviceable
521 = fgGetNode( "/instrumentation/adf/serviceable", true );
522 static SGPropertyNode *adf_freq
523 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
524 static SGPropertyNode *adf_stby_freq
525 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
526 static SGPropertyNode *adf_stby_mode
527 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
528 static SGPropertyNode *adf_timer_mode
529 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
530 // static SGPropertyNode *adf_count_mode
531 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
532 static SGPropertyNode *adf_flight_timer
533 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
534 static SGPropertyNode *adf_elapsed_timer
535 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
537 static SGPropertyNode *xpdr_serviceable
538 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
539 static SGPropertyNode *xpdr_func_knob
540 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
541 static SGPropertyNode *xpdr_flight_level
542 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
543 static SGPropertyNode *xpdr_id_code
544 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
549 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
550 if ( dme_in_range->getBoolValue() ) {
552 float minutes = dme_min->getFloatValue();
553 if ( minutes > 999 ) {
556 snprintf(digits, 7, "%03.0f", minutes);
557 for ( i = 0; i < 6; ++i ) {
560 radio_display_data[0] = digits[1] << 4 | digits[2];
561 radio_display_data[1] = 0xf0 | digits[0];
564 float knots = dme_kt->getFloatValue();
568 snprintf(digits, 7, "%03.0f", knots);
569 for ( i = 0; i < 6; ++i ) {
572 radio_display_data[2] = digits[1] << 4 | digits[2];
573 radio_display_data[3] = 0xf0 | digits[0];
576 float nm = dme_nm->getFloatValue();
580 snprintf(digits, 7, "%04.1f", nm);
581 for ( i = 0; i < 6; ++i ) {
584 radio_display_data[4] = digits[1] << 4 | digits[3];
585 radio_display_data[5] = 0x00 | digits[0];
586 // the 0x00 in the upper nibble of the 6th byte of each
587 // display turns on the decimal point
590 radio_display_data[0] = 0xbb;
591 radio_display_data[1] = 0xfb;
592 radio_display_data[2] = 0xbb;
593 radio_display_data[3] = 0xfb;
594 radio_display_data[4] = 0xbb;
595 radio_display_data[5] = 0x0b;
599 for ( i = 0; i < 6; ++i ) {
600 radio_display_data[i] = 0xff;
604 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
605 // Com1 standby frequency
606 float com1_stby = com1_stby_freq->getFloatValue();
607 if ( fabs(com1_stby) > 999.99 ) {
610 snprintf(digits, 7, "%06.3f", com1_stby);
611 for ( i = 0; i < 6; ++i ) {
614 radio_display_data[6] = digits[4] << 4 | digits[5];
615 radio_display_data[7] = digits[1] << 4 | digits[2];
616 radio_display_data[8] = 0xf0 | digits[0];
618 // Com1 in use frequency
619 float com1 = com1_freq->getFloatValue();
620 if ( fabs(com1) > 999.99 ) {
623 snprintf(digits, 7, "%06.3f", com1);
624 for ( i = 0; i < 6; ++i ) {
627 radio_display_data[9] = digits[4] << 4 | digits[5];
628 radio_display_data[10] = digits[1] << 4 | digits[2];
629 radio_display_data[11] = 0x00 | digits[0];
630 // the 0x00 in the upper nibble of the 6th byte of each display
631 // turns on the decimal point
633 radio_display_data[6] = 0xff;
634 radio_display_data[7] = 0xff;
635 radio_display_data[8] = 0xff;
636 radio_display_data[9] = 0xff;
637 radio_display_data[10] = 0xff;
638 radio_display_data[11] = 0xff;
641 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
642 // Com2 standby frequency
643 float com2_stby = com2_stby_freq->getFloatValue();
644 if ( fabs(com2_stby) > 999.99 ) {
647 snprintf(digits, 7, "%06.3f", com2_stby);
648 for ( i = 0; i < 6; ++i ) {
651 radio_display_data[18] = digits[4] << 4 | digits[5];
652 radio_display_data[19] = digits[1] << 4 | digits[2];
653 radio_display_data[20] = 0xf0 | digits[0];
655 // Com2 in use frequency
656 float com2 = com2_freq->getFloatValue();
657 if ( fabs(com2) > 999.99 ) {
660 snprintf(digits, 7, "%06.3f", com2);
661 for ( i = 0; i < 6; ++i ) {
664 radio_display_data[21] = digits[4] << 4 | digits[5];
665 radio_display_data[22] = digits[1] << 4 | digits[2];
666 radio_display_data[23] = 0x00 | digits[0];
667 // the 0x00 in the upper nibble of the 6th byte of each display
668 // turns on the decimal point
670 radio_display_data[18] = 0xff;
671 radio_display_data[19] = 0xff;
672 radio_display_data[20] = 0xff;
673 radio_display_data[21] = 0xff;
674 radio_display_data[22] = 0xff;
675 radio_display_data[23] = 0xff;
678 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
679 // Nav1 standby frequency
680 float nav1_stby = nav1_stby_freq->getFloatValue();
681 if ( fabs(nav1_stby) > 999.99 ) {
684 snprintf(digits, 7, "%06.2f", nav1_stby);
685 for ( i = 0; i < 6; ++i ) {
688 radio_display_data[12] = digits[4] << 4 | digits[5];
689 radio_display_data[13] = digits[1] << 4 | digits[2];
690 radio_display_data[14] = 0xf0 | digits[0];
692 // Nav1 in use frequency
693 float nav1 = nav1_freq->getFloatValue();
694 if ( fabs(nav1) > 999.99 ) {
697 snprintf(digits, 7, "%06.2f", nav1);
698 for ( i = 0; i < 6; ++i ) {
701 radio_display_data[15] = digits[4] << 4 | digits[5];
702 radio_display_data[16] = digits[1] << 4 | digits[2];
703 radio_display_data[17] = 0x00 | digits[0];
704 // the 0x00 in the upper nibble of the 6th byte of each display
705 // turns on the decimal point
707 radio_display_data[12] = 0xff;
708 radio_display_data[13] = 0xff;
709 radio_display_data[14] = 0xff;
710 radio_display_data[15] = 0xff;
711 radio_display_data[16] = 0xff;
712 radio_display_data[17] = 0xff;
715 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
716 // Nav2 standby frequency
717 float nav2_stby = nav2_stby_freq->getFloatValue();
718 if ( fabs(nav2_stby) > 999.99 ) {
721 snprintf(digits, 7, "%06.2f", nav2_stby);
722 for ( i = 0; i < 6; ++i ) {
725 radio_display_data[24] = digits[4] << 4 | digits[5];
726 radio_display_data[25] = digits[1] << 4 | digits[2];
727 radio_display_data[26] = 0xf0 | digits[0];
729 // Nav2 in use frequency
730 float nav2 = nav2_freq->getFloatValue();
731 if ( fabs(nav2) > 999.99 ) {
734 snprintf(digits, 7, "%06.2f", nav2);
735 for ( i = 0; i < 6; ++i ) {
738 radio_display_data[27] = digits[4] << 4 | digits[5];
739 radio_display_data[28] = digits[1] << 4 | digits[2];
740 radio_display_data[29] = 0x00 | digits[0];
741 // the 0x00 in the upper nibble of the 6th byte of each display
742 // turns on the decimal point
744 radio_display_data[24] = 0xff;
745 radio_display_data[25] = 0xff;
746 radio_display_data[26] = 0xff;
747 radio_display_data[27] = 0xff;
748 radio_display_data[28] = 0xff;
749 radio_display_data[29] = 0xff;
752 // ADF standby frequency / timer
753 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
754 if ( adf_stby_mode->getIntValue() == 0 ) {
756 float adf_stby = adf_stby_freq->getFloatValue();
757 if ( fabs(adf_stby) > 1799 ) {
760 snprintf(digits, 7, "%04.0f", adf_stby);
761 for ( i = 0; i < 6; ++i ) {
764 radio_display_data[30] = digits[3] << 4 | 0x0f;
765 radio_display_data[31] = digits[1] << 4 | digits[2];
766 if ( digits[0] == 0 ) {
767 radio_display_data[32] = 0xff;
769 radio_display_data[32] = 0xf0 | digits[0];
775 if ( adf_timer_mode->getIntValue() == 0 ) {
776 time = adf_flight_timer->getDoubleValue();
778 time = adf_elapsed_timer->getDoubleValue();
780 // cout << time << endl;
781 hours = (int)(time / 3600.0);
782 time -= hours * 3600.00;
783 min = (int)(time / 60.0);
800 // cout << big << ":" << little << endl;
801 snprintf(digits, 7, "%02d%02d", big, little);
802 for ( i = 0; i < 6; ++i ) {
805 radio_display_data[30] = digits[2] << 4 | digits[3];
806 radio_display_data[31] = digits[0] << 4 | digits[1];
807 radio_display_data[32] = 0xff;
810 // ADF in use frequency
811 float adf = adf_freq->getFloatValue();
812 if ( fabs(adf) > 1799 ) {
815 snprintf(digits, 7, "%04.0f", adf);
816 for ( i = 0; i < 6; ++i ) {
819 radio_display_data[33] = digits[2] << 4 | digits[3];
820 if ( digits[0] == 0 ) {
821 radio_display_data[34] = 0xf0 | digits[1];
823 radio_display_data[34] = digits[0] << 4 | digits[1];
825 if ( adf_stby_mode->getIntValue() == 0 ) {
826 radio_display_data[35] = 0xff;
828 radio_display_data[35] = 0x0f;
831 radio_display_data[30] = 0xff;
832 radio_display_data[31] = 0xff;
833 radio_display_data[32] = 0xff;
834 radio_display_data[33] = 0xff;
835 radio_display_data[34] = 0xff;
836 radio_display_data[35] = 0xff;
839 // Transponder code and flight level
840 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
841 if ( xpdr_func_knob->getIntValue() == 2 ) {
843 radio_display_data[36] = 8 << 4 | 8;
844 radio_display_data[37] = 8 << 4 | 8;
845 radio_display_data[38] = 0xff;
846 radio_display_data[39] = 8 << 4 | 0x0f;
847 radio_display_data[40] = 8 << 4 | 8;
850 int id_code = xpdr_id_code->getIntValue();
852 for ( i = 0; i < 4; ++i ) {
853 digits[i] = id_code / place;
854 id_code -= digits[i] * place;
857 radio_display_data[36] = digits[2] << 4 | digits[3];
858 radio_display_data[37] = digits[0] << 4 | digits[1];
859 radio_display_data[38] = 0xff;
861 if ( xpdr_func_knob->getIntValue() == 3 ||
862 xpdr_func_knob->getIntValue() == 5 )
864 // do flight level display
865 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
866 for ( i = 0; i < 6; ++i ) {
869 radio_display_data[39] = digits[2] << 4 | 0x0f;
870 radio_display_data[40] = digits[0] << 4 | digits[1];
872 // blank flight level display
873 radio_display_data[39] = 0xff;
874 radio_display_data[40] = 0xff;
879 radio_display_data[36] = 0xff;
880 radio_display_data[37] = 0xff;
881 radio_display_data[38] = 0xff;
882 radio_display_data[39] = 0xff;
883 radio_display_data[40] = 0xff;
886 ATCSetRadios( radio_display_fd, radio_display_data );
892 /////////////////////////////////////////////////////////////////////
893 // Drive the stepper motors
894 /////////////////////////////////////////////////////////////////////
896 bool FGATCOutput::do_steppers() {
897 SGPropertyNode *mag_compass
898 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
901 float diff = mag_compass->getFloatValue() - compass_position;
902 while ( diff < -180.0 ) { diff += 360.0; }
903 while ( diff > 180.0 ) { diff -= 360.0; }
905 int steps = (int)(diff * 4);
906 // cout << "steps = " << steps << endl;
907 if ( steps > 4 ) { steps = 4; }
908 if ( steps < -4 ) { steps = -4; }
910 if ( abs(steps) > 0 ) {
911 unsigned char cmd = 0x80; // stepper command
913 cmd |= 0x20; // go up
915 cmd |= 0x00; // go down
919 // sync compass_position with hardware position
920 compass_position += (float)steps / 4.0;
922 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
929 // process the hardware outputs. This code assumes the calling layer
930 // will lock the hardware.
931 bool FGATCOutput::process() {
933 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
940 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
948 bool FGATCOutput::close() {
950 #if defined( unix ) || defined( __CYGWIN__ )
958 result = ::close( lamps_fd );
959 if ( result == -1 ) {
960 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
962 snprintf( msg, 256, "Error closing %s", lamps_file );
967 result = ::close( radio_display_fd );
968 if ( result == -1 ) {
969 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
971 snprintf( msg, 256, "Error closing %s", radio_display_file );
976 result = ::close( stepper_fd );
977 if ( result == -1 ) {
978 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
980 snprintf( msg, 256, "Error closing %s", stepper_file );