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