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