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