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