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