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 lamps_out_node(NULL),
101 radio_display_node(NULL),
105 config = _config_file;
109 // Write a radios command
110 static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
111 #if defined( unix ) || defined( __CYGWIN__ )
113 lseek( fd, 0, SEEK_SET );
115 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
117 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
118 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
128 // Write a stepper command
129 static int ATCSetStepper( int fd, unsigned char channel,
130 unsigned char value )
132 #if defined( unix ) || defined( __CYGWIN__ )
134 lseek( fd, 0, SEEK_SET );
137 unsigned char buf[3];
141 int result = write( fd, buf, 2 );
143 SG_LOG( SG_IO, SG_INFO, "Write failed" );
145 SG_LOG( SG_IO, SG_DEBUG,
146 "Sent cmd = " << (int)channel << " value = " << (int)value );
154 // Read status of last stepper written to
155 static unsigned char ATCReadStepper( int fd ) {
156 #if defined( unix ) || defined( __CYGWIN__ )
160 lseek( fd, 0, SEEK_SET );
163 unsigned char buf[2];
164 result = read( fd, buf, 1 );
166 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
169 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
178 // Turn a lamp on or off
179 void ATCSetLamp( int fd, int channel, bool value ) {
180 #if defined( unix ) || defined( __CYGWIN__ )
181 // lamp channels 0-63 are written to LampPort0, channels 64-127
182 // are written to LampPort1
184 // bits 0-6 are the lamp address
185 // bit 7 is the value (on/off)
190 unsigned char buf[3];
194 result = write( fd, buf, 2 );
196 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
203 void FGATCOutput::init_config() {
204 #if defined( unix ) || defined( __CYGWIN__ )
205 if ( config.str()[0] != '/' ) {
206 // not an absolute path, prepend the standard location
208 char *envp = ::getenv( "HOME" );
209 if ( envp != NULL ) {
211 tmp.append( ".atcflightsim" );
212 tmp.append( config.str() );
216 readProperties( config.str(), globals->get_props() );
221 // Open and initialize the ATC hardware
222 bool FGATCOutput::open( int lock_fd ) {
224 SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
229 // This loads the config parameters generated by "simcal"
232 SG_LOG( SG_IO, SG_ALERT,
233 "Initializing ATC output hardware, please wait ..." );
235 snprintf( lamps_file, 256,
236 "/proc/atcflightsim/board%d/lamps", board );
237 snprintf( radio_display_file, 256,
238 "/proc/atcflightsim/board%d/radios", board );
239 snprintf( stepper_file, 256,
240 "/proc/atcflightsim/board%d/steppers", board );
242 #if defined( unix ) || defined( __CYGWIN__ )
244 /////////////////////////////////////////////////////////////////////
245 // Open the /proc files
246 /////////////////////////////////////////////////////////////////////
248 lamps_fd = ::open( lamps_file, O_WRONLY );
249 if ( lamps_fd == -1 ) {
250 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
252 snprintf( msg, 256, "Error opening %s", lamps_file );
257 radio_display_fd = ::open( radio_display_file, O_RDWR );
258 if ( radio_display_fd == -1 ) {
259 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
261 snprintf( msg, 256, "Error opening %s", radio_display_file );
266 stepper_fd = ::open( stepper_file, O_RDWR );
267 if ( stepper_fd == -1 ) {
268 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
270 snprintf( msg, 256, "Error opening %s", stepper_file );
277 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
278 /////////////////////////////////////////////////////////////////////
279 // Home the compass stepper motor
280 /////////////////////////////////////////////////////////////////////
282 SG_LOG( SG_IO, SG_ALERT,
283 " - Homing the compass stepper motor" );
285 // Lock the hardware, keep trying until we succeed
286 while ( ATCLock( lock_fd ) <= 0 );
288 // Send the stepper home command
289 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
291 // Release the hardware
292 ATCRelease( lock_fd );
294 SG_LOG( SG_IO, SG_ALERT,
295 " - Waiting for compass to come home." );
298 int timeout = 900; // about 30 seconds
300 while ( ! home && timeout > 0 ) {
301 if ( timeout % 150 == 0 ) {
302 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
304 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
307 while ( ATCLock( lock_fd ) <= 0 );
309 unsigned char result = ATCReadStepper( stepper_fd );
314 ATCRelease( lock_fd );
316 #if defined( _MSC_VER )
317 ulMilliSecondSleep(33);
318 #elif defined (WIN32) && !defined(__CYGWIN__)
327 compass_position = 0.0;
330 /////////////////////////////////////////////////////////////////////
331 // Blank the radio display
332 /////////////////////////////////////////////////////////////////////
334 SG_LOG( SG_IO, SG_ALERT,
335 " - Clearing the radios displays." );
338 unsigned char value = 0xff;
339 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
340 radio_display_data[channel] = value;
343 // Lock the hardware, keep trying until we succeed
344 while ( ATCLock( lock_fd ) <= 0 );
347 ATCSetRadios( radio_display_fd, radio_display_data );
349 ATCRelease( lock_fd );
351 /////////////////////////////////////////////////////////////////////
353 /////////////////////////////////////////////////////////////////////
355 for ( int i = 0; i < 128; ++i ) {
356 ATCSetLamp( lamps_fd, i, false );
359 /////////////////////////////////////////////////////////////////////
360 // Finished initing hardware
361 /////////////////////////////////////////////////////////////////////
363 SG_LOG( SG_IO, SG_ALERT,
364 "Done initializing ATC output hardware." );
368 /////////////////////////////////////////////////////////////////////
369 // Connect up to property values
370 /////////////////////////////////////////////////////////////////////
374 snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
375 lamps_out_node = fgGetNode( base_name );
377 snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
378 radio_display_node = fgGetNode( base_name );
380 snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
381 steppers_node = fgGetNode( base_name );
387 /////////////////////////////////////////////////////////////////////
389 /////////////////////////////////////////////////////////////////////
391 bool FGATCOutput::do_lamps() {
392 if ( lamps_out_node != NULL ) {
393 for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
394 // read the next config entry from the property tree
396 SGPropertyNode *child = lamps_out_node->getChild(i);
397 string cname = child->getName();
398 int index = child->getIndex();
401 SGPropertyNode *src_prop = NULL;
402 if ( cname == "lamp" ) {
403 SGPropertyNode *prop;
404 prop = child->getChild( "name" );
405 if ( prop != NULL ) {
406 name = prop->getStringValue();
408 prop = child->getChild( "type" );
409 if ( prop != NULL ) {
410 type = prop->getStringValue();
412 prop = child->getChild( "prop" );
413 if ( prop != NULL ) {
414 src_prop = fgGetNode( prop->getStringValue(), true );
416 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
418 SG_LOG( SG_IO, SG_DEBUG,
419 "Input config error, expecting 'lamp' but found "
429 /////////////////////////////////////////////////////////////////////
430 // Update the radio display
431 /////////////////////////////////////////////////////////////////////
434 static bool navcom1_has_power() {
435 static SGPropertyNode *navcom1_bus_power
436 = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
437 static SGPropertyNode *navcom1_power_btn
438 = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
440 return (navcom1_bus_power->getDoubleValue() > 1.0)
441 && navcom1_power_btn->getBoolValue();
444 static bool navcom2_has_power() {
445 static SGPropertyNode *navcom2_bus_power
446 = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
447 static SGPropertyNode *navcom2_power_btn
448 = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
450 return (navcom2_bus_power->getDoubleValue() > 1.0)
451 && navcom2_power_btn->getBoolValue();
454 static bool dme_has_power() {
455 static SGPropertyNode *dme_bus_power
456 = fgGetNode( "/systems/electrical/outputs/dme", true );
458 return (dme_bus_power->getDoubleValue() > 1.0);
461 static bool adf_has_power() {
462 static SGPropertyNode *adf_bus_power
463 = fgGetNode( "/systems/electrical/outputs/adf", true );
464 static SGPropertyNode *adf_power_btn
465 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
467 return (adf_bus_power->getDoubleValue() > 1.0)
468 && adf_power_btn->getBoolValue();
471 static bool xpdr_has_power() {
472 static SGPropertyNode *xpdr_bus_power
473 = fgGetNode( "/systems/electrical/outputs/transponder", true );
474 static SGPropertyNode *xpdr_func_knob
475 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
477 return (xpdr_bus_power->getDoubleValue() > 1.0)
478 && (xpdr_func_knob->getIntValue() > 0);
481 bool FGATCOutput::do_radio_display() {
482 static SGPropertyNode *dme_serviceable
483 = fgGetNode( "/instrumentation/dme/serviceable", true );
484 static SGPropertyNode *dme_in_range
485 = fgGetNode( "/instrumentation/dme/in-range", true );
486 static SGPropertyNode *dme_min
487 = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
488 static SGPropertyNode *dme_kt
489 = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
490 static SGPropertyNode *dme_nm
491 = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
493 static SGPropertyNode *comm1_serviceable
494 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
495 static SGPropertyNode *com1_freq
496 = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
497 static SGPropertyNode *com1_stby_freq
498 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
500 static SGPropertyNode *comm2_serviceable
501 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
502 static SGPropertyNode *com2_freq
503 = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
504 static SGPropertyNode *com2_stby_freq
505 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
507 static SGPropertyNode *nav1_serviceable
508 = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
509 static SGPropertyNode *nav1_freq
510 = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
511 static SGPropertyNode *nav1_stby_freq
512 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
514 static SGPropertyNode *nav2_serviceable
515 = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
516 static SGPropertyNode *nav2_freq
517 = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
518 static SGPropertyNode *nav2_stby_freq
519 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
521 static SGPropertyNode *adf_serviceable
522 = fgGetNode( "/instrumentation/adf/serviceable", true );
523 static SGPropertyNode *adf_freq
524 = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
525 static SGPropertyNode *adf_stby_freq
526 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
527 static SGPropertyNode *adf_stby_mode
528 = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
529 static SGPropertyNode *adf_timer_mode
530 = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
531 // static SGPropertyNode *adf_count_mode
532 // = fgGetNode( "/instrumentation/kr-87/modes/count", true );
533 static SGPropertyNode *adf_flight_timer
534 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
535 static SGPropertyNode *adf_elapsed_timer
536 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
538 static SGPropertyNode *xpdr_serviceable
539 = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
540 static SGPropertyNode *xpdr_func_knob
541 = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
542 static SGPropertyNode *xpdr_flight_level
543 = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
544 static SGPropertyNode *xpdr_id_code
545 = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
550 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
551 if ( dme_in_range->getBoolValue() ) {
553 float minutes = dme_min->getFloatValue();
554 if ( minutes > 999 ) {
557 snprintf(digits, 7, "%03.0f", minutes);
558 for ( i = 0; i < 6; ++i ) {
561 radio_display_data[0] = digits[1] << 4 | digits[2];
562 radio_display_data[1] = 0xf0 | digits[0];
565 float knots = dme_kt->getFloatValue();
569 snprintf(digits, 7, "%03.0f", knots);
570 for ( i = 0; i < 6; ++i ) {
573 radio_display_data[2] = digits[1] << 4 | digits[2];
574 radio_display_data[3] = 0xf0 | digits[0];
577 float nm = dme_nm->getFloatValue();
581 snprintf(digits, 7, "%04.1f", nm);
582 for ( i = 0; i < 6; ++i ) {
585 radio_display_data[4] = digits[1] << 4 | digits[3];
586 radio_display_data[5] = 0x00 | digits[0];
587 // the 0x00 in the upper nibble of the 6th byte of each
588 // display turns on the decimal point
591 radio_display_data[0] = 0xbb;
592 radio_display_data[1] = 0xfb;
593 radio_display_data[2] = 0xbb;
594 radio_display_data[3] = 0xfb;
595 radio_display_data[4] = 0xbb;
596 radio_display_data[5] = 0x0b;
600 for ( i = 0; i < 6; ++i ) {
601 radio_display_data[i] = 0xff;
605 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
606 // Com1 standby frequency
607 float com1_stby = com1_stby_freq->getFloatValue();
608 if ( fabs(com1_stby) > 999.99 ) {
611 snprintf(digits, 7, "%06.3f", com1_stby);
612 for ( i = 0; i < 6; ++i ) {
615 radio_display_data[6] = digits[4] << 4 | digits[5];
616 radio_display_data[7] = digits[1] << 4 | digits[2];
617 radio_display_data[8] = 0xf0 | digits[0];
619 // Com1 in use frequency
620 float com1 = com1_freq->getFloatValue();
621 if ( fabs(com1) > 999.99 ) {
624 snprintf(digits, 7, "%06.3f", com1);
625 for ( i = 0; i < 6; ++i ) {
628 radio_display_data[9] = digits[4] << 4 | digits[5];
629 radio_display_data[10] = digits[1] << 4 | digits[2];
630 radio_display_data[11] = 0x00 | digits[0];
631 // the 0x00 in the upper nibble of the 6th byte of each display
632 // turns on the decimal point
634 radio_display_data[6] = 0xff;
635 radio_display_data[7] = 0xff;
636 radio_display_data[8] = 0xff;
637 radio_display_data[9] = 0xff;
638 radio_display_data[10] = 0xff;
639 radio_display_data[11] = 0xff;
642 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
643 // Com2 standby frequency
644 float com2_stby = com2_stby_freq->getFloatValue();
645 if ( fabs(com2_stby) > 999.99 ) {
648 snprintf(digits, 7, "%06.3f", com2_stby);
649 for ( i = 0; i < 6; ++i ) {
652 radio_display_data[18] = digits[4] << 4 | digits[5];
653 radio_display_data[19] = digits[1] << 4 | digits[2];
654 radio_display_data[20] = 0xf0 | digits[0];
656 // Com2 in use frequency
657 float com2 = com2_freq->getFloatValue();
658 if ( fabs(com2) > 999.99 ) {
661 snprintf(digits, 7, "%06.3f", com2);
662 for ( i = 0; i < 6; ++i ) {
665 radio_display_data[21] = digits[4] << 4 | digits[5];
666 radio_display_data[22] = digits[1] << 4 | digits[2];
667 radio_display_data[23] = 0x00 | digits[0];
668 // the 0x00 in the upper nibble of the 6th byte of each display
669 // turns on the decimal point
671 radio_display_data[18] = 0xff;
672 radio_display_data[19] = 0xff;
673 radio_display_data[20] = 0xff;
674 radio_display_data[21] = 0xff;
675 radio_display_data[22] = 0xff;
676 radio_display_data[23] = 0xff;
679 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
680 // Nav1 standby frequency
681 float nav1_stby = nav1_stby_freq->getFloatValue();
682 if ( fabs(nav1_stby) > 999.99 ) {
685 snprintf(digits, 7, "%06.2f", nav1_stby);
686 for ( i = 0; i < 6; ++i ) {
689 radio_display_data[12] = digits[4] << 4 | digits[5];
690 radio_display_data[13] = digits[1] << 4 | digits[2];
691 radio_display_data[14] = 0xf0 | digits[0];
693 // Nav1 in use frequency
694 float nav1 = nav1_freq->getFloatValue();
695 if ( fabs(nav1) > 999.99 ) {
698 snprintf(digits, 7, "%06.2f", nav1);
699 for ( i = 0; i < 6; ++i ) {
702 radio_display_data[15] = digits[4] << 4 | digits[5];
703 radio_display_data[16] = digits[1] << 4 | digits[2];
704 radio_display_data[17] = 0x00 | digits[0];
705 // the 0x00 in the upper nibble of the 6th byte of each display
706 // turns on the decimal point
708 radio_display_data[12] = 0xff;
709 radio_display_data[13] = 0xff;
710 radio_display_data[14] = 0xff;
711 radio_display_data[15] = 0xff;
712 radio_display_data[16] = 0xff;
713 radio_display_data[17] = 0xff;
716 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
717 // Nav2 standby frequency
718 float nav2_stby = nav2_stby_freq->getFloatValue();
719 if ( fabs(nav2_stby) > 999.99 ) {
722 snprintf(digits, 7, "%06.2f", nav2_stby);
723 for ( i = 0; i < 6; ++i ) {
726 radio_display_data[24] = digits[4] << 4 | digits[5];
727 radio_display_data[25] = digits[1] << 4 | digits[2];
728 radio_display_data[26] = 0xf0 | digits[0];
730 // Nav2 in use frequency
731 float nav2 = nav2_freq->getFloatValue();
732 if ( fabs(nav2) > 999.99 ) {
735 snprintf(digits, 7, "%06.2f", nav2);
736 for ( i = 0; i < 6; ++i ) {
739 radio_display_data[27] = digits[4] << 4 | digits[5];
740 radio_display_data[28] = digits[1] << 4 | digits[2];
741 radio_display_data[29] = 0x00 | digits[0];
742 // the 0x00 in the upper nibble of the 6th byte of each display
743 // turns on the decimal point
745 radio_display_data[24] = 0xff;
746 radio_display_data[25] = 0xff;
747 radio_display_data[26] = 0xff;
748 radio_display_data[27] = 0xff;
749 radio_display_data[28] = 0xff;
750 radio_display_data[29] = 0xff;
753 // ADF standby frequency / timer
754 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
755 if ( adf_stby_mode->getIntValue() == 0 ) {
757 float adf_stby = adf_stby_freq->getFloatValue();
758 if ( fabs(adf_stby) > 1799 ) {
761 snprintf(digits, 7, "%04.0f", adf_stby);
762 for ( i = 0; i < 6; ++i ) {
765 radio_display_data[30] = digits[3] << 4 | 0x0f;
766 radio_display_data[31] = digits[1] << 4 | digits[2];
767 if ( digits[0] == 0 ) {
768 radio_display_data[32] = 0xff;
770 radio_display_data[32] = 0xf0 | digits[0];
776 if ( adf_timer_mode->getIntValue() == 0 ) {
777 time = adf_flight_timer->getDoubleValue();
779 time = adf_elapsed_timer->getDoubleValue();
781 // cout << time << endl;
782 hours = (int)(time / 3600.0);
783 time -= hours * 3600.00;
784 min = (int)(time / 60.0);
801 // cout << big << ":" << little << endl;
802 snprintf(digits, 7, "%02d%02d", big, little);
803 for ( i = 0; i < 6; ++i ) {
806 radio_display_data[30] = digits[2] << 4 | digits[3];
807 radio_display_data[31] = digits[0] << 4 | digits[1];
808 radio_display_data[32] = 0xff;
811 // ADF in use frequency
812 float adf = adf_freq->getFloatValue();
813 if ( fabs(adf) > 1799 ) {
816 snprintf(digits, 7, "%04.0f", adf);
817 for ( i = 0; i < 6; ++i ) {
820 radio_display_data[33] = digits[2] << 4 | digits[3];
821 if ( digits[0] == 0 ) {
822 radio_display_data[34] = 0xf0 | digits[1];
824 radio_display_data[34] = digits[0] << 4 | digits[1];
826 if ( adf_stby_mode->getIntValue() == 0 ) {
827 radio_display_data[35] = 0xff;
829 radio_display_data[35] = 0x0f;
832 radio_display_data[30] = 0xff;
833 radio_display_data[31] = 0xff;
834 radio_display_data[32] = 0xff;
835 radio_display_data[33] = 0xff;
836 radio_display_data[34] = 0xff;
837 radio_display_data[35] = 0xff;
840 // Transponder code and flight level
841 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
842 if ( xpdr_func_knob->getIntValue() == 2 ) {
844 radio_display_data[36] = 8 << 4 | 8;
845 radio_display_data[37] = 8 << 4 | 8;
846 radio_display_data[38] = 0xff;
847 radio_display_data[39] = 8 << 4 | 0x0f;
848 radio_display_data[40] = 8 << 4 | 8;
851 int id_code = xpdr_id_code->getIntValue();
853 for ( i = 0; i < 4; ++i ) {
854 digits[i] = id_code / place;
855 id_code -= digits[i] * place;
858 radio_display_data[36] = digits[2] << 4 | digits[3];
859 radio_display_data[37] = digits[0] << 4 | digits[1];
860 radio_display_data[38] = 0xff;
862 if ( xpdr_func_knob->getIntValue() == 3 ||
863 xpdr_func_knob->getIntValue() == 5 )
865 // do flight level display
866 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
867 for ( i = 0; i < 6; ++i ) {
870 radio_display_data[39] = digits[2] << 4 | 0x0f;
871 radio_display_data[40] = digits[0] << 4 | digits[1];
873 // blank flight level display
874 radio_display_data[39] = 0xff;
875 radio_display_data[40] = 0xff;
880 radio_display_data[36] = 0xff;
881 radio_display_data[37] = 0xff;
882 radio_display_data[38] = 0xff;
883 radio_display_data[39] = 0xff;
884 radio_display_data[40] = 0xff;
887 ATCSetRadios( radio_display_fd, radio_display_data );
893 /////////////////////////////////////////////////////////////////////
894 // Drive the stepper motors
895 /////////////////////////////////////////////////////////////////////
897 bool FGATCOutput::do_steppers() {
898 SGPropertyNode *mag_compass
899 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
902 float diff = mag_compass->getFloatValue() - compass_position;
903 while ( diff < -180.0 ) { diff += 360.0; }
904 while ( diff > 180.0 ) { diff -= 360.0; }
906 int steps = (int)(diff * 4);
907 // cout << "steps = " << steps << endl;
908 if ( steps > 4 ) { steps = 4; }
909 if ( steps < -4 ) { steps = -4; }
911 if ( abs(steps) > 0 ) {
912 unsigned char cmd = 0x80; // stepper command
914 cmd |= 0x20; // go up
916 cmd |= 0x00; // go down
920 // sync compass_position with hardware position
921 compass_position += (float)steps / 4.0;
923 ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
930 // process the hardware outputs. This code assumes the calling layer
931 // will lock the hardware.
932 bool FGATCOutput::process() {
934 SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
941 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
949 bool FGATCOutput::close() {
951 #if defined( unix ) || defined( __CYGWIN__ )
959 result = ::close( lamps_fd );
960 if ( result == -1 ) {
961 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
963 snprintf( msg, 256, "Error closing %s", lamps_file );
968 result = ::close( radio_display_fd );
969 if ( result == -1 ) {
970 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
972 snprintf( msg, 256, "Error closing %s", radio_display_file );
977 result = ::close( stepper_fd );
978 if ( result == -1 ) {
979 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
981 snprintf( msg, 256, "Error closing %s", stepper_file );