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