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