]> git.mxchange.org Git - flightgear.git/blob - src/Network/atc610x.cxx
31c3734d8d368c8e9ca15961284dbd432c0d26d2
[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 - curt@flightgear.org
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 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg", true );
351
352     dme_min = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
353     dme_kt = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt",
354                         true );
355     dme_nm = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
356     dme_in_range = fgGetNode( "/instrumentation/dme/in-range", true );
357
358     adf_bus_power = fgGetNode( "/systems/electrical/outputs/adf", true );
359     dme_bus_power = fgGetNode( "/systems/electrical/outputs/dme", true );
360     navcom1_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[0]",
361                                    true );
362     navcom2_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[1]",
363                                    true );
364     xpdr_bus_power = fgGetNode( "/systems/electrical/outputs/transponder",
365                                  true );
366
367     navcom1_power_btn = fgGetNode( "/radios/comm[0]/inputs/power-btn", true );
368     navcom2_power_btn = fgGetNode( "/radios/comm[1]/inputs/power-btn", true );
369
370     com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
371     com1_stby_freq
372         = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
373
374     com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
375     com2_stby_freq
376         = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
377
378     nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
379     nav1_stby_freq
380         = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
381     nav1_obs = fgGetNode( "/radios/nav[0]/radials/selected-deg", true );
382
383     nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
384     nav2_stby_freq
385         = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
386     nav2_obs = fgGetNode( "/radios/nav[1]/radials/selected-deg", true );
387
388     adf_power_btn = fgGetNode( "/radios/kr-87/inputs/power-btn", true );
389     adf_vol = fgGetNode( "/radios/kr-87/inputs/volume", true );
390     adf_adf_btn = fgGetNode( "/radios/kr-87/inputs/adf-btn", true );
391     adf_bfo_btn = fgGetNode( "/radios/kr-87/inputs/bfo-btn", true );
392     adf_freq = fgGetNode( "/radios/kr-87/outputs/selected-khz", true );
393     adf_stby_freq = fgGetNode( "/radios/kr-87/outputs/standby-khz", true );
394     adf_stby_mode = fgGetNode( "/radios/kr-87/modes/stby", true );
395     adf_timer_mode = fgGetNode( "/radios/kr-87/modes/timer", true );
396     adf_count_mode = fgGetNode( "/radios/kr-87/modes/count", true );
397     adf_flight_timer = fgGetNode( "/radios/kr-87/outputs/flight-timer", true );
398     adf_elapsed_timer = fgGetNode( "/radios/kr-87/outputs/elapsed-timer",
399                                    true );
400     adf_ant_ann = fgGetNode( "/radios/kr-87/annunciators/ant", true );
401     adf_adf_ann = fgGetNode( "/radios/kr-87/annunciators/adf", true );
402     adf_bfo_ann = fgGetNode( "/radios/kr-87/annunciators/bfo", true );
403     adf_frq_ann = fgGetNode( "/radios/kr-87/annunciators/frq", true );
404     adf_flt_ann = fgGetNode( "/radios/kr-87/annunciators/flt", true );
405     adf_et_ann = fgGetNode( "/radios/kr-87/annunciators/et", true );
406
407     inner = fgGetNode( "/radios/marker-beacon/inner", true );
408     middle = fgGetNode( "/radios/marker-beacon/middle", true );
409     outer = fgGetNode( "/radios/marker-beacon/outer", true );
410
411     xpdr_ident_btn = fgGetNode( "/radios/kt-70/inputs/ident-btn", true );
412     xpdr_digit1 = fgGetNode( "/radios/kt-70/inputs/digit1", true );
413     xpdr_digit2 = fgGetNode( "/radios/kt-70/inputs/digit2", true );
414     xpdr_digit3 = fgGetNode( "/radios/kt-70/inputs/digit3", true );
415     xpdr_digit4 = fgGetNode( "/radios/kt-70/inputs/digit4", true );
416     xpdr_func_knob = fgGetNode( "/radios/kt-70/inputs/func-knob", true );
417     xpdr_id_code = fgGetNode( "/radios/kt-70/outputs/id-code", true );
418     xpdr_flight_level = fgGetNode( "/radios/kt-70/outputs/flight-level", true );
419     xpdr_fl_ann = fgGetNode( "/radios/kt-70/annunciators/fl", true );
420     xpdr_alt_ann = fgGetNode( "/radios/kt-70/annunciators/alt", true );
421     xpdr_gnd_ann = fgGetNode( "/radios/kt-70/annunciators/gnd", true );
422     xpdr_on_ann = fgGetNode( "/radios/kt-70/annunciators/on", true );
423     xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
424     xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
425
426     ati_bird
427       = fgGetNode( "/instrumentation/attitude-indicator/horizon-offset-deg",
428                    true );
429     alt_press = fgGetNode( "/instrumentation/altimeter/setting-inhg", true );
430     adf_hdg = fgGetNode( "/radios/kr-87/inputs/rotation-deg", true );
431     hdg_bug = fgGetNode( "/autopilot/settings/heading-bug-deg", true );
432
433     elevator_center = fgGetNode( "/input/atc610x/elevator/center", true );
434     elevator_min = fgGetNode( "/input/atc610x/elevator/min", true );
435     elevator_max = fgGetNode( "/input/atc610x/elevator/max", true );
436
437     ailerons_center = fgGetNode( "/input/atc610x/ailerons/center", true );
438     ailerons_min = fgGetNode( "/input/atc610x/ailerons/min", true );
439     ailerons_max = fgGetNode( "/input/atc610x/ailerons/max", true );
440
441     rudder_center = fgGetNode( "/input/atc610x/rudder/center", true );
442     rudder_min = fgGetNode( "/input/atc610x/rudder/min", true );
443     rudder_max = fgGetNode( "/input/atc610x/rudder/max", true );
444
445     brake_left_min = fgGetNode( "/input/atc610x/brake-left/min", true );
446     brake_left_max = fgGetNode( "/input/atc610x/brake-left/max", true );
447
448     brake_right_min = fgGetNode( "/input/atc610x/brake-right/min", true );
449     brake_right_max = fgGetNode( "/input/atc610x/brake-right/max", true );
450
451     throttle_min = fgGetNode( "/input/atc610x/throttle/min", true );
452     throttle_max = fgGetNode( "/input/atc610x/throttle/max", true );
453
454     mixture_min = fgGetNode( "/input/atc610x/mixture/min", true );
455     mixture_max = fgGetNode( "/input/atc610x/mixture/max", true );
456
457     trim_center = fgGetNode( "/input/atc610x/trim/center", true );
458     trim_min = fgGetNode( "/input/atc610x/trim/min", true );
459     trim_max = fgGetNode( "/input/atc610x/trim/max", true );
460
461     nav1vol_min = fgGetNode( "/input/atc610x/nav1vol/min", true );
462     nav1vol_max = fgGetNode( "/input/atc610x/nav1vol/max", true );
463
464     nav2vol_min = fgGetNode( "/input/atc610x/nav2vol/min", true );
465     nav2vol_max = fgGetNode( "/input/atc610x/nav2vol/max", true );
466
467     ignore_flight_controls
468         = fgGetNode( "/input/atc610x/ignore-flight-controls", true );
469
470     comm1_serviceable = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
471     comm2_serviceable = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
472     nav1_serviceable = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
473     nav2_serviceable = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
474     adf_serviceable = fgGetNode( "/instrumentation/adf/serviceable", true );
475     xpdr_serviceable = fgGetNode( "/radios/kt-70/inputs/serviceable",
476                                  true );
477     dme_serviceable = fgGetNode( "/instrumentation/dme/serviceable", true );
478
479     dme_selector
480         = fgGetNode( "/input/atc-board/radio-switches/raw/dme-switch-position");
481
482     // default to having everything serviceable
483     comm1_serviceable->setBoolValue( true );
484     comm2_serviceable->setBoolValue( true );
485     nav1_serviceable->setBoolValue( true );
486     nav2_serviceable->setBoolValue( true );
487     adf_serviceable->setBoolValue( true );
488     xpdr_serviceable->setBoolValue( true );
489     dme_serviceable->setBoolValue( true );
490
491     return true;
492 }
493
494
495 /////////////////////////////////////////////////////////////////////
496 // Write the lights
497 /////////////////////////////////////////////////////////////////////
498
499 bool FGATC610x::do_lights() {
500
501     // Marker beacons
502     ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
503     ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
504     ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
505
506     // ADF annunciators
507     ATC610xSetLamp( lamps_fd, 11, adf_ant_ann->getBoolValue() ); // ANT
508     ATC610xSetLamp( lamps_fd, 12, adf_adf_ann->getBoolValue() ); // ADF
509     ATC610xSetLamp( lamps_fd, 13, adf_bfo_ann->getBoolValue() ); // BFO
510     ATC610xSetLamp( lamps_fd, 14, adf_frq_ann->getBoolValue() ); // FRQ
511     ATC610xSetLamp( lamps_fd, 15, adf_flt_ann->getBoolValue() ); // FLT
512     ATC610xSetLamp( lamps_fd, 16, adf_et_ann->getBoolValue() ); // ET
513
514     // Transponder annunciators
515     ATC610xSetLamp( lamps_fd, 17, xpdr_fl_ann->getBoolValue() ); // FL
516     ATC610xSetLamp( lamps_fd, 18, xpdr_alt_ann->getBoolValue() ); // ALT
517     ATC610xSetLamp( lamps_fd, 19, xpdr_gnd_ann->getBoolValue() ); // GND
518     ATC610xSetLamp( lamps_fd, 20, xpdr_on_ann->getBoolValue() ); // ON
519     ATC610xSetLamp( lamps_fd, 21, xpdr_sby_ann->getBoolValue() ); // SBY
520     ATC610xSetLamp( lamps_fd, 22, xpdr_reply_ann->getBoolValue() ); // R
521
522     return true;
523 }
524
525
526 /////////////////////////////////////////////////////////////////////
527 // Update the radio display 
528 /////////////////////////////////////////////////////////////////////
529
530 bool FGATC610x::do_radio_display() {
531
532     char digits[10];
533     int i;
534
535     if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
536         if ( dme_in_range->getBoolValue() ) {
537             // DME minutes
538             float minutes = dme_min->getFloatValue();
539             if ( minutes > 999 ) {
540                 minutes = 999.0;
541             }
542             snprintf(digits, 7, "%03.0f", minutes);
543             for ( i = 0; i < 6; ++i ) {
544                 digits[i] -= '0';
545             }
546             radio_display_data[0] = digits[1] << 4 | digits[2];
547             radio_display_data[1] = 0xf0 | digits[0];
548         
549             // DME knots
550             float knots = dme_kt->getFloatValue();
551             if ( knots > 999 ) {
552                 knots = 999.0;
553             }
554             snprintf(digits, 7, "%03.0f", knots);
555             for ( i = 0; i < 6; ++i ) {
556                 digits[i] -= '0';
557             }
558             radio_display_data[2] = digits[1] << 4 | digits[2];
559             radio_display_data[3] = 0xf0 | digits[0];
560
561             // DME distance (nm)
562             float nm = dme_nm->getFloatValue();
563             if ( nm > 99 ) {
564                 nm = 99.0;
565             }
566             snprintf(digits, 7, "%04.1f", nm);
567             for ( i = 0; i < 6; ++i ) {
568                 digits[i] -= '0';
569             }
570             radio_display_data[4] = digits[1] << 4 | digits[3];
571             radio_display_data[5] = 0x00 | digits[0];
572             // the 0x00 in the upper nibble of the 6th byte of each
573             // display turns on the decimal point
574         } else {
575             // out of range
576             radio_display_data[0] = 0xbb;
577             radio_display_data[1] = 0xfb;
578             radio_display_data[2] = 0xbb;
579             radio_display_data[3] = 0xfb;
580             radio_display_data[4] = 0xbb;
581             radio_display_data[5] = 0x0b;
582         }
583     } else {
584         // blank dem display
585         for ( i = 0; i < 6; ++i ) {
586             radio_display_data[i] = 0xff;
587         }
588     }
589
590     if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
591         // Com1 standby frequency
592         float com1_stby = com1_stby_freq->getFloatValue();
593         if ( fabs(com1_stby) > 999.99 ) {
594             com1_stby = 0.0;
595         }
596         snprintf(digits, 7, "%06.3f", com1_stby);
597         for ( i = 0; i < 6; ++i ) {
598             digits[i] -= '0';
599         }
600         radio_display_data[6] = digits[4] << 4 | digits[5];
601         radio_display_data[7] = digits[1] << 4 | digits[2];
602         radio_display_data[8] = 0xf0 | digits[0];
603
604         // Com1 in use frequency
605         float com1 = com1_freq->getFloatValue();
606         if ( fabs(com1) > 999.99 ) {
607             com1 = 0.0;
608         }
609         snprintf(digits, 7, "%06.3f", com1);
610         for ( i = 0; i < 6; ++i ) {
611             digits[i] -= '0';
612         }
613         radio_display_data[9] = digits[4] << 4 | digits[5];
614         radio_display_data[10] = digits[1] << 4 | digits[2];
615         radio_display_data[11] = 0x00 | digits[0];
616         // the 0x00 in the upper nibble of the 6th byte of each display
617         // turns on the decimal point
618     } else {
619         radio_display_data[6] = 0xff;
620         radio_display_data[7] = 0xff;
621         radio_display_data[8] = 0xff;
622         radio_display_data[9] = 0xff;
623         radio_display_data[10] = 0xff;
624         radio_display_data[11] = 0xff;
625     }
626
627     if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
628         // Com2 standby frequency
629         float com2_stby = com2_stby_freq->getFloatValue();
630         if ( fabs(com2_stby) > 999.99 ) {
631             com2_stby = 0.0;
632         }
633         snprintf(digits, 7, "%06.3f", com2_stby);
634         for ( i = 0; i < 6; ++i ) {
635             digits[i] -= '0';
636         }
637         radio_display_data[18] = digits[4] << 4 | digits[5];
638         radio_display_data[19] = digits[1] << 4 | digits[2];
639         radio_display_data[20] = 0xf0 | digits[0];
640
641         // Com2 in use frequency
642         float com2 = com2_freq->getFloatValue();
643         if ( fabs(com2) > 999.99 ) {
644         com2 = 0.0;
645         }
646         snprintf(digits, 7, "%06.3f", com2);
647         for ( i = 0; i < 6; ++i ) {
648             digits[i] -= '0';
649         }
650         radio_display_data[21] = digits[4] << 4 | digits[5];
651         radio_display_data[22] = digits[1] << 4 | digits[2];
652         radio_display_data[23] = 0x00 | digits[0];
653         // the 0x00 in the upper nibble of the 6th byte of each display
654         // turns on the decimal point
655     } else {
656         radio_display_data[18] = 0xff;
657         radio_display_data[19] = 0xff;
658         radio_display_data[20] = 0xff;
659         radio_display_data[21] = 0xff;
660         radio_display_data[22] = 0xff;
661         radio_display_data[23] = 0xff;
662     }
663
664     if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
665         // Nav1 standby frequency
666         float nav1_stby = nav1_stby_freq->getFloatValue();
667         if ( fabs(nav1_stby) > 999.99 ) {
668         nav1_stby = 0.0;
669         }
670         snprintf(digits, 7, "%06.2f", nav1_stby);
671         for ( i = 0; i < 6; ++i ) {
672             digits[i] -= '0';
673         }
674         radio_display_data[12] = digits[4] << 4 | digits[5];
675         radio_display_data[13] = digits[1] << 4 | digits[2];
676         radio_display_data[14] = 0xf0 | digits[0];
677
678         // Nav1 in use frequency
679         float nav1 = nav1_freq->getFloatValue();
680         if ( fabs(nav1) > 999.99 ) {
681             nav1 = 0.0;
682         }
683         snprintf(digits, 7, "%06.2f", nav1);
684         for ( i = 0; i < 6; ++i ) {
685             digits[i] -= '0';
686         }
687         radio_display_data[15] = digits[4] << 4 | digits[5];
688         radio_display_data[16] = digits[1] << 4 | digits[2];
689         radio_display_data[17] = 0x00 | digits[0];
690         // the 0x00 in the upper nibble of the 6th byte of each display
691         // turns on the decimal point
692     } else {
693         radio_display_data[12] = 0xff;
694         radio_display_data[13] = 0xff;
695         radio_display_data[14] = 0xff;
696         radio_display_data[15] = 0xff;
697         radio_display_data[16] = 0xff;
698         radio_display_data[17] = 0xff;
699     }
700
701     if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
702         // Nav2 standby frequency
703         float nav2_stby = nav2_stby_freq->getFloatValue();
704         if ( fabs(nav2_stby) > 999.99 ) {
705             nav2_stby = 0.0;
706         }
707         snprintf(digits, 7, "%06.2f", nav2_stby);
708         for ( i = 0; i < 6; ++i ) {
709             digits[i] -= '0';
710         }
711         radio_display_data[24] = digits[4] << 4 | digits[5];
712         radio_display_data[25] = digits[1] << 4 | digits[2];
713         radio_display_data[26] = 0xf0 | digits[0];
714
715         // Nav2 in use frequency
716         float nav2 = nav2_freq->getFloatValue();
717         if ( fabs(nav2) > 999.99 ) {
718             nav2 = 0.0;
719         }
720         snprintf(digits, 7, "%06.2f", nav2);
721         for ( i = 0; i < 6; ++i ) {
722             digits[i] -= '0';
723         }
724         radio_display_data[27] = digits[4] << 4 | digits[5];
725         radio_display_data[28] = digits[1] << 4 | digits[2];
726         radio_display_data[29] = 0x00 | digits[0];
727         // the 0x00 in the upper nibble of the 6th byte of each display
728         // turns on the decimal point
729     } else {
730         radio_display_data[24] = 0xff;
731         radio_display_data[25] = 0xff;
732         radio_display_data[26] = 0xff;
733         radio_display_data[27] = 0xff;
734         radio_display_data[28] = 0xff;
735         radio_display_data[29] = 0xff;
736     }
737
738     // ADF standby frequency / timer
739     if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
740         if ( adf_stby_mode->getIntValue() == 0 ) {
741             // frequency
742             float adf_stby = adf_stby_freq->getFloatValue();
743             if ( fabs(adf_stby) > 1799 ) {
744                 adf_stby = 1799;
745             }
746             snprintf(digits, 7, "%04.0f", adf_stby);
747             for ( i = 0; i < 6; ++i ) {
748                 digits[i] -= '0';
749             }
750             radio_display_data[30] = digits[3] << 4 | 0x0f;
751             radio_display_data[31] = digits[1] << 4 | digits[2];
752             if ( digits[0] == 0 ) {
753                 radio_display_data[32] = 0xff;
754             } else {
755                 radio_display_data[32] = 0xf0 | digits[0];
756             }
757         } else {
758             // timer
759             double time;
760             int hours, min, sec;
761             if ( adf_timer_mode->getIntValue() == 0 ) {
762                 time = adf_flight_timer->getDoubleValue();
763             } else {
764                 time = adf_elapsed_timer->getDoubleValue();
765             }
766             // cout << time << endl;
767             hours = (int)(time / 3600.0);
768             time -= hours * 3600.00;
769             min = (int)(time / 60.0);
770             time -= min * 60.0;
771             sec = (int)time;
772             int big, little;
773             if ( hours > 0 ) {
774                 big = hours;
775                 if ( big > 99 ) {
776                     big = 99;
777                 }
778                 little = min;
779             } else {
780                 big = min;
781                 little = sec;
782             }
783             if ( big > 99 ) {
784                 big = 99;
785             }
786             // cout << big << ":" << little << endl;
787             snprintf(digits, 7, "%02d%02d", big, little);
788             for ( i = 0; i < 6; ++i ) {
789                 digits[i] -= '0';
790             }
791             radio_display_data[30] = digits[2] << 4 | digits[3];
792             radio_display_data[31] = digits[0] << 4 | digits[1];
793             radio_display_data[32] = 0xff;
794         }
795
796         // ADF in use frequency
797         float adf = adf_freq->getFloatValue();
798         if ( fabs(adf) > 1799 ) {
799             adf = 1799;
800         }
801         snprintf(digits, 7, "%04.0f", adf);
802         for ( i = 0; i < 6; ++i ) {
803             digits[i] -= '0';
804         }
805         radio_display_data[33] = digits[2] << 4 | digits[3];
806         if ( digits[0] == 0 ) {
807             radio_display_data[34] = 0xf0 | digits[1];
808         } else {
809             radio_display_data[34] = digits[0] << 4 | digits[1];
810         }
811         if ( adf_stby_mode->getIntValue() == 0 ) {
812           radio_display_data[35] = 0xff;
813         } else {
814           radio_display_data[35] = 0x0f;
815         }
816     } else {
817         radio_display_data[30] = 0xff;
818         radio_display_data[31] = 0xff;
819         radio_display_data[32] = 0xff;
820         radio_display_data[33] = 0xff;
821         radio_display_data[34] = 0xff;
822         radio_display_data[35] = 0xff;
823     }
824     
825     // Transponder code and flight level
826     if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
827         if ( xpdr_func_knob->getIntValue() == 2 ) {
828             // test mode
829             radio_display_data[36] = 8 << 4 | 8;
830             radio_display_data[37] = 8 << 4 | 8;
831             radio_display_data[38] = 0xff;
832             radio_display_data[39] = 8 << 4 | 0x0f;
833             radio_display_data[40] = 8 << 4 | 8;
834         } else {
835             // other on modes
836             int id_code = xpdr_id_code->getIntValue();
837             int place = 1000;
838             for ( i = 0; i < 4; ++i ) {
839                 digits[i] = id_code / place;
840                 id_code -= digits[i] * place;
841                 place /= 10;
842             }
843             radio_display_data[36] = digits[2] << 4 | digits[3];
844             radio_display_data[37] = digits[0] << 4 | digits[1];
845             radio_display_data[38] = 0xff;
846
847             if ( xpdr_func_knob->getIntValue() == 3 ||
848                  xpdr_func_knob->getIntValue() == 5 )
849             {
850                 // do flight level display
851                 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
852                 for ( i = 0; i < 6; ++i ) {
853                     digits[i] -= '0';
854                 }
855                 radio_display_data[39] = digits[2] << 4 | 0x0f;
856                 radio_display_data[40] = digits[0] << 4 | digits[1];
857             } else {
858                 // blank flight level display
859                 radio_display_data[39] = 0xff;
860                 radio_display_data[40] = 0xff;
861             }
862         }
863     } else {
864         // off
865         radio_display_data[36] = 0xff;
866         radio_display_data[37] = 0xff;
867         radio_display_data[38] = 0xff;
868         radio_display_data[39] = 0xff;
869         radio_display_data[40] = 0xff;
870     }
871
872     ATC610xSetRadios( radios_fd, radio_display_data );
873
874     return true;
875 }
876
877
878 /////////////////////////////////////////////////////////////////////
879 // Drive the stepper motors
880 /////////////////////////////////////////////////////////////////////
881
882 bool FGATC610x::do_steppers() {
883     float diff = mag_compass->getFloatValue() - compass_position;
884     while ( diff < -180.0 ) { diff += 360.0; }
885     while ( diff >  180.0 ) { diff -= 360.0; }
886
887     int steps = (int)(diff * 4);
888     // cout << "steps = " << steps << endl;
889     if ( steps > 4 ) { steps = 4; }
890     if ( steps < -4 ) { steps = -4; }
891
892     if ( abs(steps) > 0 ) {
893         unsigned char cmd = 0x80;       // stepper command
894         if ( steps > 0 ) {
895             cmd |= 0x20;                // go up
896         } else {
897             cmd |= 0x00;                // go down
898         }
899         cmd |= abs(steps);
900
901         // sync compass_position with hardware position
902         compass_position += (float)steps / 4.0;
903
904         ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
905     }
906
907     return true;
908 }
909
910
911 bool FGATC610x::process() {
912     
913     // Lock the hardware, skip if it's not ready yet
914     if ( ATC610xLock( lock_fd ) > 0 ) {
915
916         // process the ATC inputs
917         if ( input0 != NULL ) {
918             input0->process();
919         }
920         if ( input1 != NULL ) {
921             input1->process();
922         }
923
924         // run our custom nasal script.  This is a layer above the raw
925         // hardware inputs.  It handles situations where there isn't a
926         // direct 1-1 linear mapping between ATC functionality and FG
927         // functionality, and handles situations where FG expects more
928         // functionality from the interface than the ATC hardware can
929         // directly provide.
930
931         FGNasalSys *n = (FGNasalSys*)globals->get_subsystem("nasal");
932         bool result = n->parseAndRun( "atcsim.do_hardware()" );
933         if ( !result ) {
934             SG_LOG( SG_GENERAL, SG_ALERT,
935                     "do_atcflightsim_hardware() failed!" );
936         }
937
938         do_lights();
939         do_radio_display();
940         do_steppers();
941         
942         ATC610xRelease( lock_fd );
943
944         return true;
945     } else {
946         return false;
947     }
948 }
949
950
951 bool FGATC610x::close() {
952
953     return true;
954 }