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