]> git.mxchange.org Git - flightgear.git/blob - src/Network/atc610x.cxx
Make adf volume and adf on/off seperate properties.
[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/props.hxx>
47
48 #include <Main/fg_props.hxx>
49 #include <Main/globals.hxx>
50
51 #include "atc610x.hxx"
52
53 SG_USING_STD(string);
54
55 // Lock the ATC 610 hardware
56 static int ATC610xLock( int fd ) {
57     // rewind
58     lseek( fd, 0, SEEK_SET );
59
60     char tmp[2];
61     int result = read( fd, tmp, 1 );
62     if ( result != 1 ) {
63         SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
64     }
65
66     return result;
67 }
68
69
70 // Write a radios command
71 static int ATC610xRelease( int fd ) {
72     // rewind
73     lseek( fd, 0, SEEK_SET );
74
75     char tmp[2];
76     tmp[0] = tmp[1] = 0;
77     int result = write( fd, tmp, 1 );
78
79     if ( result != 1 ) {
80         SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
81     }
82
83     return result;
84 }
85
86
87 // Read analog inputs
88 static void ATC610xReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
89     // rewind
90     lseek( fd, 0, SEEK_SET );
91
92     int result = read( fd, analog_in_bytes, ATC_ANAL_IN_BYTES );
93     if ( result != ATC_ANAL_IN_BYTES ) {
94         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
95         exit( -1 );
96     }
97 }
98
99
100 // Write a radios command
101 static int ATC610xSetRadios( int fd,
102                              unsigned char data[ATC_RADIO_DISPLAY_BYTES] )
103 {
104     // rewind
105     lseek( fd, 0, SEEK_SET );
106
107     int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
108
109     if ( result != ATC_RADIO_DISPLAY_BYTES ) {
110         SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
111     }
112
113     return result;
114 }
115
116
117 // Read status of last radios written to
118 static void ATC610xReadRadios( int fd, unsigned char *switch_data ) {
119     // rewind
120     lseek( fd, 0, SEEK_SET );
121
122     int result = read( fd, switch_data, ATC_RADIO_SWITCH_BYTES );
123     if ( result != ATC_RADIO_SWITCH_BYTES ) {
124         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
125         exit( -1 );
126     }
127 }
128
129 // Write a stepper command
130 static int ATC610xSetStepper( int fd, unsigned char channel,
131                               unsigned char value )
132 {
133     // rewind
134     lseek( fd, 0, SEEK_SET );
135
136     // Write the value
137     unsigned char buf[3];
138     buf[0] = channel;
139     buf[1] = value;
140     buf[2] = 0;
141     int result = write( fd, buf, 2 );
142     if ( result != 2 ) {
143         SG_LOG( SG_IO, SG_INFO, "Write failed" );
144     }
145     SG_LOG( SG_IO, SG_DEBUG,
146             "Sent cmd = " << (int)channel << " value = " << (int)value );
147     return result;
148 }
149
150
151 // Read status of last stepper written to
152 static unsigned char ATC610xReadStepper( int fd ) {
153     int result;
154
155     // rewind
156     lseek( fd, 0, SEEK_SET );
157
158     // Write the value
159     unsigned char buf[2];
160     result = read( fd, buf, 1 );
161     if ( result != 1 ) {
162         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
163         exit( -1 );
164     }
165     SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
166
167     return buf[0];
168 }
169
170
171 // Read switch inputs
172 static void ATC610xReadSwitches( int fd, unsigned char *switch_bytes ) {
173     // rewind
174     lseek( fd, 0, SEEK_SET );
175
176     int result = read( fd, switch_bytes, ATC_SWITCH_BYTES );
177     if ( result != ATC_SWITCH_BYTES ) {
178         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
179         exit( -1 );
180     }
181 }
182
183
184 // Turn a lamp on or off
185 void ATC610xSetLamp( int fd, int channel, bool value ) {
186     // lamp channels 0-63 are written to LampPort0, channels 64-127
187     // are written to LampPort1
188
189     // bits 0-6 are the lamp address
190     // bit 7 is the value (on/off)
191
192     int result;
193
194     // Write the value
195     unsigned char buf[3];
196     buf[0] = channel;
197     buf[1] = value;
198     buf[2] = 0;
199     result = write( fd, buf, 2 );
200     if ( result != 2 ) {
201         SG_LOG( SG_IO, SG_ALERT,  "Write failed" );
202         exit( -1 );
203     }
204 }
205
206
207 // Open and initialize ATC 610x hardware
208 bool FGATC610x::open() {
209     if ( is_enabled() ) {
210         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
211                 << "is already in use, ignoring" );
212         return false;
213     }
214
215     SG_LOG( SG_IO, SG_ALERT,
216             "Initializing ATC 610x hardware, please wait ..." );
217
218     set_hz( 30 );               // default to processing requests @ 30Hz
219     set_enabled( true );
220
221     board = 0;                  // 610x uses a single board number = 0
222
223     snprintf( lock_file, 256, "/proc/atc610x/board%d/lock", board );
224     snprintf( analog_in_file, 256, "/proc/atc610x/board%d/analog_in", board );
225     snprintf( lamps_file, 256, "/proc/atc610x/board%d/lamps", board );
226     snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
227     snprintf( stepper_file, 256, "/proc/atc610x/board%d/steppers", board );
228     snprintf( switches_file, 256, "/proc/atc610x/board%d/switches", board );
229
230     /////////////////////////////////////////////////////////////////////
231     // Open the /proc files
232     /////////////////////////////////////////////////////////////////////
233
234     lock_fd = ::open( lock_file, O_RDWR );
235     if ( lock_fd == -1 ) {
236         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
237         char msg[256];
238         snprintf( msg, 256, "Error opening %s", lock_file );
239         perror( msg );
240         exit( -1 );
241     }
242
243     analog_in_fd = ::open( analog_in_file, O_RDONLY );
244     if ( analog_in_fd == -1 ) {
245         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
246         char msg[256];
247         snprintf( msg, 256, "Error opening %s", analog_in_file );
248         perror( msg );
249         exit( -1 );
250     }
251
252     lamps_fd = ::open( lamps_file, O_WRONLY );
253     if ( lamps_fd == -1 ) {
254         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
255         char msg[256];
256         snprintf( msg, 256, "Error opening %s", lamps_file );
257         perror( msg );
258         exit( -1 );
259     }
260
261     radios_fd = ::open( radios_file, O_RDWR );
262     if ( radios_fd == -1 ) {
263         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
264         char msg[256];
265         snprintf( msg, 256, "Error opening %s", radios_file );
266         perror( msg );
267         exit( -1 );
268     }
269
270     stepper_fd = ::open( stepper_file, O_RDWR );
271     if ( stepper_fd == -1 ) {
272         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
273         char msg[256];
274         snprintf( msg, 256, "Error opening %s", stepper_file );
275         perror( msg );
276         exit( -1 );
277     }
278
279     switches_fd = ::open( switches_file, O_RDONLY );
280     if ( switches_fd == -1 ) {
281         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
282         char msg[256];
283         snprintf( msg, 256, "Error opening %s", switches_file );
284         perror( msg );
285         exit( -1 );
286     }
287
288     /////////////////////////////////////////////////////////////////////
289     // Home the compass stepper motor
290     /////////////////////////////////////////////////////////////////////
291
292     SG_LOG( SG_IO, SG_ALERT,
293             "  - Homing the compass stepper motor" );
294
295     // Lock the hardware, keep trying until we succeed
296     while ( ATC610xLock( lock_fd ) <= 0 );
297
298     // Send the stepper home command
299     ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
300
301     // Release the hardware
302     ATC610xRelease( lock_fd );
303
304     SG_LOG( SG_IO, SG_ALERT,
305             "  - Waiting for compass to come home." );
306
307     bool home = false;
308     int timeout = 900;          // about 30 seconds
309     while ( ! home && timeout > 0 ) {
310         if ( timeout % 150 == 0 ) {
311             SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
312         } else {
313             SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
314         }
315
316         while ( ATC610xLock( lock_fd ) <= 0 );
317
318         unsigned char result = ATC610xReadStepper( stepper_fd );
319         if ( result == 0 ) {
320             home = true;
321         }
322
323         ATC610xRelease( lock_fd );
324
325 #if defined( _MSC_VER )
326         ulMilliSecondSleep(33);
327 #elif defined (WIN32) && !defined(__CYGWIN__)
328         Sleep (33);
329 #else
330         usleep(33);
331 #endif
332
333         --timeout;
334     }
335
336     compass_position = 0.0;
337
338     /////////////////////////////////////////////////////////////////////
339     // Blank the radio display
340     /////////////////////////////////////////////////////////////////////
341
342     SG_LOG( SG_IO, SG_ALERT,
343             "  - Clearing the radios displays." );
344
345     // Prepair the data
346     unsigned char value = 0xff;
347     for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
348         radio_display_data[channel] = value;
349     }
350
351     // Lock the hardware, keep trying until we succeed
352     while ( ATC610xLock( lock_fd ) <= 0 );
353
354     // Set radio display
355     ATC610xSetRadios( radios_fd, radio_display_data );
356
357     ATC610xRelease( lock_fd );
358
359     /////////////////////////////////////////////////////////////////////
360     // Blank the lamps
361     /////////////////////////////////////////////////////////////////////
362
363     for ( int i = 0; i < 128; ++i ) {
364         ATC610xSetLamp( lamps_fd, i, false );
365     }
366
367     /////////////////////////////////////////////////////////////////////
368     // Finished initing hardware
369     /////////////////////////////////////////////////////////////////////
370
371     SG_LOG( SG_IO, SG_ALERT,
372             "Done initializing ATC 610x hardware." );
373
374     /////////////////////////////////////////////////////////////////////
375     // Connect up to property values
376     /////////////////////////////////////////////////////////////////////
377
378     mag_compass = fgGetNode( "/steam/mag-compass-deg", true );
379
380     dme_min = fgGetNode( "/radios/dme/ete-min", true );
381     dme_kt = fgGetNode( "/radios/dme/speed-kt", true );
382     dme_nm = fgGetNode( "/radios/dme/distance-nm", true );
383
384     com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
385     com1_stby_freq
386         = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
387     com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
388     com2_stby_freq
389         = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
390
391     nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
392     nav1_stby_freq
393         = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
394
395     nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
396     nav2_stby_freq
397         = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
398
399     adf_power = fgGetNode( "/radios/kr-87/inputs/power-btn", true );
400     adf_vol = fgGetNode( "/radios/kr-87/inputs/volume", true );
401     adf_adf_btn = fgGetNode( "/radios/kr-87/inputs/adf-btn", true );
402     adf_bfo_btn = fgGetNode( "/radios/kr-87/inputs/bfo-btn", true );
403     adf_freq = fgGetNode( "/radios/kr-87/outputs/selected-khz", true );
404     adf_stby_freq = fgGetNode( "/radios/kr-87/outputs/standby-khz", true );
405     adf_stby_mode = fgGetNode( "/radios/kr-87/modes/stby", true );
406     adf_timer_mode = fgGetNode( "/radios/kr-87/modes/timer", true );
407     adf_count_mode = fgGetNode( "/radios/kr-87/modes/count", true );
408     adf_flight_timer = fgGetNode( "/radios/kr-87/outputs/flight-timer", true );
409     adf_elapsed_timer = fgGetNode( "/radios/kr-87/outputs/elapsed-timer",
410                                    true );
411     adf_ant_ann = fgGetNode( "/radios/kr-87/annunciators/ant", true );
412     adf_adf_ann = fgGetNode( "/radios/kr-87/annunciators/adf", true );
413     adf_bfo_ann = fgGetNode( "/radios/kr-87/annunciators/bfo", true );
414     adf_frq_ann = fgGetNode( "/radios/kr-87/annunciators/frq", true );
415     adf_flt_ann = fgGetNode( "/radios/kr-87/annunciators/flt", true );
416     adf_et_ann = fgGetNode( "/radios/kr-87/annunciators/et", true );
417
418     inner = fgGetNode( "/radios/marker-beacon/inner", true );
419     middle = fgGetNode( "/radios/marker-beacon/middle", true );
420     outer = fgGetNode( "/radios/marker-beacon/outer", true );
421
422     xpdr_ident_btn = fgGetNode( "/radios/kt-70/inputs/ident-btn", true );
423     xpdr_digit1 = fgGetNode( "/radios/kt-70/inputs/digit1", true );
424     xpdr_digit2 = fgGetNode( "/radios/kt-70/inputs/digit2", true );
425     xpdr_digit3 = fgGetNode( "/radios/kt-70/inputs/digit3", true );
426     xpdr_digit4 = fgGetNode( "/radios/kt-70/inputs/digit4", true );
427     xpdr_func_knob = fgGetNode( "/radios/kt-70/inputs/func-knob", true );
428     xpdr_id_code = fgGetNode( "/radios/kt-70/outputs/id-code", true );
429     xpdr_flight_level = fgGetNode( "/radios/kt-70/outputs/flight-level", true );
430     xpdr_fl_ann = fgGetNode( "/radios/kt-70/annunciators/fl", true );
431     xpdr_alt_ann = fgGetNode( "/radios/kt-70/annunciators/alt", true );
432     xpdr_gnd_ann = fgGetNode( "/radios/kt-70/annunciators/gnd", true );
433     xpdr_on_ann = fgGetNode( "/radios/kt-70/annunciators/on", true );
434     xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
435     xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
436
437     return true;
438 }
439
440
441 /////////////////////////////////////////////////////////////////////
442 // Read analog inputs
443 /////////////////////////////////////////////////////////////////////
444
445 #define ATC_AILERON_CENTER 535
446 #define ATC_ELEVATOR_TRIM_CENTER 512
447 #define ATC_ELEVATOR_CENTER 543
448 #define ATC_RUDDER_CENTER 519
449
450 bool FGATC610x::do_analog_in() {
451     // Read raw data in byte form
452     ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
453
454     // Convert to integer values
455     for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
456         unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
457         unsigned char lo = analog_in_bytes[2 * channel + 1];
458         analog_in_data[channel] = hi * 256 + lo;
459
460         // printf("%02x %02x ", hi, lo );
461         // printf("%04d ", value );
462     }
463
464     float tmp, tmp1, tmp2;
465
466     // aileron
467     tmp = (float)(analog_in_data[0] - ATC_AILERON_CENTER) / 256.0f;
468     fgSetFloat( "/controls/aileron", tmp );
469     // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
470
471     // elevator
472     tmp = (float)(analog_in_data[4] - ATC_ELEVATOR_TRIM_CENTER) / 512.0f;
473     fgSetFloat( "/controls/elevator-trim", tmp );
474     // cout << "trim = " << analog_in_data[4] << " = " << tmp;
475
476     // trim
477     tmp = (float)(ATC_ELEVATOR_CENTER - analog_in_data[5]) / 100.0f;
478     fgSetFloat( "/controls/elevator", tmp );
479     // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
480
481     // mixture
482     tmp = (float)analog_in_data[7] / 680.0f;
483     fgSetFloat( "/controls/mixture[0]", tmp );
484     fgSetFloat( "/controls/mixture[1]", tmp );
485
486     // throttle
487     tmp = ((float)analog_in_data[8] - 141.0) / 632.0f;
488     fgSetFloat( "/controls/throttle[0]", tmp );
489     fgSetFloat( "/controls/throttle[1]", tmp );
490
491     // rudder
492     tmp = (float)(ATC_RUDDER_CENTER - analog_in_data[10]) / 145.0f;
493     fgSetFloat( "/controls/rudder", tmp );
494
495     // nav1 volume
496     tmp = (float)analog_in_data[25] / 1024.0f;
497     fgSetFloat( "/radios/nav[0]/volume", tmp );
498
499     // nav2 volume
500     tmp = (float)analog_in_data[24] / 1024.0f;
501     fgSetFloat( "/radios/nav[1]/volume", tmp );
502
503     // adf volume
504     tmp = (float)analog_in_data[26] / 1024.0f;
505     fgSetFloat( "/radios/kr-87/inputs/volume", tmp );
506
507     // nav2 obs tuner
508     tmp = (float)analog_in_data[29] * 360.0f / 1024.0f;
509     fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
510
511     // nav1 obs tuner
512     tmp1 = (float)analog_in_data[30] * 360.0f / 1024.0f;
513     tmp2 = (float)analog_in_data[31] * 360.0f / 1024.0f;
514     fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp1 );
515
516     return true;
517 }
518
519
520 /////////////////////////////////////////////////////////////////////
521 // Write the lights
522 /////////////////////////////////////////////////////////////////////
523
524 bool FGATC610x::do_lights() {
525
526     // Marker beacons
527     ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
528     ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
529     ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
530
531     // ADF annunciators
532     ATC610xSetLamp( lamps_fd, 11, adf_ant_ann->getBoolValue() ); // ANT
533     ATC610xSetLamp( lamps_fd, 12, adf_adf_ann->getBoolValue() ); // ADF
534     ATC610xSetLamp( lamps_fd, 13, adf_bfo_ann->getBoolValue() ); // BFO
535     ATC610xSetLamp( lamps_fd, 14, adf_frq_ann->getBoolValue() ); // FRQ
536     ATC610xSetLamp( lamps_fd, 15, adf_flt_ann->getBoolValue() ); // FLT
537     ATC610xSetLamp( lamps_fd, 16, adf_et_ann->getBoolValue() ); // ET
538
539     // Transponder annunciators
540     ATC610xSetLamp( lamps_fd, 17, xpdr_fl_ann->getBoolValue() ); // FL
541     ATC610xSetLamp( lamps_fd, 18, xpdr_alt_ann->getBoolValue() ); // ALT
542     ATC610xSetLamp( lamps_fd, 19, xpdr_gnd_ann->getBoolValue() ); // GND
543     ATC610xSetLamp( lamps_fd, 20, xpdr_on_ann->getBoolValue() ); // ON
544     ATC610xSetLamp( lamps_fd, 21, xpdr_sby_ann->getBoolValue() ); // SBY
545     ATC610xSetLamp( lamps_fd, 22, xpdr_reply_ann->getBoolValue() ); // R
546
547     return true;
548 }
549
550
551 /////////////////////////////////////////////////////////////////////
552 // Read radio switches 
553 /////////////////////////////////////////////////////////////////////
554
555 bool FGATC610x::do_radio_switches() {
556     double freq, coarse_freq, fine_freq, value;
557     int diff;
558
559     ATC610xReadRadios( radios_fd, radio_switch_data );
560
561     // DME Switch
562     dme_switch = (radio_switch_data[7] >> 4) & 0x03;
563     if ( dme_switch == 0 ) {
564         // off
565         fgSetInt( "/radios/dme/switch-position", 0 );
566     } else if ( dme_switch == 2 ) {
567         // nav1
568         fgSetInt( "/radios/dme/switch-position", 1 );
569     } else if ( dme_switch == 1 ) {
570         // nav2
571         fgSetInt( "/radios/dme/switch-position", 3 );
572     }
573
574     // Com1 Swap
575     int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
576     static int last_com1_swap;
577     if ( com1_swap && (last_com1_swap != com1_swap) ) {
578         float tmp = com1_freq->getFloatValue();
579         fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
580                    com1_stby_freq->getFloatValue() );
581         fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
582     }
583     last_com1_swap = com1_swap;
584
585     // Com2 Swap
586     int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
587     static int last_com2_swap;
588     if ( com2_swap && (last_com2_swap != com2_swap) ) {
589         float tmp = com2_freq->getFloatValue();
590         fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
591                    com2_stby_freq->getFloatValue() );
592         fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
593     }
594     last_com2_swap = com2_swap;
595
596     // Nav1 Swap
597     int nav1_swap = radio_switch_data[11] & 0x01;
598     static int last_nav1_swap;
599     if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
600         float tmp = nav1_freq->getFloatValue();
601         fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
602                    nav1_stby_freq->getFloatValue() );
603         fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
604     }
605     last_nav1_swap = nav1_swap;
606
607     // Nav2 Swap
608     int nav2_swap = !(radio_switch_data[19] & 0x01);
609     static int last_nav2_swap;
610     if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
611         float tmp = nav2_freq->getFloatValue();
612         fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
613                    nav2_stby_freq->getFloatValue() );
614         fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
615     }
616     last_nav2_swap = nav2_swap;
617
618     // Com1 Tuner
619     int com1_tuner_fine = ((radio_switch_data[5] >> 4) & 0x0f) - 1;
620     int com1_tuner_coarse = (radio_switch_data[5] & 0x0f) - 1;
621     static int last_com1_tuner_fine = com1_tuner_fine;
622     static int last_com1_tuner_coarse = com1_tuner_coarse;
623
624     freq = com1_stby_freq->getFloatValue();
625     coarse_freq = (int)freq;
626     fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
627
628     if ( com1_tuner_fine != last_com1_tuner_fine ) {
629         diff = com1_tuner_fine - last_com1_tuner_fine;
630         if ( abs(diff) > 4 ) {
631             // roll over
632             if ( com1_tuner_fine < last_com1_tuner_fine ) {
633                 // going up
634                 diff = 12 - last_com1_tuner_fine + com1_tuner_fine;
635             } else {
636                 // going down
637                 diff = com1_tuner_fine - 12 - last_com1_tuner_fine;
638             }
639         }
640         fine_freq += diff;
641     }
642     while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
643     while ( fine_freq < 0.0 )  { fine_freq += 40.0; }
644
645     if ( com1_tuner_coarse != last_com1_tuner_coarse ) {
646         diff = com1_tuner_coarse - last_com1_tuner_coarse;
647         if ( abs(diff) > 4 ) {
648             // roll over
649             if ( com1_tuner_coarse < last_com1_tuner_coarse ) {
650                 // going up
651                 diff = 12 - last_com1_tuner_coarse + com1_tuner_coarse;
652             } else {
653                 // going down
654                 diff = com1_tuner_coarse - 12 - last_com1_tuner_coarse;
655             }
656         }
657         coarse_freq += diff;
658     }
659     if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
660     if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
661
662     last_com1_tuner_fine = com1_tuner_fine;
663     last_com1_tuner_coarse = com1_tuner_coarse;
664
665     fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", 
666                 coarse_freq + fine_freq / 40.0 );
667
668     // Com2 Tuner
669     int com2_tuner_fine = ((radio_switch_data[13] >> 4) & 0x0f) - 1;
670     int com2_tuner_coarse = (radio_switch_data[13] & 0x0f) - 1;
671     static int last_com2_tuner_fine = com2_tuner_fine;
672     static int last_com2_tuner_coarse = com2_tuner_coarse;
673
674     freq = com2_stby_freq->getFloatValue();
675     coarse_freq = (int)freq;
676     fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
677
678     if ( com2_tuner_fine != last_com2_tuner_fine ) {
679         diff = com2_tuner_fine - last_com2_tuner_fine;
680         if ( abs(diff) > 4 ) {
681             // roll over
682             if ( com2_tuner_fine < last_com2_tuner_fine ) {
683                 // going up
684                 diff = 12 - last_com2_tuner_fine + com2_tuner_fine;
685             } else {
686                 // going down
687                 diff = com2_tuner_fine - 12 - last_com2_tuner_fine;
688             }
689         }
690         fine_freq += diff;
691     }
692     while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
693     while ( fine_freq < 0.0 )  { fine_freq += 40.0; }
694
695     if ( com2_tuner_coarse != last_com2_tuner_coarse ) {
696         diff = com2_tuner_coarse - last_com2_tuner_coarse;
697         if ( abs(diff) > 4 ) {
698             // roll over
699             if ( com2_tuner_coarse < last_com2_tuner_coarse ) {
700                 // going up
701                 diff = 12 - last_com2_tuner_coarse + com2_tuner_coarse;
702             } else {
703                 // going down
704                 diff = com2_tuner_coarse - 12 - last_com2_tuner_coarse;
705             }
706         }
707         coarse_freq += diff;
708     }
709     if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
710     if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
711
712     last_com2_tuner_fine = com2_tuner_fine;
713     last_com2_tuner_coarse = com2_tuner_coarse;
714
715     fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz",
716                 coarse_freq + fine_freq / 40.0 );
717
718     // Nav1 Tuner
719     int nav1_tuner_fine = ((radio_switch_data[9] >> 4) & 0x0f) - 1;
720     int nav1_tuner_coarse = (radio_switch_data[9] & 0x0f) - 1;
721     static int last_nav1_tuner_fine = nav1_tuner_fine;
722     static int last_nav1_tuner_coarse = nav1_tuner_coarse;
723
724     freq = nav1_stby_freq->getFloatValue();
725     coarse_freq = (int)freq;
726     fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
727
728     if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
729         diff = nav1_tuner_fine - last_nav1_tuner_fine;
730         if ( abs(diff) > 4 ) {
731             // roll over
732             if ( nav1_tuner_fine < last_nav1_tuner_fine ) {
733                 // going up
734                 diff = 12 - last_nav1_tuner_fine + nav1_tuner_fine;
735             } else {
736                 // going down
737                 diff = nav1_tuner_fine - 12 - last_nav1_tuner_fine;
738             }
739         }
740         fine_freq += diff;
741     }
742     while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
743     while ( fine_freq < 0.0 )  { fine_freq += 20.0; }
744
745     if ( nav1_tuner_coarse != last_nav1_tuner_coarse ) {
746         diff = nav1_tuner_coarse - last_nav1_tuner_coarse;
747         if ( abs(diff) > 4 ) {
748             // roll over
749             if ( nav1_tuner_coarse < last_nav1_tuner_coarse ) {
750                 // going up
751                 diff = 12 - last_nav1_tuner_coarse + nav1_tuner_coarse;
752             } else {
753                 // going down
754                 diff = nav1_tuner_coarse - 12 - last_nav1_tuner_coarse;
755             }
756         }
757         coarse_freq += diff;
758     }
759     if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
760     if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
761
762     last_nav1_tuner_fine = nav1_tuner_fine;
763     last_nav1_tuner_coarse = nav1_tuner_coarse;
764
765     fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz",
766                 coarse_freq + fine_freq / 20.0 );
767
768     // Nav2 Tuner
769     int nav2_tuner_fine = ((radio_switch_data[17] >> 4) & 0x0f) - 1;
770     int nav2_tuner_coarse = (radio_switch_data[17] & 0x0f) - 1;
771     static int last_nav2_tuner_fine = nav2_tuner_fine;
772     static int last_nav2_tuner_coarse = nav2_tuner_coarse;
773
774     freq = nav2_stby_freq->getFloatValue();
775     coarse_freq = (int)freq;
776     fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
777
778     if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
779         diff = nav2_tuner_fine - last_nav2_tuner_fine;
780         if ( abs(diff) > 4 ) {
781             // roll over
782             if ( nav2_tuner_fine < last_nav2_tuner_fine ) {
783                 // going up
784                 diff = 12 - last_nav2_tuner_fine + nav2_tuner_fine;
785             } else {
786                 // going down
787                 diff = nav2_tuner_fine - 12 - last_nav2_tuner_fine;
788             }
789         }
790         fine_freq += diff;
791     }
792     while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
793     while ( fine_freq < 0.0 )  { fine_freq += 20.0; }
794
795     if ( nav2_tuner_coarse != last_nav2_tuner_coarse ) {
796         diff = nav2_tuner_coarse - last_nav2_tuner_coarse;
797         if ( abs(diff) > 4 ) {
798             // roll over
799             if ( nav2_tuner_coarse < last_nav2_tuner_coarse ) {
800                 // going up
801                 diff = 12 - last_nav2_tuner_coarse + nav2_tuner_coarse;
802             } else {
803                 // going down
804                 diff = nav2_tuner_coarse - 12 - last_nav2_tuner_coarse;
805             }
806         }
807         coarse_freq += diff;
808     }
809     if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
810     if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
811
812     last_nav2_tuner_fine = nav2_tuner_fine;
813     last_nav2_tuner_coarse = nav2_tuner_coarse;
814
815     fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", 
816                 coarse_freq + fine_freq / 20.0);
817
818     // ADF Tuner
819     int adf_tuner_fine = ((radio_switch_data[21] >> 4) & 0x0f) - 1;
820     int adf_tuner_coarse = (radio_switch_data[21] & 0x0f) - 1;
821     static int last_adf_tuner_fine = adf_tuner_fine;
822     static int last_adf_tuner_coarse = adf_tuner_coarse;
823
824     // cout << "adf_stby_mode = " << adf_stby_mode->getIntValue() << endl;
825     if ( adf_count_mode->getIntValue() == 2 ) {
826         // tune count down timer
827         value = adf_elapsed_timer->getDoubleValue();
828     } else {
829         // tune frequency
830         if ( adf_stby_mode->getIntValue() == 1 ) {
831             value = adf_freq->getFloatValue();
832         } else {
833             value = adf_stby_freq->getFloatValue();
834         }
835     }
836
837     if ( adf_tuner_fine != last_adf_tuner_fine ) {
838         diff = adf_tuner_fine - last_adf_tuner_fine;
839         if ( abs(diff) > 4 ) {
840             // roll over
841             if ( adf_tuner_fine < last_adf_tuner_fine ) {
842                 // going up
843                 diff = 12 - last_adf_tuner_fine + adf_tuner_fine;
844             } else {
845                 // going down
846                 diff = adf_tuner_fine - 12 - last_adf_tuner_fine;
847             }
848         }
849         value += diff;
850     }
851
852     if ( adf_tuner_coarse != last_adf_tuner_coarse ) {
853         diff = adf_tuner_coarse - last_adf_tuner_coarse;
854         if ( abs(diff) > 4 ) {
855             // roll over
856             if ( adf_tuner_coarse < last_adf_tuner_coarse ) {
857                 // going up
858                 diff = 12 - last_adf_tuner_coarse + adf_tuner_coarse;
859             } else {
860                 // going down
861                 diff = adf_tuner_coarse - 12 - last_adf_tuner_coarse;
862             }
863         }
864         if ( adf_count_mode->getIntValue() == 2 ) {
865             value += 60 * diff;
866         } else {
867             value += 25 * diff;
868         }
869     }
870     if ( adf_count_mode->getIntValue() == 2 ) {
871         if ( value < 0 ) { value += 3600; }
872         if ( value > 3599 ) { value -= 3600; }
873     } else {
874         if ( value < 200 ) { value += 1600; }
875         if ( value > 1799 ) { value -= 1600; }
876     }
877  
878     last_adf_tuner_fine = adf_tuner_fine;
879     last_adf_tuner_coarse = adf_tuner_coarse;
880
881     if ( adf_count_mode->getIntValue() == 2 ) {
882         fgSetFloat( "/radios/kr-87/outputs/elapsed-timer", value );
883     } else {
884         if ( adf_stby_mode->getIntValue() == 1 ) {
885             fgSetFloat( "/radios/kr-87/outputs/selected-khz", value );
886         } else {
887             fgSetFloat( "/radios/kr-87/outputs/standby-khz", value );
888         }
889     }
890
891     // ADF buttons 
892     fgSetInt( "/radios/kr-87/inputs/adf-btn",
893               !(radio_switch_data[23] & 0x01) );
894     fgSetInt( "/radios/kr-87/inputs/bfo-btn",
895               !(radio_switch_data[23] >> 1 & 0x01) );
896     fgSetInt( "/radios/kr-87/inputs/frq-btn",
897               (radio_switch_data[23] >> 2 & 0x01) );
898     fgSetInt( "/radios/kr-87/inputs/flt-et-btn",
899               !(radio_switch_data[23] >> 3 & 0x01) );
900     fgSetInt( "/radios/kr-87/inputs/set-rst-btn",
901               !(radio_switch_data[23] >> 4 & 0x01) );
902     fgSetInt( "/radios/kr-87/inputs/power-btn",
903               radio_switch_data[23] >> 4 & 0x01 );
904     /* cout << "adf = " << !(radio_switch_data[23] & 0x01)
905          << " bfo = " << !(radio_switch_data[23] >> 1 & 0x01)
906          << " stby = " << !(radio_switch_data[23] >> 2 & 0x01)
907          << " timer = " << !(radio_switch_data[23] >> 3 & 0x01)
908          << " set/rst = " << !(radio_switch_data[23] >> 4 & 0x01)
909          << endl; */
910
911     // Transponder Tuner
912     int i;
913     int digit_tuner[4];
914
915     digit_tuner[0] = radio_switch_data[25] & 0x0f;
916     digit_tuner[1] = ( radio_switch_data[25] >> 4 ) & 0x0f;
917     digit_tuner[2] = radio_switch_data[29] & 0x0f;
918     digit_tuner[3] = ( radio_switch_data[29] >> 4 ) & 0x0f;
919     static bool first_time = true;
920     static int last_digit_tuner[4];
921     if ( first_time ) {
922         first_time = false;
923         for ( i = 0; i < 4; ++i ) {
924             last_digit_tuner[i] = digit_tuner[i];
925         }
926     }
927
928     int id_code = xpdr_id_code->getIntValue();
929     int digit[4];
930     int place = 1000;
931     for ( i = 0; i < 4; ++i ) {
932         digit[i] = id_code / place;
933         id_code -= digit[i] * place;
934         place /= 10;
935     }
936
937     for ( i = 0; i < 4; ++i ) {
938         if ( digit_tuner[i] != last_digit_tuner[i] ) {
939             diff = digit_tuner[i] - last_digit_tuner[i];
940             if ( abs(diff) > 4 ) {
941                 // roll over
942                 if ( digit_tuner[i] < last_digit_tuner[i] ) {
943                     // going up
944                     diff = 15 - last_digit_tuner[i] + digit_tuner[i];
945                 } else {
946                     // going down
947                     diff = digit_tuner[i] - 15 - last_digit_tuner[i];
948                 }
949             }
950             digit[i] += diff;
951         }
952         while ( digit[i] >= 8 ) { digit[i] -= 8; }
953         while ( digit[i] < 0 )  { digit[i] += 8; }
954         last_digit_tuner[i] = digit_tuner[i];
955     }
956
957     fgSetInt( "/radios/kt-70/inputs/digit1", digit[0] );
958     fgSetInt( "/radios/kt-70/inputs/digit2", digit[1] );
959     fgSetInt( "/radios/kt-70/inputs/digit3", digit[2] );
960     fgSetInt( "/radios/kt-70/inputs/digit4", digit[3] );
961
962     int tmp = 0;
963     for ( i = 0; i < 5; ++i ) {
964         if ( radio_switch_data[27] >> i & 0x01 ) {
965             tmp = i + 1;
966         }
967     }
968     fgSetInt( "/radios/kt-70/inputs/func-knob", tmp );
969     fgSetInt( "/radios/kt-70/inputs/ident-btn",
970               !(radio_switch_data[27] >> 5 & 0x01) );
971
972     return true;
973 }
974
975
976 /////////////////////////////////////////////////////////////////////
977 // Update the radio display 
978 /////////////////////////////////////////////////////////////////////
979
980 bool FGATC610x::do_radio_display() {
981
982     char digits[10];
983     int i;
984
985     if ( dme_switch != 0 ) {
986         // DME minutes
987         float minutes = dme_min->getFloatValue();
988         if ( minutes > 999 ) {
989             minutes = 999.0;
990         }
991         snprintf(digits, 7, "%03.0f", minutes);
992         for ( i = 0; i < 6; ++i ) {
993             digits[i] -= '0';
994         }
995         radio_display_data[0] = digits[1] << 4 | digits[2];
996         radio_display_data[1] = 0xf0 | digits[0];
997         
998         // DME knots
999         float knots = dme_kt->getFloatValue();
1000         if ( knots > 999 ) {
1001             knots = 999.0;
1002         }
1003         snprintf(digits, 7, "%03.0f", knots);
1004         for ( i = 0; i < 6; ++i ) {
1005             digits[i] -= '0';
1006         }
1007         radio_display_data[2] = digits[1] << 4 | digits[2];
1008         radio_display_data[3] = 0xf0 | digits[0];
1009
1010         // DME distance (nm)
1011         float nm = dme_nm->getFloatValue();
1012         if ( nm > 99 ) {
1013             nm = 99.0;
1014         }
1015         snprintf(digits, 7, "%04.1f", nm);
1016         for ( i = 0; i < 6; ++i ) {
1017             digits[i] -= '0';
1018         }
1019         radio_display_data[4] = digits[1] << 4 | digits[3];
1020         radio_display_data[5] = 0x00 | digits[0];
1021         // the 0x00 in the upper nibble of the 6th byte of each
1022         // display turns on the decimal point
1023     } else {
1024         // blank dem display
1025         for ( i = 0; i < 6; ++i ) {
1026             radio_display_data[i] = 0xff;
1027         }
1028     }
1029
1030     // Com1 standby frequency
1031     float com1_stby = com1_stby_freq->getFloatValue();
1032     if ( fabs(com1_stby) > 999.99 ) {
1033         com1_stby = 0.0;
1034     }
1035     snprintf(digits, 7, "%06.3f", com1_stby);
1036     for ( i = 0; i < 6; ++i ) {
1037         digits[i] -= '0';
1038     }
1039     radio_display_data[6] = digits[4] << 4 | digits[5];
1040     radio_display_data[7] = digits[1] << 4 | digits[2];
1041     radio_display_data[8] = 0xf0 | digits[0];
1042
1043     // Com1 in use frequency
1044     float com1 = com1_freq->getFloatValue();
1045     if ( fabs(com1) > 999.99 ) {
1046         com1 = 0.0;
1047     }
1048     snprintf(digits, 7, "%06.3f", com1);
1049     for ( i = 0; i < 6; ++i ) {
1050         digits[i] -= '0';
1051     }
1052     radio_display_data[9] = digits[4] << 4 | digits[5];
1053     radio_display_data[10] = digits[1] << 4 | digits[2];
1054     radio_display_data[11] = 0x00 | digits[0];
1055     // the 0x00 in the upper nibble of the 6th byte of each display
1056     // turns on the decimal point
1057
1058     // Com2 standby frequency
1059     float com2_stby = com2_stby_freq->getFloatValue();
1060     if ( fabs(com2_stby) > 999.99 ) {
1061         com2_stby = 0.0;
1062     }
1063     snprintf(digits, 7, "%06.3f", com2_stby);
1064     for ( i = 0; i < 6; ++i ) {
1065         digits[i] -= '0';
1066     }
1067     radio_display_data[18] = digits[4] << 4 | digits[5];
1068     radio_display_data[19] = digits[1] << 4 | digits[2];
1069     radio_display_data[20] = 0xf0 | digits[0];
1070
1071     // Com2 in use frequency
1072     float com2 = com2_freq->getFloatValue();
1073     if ( fabs(com2) > 999.99 ) {
1074         com2 = 0.0;
1075     }
1076     snprintf(digits, 7, "%06.3f", com2);
1077     for ( i = 0; i < 6; ++i ) {
1078         digits[i] -= '0';
1079     }
1080     radio_display_data[21] = digits[4] << 4 | digits[5];
1081     radio_display_data[22] = digits[1] << 4 | digits[2];
1082     radio_display_data[23] = 0x00 | digits[0];
1083     // the 0x00 in the upper nibble of the 6th byte of each display
1084     // turns on the decimal point
1085
1086     // Nav1 standby frequency
1087     float nav1_stby = nav1_stby_freq->getFloatValue();
1088     if ( fabs(nav1_stby) > 999.99 ) {
1089         nav1_stby = 0.0;
1090     }
1091     snprintf(digits, 7, "%06.2f", nav1_stby);
1092     for ( i = 0; i < 6; ++i ) {
1093         digits[i] -= '0';
1094     }
1095     radio_display_data[12] = digits[4] << 4 | digits[5];
1096     radio_display_data[13] = digits[1] << 4 | digits[2];
1097     radio_display_data[14] = 0xf0 | digits[0];
1098
1099     // Nav1 in use frequency
1100     float nav1 = nav1_freq->getFloatValue();
1101     if ( fabs(nav1) > 999.99 ) {
1102         nav1 = 0.0;
1103     }
1104     snprintf(digits, 7, "%06.2f", nav1);
1105     for ( i = 0; i < 6; ++i ) {
1106         digits[i] -= '0';
1107     }
1108     radio_display_data[15] = digits[4] << 4 | digits[5];
1109     radio_display_data[16] = digits[1] << 4 | digits[2];
1110     radio_display_data[17] = 0x00 | digits[0];
1111     // the 0x00 in the upper nibble of the 6th byte of each display
1112     // turns on the decimal point
1113
1114     // Nav2 standby frequency
1115     float nav2_stby = nav2_stby_freq->getFloatValue();
1116     if ( fabs(nav2_stby) > 999.99 ) {
1117         nav2_stby = 0.0;
1118     }
1119     snprintf(digits, 7, "%06.2f", nav2_stby);
1120     for ( i = 0; i < 6; ++i ) {
1121         digits[i] -= '0';
1122     }
1123     radio_display_data[24] = digits[4] << 4 | digits[5];
1124     radio_display_data[25] = digits[1] << 4 | digits[2];
1125     radio_display_data[26] = 0xf0 | digits[0];
1126
1127     // Nav2 in use frequency
1128     float nav2 = nav2_freq->getFloatValue();
1129     if ( fabs(nav2) > 999.99 ) {
1130         nav2 = 0.0;
1131     }
1132     snprintf(digits, 7, "%06.2f", nav2);
1133     for ( i = 0; i < 6; ++i ) {
1134         digits[i] -= '0';
1135     }
1136     radio_display_data[27] = digits[4] << 4 | digits[5];
1137     radio_display_data[28] = digits[1] << 4 | digits[2];
1138     radio_display_data[29] = 0x00 | digits[0];
1139     // the 0x00 in the upper nibble of the 6th byte of each display
1140     // turns on the decimal point
1141
1142     // ADF standby frequency / timer
1143     if ( adf_vol->getDoubleValue() >= 0.01 ) {
1144         if ( adf_stby_mode->getIntValue() == 0 ) {
1145             // frequency
1146             float adf_stby = adf_stby_freq->getFloatValue();
1147             if ( fabs(adf_stby) > 1799 ) {
1148                 adf_stby = 1799;
1149             }
1150             snprintf(digits, 7, "%04.0f", adf_stby);
1151             for ( i = 0; i < 6; ++i ) {
1152                 digits[i] -= '0';
1153             }
1154             radio_display_data[30] = digits[3] << 4 | 0x0f;
1155             radio_display_data[31] = digits[1] << 4 | digits[2];
1156             if ( digits[0] == 0 ) {
1157                 radio_display_data[32] = 0xff;
1158             } else {
1159                 radio_display_data[32] = 0xf0 | digits[0];
1160             }
1161         } else {
1162             // timer
1163             double time;
1164             int hours, min, sec;
1165             if ( adf_timer_mode->getIntValue() == 0 ) {
1166                 time = adf_flight_timer->getDoubleValue();
1167             } else {
1168                 time = adf_elapsed_timer->getDoubleValue();
1169             }
1170             // cout << time << endl;
1171             hours = (int)(time / 3600.0);
1172             time -= hours * 3600.00;
1173             min = (int)(time / 60.0);
1174             time -= min * 60.0;
1175             sec = (int)time;
1176             int big, little;
1177             if ( hours > 0 ) {
1178                 big = hours;
1179                 if ( big > 99 ) {
1180                     big = 99;
1181                 }
1182                 little = min;
1183             } else {
1184                 big = min;
1185                 little = sec;
1186             }
1187             if ( big > 99 ) {
1188                 big = 99;
1189             }
1190             // cout << big << ":" << little << endl;
1191             snprintf(digits, 7, "%02d%02d", big, little);
1192             for ( i = 0; i < 6; ++i ) {
1193                 digits[i] -= '0';
1194             }
1195             radio_display_data[30] = digits[3] << 4 | 0x0f;
1196             radio_display_data[31] = digits[1] << 4 | digits[2];
1197             radio_display_data[32] = 0xf0 | digits[0];
1198         }
1199
1200         // ADF in use frequency
1201         float adf = adf_freq->getFloatValue();
1202         if ( fabs(adf) > 1799 ) {
1203             adf = 1799;
1204         }
1205         snprintf(digits, 7, "%04.0f", adf);
1206         for ( i = 0; i < 6; ++i ) {
1207             digits[i] -= '0';
1208         }
1209         radio_display_data[33] = digits[2] << 4 | digits[3];
1210         if ( digits[0] == 0 ) {
1211             radio_display_data[34] = 0xf0 | digits[1];
1212         } else {
1213             radio_display_data[34] = digits[0] << 4 | digits[1];
1214         }
1215     } else {
1216         radio_display_data[30] = 0xff;
1217         radio_display_data[31] = 0xff;
1218         radio_display_data[32] = 0xff;
1219         radio_display_data[33] = 0xff;
1220         radio_display_data[34] = 0xff;
1221     }
1222     
1223     // Transponder code and flight level
1224     if ( xpdr_func_knob->getIntValue() == 2 ) {
1225         // test mode
1226         radio_display_data[36] = 8 << 4 | 8;
1227         radio_display_data[37] = 8 << 4 | 8;
1228         radio_display_data[38] = 0xff;
1229         radio_display_data[39] = 8 << 4 | 0x0f;
1230         radio_display_data[40] = 8 << 4 | 8;
1231     } else if ( xpdr_func_knob->getIntValue() > 0 ) {
1232         // other on modes
1233         int id_code = xpdr_id_code->getIntValue();
1234         int place = 1000;
1235         for ( i = 0; i < 4; ++i ) {
1236             digits[i] = id_code / place;
1237             id_code -= digits[i] * place;
1238             place /= 10;
1239         }
1240         radio_display_data[36] = digits[2] << 4 | digits[3];
1241         radio_display_data[37] = digits[0] << 4 | digits[1];
1242         radio_display_data[38] = 0xff;
1243
1244         if ( xpdr_func_knob->getIntValue() == 3 ||
1245              xpdr_func_knob->getIntValue() == 5 )
1246         {
1247             // do flight level display
1248             snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
1249             for ( i = 0; i < 6; ++i ) {
1250                 digits[i] -= '0';
1251             }
1252            radio_display_data[39] = digits[2] << 4 | 0x0f;
1253            radio_display_data[40] = digits[0] << 4 | digits[1];
1254         } else {
1255             // blank flight level display
1256             radio_display_data[39] = 0xff;
1257             radio_display_data[40] = 0xff;
1258         }
1259     } else {
1260         // off
1261         radio_display_data[36] = 0xff;
1262         radio_display_data[37] = 0xff;
1263         radio_display_data[38] = 0xff;
1264         radio_display_data[39] = 0xff;
1265         radio_display_data[40] = 0xff;
1266     }
1267
1268     ATC610xSetRadios( radios_fd, radio_display_data );
1269
1270     return true;
1271 }
1272
1273
1274 /////////////////////////////////////////////////////////////////////
1275 // Drive the stepper motors
1276 /////////////////////////////////////////////////////////////////////
1277
1278 bool FGATC610x::do_steppers() {
1279     float diff = mag_compass->getFloatValue() - compass_position;
1280     while ( diff < -180.0 ) { diff += 360.0; }
1281     while ( diff >  180.0 ) { diff -= 360.0; }
1282
1283     int steps = (int)(diff * 4);
1284     // cout << "steps = " << steps << endl;
1285     if ( steps > 4 ) { steps = 4; }
1286     if ( steps < -4 ) { steps = -4; }
1287
1288     if ( abs(steps) > 0 ) {
1289         unsigned char cmd = 0x80;       // stepper command
1290         if ( steps > 0 ) {
1291             cmd |= 0x20;                // go up
1292         } else {
1293             cmd |= 0x00;                // go down
1294         }
1295         cmd |= abs(steps);
1296
1297         // sync compass_position with hardware position
1298         compass_position += (float)steps / 4.0;
1299
1300         ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1301     }
1302
1303     return true;
1304 }
1305
1306
1307 /////////////////////////////////////////////////////////////////////
1308 // Read the switch positions
1309 /////////////////////////////////////////////////////////////////////
1310
1311 // decode the packed switch data
1312 static void update_switch_matrix(
1313         int board,
1314         unsigned char switch_data[ATC_SWITCH_BYTES],
1315         int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
1316 {
1317     for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
1318         unsigned char switches = switch_data[row];
1319
1320         for( int column = 0; column < ATC_NUM_COLS; ++column ) {
1321             switch_matrix[board][column][row] = switches & 1;
1322             switches = switches >> 1;
1323         }                       
1324     }
1325 }                     
1326
1327 bool FGATC610x::do_switches() {
1328     ATC610xReadSwitches( switches_fd, switch_data );
1329
1330     // unpack the switch data
1331     int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
1332     update_switch_matrix( board, switch_data, switch_matrix );
1333
1334     // magnetos and starter switch
1335     int magnetos = 0;
1336     bool starter = false;
1337     if ( switch_matrix[board][3][1] == 1 ) {
1338         magnetos = 3;
1339         starter = true;
1340     } else if ( switch_matrix[board][2][1] == 1 ) {
1341         magnetos = 3;
1342         starter = false;
1343     } else if ( switch_matrix[board][1][1] == 1 ) {
1344         magnetos = 2;
1345         starter = false;
1346     } else if ( switch_matrix[board][0][1] == 1 ) {
1347         magnetos = 1;
1348         starter = false;
1349     } else {
1350         magnetos = 0;
1351         starter = false;
1352     }
1353
1354     // do a bit of filtering on the magneto/starter switch and the
1355     // flap lever because these are not well debounced in hardware
1356     static int mag1, mag2, mag3;
1357     mag3 = mag2;
1358     mag2 = mag1;
1359     mag1 = magnetos;
1360     if ( mag1 == mag2 && mag2 == mag3 ) {
1361         fgSetInt( "/controls/magnetos[0]", magnetos );
1362     }
1363     static bool start1, start2, start3;
1364     start3 = start2;
1365     start2 = start1;
1366     start1 = starter;
1367     if ( start1 == start2 && start2 == start3 ) {
1368         fgSetBool( "/controls/starter[0]", starter );
1369     }
1370
1371     // flaps
1372     float flaps = 0.0;
1373     if ( switch_matrix[board][6][3] ) {
1374         flaps = 1.0;
1375     } else if ( switch_matrix[board][5][3] ) {
1376         flaps = 2.0 / 3.0;
1377     } else if ( switch_matrix[board][4][3] ) {
1378         flaps = 1.0 / 3.0;
1379     } else if ( !switch_matrix[board][4][3] ) {
1380         flaps = 0.0;
1381     }
1382
1383     // do a bit of filtering on the magneto/starter switch and the
1384     // flap lever because these are not well debounced in hardware
1385     static float flap1, flap2, flap3;
1386     flap3 = flap2;
1387     flap2 = flap1;
1388     flap1 = flaps;
1389     if ( flap1 == flap2 && flap2 == flap3 ) {
1390         fgSetFloat( "/controls/flaps", flaps );
1391     }
1392
1393     // fuel selector (also filtered)
1394     int fuel = 0;
1395     if ( switch_matrix[board][2][3] ) {
1396         // both
1397         fuel = 3;
1398     } else if ( switch_matrix[board][1][3] ) {
1399         // left
1400         fuel = 1;
1401     } else if ( switch_matrix[board][3][3] ) {
1402         // right
1403         fuel = 2;
1404     } else {
1405         // fuel cutoff
1406         fuel = 0;
1407     }
1408
1409     static int fuel1, fuel2, fuel3;
1410     fuel3 = fuel2;
1411     fuel2 = fuel1;
1412     fuel1 = fuel;
1413     if ( fuel1 == fuel2 && fuel2 == fuel3 ) {
1414         fgSetBool( "/controls/fuel-selector[0]", (fuel & 0x01) > 0 );
1415         fgSetBool( "/controls/fuel-selector[1]", (fuel & 0x02) > 0 );
1416     }
1417
1418     return true;
1419 }
1420
1421
1422 bool FGATC610x::process() {
1423     // Lock the hardware, skip if it's not ready yet
1424     if ( ATC610xLock( lock_fd ) > 0 ) {
1425
1426         do_analog_in();
1427         do_lights();
1428         do_radio_switches();
1429         do_radio_display();
1430         do_steppers();
1431         do_switches();
1432         
1433         ATC610xRelease( lock_fd );
1434
1435         return true;
1436     } else {
1437         return false;
1438     }
1439 }
1440
1441
1442 bool FGATC610x::close() {
1443
1444     return true;
1445 }