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