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