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