]> git.mxchange.org Git - flightgear.git/blob - src/Network/ATC-Outputs.cxx
Development for two new features:
[flightgear.git] / src / Network / ATC-Outputs.cxx
1 // ATC-Outputs.hxx -- Translate FGFS properties to ATC hardware outputs.
2 //
3 // Written by Curtis Olson, started November 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #if defined( unix ) || defined( __CYGWIN__ )
31 #  include <sys/types.h>
32 #  include <sys/stat.h>
33 #  include <fcntl.h>
34 #  include <unistd.h>
35 #  include <ostream>
36 #endif
37
38 #include <errno.h>
39 #include <math.h>
40
41 #include <string>
42
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/misc/sg_path.hxx>
45 #include <simgear/props/props_io.hxx>
46
47 #include <Main/fg_props.hxx>
48
49 #include "ATC-Outputs.hxx"
50
51 using std::string;
52
53
54
55 // Lock the ATC hardware
56 static int ATCLock( int fd ) {
57 #if defined( unix ) || defined( __CYGWIN__ )
58     // rewind
59     lseek( fd, 0, SEEK_SET );
60
61     char tmp[2];
62     int result = read( fd, tmp, 1 );
63     if ( result != 1 ) {
64         SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
65     }
66
67     return result;
68 #else
69     return -1;
70 #endif
71 }
72
73
74 // Release the ATC hardware
75 static int ATCRelease( int fd ) {
76 #if defined( unix ) || defined( __CYGWIN__ )
77     // rewind
78     lseek( fd, 0, SEEK_SET );
79
80     char tmp[2];
81     tmp[0] = tmp[1] = 0;
82     int result = write( fd, tmp, 1 );
83
84     if ( result != 1 ) {
85         SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
86     }
87
88     return result;
89 #else
90     return -1;
91 #endif
92 }
93
94
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 ) :
99     is_open(false),
100     lamps_out_node(NULL),
101     radio_display_node(NULL),
102     steppers_node(NULL)
103 {
104     board = _board;
105     config = _config_file;
106 }
107
108
109 // Write a radios command
110 static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
111 #if defined( unix ) || defined( __CYGWIN__ )
112     // rewind
113     lseek( fd, 0, SEEK_SET );
114
115     int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
116
117     if ( result != ATC_RADIO_DISPLAY_BYTES ) {
118         SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
119     }
120
121     return result;
122 #else
123     return -1;
124 #endif
125 }
126
127
128 // Write a stepper command
129 static int ATCSetStepper( int fd, unsigned char channel,
130                               unsigned char value )
131 {
132 #if defined( unix ) || defined( __CYGWIN__ )
133     // rewind
134     lseek( fd, 0, SEEK_SET );
135
136     // Write the value
137     unsigned char buf[3];
138     buf[0] = channel;
139     buf[1] = value;
140     buf[2] = 0;
141     int result = write( fd, buf, 2 );
142     if ( result != 2 ) {
143         SG_LOG( SG_IO, SG_INFO, "Write failed" );
144     }
145     SG_LOG( SG_IO, SG_DEBUG,
146             "Sent cmd = " << (int)channel << " value = " << (int)value );
147     return result;
148 #else
149     return -1;
150 #endif
151 }
152
153
154 // Read status of last stepper written to
155 static unsigned char ATCReadStepper( int fd ) {
156 #if defined( unix ) || defined( __CYGWIN__ )
157     int result;
158
159     // rewind
160     lseek( fd, 0, SEEK_SET );
161
162     // Write the value
163     unsigned char buf[2];
164     result = read( fd, buf, 1 );
165     if ( result != 1 ) {
166         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
167         exit( -1 );
168     }
169     SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
170
171     return buf[0];
172 #else
173     return 0;
174 #endif
175 }
176
177
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
183
184     // bits 0-6 are the lamp address
185     // bit 7 is the value (on/off)
186
187     int result;
188
189     // Write the value
190     unsigned char buf[3];
191     buf[0] = channel;
192     buf[1] = value;
193     buf[2] = 0;
194     result = write( fd, buf, 2 );
195     if ( result != 2 ) {
196         SG_LOG( SG_IO, SG_ALERT,  "Write failed" );
197         exit( -1 );
198     }
199 #endif
200 }
201
202
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
207         SGPath tmp;
208         char *envp = ::getenv( "HOME" );
209         if ( envp != NULL ) {
210             tmp = envp;
211             tmp.append( ".atcflightsim" );
212             tmp.append( config.str() );
213             config = tmp;
214         }
215     }
216     readProperties( config.str(), globals->get_props() );
217 #endif
218 }
219
220
221 // Open and initialize the ATC hardware
222 bool FGATCOutput::open( int lock_fd ) {
223     if ( is_open ) {
224         SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
225                 << board );
226         return false;
227     }
228
229     // This loads the config parameters generated by "simcal"
230     init_config();
231
232     SG_LOG( SG_IO, SG_ALERT,
233             "Initializing ATC output hardware, please wait ..." );
234
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 );
241
242 #if defined( unix ) || defined( __CYGWIN__ )
243
244     /////////////////////////////////////////////////////////////////////
245     // Open the /proc files
246     /////////////////////////////////////////////////////////////////////
247
248     lamps_fd = ::open( lamps_file, O_WRONLY );
249     if ( lamps_fd == -1 ) {
250         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
251         char msg[256];
252         snprintf( msg, 256, "Error opening %s", lamps_file );
253         perror( msg );
254         exit( -1 );
255     }
256
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 );
260         char msg[256];
261         snprintf( msg, 256, "Error opening %s", radio_display_file );
262         perror( msg );
263         exit( -1 );
264     }
265
266     stepper_fd = ::open( stepper_file, O_RDWR );
267     if ( stepper_fd == -1 ) {
268         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
269         char msg[256];
270         snprintf( msg, 256, "Error opening %s", stepper_file );
271         perror( msg );
272         exit( -1 );
273     }
274
275 #endif
276
277 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
278     /////////////////////////////////////////////////////////////////////
279     // Home the compass stepper motor
280     /////////////////////////////////////////////////////////////////////
281
282     SG_LOG( SG_IO, SG_ALERT,
283             "  - Homing the compass stepper motor" );
284
285     // Lock the hardware, keep trying until we succeed
286     while ( ATCLock( lock_fd ) <= 0 );
287
288     // Send the stepper home command
289     ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
290
291     // Release the hardware
292     ATCRelease( lock_fd );
293
294     SG_LOG( SG_IO, SG_ALERT,
295             "  - Waiting for compass to come home." );
296
297     bool home = false;
298     int timeout = 900;          // about 30 seconds
299     timeout = 0;
300     while ( ! home && timeout > 0 ) {
301         if ( timeout % 150 == 0 ) {
302             SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
303         } else {
304             SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
305         }
306
307         while ( ATCLock( lock_fd ) <= 0 );
308
309         unsigned char result = ATCReadStepper( stepper_fd );
310         if ( result == 0 ) {
311             home = true;
312         }
313
314         ATCRelease( lock_fd );
315
316 #if defined( _MSC_VER )
317         ulMilliSecondSleep(33);
318 #elif defined (WIN32) && !defined(__CYGWIN__)
319         Sleep (33);
320 #else
321         usleep(33);
322 #endif
323
324         --timeout;
325     }
326
327     compass_position = 0.0;
328 #endif
329
330     /////////////////////////////////////////////////////////////////////
331     // Blank the radio display
332     /////////////////////////////////////////////////////////////////////
333
334     SG_LOG( SG_IO, SG_ALERT,
335             "  - Clearing the radios displays." );
336
337     // Prepair the data
338     unsigned char value = 0xff;
339     for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
340         radio_display_data[channel] = value;
341     }
342
343     // Lock the hardware, keep trying until we succeed
344     while ( ATCLock( lock_fd ) <= 0 );
345
346     // Set radio display
347     ATCSetRadios( radio_display_fd, radio_display_data );
348
349     ATCRelease( lock_fd );
350
351     /////////////////////////////////////////////////////////////////////
352     // Blank the lamps
353     /////////////////////////////////////////////////////////////////////
354
355     for ( int i = 0; i < 128; ++i ) {
356         ATCSetLamp( lamps_fd, i, false );
357     }
358
359     /////////////////////////////////////////////////////////////////////
360     // Finished initing hardware
361     /////////////////////////////////////////////////////////////////////
362
363     SG_LOG( SG_IO, SG_ALERT,
364             "Done initializing ATC output hardware." );
365
366     is_open = true;
367
368     /////////////////////////////////////////////////////////////////////
369     // Connect up to property values
370     /////////////////////////////////////////////////////////////////////
371
372     char base_name[256];
373
374     snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
375     lamps_out_node = fgGetNode( base_name );
376
377     snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
378     radio_display_node = fgGetNode( base_name );
379
380     snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
381     steppers_node = fgGetNode( base_name );
382
383     return true;
384 }
385
386
387 /////////////////////////////////////////////////////////////////////
388 // Write the lights
389 /////////////////////////////////////////////////////////////////////
390
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
395
396             SGPropertyNode *child = lamps_out_node->getChild(i);
397             string cname = child->getName();
398             int index = child->getIndex();
399             string name = "";
400             string type = "";
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();
407                 }
408                 prop = child->getChild( "type" );
409                 if ( prop != NULL ) {
410                     type = prop->getStringValue();
411                 }
412                 prop = child->getChild( "prop" );
413                 if ( prop != NULL ) {
414                     src_prop = fgGetNode( prop->getStringValue(), true );
415                 }
416                 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
417             } else {
418                 SG_LOG( SG_IO, SG_DEBUG,
419                         "Input config error, expecting 'lamp' but found "
420                         << cname );
421             }
422         }
423     }
424
425     return true;
426 }
427
428
429 /////////////////////////////////////////////////////////////////////
430 // Update the radio display 
431 /////////////////////////////////////////////////////////////////////
432
433
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 );
439
440     return (navcom1_bus_power->getDoubleValue() > 1.0)
441         && navcom1_power_btn->getBoolValue();
442 }
443
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 );
449
450     return (navcom2_bus_power->getDoubleValue() > 1.0)
451         && navcom2_power_btn->getBoolValue();
452 }
453
454 static bool dme_has_power() {
455     static SGPropertyNode *dme_bus_power
456         = fgGetNode( "/systems/electrical/outputs/dme", true );
457     
458     return (dme_bus_power->getDoubleValue() > 1.0);
459 }
460
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 );
466
467     return (adf_bus_power->getDoubleValue() > 1.0)
468         && adf_power_btn->getBoolValue();
469 }
470
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 );
476
477     return (xpdr_bus_power->getDoubleValue() > 1.0)
478         && (xpdr_func_knob->getIntValue() > 0);
479 }
480
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 );
492
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 );
499
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 );
506
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 );
513
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 );
520
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 );
537
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 );
546
547     char digits[10];
548     int i;
549
550     if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
551         if ( dme_in_range->getBoolValue() ) {
552             // DME minutes
553             float minutes = dme_min->getFloatValue();
554             if ( minutes > 999 ) {
555                 minutes = 999.0;
556             }
557             snprintf(digits, 7, "%03.0f", minutes);
558             for ( i = 0; i < 6; ++i ) {
559                 digits[i] -= '0';
560             }
561             radio_display_data[0] = digits[1] << 4 | digits[2];
562             radio_display_data[1] = 0xf0 | digits[0];
563         
564             // DME knots
565             float knots = dme_kt->getFloatValue();
566             if ( knots > 999 ) {
567                 knots = 999.0;
568             }
569             snprintf(digits, 7, "%03.0f", knots);
570             for ( i = 0; i < 6; ++i ) {
571                 digits[i] -= '0';
572             }
573             radio_display_data[2] = digits[1] << 4 | digits[2];
574             radio_display_data[3] = 0xf0 | digits[0];
575
576             // DME distance (nm)
577             float nm = dme_nm->getFloatValue();
578             if ( nm > 99 ) {
579                 nm = 99.0;
580             }
581             snprintf(digits, 7, "%04.1f", nm);
582             for ( i = 0; i < 6; ++i ) {
583                 digits[i] -= '0';
584             }
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
589         } else {
590             // out of range
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;
597         }
598     } else {
599         // blank dem display
600         for ( i = 0; i < 6; ++i ) {
601             radio_display_data[i] = 0xff;
602         }
603     }
604
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 ) {
609             com1_stby = 0.0;
610         }
611         snprintf(digits, 7, "%06.3f", com1_stby);
612         for ( i = 0; i < 6; ++i ) {
613             digits[i] -= '0';
614         }
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];
618
619         // Com1 in use frequency
620         float com1 = com1_freq->getFloatValue();
621         if ( fabs(com1) > 999.99 ) {
622             com1 = 0.0;
623         }
624         snprintf(digits, 7, "%06.3f", com1);
625         for ( i = 0; i < 6; ++i ) {
626             digits[i] -= '0';
627         }
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
633     } else {
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;
640     }
641
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 ) {
646             com2_stby = 0.0;
647         }
648         snprintf(digits, 7, "%06.3f", com2_stby);
649         for ( i = 0; i < 6; ++i ) {
650             digits[i] -= '0';
651         }
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];
655
656         // Com2 in use frequency
657         float com2 = com2_freq->getFloatValue();
658         if ( fabs(com2) > 999.99 ) {
659         com2 = 0.0;
660         }
661         snprintf(digits, 7, "%06.3f", com2);
662         for ( i = 0; i < 6; ++i ) {
663             digits[i] -= '0';
664         }
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
670     } else {
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;
677     }
678
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 ) {
683         nav1_stby = 0.0;
684         }
685         snprintf(digits, 7, "%06.2f", nav1_stby);
686         for ( i = 0; i < 6; ++i ) {
687             digits[i] -= '0';
688         }
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];
692
693         // Nav1 in use frequency
694         float nav1 = nav1_freq->getFloatValue();
695         if ( fabs(nav1) > 999.99 ) {
696             nav1 = 0.0;
697         }
698         snprintf(digits, 7, "%06.2f", nav1);
699         for ( i = 0; i < 6; ++i ) {
700             digits[i] -= '0';
701         }
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
707     } else {
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;
714     }
715
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 ) {
720             nav2_stby = 0.0;
721         }
722         snprintf(digits, 7, "%06.2f", nav2_stby);
723         for ( i = 0; i < 6; ++i ) {
724             digits[i] -= '0';
725         }
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];
729
730         // Nav2 in use frequency
731         float nav2 = nav2_freq->getFloatValue();
732         if ( fabs(nav2) > 999.99 ) {
733             nav2 = 0.0;
734         }
735         snprintf(digits, 7, "%06.2f", nav2);
736         for ( i = 0; i < 6; ++i ) {
737             digits[i] -= '0';
738         }
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
744     } else {
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;
751     }
752
753     // ADF standby frequency / timer
754     if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
755         if ( adf_stby_mode->getIntValue() == 0 ) {
756             // frequency
757             float adf_stby = adf_stby_freq->getFloatValue();
758             if ( fabs(adf_stby) > 1799 ) {
759                 adf_stby = 1799;
760             }
761             snprintf(digits, 7, "%04.0f", adf_stby);
762             for ( i = 0; i < 6; ++i ) {
763                 digits[i] -= '0';
764             }
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;
769             } else {
770                 radio_display_data[32] = 0xf0 | digits[0];
771             }
772         } else {
773             // timer
774             double time;
775             int hours, min, sec;
776             if ( adf_timer_mode->getIntValue() == 0 ) {
777                 time = adf_flight_timer->getDoubleValue();
778             } else {
779                 time = adf_elapsed_timer->getDoubleValue();
780             }
781             // cout << time << endl;
782             hours = (int)(time / 3600.0);
783             time -= hours * 3600.00;
784             min = (int)(time / 60.0);
785             time -= min * 60.0;
786             sec = (int)time;
787             int big, little;
788             if ( hours > 0 ) {
789                 big = hours;
790                 if ( big > 99 ) {
791                     big = 99;
792                 }
793                 little = min;
794             } else {
795                 big = min;
796                 little = sec;
797             }
798             if ( big > 99 ) {
799                 big = 99;
800             }
801             // cout << big << ":" << little << endl;
802             snprintf(digits, 7, "%02d%02d", big, little);
803             for ( i = 0; i < 6; ++i ) {
804                 digits[i] -= '0';
805             }
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;
809         }
810
811         // ADF in use frequency
812         float adf = adf_freq->getFloatValue();
813         if ( fabs(adf) > 1799 ) {
814             adf = 1799;
815         }
816         snprintf(digits, 7, "%04.0f", adf);
817         for ( i = 0; i < 6; ++i ) {
818             digits[i] -= '0';
819         }
820         radio_display_data[33] = digits[2] << 4 | digits[3];
821         if ( digits[0] == 0 ) {
822             radio_display_data[34] = 0xf0 | digits[1];
823         } else {
824             radio_display_data[34] = digits[0] << 4 | digits[1];
825         }
826         if ( adf_stby_mode->getIntValue() == 0 ) {
827           radio_display_data[35] = 0xff;
828         } else {
829           radio_display_data[35] = 0x0f;
830         }
831     } else {
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;
838     }
839     
840     // Transponder code and flight level
841     if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
842         if ( xpdr_func_knob->getIntValue() == 2 ) {
843             // test mode
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;
849         } else {
850             // other on modes
851             int id_code = xpdr_id_code->getIntValue();
852             int place = 1000;
853             for ( i = 0; i < 4; ++i ) {
854                 digits[i] = id_code / place;
855                 id_code -= digits[i] * place;
856                 place /= 10;
857             }
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;
861
862             if ( xpdr_func_knob->getIntValue() == 3 ||
863                  xpdr_func_knob->getIntValue() == 5 )
864             {
865                 // do flight level display
866                 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
867                 for ( i = 0; i < 6; ++i ) {
868                     digits[i] -= '0';
869                 }
870                 radio_display_data[39] = digits[2] << 4 | 0x0f;
871                 radio_display_data[40] = digits[0] << 4 | digits[1];
872             } else {
873                 // blank flight level display
874                 radio_display_data[39] = 0xff;
875                 radio_display_data[40] = 0xff;
876             }
877         }
878     } else {
879         // off
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;
885     }
886
887     ATCSetRadios( radio_display_fd, radio_display_data );
888
889     return true;
890 }
891
892
893 /////////////////////////////////////////////////////////////////////
894 // Drive the stepper motors
895 /////////////////////////////////////////////////////////////////////
896
897 bool FGATCOutput::do_steppers() {
898     SGPropertyNode *mag_compass
899         = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
900                      true );
901
902     float diff = mag_compass->getFloatValue() - compass_position;
903     while ( diff < -180.0 ) { diff += 360.0; }
904     while ( diff >  180.0 ) { diff -= 360.0; }
905
906     int steps = (int)(diff * 4);
907     // cout << "steps = " << steps << endl;
908     if ( steps > 4 ) { steps = 4; }
909     if ( steps < -4 ) { steps = -4; }
910
911     if ( abs(steps) > 0 ) {
912         unsigned char cmd = 0x80;       // stepper command
913         if ( steps > 0 ) {
914             cmd |= 0x20;                // go up
915         } else {
916             cmd |= 0x00;                // go down
917         }
918         cmd |= abs(steps);
919
920         // sync compass_position with hardware position
921         compass_position += (float)steps / 4.0;
922
923         ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
924     }
925
926     return true;
927 }
928
929
930 // process the hardware outputs.  This code assumes the calling layer
931 // will lock the hardware.
932 bool FGATCOutput::process() {
933     if ( !is_open ) {
934         SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
935                 << board );
936         return false;
937     }
938
939     do_lamps();
940     do_radio_display();
941 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
942     do_steppers();
943 #endif
944         
945     return true;
946 }
947
948
949 bool FGATCOutput::close() {
950
951 #if defined( unix ) || defined( __CYGWIN__ )
952
953     if ( !is_open ) {
954         return true;
955     }
956
957     int result;
958
959     result = ::close( lamps_fd );
960     if ( result == -1 ) {
961         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
962         char msg[256];
963         snprintf( msg, 256, "Error closing %s", lamps_file );
964         perror( msg );
965         exit( -1 );
966     }
967
968     result = ::close( radio_display_fd );
969     if ( result == -1 ) {
970         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
971         char msg[256];
972         snprintf( msg, 256, "Error closing %s", radio_display_file );
973         perror( msg );
974         exit( -1 );
975     }
976
977     result = ::close( stepper_fd );
978     if ( result == -1 ) {
979         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
980         char msg[256];
981         snprintf( msg, 256, "Error closing %s", stepper_file );
982         perror( msg );
983         exit( -1 );
984     }
985
986 #endif
987
988     return true;
989 }