]> git.mxchange.org Git - flightgear.git/blob - src/Network/ATC-Outputs.cxx
5747a58b7a1a2bf534d5ff7263651d9aea42697a
[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., 675 Mass Ave, Cambridge, MA 02139, 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 #endif
35
36 #include <errno.h>
37 #include <math.h>
38
39 #include STL_STRING
40
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/misc/sg_path.hxx>
43
44 #include <Main/fg_props.hxx>
45
46 #include "ATC-Outputs.hxx"
47
48 SG_USING_STD(string);
49
50
51
52 // Lock the ATC hardware
53 static int ATCLock( int fd ) {
54 #if defined( unix ) || defined( __CYGWIN__ )
55     // rewind
56     lseek( fd, 0, SEEK_SET );
57
58     char tmp[2];
59     int result = read( fd, tmp, 1 );
60     if ( result != 1 ) {
61         SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
62     }
63
64     return result;
65 #else
66     return -1;
67 #endif
68 }
69
70
71 // Release the ATC hardware
72 static int ATCRelease( int fd ) {
73 #if defined( unix ) || defined( __CYGWIN__ )
74     // rewind
75     lseek( fd, 0, SEEK_SET );
76
77     char tmp[2];
78     tmp[0] = tmp[1] = 0;
79     int result = write( fd, tmp, 1 );
80
81     if ( result != 1 ) {
82         SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
83     }
84
85     return result;
86 #else
87     return -1;
88 #endif
89 }
90
91
92 // Constructor: The _board parameter specifies which board to
93 // reference.  Possible values are 0 or 1.  The _config_file parameter
94 // specifies the location of the output config file (xml)
95 FGATCOutput::FGATCOutput( const int _board, const SGPath &_config_file ) :
96     is_open(false),
97     lamps_out_node(NULL),
98     radio_display_node(NULL),
99     steppers_node(NULL)
100 {
101     board = _board;
102     config = _config_file;
103 }
104
105
106 // Write a radios command
107 static int ATCSetRadios( int fd, unsigned char data[ATC_RADIO_DISPLAY_BYTES] ) {
108 #if defined( unix ) || defined( __CYGWIN__ )
109     // rewind
110     lseek( fd, 0, SEEK_SET );
111
112     int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
113
114     if ( result != ATC_RADIO_DISPLAY_BYTES ) {
115         SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
116     }
117
118     return result;
119 #else
120     return -1;
121 #endif
122 }
123
124
125 // Write a stepper command
126 static int ATCSetStepper( int fd, unsigned char channel,
127                               unsigned char value )
128 {
129 #if defined( unix ) || defined( __CYGWIN__ )
130     // rewind
131     lseek( fd, 0, SEEK_SET );
132
133     // Write the value
134     unsigned char buf[3];
135     buf[0] = channel;
136     buf[1] = value;
137     buf[2] = 0;
138     int result = write( fd, buf, 2 );
139     if ( result != 2 ) {
140         SG_LOG( SG_IO, SG_INFO, "Write failed" );
141     }
142     SG_LOG( SG_IO, SG_DEBUG,
143             "Sent cmd = " << (int)channel << " value = " << (int)value );
144     return result;
145 #else
146     return -1;
147 #endif
148 }
149
150
151 // Read status of last stepper written to
152 static unsigned char ATCReadStepper( int fd ) {
153 #if defined( unix ) || defined( __CYGWIN__ )
154     int result;
155
156     // rewind
157     lseek( fd, 0, SEEK_SET );
158
159     // Write the value
160     unsigned char buf[2];
161     result = read( fd, buf, 1 );
162     if ( result != 1 ) {
163         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
164         exit( -1 );
165     }
166     SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
167
168     return buf[0];
169 #else
170     return 0;
171 #endif
172 }
173
174
175 // Turn a lamp on or off
176 void ATCSetLamp( int fd, int channel, bool value ) {
177 #if defined( unix ) || defined( __CYGWIN__ )
178     // lamp channels 0-63 are written to LampPort0, channels 64-127
179     // are written to LampPort1
180
181     // bits 0-6 are the lamp address
182     // bit 7 is the value (on/off)
183
184     int result;
185
186     // Write the value
187     unsigned char buf[3];
188     buf[0] = channel;
189     buf[1] = value;
190     buf[2] = 0;
191     result = write( fd, buf, 2 );
192     if ( result != 2 ) {
193         SG_LOG( SG_IO, SG_ALERT,  "Write failed" );
194         exit( -1 );
195     }
196 #endif
197 }
198
199
200 void FGATCOutput::init_config() {
201 #if defined( unix ) || defined( __CYGWIN__ )
202     if ( config.str()[0] != '/' ) {
203         // not an absolute path, prepend the standard location
204         SGPath tmp;
205         char *envp = ::getenv( "HOME" );
206         if ( envp != NULL ) {
207             tmp = envp;
208             tmp.append( ".atcflightsim" );
209             tmp.append( config.str() );
210             config = tmp;
211         }
212     }
213     readProperties( config.str(), globals->get_props() );
214 #endif
215 }
216
217
218 // Open and initialize the ATC hardware
219 bool FGATCOutput::open( int lock_fd ) {
220     if ( is_open ) {
221         SG_LOG( SG_IO, SG_ALERT, "This board is already open for output! "
222                 << board );
223         return false;
224     }
225
226     // This loads the config parameters generated by "simcal"
227     init_config();
228
229     SG_LOG( SG_IO, SG_ALERT,
230             "Initializing ATC output hardware, please wait ..." );
231
232     snprintf( lamps_file, 256,
233               "/proc/atcflightsim/board%d/lamps", board );
234     snprintf( radio_display_file, 256,
235               "/proc/atcflightsim/board%d/radios", board );
236     snprintf( stepper_file, 256,
237               "/proc/atcflightsim/board%d/steppers", board );
238
239 #if defined( unix ) || defined( __CYGWIN__ )
240
241     /////////////////////////////////////////////////////////////////////
242     // Open the /proc files
243     /////////////////////////////////////////////////////////////////////
244
245     lamps_fd = ::open( lamps_file, O_WRONLY );
246     if ( lamps_fd == -1 ) {
247         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
248         char msg[256];
249         snprintf( msg, 256, "Error opening %s", lamps_file );
250         perror( msg );
251         exit( -1 );
252     }
253
254     radio_display_fd = ::open( radio_display_file, O_RDWR );
255     if ( radio_display_fd == -1 ) {
256         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
257         char msg[256];
258         snprintf( msg, 256, "Error opening %s", radio_display_file );
259         perror( msg );
260         exit( -1 );
261     }
262
263     stepper_fd = ::open( stepper_file, O_RDWR );
264     if ( stepper_fd == -1 ) {
265         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
266         char msg[256];
267         snprintf( msg, 256, "Error opening %s", stepper_file );
268         perror( msg );
269         exit( -1 );
270     }
271
272 #endif
273
274 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
275     /////////////////////////////////////////////////////////////////////
276     // Home the compass stepper motor
277     /////////////////////////////////////////////////////////////////////
278
279     SG_LOG( SG_IO, SG_ALERT,
280             "  - Homing the compass stepper motor" );
281
282     // Lock the hardware, keep trying until we succeed
283     while ( ATCLock( lock_fd ) <= 0 );
284
285     // Send the stepper home command
286     ATCSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
287
288     // Release the hardware
289     ATCRelease( lock_fd );
290
291     SG_LOG( SG_IO, SG_ALERT,
292             "  - Waiting for compass to come home." );
293
294     bool home = false;
295     int timeout = 900;          // about 30 seconds
296     timeout = 0;
297     while ( ! home && timeout > 0 ) {
298         if ( timeout % 150 == 0 ) {
299             SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
300         } else {
301             SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
302         }
303
304         while ( ATCLock( lock_fd ) <= 0 );
305
306         unsigned char result = ATCReadStepper( stepper_fd );
307         if ( result == 0 ) {
308             home = true;
309         }
310
311         ATCRelease( lock_fd );
312
313 #if defined( _MSC_VER )
314         ulMilliSecondSleep(33);
315 #elif defined (WIN32) && !defined(__CYGWIN__)
316         Sleep (33);
317 #else
318         usleep(33);
319 #endif
320
321         --timeout;
322     }
323
324     compass_position = 0.0;
325 #endif
326
327     /////////////////////////////////////////////////////////////////////
328     // Blank the radio display
329     /////////////////////////////////////////////////////////////////////
330
331     SG_LOG( SG_IO, SG_ALERT,
332             "  - Clearing the radios displays." );
333
334     // Prepair the data
335     unsigned char value = 0xff;
336     for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
337         radio_display_data[channel] = value;
338     }
339
340     // Lock the hardware, keep trying until we succeed
341     while ( ATCLock( lock_fd ) <= 0 );
342
343     // Set radio display
344     ATCSetRadios( radio_display_fd, radio_display_data );
345
346     ATCRelease( lock_fd );
347
348     /////////////////////////////////////////////////////////////////////
349     // Blank the lamps
350     /////////////////////////////////////////////////////////////////////
351
352     for ( int i = 0; i < 128; ++i ) {
353         ATCSetLamp( lamps_fd, i, false );
354     }
355
356     /////////////////////////////////////////////////////////////////////
357     // Finished initing hardware
358     /////////////////////////////////////////////////////////////////////
359
360     SG_LOG( SG_IO, SG_ALERT,
361             "Done initializing ATC output hardware." );
362
363     is_open = true;
364
365     /////////////////////////////////////////////////////////////////////
366     // Connect up to property values
367     /////////////////////////////////////////////////////////////////////
368
369     char base_name[256];
370
371     snprintf( base_name, 256, "/output/atc-board[%d]/lamps", board );
372     lamps_out_node = fgGetNode( base_name );
373
374     snprintf( base_name, 256, "/output/atc-board[%d]/radio-display", board );
375     radio_display_node = fgGetNode( base_name );
376
377     snprintf( base_name, 256, "/output/atc-board[%d]/steppers", board );
378     steppers_node = fgGetNode( base_name );
379
380     return true;
381 }
382
383
384 /////////////////////////////////////////////////////////////////////
385 // Write the lights
386 /////////////////////////////////////////////////////////////////////
387
388 bool FGATCOutput::do_lamps() {
389     if ( lamps_out_node != NULL ) {
390         for ( int i = 0; i < lamps_out_node->nChildren(); ++i ) {
391             // read the next config entry from the property tree
392
393             SGPropertyNode *child = lamps_out_node->getChild(i);
394             string cname = child->getName();
395             int index = child->getIndex();
396             string name = "";
397             string type = "";
398             SGPropertyNode *src_prop = NULL;
399             if ( cname == "lamp" ) {
400                 SGPropertyNode *prop;
401                 prop = child->getChild( "name" );
402                 if ( prop != NULL ) {
403                     name = prop->getStringValue();
404                 }
405                 prop = child->getChild( "type" );
406                 if ( prop != NULL ) {
407                     type = prop->getStringValue();
408                 }
409                 prop = child->getChild( "prop" );
410                 if ( prop != NULL ) {
411                     src_prop = fgGetNode( prop->getStringValue(), true );
412                 }
413                 ATCSetLamp( lamps_fd, index, src_prop->getBoolValue() );
414             } else {
415                 SG_LOG( SG_IO, SG_DEBUG,
416                         "Input config error, expecting 'lamp' but found "
417                         << cname );
418             }
419         }
420     }
421
422     return true;
423 }
424
425
426 /////////////////////////////////////////////////////////////////////
427 // Update the radio display 
428 /////////////////////////////////////////////////////////////////////
429
430
431 static bool navcom1_has_power() {
432     static SGPropertyNode *navcom1_bus_power
433         = fgGetNode( "/systems/electrical/outputs/nav[0]", true );
434     static SGPropertyNode *navcom1_power_btn
435         = fgGetNode( "/instrumentation/nav[0]/power-btn", true );
436
437     return (navcom1_bus_power->getDoubleValue() > 1.0)
438         && navcom1_power_btn->getBoolValue();
439 }
440
441 static bool navcom2_has_power() {
442     static SGPropertyNode *navcom2_bus_power
443         = fgGetNode( "/systems/electrical/outputs/nav[1]", true );
444     static SGPropertyNode *navcom2_power_btn
445         = fgGetNode( "/instrumentation/nav[1]/power-btn", true );
446
447     return (navcom2_bus_power->getDoubleValue() > 1.0)
448         && navcom2_power_btn->getBoolValue();
449 }
450
451 static bool dme_has_power() {
452     static SGPropertyNode *dme_bus_power
453         = fgGetNode( "/systems/electrical/outputs/dme", true );
454     
455     return (dme_bus_power->getDoubleValue() > 1.0);
456 }
457
458 static bool adf_has_power() {
459     static SGPropertyNode *adf_bus_power
460         = fgGetNode( "/systems/electrical/outputs/adf", true );
461     static SGPropertyNode *adf_power_btn
462         = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
463
464     return (adf_bus_power->getDoubleValue() > 1.0)
465         && adf_power_btn->getBoolValue();
466 }
467
468 static bool xpdr_has_power() {
469     static SGPropertyNode *xpdr_bus_power
470         = fgGetNode( "/systems/electrical/outputs/transponder", true );
471     static SGPropertyNode *xpdr_func_knob
472         = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
473
474     return (xpdr_bus_power->getDoubleValue() > 1.0)
475         && (xpdr_func_knob->getIntValue() > 0);
476 }
477
478 bool FGATCOutput::do_radio_display() {
479     static SGPropertyNode *dme_serviceable
480         = fgGetNode( "/instrumentation/dme/serviceable", true );
481     static SGPropertyNode *dme_in_range
482         = fgGetNode( "/instrumentation/dme/in-range", true );
483     static SGPropertyNode *dme_min
484         = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
485     static SGPropertyNode *dme_kt
486         = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt", true );
487     static SGPropertyNode *dme_nm
488         = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
489
490     static SGPropertyNode *comm1_serviceable
491         = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
492     static SGPropertyNode *com1_freq
493         = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true);
494     static SGPropertyNode *com1_stby_freq
495         = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
496
497     static SGPropertyNode *comm2_serviceable
498         = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
499     static SGPropertyNode *com2_freq
500         = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true);
501     static SGPropertyNode *com2_stby_freq
502         = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
503
504     static SGPropertyNode *nav1_serviceable
505         = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
506     static SGPropertyNode *nav1_freq
507         = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
508     static SGPropertyNode *nav1_stby_freq
509         = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
510
511     static SGPropertyNode *nav2_serviceable
512         = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
513     static SGPropertyNode *nav2_freq
514         = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
515     static SGPropertyNode *nav2_stby_freq
516         = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
517
518     static SGPropertyNode *adf_serviceable
519         = fgGetNode( "/instrumentation/adf/serviceable", true );
520     static SGPropertyNode *adf_freq
521         = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
522     static SGPropertyNode *adf_stby_freq
523         = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
524     static SGPropertyNode *adf_stby_mode
525         = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
526     static SGPropertyNode *adf_timer_mode
527         = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
528     // static SGPropertyNode *adf_count_mode
529     //     = fgGetNode( "/instrumentation/kr-87/modes/count", true );
530     static SGPropertyNode *adf_flight_timer
531         = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
532     static SGPropertyNode *adf_elapsed_timer
533         = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer", true );
534
535     static SGPropertyNode *xpdr_serviceable
536         = fgGetNode( "/instrumentation/transponder/inputs/serviceable", true );
537     static SGPropertyNode *xpdr_func_knob
538         = fgGetNode( "/instrumentation/transponder/inputs/func-knob", true );
539     static SGPropertyNode *xpdr_flight_level
540         = fgGetNode( "/instrumentation/transponder/outputs/flight-level", true );
541     static SGPropertyNode *xpdr_id_code
542         = fgGetNode( "/instrumentation/transponder/outputs/id-code", true );
543
544     char digits[10];
545     int i;
546
547     if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
548         if ( dme_in_range->getBoolValue() ) {
549             // DME minutes
550             float minutes = dme_min->getFloatValue();
551             if ( minutes > 999 ) {
552                 minutes = 999.0;
553             }
554             snprintf(digits, 7, "%03.0f", minutes);
555             for ( i = 0; i < 6; ++i ) {
556                 digits[i] -= '0';
557             }
558             radio_display_data[0] = digits[1] << 4 | digits[2];
559             radio_display_data[1] = 0xf0 | digits[0];
560         
561             // DME knots
562             float knots = dme_kt->getFloatValue();
563             if ( knots > 999 ) {
564                 knots = 999.0;
565             }
566             snprintf(digits, 7, "%03.0f", knots);
567             for ( i = 0; i < 6; ++i ) {
568                 digits[i] -= '0';
569             }
570             radio_display_data[2] = digits[1] << 4 | digits[2];
571             radio_display_data[3] = 0xf0 | digits[0];
572
573             // DME distance (nm)
574             float nm = dme_nm->getFloatValue();
575             if ( nm > 99 ) {
576                 nm = 99.0;
577             }
578             snprintf(digits, 7, "%04.1f", nm);
579             for ( i = 0; i < 6; ++i ) {
580                 digits[i] -= '0';
581             }
582             radio_display_data[4] = digits[1] << 4 | digits[3];
583             radio_display_data[5] = 0x00 | digits[0];
584             // the 0x00 in the upper nibble of the 6th byte of each
585             // display turns on the decimal point
586         } else {
587             // out of range
588             radio_display_data[0] = 0xbb;
589             radio_display_data[1] = 0xfb;
590             radio_display_data[2] = 0xbb;
591             radio_display_data[3] = 0xfb;
592             radio_display_data[4] = 0xbb;
593             radio_display_data[5] = 0x0b;
594         }
595     } else {
596         // blank dem display
597         for ( i = 0; i < 6; ++i ) {
598             radio_display_data[i] = 0xff;
599         }
600     }
601
602     if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
603         // Com1 standby frequency
604         float com1_stby = com1_stby_freq->getFloatValue();
605         if ( fabs(com1_stby) > 999.99 ) {
606             com1_stby = 0.0;
607         }
608         snprintf(digits, 7, "%06.3f", com1_stby);
609         for ( i = 0; i < 6; ++i ) {
610             digits[i] -= '0';
611         }
612         radio_display_data[6] = digits[4] << 4 | digits[5];
613         radio_display_data[7] = digits[1] << 4 | digits[2];
614         radio_display_data[8] = 0xf0 | digits[0];
615
616         // Com1 in use frequency
617         float com1 = com1_freq->getFloatValue();
618         if ( fabs(com1) > 999.99 ) {
619             com1 = 0.0;
620         }
621         snprintf(digits, 7, "%06.3f", com1);
622         for ( i = 0; i < 6; ++i ) {
623             digits[i] -= '0';
624         }
625         radio_display_data[9] = digits[4] << 4 | digits[5];
626         radio_display_data[10] = digits[1] << 4 | digits[2];
627         radio_display_data[11] = 0x00 | digits[0];
628         // the 0x00 in the upper nibble of the 6th byte of each display
629         // turns on the decimal point
630     } else {
631         radio_display_data[6] = 0xff;
632         radio_display_data[7] = 0xff;
633         radio_display_data[8] = 0xff;
634         radio_display_data[9] = 0xff;
635         radio_display_data[10] = 0xff;
636         radio_display_data[11] = 0xff;
637     }
638
639     if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
640         // Com2 standby frequency
641         float com2_stby = com2_stby_freq->getFloatValue();
642         if ( fabs(com2_stby) > 999.99 ) {
643             com2_stby = 0.0;
644         }
645         snprintf(digits, 7, "%06.3f", com2_stby);
646         for ( i = 0; i < 6; ++i ) {
647             digits[i] -= '0';
648         }
649         radio_display_data[18] = digits[4] << 4 | digits[5];
650         radio_display_data[19] = digits[1] << 4 | digits[2];
651         radio_display_data[20] = 0xf0 | digits[0];
652
653         // Com2 in use frequency
654         float com2 = com2_freq->getFloatValue();
655         if ( fabs(com2) > 999.99 ) {
656         com2 = 0.0;
657         }
658         snprintf(digits, 7, "%06.3f", com2);
659         for ( i = 0; i < 6; ++i ) {
660             digits[i] -= '0';
661         }
662         radio_display_data[21] = digits[4] << 4 | digits[5];
663         radio_display_data[22] = digits[1] << 4 | digits[2];
664         radio_display_data[23] = 0x00 | digits[0];
665         // the 0x00 in the upper nibble of the 6th byte of each display
666         // turns on the decimal point
667     } else {
668         radio_display_data[18] = 0xff;
669         radio_display_data[19] = 0xff;
670         radio_display_data[20] = 0xff;
671         radio_display_data[21] = 0xff;
672         radio_display_data[22] = 0xff;
673         radio_display_data[23] = 0xff;
674     }
675
676     if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
677         // Nav1 standby frequency
678         float nav1_stby = nav1_stby_freq->getFloatValue();
679         if ( fabs(nav1_stby) > 999.99 ) {
680         nav1_stby = 0.0;
681         }
682         snprintf(digits, 7, "%06.2f", nav1_stby);
683         for ( i = 0; i < 6; ++i ) {
684             digits[i] -= '0';
685         }
686         radio_display_data[12] = digits[4] << 4 | digits[5];
687         radio_display_data[13] = digits[1] << 4 | digits[2];
688         radio_display_data[14] = 0xf0 | digits[0];
689
690         // Nav1 in use frequency
691         float nav1 = nav1_freq->getFloatValue();
692         if ( fabs(nav1) > 999.99 ) {
693             nav1 = 0.0;
694         }
695         snprintf(digits, 7, "%06.2f", nav1);
696         for ( i = 0; i < 6; ++i ) {
697             digits[i] -= '0';
698         }
699         radio_display_data[15] = digits[4] << 4 | digits[5];
700         radio_display_data[16] = digits[1] << 4 | digits[2];
701         radio_display_data[17] = 0x00 | digits[0];
702         // the 0x00 in the upper nibble of the 6th byte of each display
703         // turns on the decimal point
704     } else {
705         radio_display_data[12] = 0xff;
706         radio_display_data[13] = 0xff;
707         radio_display_data[14] = 0xff;
708         radio_display_data[15] = 0xff;
709         radio_display_data[16] = 0xff;
710         radio_display_data[17] = 0xff;
711     }
712
713     if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
714         // Nav2 standby frequency
715         float nav2_stby = nav2_stby_freq->getFloatValue();
716         if ( fabs(nav2_stby) > 999.99 ) {
717             nav2_stby = 0.0;
718         }
719         snprintf(digits, 7, "%06.2f", nav2_stby);
720         for ( i = 0; i < 6; ++i ) {
721             digits[i] -= '0';
722         }
723         radio_display_data[24] = digits[4] << 4 | digits[5];
724         radio_display_data[25] = digits[1] << 4 | digits[2];
725         radio_display_data[26] = 0xf0 | digits[0];
726
727         // Nav2 in use frequency
728         float nav2 = nav2_freq->getFloatValue();
729         if ( fabs(nav2) > 999.99 ) {
730             nav2 = 0.0;
731         }
732         snprintf(digits, 7, "%06.2f", nav2);
733         for ( i = 0; i < 6; ++i ) {
734             digits[i] -= '0';
735         }
736         radio_display_data[27] = digits[4] << 4 | digits[5];
737         radio_display_data[28] = digits[1] << 4 | digits[2];
738         radio_display_data[29] = 0x00 | digits[0];
739         // the 0x00 in the upper nibble of the 6th byte of each display
740         // turns on the decimal point
741     } else {
742         radio_display_data[24] = 0xff;
743         radio_display_data[25] = 0xff;
744         radio_display_data[26] = 0xff;
745         radio_display_data[27] = 0xff;
746         radio_display_data[28] = 0xff;
747         radio_display_data[29] = 0xff;
748     }
749
750     // ADF standby frequency / timer
751     if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
752         if ( adf_stby_mode->getIntValue() == 0 ) {
753             // frequency
754             float adf_stby = adf_stby_freq->getFloatValue();
755             if ( fabs(adf_stby) > 1799 ) {
756                 adf_stby = 1799;
757             }
758             snprintf(digits, 7, "%04.0f", adf_stby);
759             for ( i = 0; i < 6; ++i ) {
760                 digits[i] -= '0';
761             }
762             radio_display_data[30] = digits[3] << 4 | 0x0f;
763             radio_display_data[31] = digits[1] << 4 | digits[2];
764             if ( digits[0] == 0 ) {
765                 radio_display_data[32] = 0xff;
766             } else {
767                 radio_display_data[32] = 0xf0 | digits[0];
768             }
769         } else {
770             // timer
771             double time;
772             int hours, min, sec;
773             if ( adf_timer_mode->getIntValue() == 0 ) {
774                 time = adf_flight_timer->getDoubleValue();
775             } else {
776                 time = adf_elapsed_timer->getDoubleValue();
777             }
778             // cout << time << endl;
779             hours = (int)(time / 3600.0);
780             time -= hours * 3600.00;
781             min = (int)(time / 60.0);
782             time -= min * 60.0;
783             sec = (int)time;
784             int big, little;
785             if ( hours > 0 ) {
786                 big = hours;
787                 if ( big > 99 ) {
788                     big = 99;
789                 }
790                 little = min;
791             } else {
792                 big = min;
793                 little = sec;
794             }
795             if ( big > 99 ) {
796                 big = 99;
797             }
798             // cout << big << ":" << little << endl;
799             snprintf(digits, 7, "%02d%02d", big, little);
800             for ( i = 0; i < 6; ++i ) {
801                 digits[i] -= '0';
802             }
803             radio_display_data[30] = digits[2] << 4 | digits[3];
804             radio_display_data[31] = digits[0] << 4 | digits[1];
805             radio_display_data[32] = 0xff;
806         }
807
808         // ADF in use frequency
809         float adf = adf_freq->getFloatValue();
810         if ( fabs(adf) > 1799 ) {
811             adf = 1799;
812         }
813         snprintf(digits, 7, "%04.0f", adf);
814         for ( i = 0; i < 6; ++i ) {
815             digits[i] -= '0';
816         }
817         radio_display_data[33] = digits[2] << 4 | digits[3];
818         if ( digits[0] == 0 ) {
819             radio_display_data[34] = 0xf0 | digits[1];
820         } else {
821             radio_display_data[34] = digits[0] << 4 | digits[1];
822         }
823         if ( adf_stby_mode->getIntValue() == 0 ) {
824           radio_display_data[35] = 0xff;
825         } else {
826           radio_display_data[35] = 0x0f;
827         }
828     } else {
829         radio_display_data[30] = 0xff;
830         radio_display_data[31] = 0xff;
831         radio_display_data[32] = 0xff;
832         radio_display_data[33] = 0xff;
833         radio_display_data[34] = 0xff;
834         radio_display_data[35] = 0xff;
835     }
836     
837     // Transponder code and flight level
838     if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
839         if ( xpdr_func_knob->getIntValue() == 2 ) {
840             // test mode
841             radio_display_data[36] = 8 << 4 | 8;
842             radio_display_data[37] = 8 << 4 | 8;
843             radio_display_data[38] = 0xff;
844             radio_display_data[39] = 8 << 4 | 0x0f;
845             radio_display_data[40] = 8 << 4 | 8;
846         } else {
847             // other on modes
848             int id_code = xpdr_id_code->getIntValue();
849             int place = 1000;
850             for ( i = 0; i < 4; ++i ) {
851                 digits[i] = id_code / place;
852                 id_code -= digits[i] * place;
853                 place /= 10;
854             }
855             radio_display_data[36] = digits[2] << 4 | digits[3];
856             radio_display_data[37] = digits[0] << 4 | digits[1];
857             radio_display_data[38] = 0xff;
858
859             if ( xpdr_func_knob->getIntValue() == 3 ||
860                  xpdr_func_knob->getIntValue() == 5 )
861             {
862                 // do flight level display
863                 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
864                 for ( i = 0; i < 6; ++i ) {
865                     digits[i] -= '0';
866                 }
867                 radio_display_data[39] = digits[2] << 4 | 0x0f;
868                 radio_display_data[40] = digits[0] << 4 | digits[1];
869             } else {
870                 // blank flight level display
871                 radio_display_data[39] = 0xff;
872                 radio_display_data[40] = 0xff;
873             }
874         }
875     } else {
876         // off
877         radio_display_data[36] = 0xff;
878         radio_display_data[37] = 0xff;
879         radio_display_data[38] = 0xff;
880         radio_display_data[39] = 0xff;
881         radio_display_data[40] = 0xff;
882     }
883
884     ATCSetRadios( radio_display_fd, radio_display_data );
885
886     return true;
887 }
888
889
890 /////////////////////////////////////////////////////////////////////
891 // Drive the stepper motors
892 /////////////////////////////////////////////////////////////////////
893
894 bool FGATCOutput::do_steppers() {
895     SGPropertyNode *mag_compass
896         = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
897                      true );
898
899     float diff = mag_compass->getFloatValue() - compass_position;
900     while ( diff < -180.0 ) { diff += 360.0; }
901     while ( diff >  180.0 ) { diff -= 360.0; }
902
903     int steps = (int)(diff * 4);
904     // cout << "steps = " << steps << endl;
905     if ( steps > 4 ) { steps = 4; }
906     if ( steps < -4 ) { steps = -4; }
907
908     if ( abs(steps) > 0 ) {
909         unsigned char cmd = 0x80;       // stepper command
910         if ( steps > 0 ) {
911             cmd |= 0x20;                // go up
912         } else {
913             cmd |= 0x00;                // go down
914         }
915         cmd |= abs(steps);
916
917         // sync compass_position with hardware position
918         compass_position += (float)steps / 4.0;
919
920         ATCSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
921     }
922
923     return true;
924 }
925
926
927 // process the hardware outputs.  This code assumes the calling layer
928 // will lock the hardware.
929 bool FGATCOutput::process() {
930     if ( !is_open ) {
931         SG_LOG( SG_IO, SG_ALERT, "This board has not been opened for output! "
932                 << board );
933         return false;
934     }
935
936     do_lamps();
937     do_radio_display();
938 #ifdef ATCFLIGHTSIM_HAVE_COMPASS
939     do_steppers();
940 #endif
941         
942     return true;
943 }
944
945
946 bool FGATCOutput::close() {
947
948 #if defined( unix ) || defined( __CYGWIN__ )
949
950     if ( !is_open ) {
951         return true;
952     }
953
954     int result;
955
956     result = ::close( lamps_fd );
957     if ( result == -1 ) {
958         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
959         char msg[256];
960         snprintf( msg, 256, "Error closing %s", lamps_file );
961         perror( msg );
962         exit( -1 );
963     }
964
965     result = ::close( radio_display_fd );
966     if ( result == -1 ) {
967         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
968         char msg[256];
969         snprintf( msg, 256, "Error closing %s", radio_display_file );
970         perror( msg );
971         exit( -1 );
972     }
973
974     result = ::close( stepper_fd );
975     if ( result == -1 ) {
976         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
977         char msg[256];
978         snprintf( msg, 256, "Error closing %s", stepper_file );
979         perror( msg );
980         exit( -1 );
981     }
982
983 #endif
984
985     return true;
986 }