]> git.mxchange.org Git - flightgear.git/blob - src/Network/atc610x.cxx
Moved random ground cover object management code (userdata.[ch]xx) over
[flightgear.git] / src / Network / atc610x.cxx
1 // atc610x.cxx -- FGFS interface to ATC 610x hardware
2 //
3 // Written by Curtis Olson, started January 2002
4 //
5 // Copyright (C) 2002  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <stdlib.h>             // atoi() atof() abs()
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <stdio.h>              //snprintf
35 #if defined( _MSC_VER ) || defined(__MINGW32__)
36 #  include <io.h>                 //lseek, read, write
37 #endif
38
39 #include STL_STRING
40
41 #include <plib/ul.h>
42
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/io/iochannel.hxx>
45 #include <simgear/math/sg_types.hxx>
46 #include <simgear/misc/sg_path.hxx>
47 #include <simgear/props/props.hxx>
48
49 #include <Main/fg_props.hxx>
50 #include <Main/globals.hxx>
51
52 #include "atc610x.hxx"
53
54 SG_USING_STD(string);
55
56
57 // Lock the ATC 610 hardware
58 static int ATC610xLock( int fd ) {
59     // rewind
60     lseek( fd, 0, SEEK_SET );
61
62     char tmp[2];
63     int result = read( fd, tmp, 1 );
64     if ( result != 1 ) {
65         SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
66     }
67
68     return result;
69 }
70
71
72 // Write a radios command
73 static int ATC610xRelease( int fd ) {
74     // rewind
75     lseek( fd, 0, SEEK_SET );
76
77     char tmp[2];
78     tmp[0] = tmp[1] = 0;
79     int result = write( fd, tmp, 1 );
80
81     if ( result != 1 ) {
82         SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
83     }
84
85     return result;
86 }
87
88
89 // Read analog inputs
90 static void ATC610xReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
91     // rewind
92     lseek( fd, 0, SEEK_SET );
93
94     int result = read( fd, analog_in_bytes, ATC_ANAL_IN_BYTES );
95     if ( result != ATC_ANAL_IN_BYTES ) {
96         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
97         exit( -1 );
98     }
99 }
100
101
102 // Write a radios command
103 static int ATC610xSetRadios( int fd,
104                              unsigned char data[ATC_RADIO_DISPLAY_BYTES] )
105 {
106     // rewind
107     lseek( fd, 0, SEEK_SET );
108
109     int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
110
111     if ( result != ATC_RADIO_DISPLAY_BYTES ) {
112         SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
113     }
114
115     return result;
116 }
117
118
119 // Read status of last radios written to
120 static void ATC610xReadRadios( int fd, unsigned char *switch_data ) {
121     // rewind
122     lseek( fd, 0, SEEK_SET );
123
124     int result = read( fd, switch_data, ATC_RADIO_SWITCH_BYTES );
125     if ( result != ATC_RADIO_SWITCH_BYTES ) {
126         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
127         exit( -1 );
128     }
129 }
130
131 // Write a stepper command
132 static int ATC610xSetStepper( int fd, unsigned char channel,
133                               unsigned char value )
134 {
135     // rewind
136     lseek( fd, 0, SEEK_SET );
137
138     // Write the value
139     unsigned char buf[3];
140     buf[0] = channel;
141     buf[1] = value;
142     buf[2] = 0;
143     int result = write( fd, buf, 2 );
144     if ( result != 2 ) {
145         SG_LOG( SG_IO, SG_INFO, "Write failed" );
146     }
147     SG_LOG( SG_IO, SG_DEBUG,
148             "Sent cmd = " << (int)channel << " value = " << (int)value );
149     return result;
150 }
151
152
153 // Read status of last stepper written to
154 static unsigned char ATC610xReadStepper( int fd ) {
155     int result;
156
157     // rewind
158     lseek( fd, 0, SEEK_SET );
159
160     // Write the value
161     unsigned char buf[2];
162     result = read( fd, buf, 1 );
163     if ( result != 1 ) {
164         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
165         exit( -1 );
166     }
167     SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
168
169     return buf[0];
170 }
171
172
173 // Read switch inputs
174 static void ATC610xReadSwitches( int fd, unsigned char *switch_bytes ) {
175     // rewind
176     lseek( fd, 0, SEEK_SET );
177
178     int result = read( fd, switch_bytes, ATC_SWITCH_BYTES );
179     if ( result != ATC_SWITCH_BYTES ) {
180         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
181         exit( -1 );
182     }
183 }
184
185
186 // Turn a lamp on or off
187 void ATC610xSetLamp( int fd, int channel, bool value ) {
188     // lamp channels 0-63 are written to LampPort0, channels 64-127
189     // are written to LampPort1
190
191     // bits 0-6 are the lamp address
192     // bit 7 is the value (on/off)
193
194     int result;
195
196     // Write the value
197     unsigned char buf[3];
198     buf[0] = channel;
199     buf[1] = value;
200     buf[2] = 0;
201     result = write( fd, buf, 2 );
202     if ( result != 2 ) {
203         SG_LOG( SG_IO, SG_ALERT,  "Write failed" );
204         exit( -1 );
205     }
206 }
207
208
209 void FGATC610x::init_config() {
210 #if defined( unix ) || defined( __CYGWIN__ )
211     // Next check home directory for .fgfsrc.hostname file
212     char *envp = ::getenv( "HOME" );
213     if ( envp != NULL ) {
214         SGPath atc610x_config( envp );
215         atc610x_config.append( ".fgfs-atc610x.xml" );
216         readProperties( atc610x_config.str(), globals->get_props() );
217     }
218 #endif
219 }
220
221
222 // Open and initialize ATC 610x hardware
223 bool FGATC610x::open() {
224     if ( is_enabled() ) {
225         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
226                 << "is already in use, ignoring" );
227         return false;
228     }
229
230     // This loads the config parameters generated by "simcal"
231     init_config();
232
233     SG_LOG( SG_IO, SG_ALERT,
234             "Initializing ATC 610x hardware, please wait ..." );
235
236     set_hz( 30 );               // default to processing requests @ 30Hz
237     set_enabled( true );
238
239     board = 0;                  // 610x uses a single board number = 0
240
241     snprintf( lock_file, 256, "/proc/atc610x/board%d/lock", board );
242     snprintf( analog_in_file, 256, "/proc/atc610x/board%d/analog_in", board );
243     snprintf( lamps_file, 256, "/proc/atc610x/board%d/lamps", board );
244     snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
245     snprintf( stepper_file, 256, "/proc/atc610x/board%d/steppers", board );
246     snprintf( switches_file, 256, "/proc/atc610x/board%d/switches", board );
247
248     /////////////////////////////////////////////////////////////////////
249     // Open the /proc files
250     /////////////////////////////////////////////////////////////////////
251
252     lock_fd = ::open( lock_file, O_RDWR );
253     if ( lock_fd == -1 ) {
254         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
255         char msg[256];
256         snprintf( msg, 256, "Error opening %s", lock_file );
257         perror( msg );
258         exit( -1 );
259     }
260
261     analog_in_fd = ::open( analog_in_file, O_RDONLY );
262     if ( analog_in_fd == -1 ) {
263         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
264         char msg[256];
265         snprintf( msg, 256, "Error opening %s", analog_in_file );
266         perror( msg );
267         exit( -1 );
268     }
269
270     lamps_fd = ::open( lamps_file, O_WRONLY );
271     if ( lamps_fd == -1 ) {
272         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
273         char msg[256];
274         snprintf( msg, 256, "Error opening %s", lamps_file );
275         perror( msg );
276         exit( -1 );
277     }
278
279     radios_fd = ::open( radios_file, O_RDWR );
280     if ( radios_fd == -1 ) {
281         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
282         char msg[256];
283         snprintf( msg, 256, "Error opening %s", radios_file );
284         perror( msg );
285         exit( -1 );
286     }
287
288     stepper_fd = ::open( stepper_file, O_RDWR );
289     if ( stepper_fd == -1 ) {
290         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
291         char msg[256];
292         snprintf( msg, 256, "Error opening %s", stepper_file );
293         perror( msg );
294         exit( -1 );
295     }
296
297     switches_fd = ::open( switches_file, O_RDONLY );
298     if ( switches_fd == -1 ) {
299         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
300         char msg[256];
301         snprintf( msg, 256, "Error opening %s", switches_file );
302         perror( msg );
303         exit( -1 );
304     }
305
306     /////////////////////////////////////////////////////////////////////
307     // Home the compass stepper motor
308     /////////////////////////////////////////////////////////////////////
309
310     SG_LOG( SG_IO, SG_ALERT,
311             "  - Homing the compass stepper motor" );
312
313     // Lock the hardware, keep trying until we succeed
314     while ( ATC610xLock( lock_fd ) <= 0 );
315
316     // Send the stepper home command
317     ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
318
319     // Release the hardware
320     ATC610xRelease( lock_fd );
321
322     SG_LOG( SG_IO, SG_ALERT,
323             "  - Waiting for compass to come home." );
324
325     bool home = false;
326     int timeout = 900;          // about 30 seconds
327     timeout = 0;
328     while ( ! home && timeout > 0 ) {
329         if ( timeout % 150 == 0 ) {
330             SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
331         } else {
332             SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
333         }
334
335         while ( ATC610xLock( lock_fd ) <= 0 );
336
337         unsigned char result = ATC610xReadStepper( stepper_fd );
338         if ( result == 0 ) {
339             home = true;
340         }
341
342         ATC610xRelease( lock_fd );
343
344 #if defined( _MSC_VER )
345         ulMilliSecondSleep(33);
346 #elif defined (WIN32) && !defined(__CYGWIN__)
347         Sleep (33);
348 #else
349         usleep(33);
350 #endif
351
352         --timeout;
353     }
354
355     compass_position = 0.0;
356
357     /////////////////////////////////////////////////////////////////////
358     // Blank the radio display
359     /////////////////////////////////////////////////////////////////////
360
361     SG_LOG( SG_IO, SG_ALERT,
362             "  - Clearing the radios displays." );
363
364     // Prepair the data
365     unsigned char value = 0xff;
366     for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
367         radio_display_data[channel] = value;
368     }
369
370     // Lock the hardware, keep trying until we succeed
371     while ( ATC610xLock( lock_fd ) <= 0 );
372
373     // Set radio display
374     ATC610xSetRadios( radios_fd, radio_display_data );
375
376     ATC610xRelease( lock_fd );
377
378     /////////////////////////////////////////////////////////////////////
379     // Blank the lamps
380     /////////////////////////////////////////////////////////////////////
381
382     for ( int i = 0; i < 128; ++i ) {
383         ATC610xSetLamp( lamps_fd, i, false );
384     }
385
386     /////////////////////////////////////////////////////////////////////
387     // Finished initing hardware
388     /////////////////////////////////////////////////////////////////////
389
390     SG_LOG( SG_IO, SG_ALERT,
391             "Done initializing ATC 610x hardware." );
392
393     /////////////////////////////////////////////////////////////////////
394     // Connect up to property values
395     /////////////////////////////////////////////////////////////////////
396
397     mag_compass = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg", true );
398
399     dme_min = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
400     dme_kt = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt",
401                         true );
402     dme_nm = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
403     dme_in_range = fgGetNode( "/instrumentation/dme/in-range", true );
404
405     adf_bus_power = fgGetNode( "/systems/electrical/outputs/adf", true );
406     dme_bus_power = fgGetNode( "/systems/electrical/outputs/dme", true );
407     navcom1_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[0]",
408                                    true );
409     navcom2_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[1]",
410                                    true );
411     xpdr_bus_power = fgGetNode( "/systems/electrical/outputs/transponder",
412                                  true );
413
414     navcom1_power_btn = fgGetNode( "/radios/comm[0]/inputs/power-btn", true );
415     navcom2_power_btn = fgGetNode( "/radios/comm[1]/inputs/power-btn", true );
416
417     com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
418     com1_stby_freq
419         = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
420
421     com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
422     com2_stby_freq
423         = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
424
425     nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
426     nav1_stby_freq
427         = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
428     nav1_obs = fgGetNode( "/radios/nav[0]/radials/selected-deg", true );
429
430     nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
431     nav2_stby_freq
432         = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
433     nav2_obs = fgGetNode( "/radios/nav[1]/radials/selected-deg", true );
434
435     adf_power_btn = fgGetNode( "/radios/kr-87/inputs/power-btn", true );
436     adf_vol = fgGetNode( "/radios/kr-87/inputs/volume", true );
437     adf_adf_btn = fgGetNode( "/radios/kr-87/inputs/adf-btn", true );
438     adf_bfo_btn = fgGetNode( "/radios/kr-87/inputs/bfo-btn", true );
439     adf_freq = fgGetNode( "/radios/kr-87/outputs/selected-khz", true );
440     adf_stby_freq = fgGetNode( "/radios/kr-87/outputs/standby-khz", true );
441     adf_stby_mode = fgGetNode( "/radios/kr-87/modes/stby", true );
442     adf_timer_mode = fgGetNode( "/radios/kr-87/modes/timer", true );
443     adf_count_mode = fgGetNode( "/radios/kr-87/modes/count", true );
444     adf_flight_timer = fgGetNode( "/radios/kr-87/outputs/flight-timer", true );
445     adf_elapsed_timer = fgGetNode( "/radios/kr-87/outputs/elapsed-timer",
446                                    true );
447     adf_ant_ann = fgGetNode( "/radios/kr-87/annunciators/ant", true );
448     adf_adf_ann = fgGetNode( "/radios/kr-87/annunciators/adf", true );
449     adf_bfo_ann = fgGetNode( "/radios/kr-87/annunciators/bfo", true );
450     adf_frq_ann = fgGetNode( "/radios/kr-87/annunciators/frq", true );
451     adf_flt_ann = fgGetNode( "/radios/kr-87/annunciators/flt", true );
452     adf_et_ann = fgGetNode( "/radios/kr-87/annunciators/et", true );
453
454     inner = fgGetNode( "/radios/marker-beacon/inner", true );
455     middle = fgGetNode( "/radios/marker-beacon/middle", true );
456     outer = fgGetNode( "/radios/marker-beacon/outer", true );
457
458     xpdr_ident_btn = fgGetNode( "/radios/kt-70/inputs/ident-btn", true );
459     xpdr_digit1 = fgGetNode( "/radios/kt-70/inputs/digit1", true );
460     xpdr_digit2 = fgGetNode( "/radios/kt-70/inputs/digit2", true );
461     xpdr_digit3 = fgGetNode( "/radios/kt-70/inputs/digit3", true );
462     xpdr_digit4 = fgGetNode( "/radios/kt-70/inputs/digit4", true );
463     xpdr_func_knob = fgGetNode( "/radios/kt-70/inputs/func-knob", true );
464     xpdr_id_code = fgGetNode( "/radios/kt-70/outputs/id-code", true );
465     xpdr_flight_level = fgGetNode( "/radios/kt-70/outputs/flight-level", true );
466     xpdr_fl_ann = fgGetNode( "/radios/kt-70/annunciators/fl", true );
467     xpdr_alt_ann = fgGetNode( "/radios/kt-70/annunciators/alt", true );
468     xpdr_gnd_ann = fgGetNode( "/radios/kt-70/annunciators/gnd", true );
469     xpdr_on_ann = fgGetNode( "/radios/kt-70/annunciators/on", true );
470     xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
471     xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
472
473     ati_bird
474       = fgGetNode( "/instrumentation/attitude-indicator/horizon-offset-deg",
475                    true );
476     alt_press = fgGetNode( "/instrumentation/altimeter/setting-inhg", true );
477     adf_hdg = fgGetNode( "/radios/kr-87/inputs/rotation-deg", true );
478
479     elevator_center = fgGetNode( "/input/atc610x/elevator/center", true );
480     elevator_min = fgGetNode( "/input/atc610x/elevator/min", true );
481     elevator_max = fgGetNode( "/input/atc610x/elevator/max", true );
482
483     ailerons_center = fgGetNode( "/input/atc610x/ailerons/center", true );
484     ailerons_min = fgGetNode( "/input/atc610x/ailerons/min", true );
485     ailerons_max = fgGetNode( "/input/atc610x/ailerons/max", true );
486
487     rudder_center = fgGetNode( "/input/atc610x/rudder/center", true );
488     rudder_min = fgGetNode( "/input/atc610x/rudder/min", true );
489     rudder_max = fgGetNode( "/input/atc610x/rudder/max", true );
490
491     brake_left_min = fgGetNode( "/input/atc610x/brake-left/min", true );
492     brake_left_max = fgGetNode( "/input/atc610x/brake-left/max", true );
493
494     brake_right_min = fgGetNode( "/input/atc610x/brake-right/min", true );
495     brake_right_max = fgGetNode( "/input/atc610x/brake-right/max", true );
496
497     throttle_min = fgGetNode( "/input/atc610x/throttle/min", true );
498     throttle_max = fgGetNode( "/input/atc610x/throttle/max", true );
499
500     mixture_min = fgGetNode( "/input/atc610x/mixture/min", true );
501     mixture_max = fgGetNode( "/input/atc610x/mixture/max", true );
502
503     trim_center = fgGetNode( "/input/atc610x/trim/center", true );
504     trim_min = fgGetNode( "/input/atc610x/trim/min", true );
505     trim_max = fgGetNode( "/input/atc610x/trim/max", true );
506
507     nav1vol_min = fgGetNode( "/input/atc610x/nav1vol/min", true );
508     nav1vol_max = fgGetNode( "/input/atc610x/nav1vol/max", true );
509
510     nav2vol_min = fgGetNode( "/input/atc610x/nav2vol/min", true );
511     nav2vol_max = fgGetNode( "/input/atc610x/nav2vol/max", true );
512
513     comm1_servicable = fgGetNode( "/instrumentation/comm[0]/servicable", true );
514     comm2_servicable = fgGetNode( "/instrumentation/comm[1]/servicable", true );
515     nav1_servicable = fgGetNode( "/instrumentation/nav[0]/servicable", true );
516     nav2_servicable = fgGetNode( "/instrumentation/nav[1]/servicable", true );
517     adf_servicable = fgGetNode( "/instrumentation/adf/servicable", true );
518     xpdr_servicable = fgGetNode( "/instrumentation/transponder/servicable",
519                                  true );
520     dme_servicable = fgGetNode( "/instrumentation/dme/servicable", true );
521
522     // default to having everything servicable
523     comm1_servicable->setBoolValue( true );
524     comm2_servicable->setBoolValue( true );
525     nav1_servicable->setBoolValue( true );
526     nav2_servicable->setBoolValue( true );
527     adf_servicable->setBoolValue( true );
528     xpdr_servicable->setBoolValue( true );
529     dme_servicable->setBoolValue( true );
530
531     return true;
532 }
533
534
535 /////////////////////////////////////////////////////////////////////
536 // Read analog inputs
537 /////////////////////////////////////////////////////////////////////
538
539 // scale a number between min and max (with center defined) to a scale
540 // from -1.0 to 1.0
541 static double scale( int center, int min, int max, int value ) {
542     // cout << center << " " << min << " " << max << " " << value << " ";
543     double result;
544     double range;
545
546     if ( value <= center ) {
547         range = center - min;
548         result = (value - center) / range;
549     } else {
550         range = max - center;
551         result = (value - center) / range;            
552     }
553
554     if ( result < -1.0 ) result = -1.0;
555     if ( result > 1.0 ) result = 1.0;
556
557     // cout << result << endl;
558
559     return result;
560 }
561
562
563 // scale a number between min and max to a scale from 0.0 to 1.0
564 static double scale( int min, int max, int value ) {
565     // cout << center << " " << min << " " << max << " " << value << " ";
566     double result;
567     double range;
568
569     range = max - min;
570     result = (value - min) / range;
571
572     if ( result < 0.0 ) result = 0.0;
573     if ( result > 1.0 ) result = 1.0;
574
575     // cout << result << endl;
576
577     return result;
578 }
579
580
581 static int tony_magic( int raw, int obs[3] ) {
582     int result = 0;
583
584     obs[0] = raw;
585
586     if ( obs[1] < 30 ) {
587         if ( obs[2] >= 68 && obs[2] < 480 ) {
588             result = -6;
589         } else if ( obs[2] >= 480 ) {
590             result = 6;
591         }
592         obs[2] = obs[1];
593         obs[1] = obs[0];
594     } else if ( obs[1] < 68 ) {
595         // do nothing
596         obs[1] = obs[0];
597     } else if ( obs[2] < 30 ) {
598         if ( obs[1] >= 68 && obs[1] < 480 ) {
599             result = 6;
600             obs[2] = obs[1];
601             obs[1] = obs[0];
602         } else if ( obs[1] >= 480 ) {
603             result = -6;
604             if ( obs[0] < obs[1] ) {
605                 obs[2] = obs[1];
606                 obs[1] = obs[0];
607             } else {
608                 obs[2] = obs[0];
609                 obs[1] = obs[0];
610             }
611         }
612     } else if ( obs[1] > 980 ) {
613         if ( obs[2] <= 956 && obs[2] > 480 ) {
614             result = 6;
615         } else if ( obs[2] <= 480 ) {
616             result = -6;
617         }
618         obs[2] = obs[1];
619         obs[1] = obs[0];
620     } else if ( obs[1] > 956 ) {
621         // do nothing
622         obs[1] = obs[0];
623     } else if ( obs[2] > 980 ) {
624         if ( obs[1] <= 956 && obs[1] > 480 ) {
625             result = -6;
626             obs[2] = obs[1];
627             obs[1] = obs[0];
628         } else if ( obs[1] <= 480 ) {
629             result = 6;
630             if ( obs[0] > obs[1] ) {
631                 obs[2] = obs[1];
632                 obs[1] = obs[0];
633             } else {
634                 obs[2] = obs[0];
635                 obs[1] = obs[0];
636             }
637         }
638     } else {
639         if ( obs[1] < 480 && obs[2] > 480 ) {
640             // crossed gap going up
641             if ( obs[0] < obs[1] ) {
642                 // caught a bogus intermediate value coming out of the gap
643                 obs[1] = obs[0];
644             }
645         } else if ( obs[1] > 480 && obs[2] < 480 ) {
646             // crossed gap going down
647             if ( obs[0] > obs[1] ) {
648                 // caught a bogus intermediate value coming out of the gap
649               obs[1] = obs[0];
650             }
651         } else if ( obs[0] > 480 && obs[1] < 480 && obs[2] < 480 ) {
652             // crossed the gap going down
653             if ( obs[1] > obs[2] ) {
654                 // caught a bogus intermediate value coming out of the gap
655                 obs[1] = obs[2];
656             }
657         } else if ( obs[0] < 480 && obs[1] > 480 && obs[2] > 480 ) {
658             // crossed the gap going up
659             if ( obs[1] < obs[2] ) {
660                 // caught a bogus intermediate value coming out of the gap
661                 obs[1] = obs[2];
662             }
663         }
664         result = obs[1] - obs[2];
665         if ( abs(result) > 400 ) {
666             // ignore
667             result = 0;
668         }
669         obs[2] = obs[1];
670         obs[1] = obs[0];
671     }
672
673     // cout << " result = " << result << endl;
674     if ( result < -500 ) { result += 1024; }
675     if ( result > 500 ) { result -= 1024; }
676
677     return result;
678 }
679
680
681 static double instr_pot_filter( double ave, double val ) {
682     if ( fabs(ave - val) < 400 || fabs(val) < fabs(ave) ) {
683         return 0.5 * ave + 0.5 * val;
684     } else {
685         return ave;
686     }
687 }
688
689
690 bool FGATC610x::do_analog_in() {
691     // Read raw data in byte form
692     ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
693
694     // Convert to integer values
695     for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
696         unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
697         unsigned char lo = analog_in_bytes[2 * channel + 1];
698         analog_in_data[channel] = hi * 256 + lo;
699
700         // printf("%02x %02x ", hi, lo );
701         // printf("%04d ", value );
702     }
703
704     float tmp;
705
706     // aileron
707     tmp = scale( ailerons_center->getIntValue(), ailerons_min->getIntValue(),
708                  ailerons_max->getIntValue(), analog_in_data[0] );
709     fgSetFloat( "/controls/flight/aileron", tmp );
710     // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
711     // elevator
712     tmp = -scale( elevator_center->getIntValue(), elevator_min->getIntValue(),
713                   elevator_max->getIntValue(), analog_in_data[5] );
714     fgSetFloat( "/controls/flight/elevator", tmp * 0.5 );
715     // cout << "trim = " << analog_in_data[4] << " = " << tmp;
716
717     // elevator trim
718     tmp = scale( trim_center->getIntValue(), trim_min->getIntValue(),
719                  trim_max->getIntValue(), analog_in_data[4] );
720     fgSetFloat( "/controls/flight/elevator-trim", tmp );
721     // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
722
723     // mixture
724     tmp = scale( mixture_min->getIntValue(), mixture_max->getIntValue(),
725                  analog_in_data[6] );
726     fgSetFloat( "/controls/engines/engine[0]/mixture", tmp );
727     fgSetFloat( "/controls/engines/engine[1]/mixture", tmp );
728
729     // throttle
730     tmp = scale( throttle_min->getIntValue(), throttle_max->getIntValue(),
731                  analog_in_data[8] );
732     fgSetFloat( "/controls/engines/engine[0]/throttle", tmp );
733     fgSetFloat( "/controls/engines/engine[1]/throttle", tmp );
734     // cout << "throttle = " << tmp << endl;
735
736     if ( use_rudder ) {
737         // rudder
738         tmp = scale( rudder_center->getIntValue(), rudder_min->getIntValue(),
739                      rudder_max->getIntValue(), analog_in_data[10] );
740         fgSetFloat( "/controls/flight/rudder", -tmp );
741
742         // toe brakes
743         tmp = scale( brake_left_min->getIntValue(),
744                      brake_left_max->getIntValue(),
745                      analog_in_data[20] );
746         fgSetFloat( "/controls/gear/wheel[0]/brake", tmp );
747         tmp = scale( brake_right_min->getIntValue(),
748                      brake_right_max->getIntValue(),
749                      analog_in_data[21] );
750         fgSetFloat( "/controls/gear/wheel[1]/brake", tmp );
751     }
752
753     // nav1 volume
754     tmp = (float)analog_in_data[25] / 1024.0f;
755     fgSetFloat( "/radios/nav[0]/volume", tmp );
756
757     // nav2 volume
758     tmp = (float)analog_in_data[24] / 1024.0f;
759     fgSetFloat( "/radios/nav[1]/volume", tmp );
760
761     // adf volume
762     tmp = (float)analog_in_data[26] / 1024.0f;
763     fgSetFloat( "/radios/kr-87/inputs/volume", tmp );
764
765     // instrument panel pots
766     static bool first = true;
767     static int obs1[3], obs2[3], obs3[3], obs4[3], obs5[3];
768     static double diff1_ave = 0.0;
769     static double diff2_ave = 0.0;
770     static double diff3_ave = 0.0;
771     static double diff4_ave = 0.0;
772     static double diff5_ave = 0.0;
773
774     if ( first ) {
775         first = false;
776         obs1[0] = obs1[1] = obs1[2] = analog_in_data[11];
777         obs2[0] = obs2[1] = obs2[2] = analog_in_data[28];
778         obs3[0] = obs3[1] = obs3[2] = analog_in_data[29];
779         obs4[0] = obs4[1] = obs4[2] = analog_in_data[30];
780         obs5[0] = obs5[1] = obs5[2] = analog_in_data[31];
781     }
782
783     int diff1 = tony_magic( analog_in_data[11], obs1 );
784     int diff2 = tony_magic( analog_in_data[28], obs2 );
785     int diff3 = tony_magic( analog_in_data[29], obs3 );
786     int diff4 = tony_magic( analog_in_data[30], obs4 );
787     int diff5 = tony_magic( analog_in_data[31], obs5 );
788
789     diff1_ave = instr_pot_filter( diff1_ave, diff1 );
790     diff2_ave = instr_pot_filter( diff2_ave, diff2 );
791     diff3_ave = instr_pot_filter( diff3_ave, diff3 );
792     diff4_ave = instr_pot_filter( diff4_ave, diff4 );
793     diff5_ave = instr_pot_filter( diff5_ave, diff5 );
794
795     tmp = alt_press->getDoubleValue() + (diff1_ave * (0.25/888.0) );
796     if ( tmp < 27.9 ) { tmp = 27.9; }
797     if ( tmp > 31.4 ) { tmp = 31.4; }
798     fgSetFloat( "/instrumentation/altimeter/setting-inhg", tmp );
799
800     tmp = ati_bird->getDoubleValue() + (diff2_ave * (20.0/888.0) );
801     if ( tmp < -10.0 ) { tmp = -10.0; }
802     if ( tmp > 10.0 ) { tmp = 10.0; }
803     fgSetFloat( "/instrumentation/attitude-indicator/horizon-offset-deg", tmp );
804
805     tmp = nav1_obs->getDoubleValue() + (diff3_ave * (72.0/888.0) );
806     while ( tmp >= 360.0 ) { tmp -= 360.0; }
807     while ( tmp < 0.0 ) { tmp += 360.0; }
808     // cout << " obs = " << tmp << endl;
809     fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp );
810
811     tmp = nav2_obs->getDoubleValue() + (diff4_ave * (72.0/888.0) );
812     while ( tmp >= 360.0 ) { tmp -= 360.0; }
813     while ( tmp < 0.0 ) { tmp += 360.0; }
814     // cout << " obs = " << tmp << endl;
815     fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
816
817     tmp = adf_hdg->getDoubleValue() + (diff5_ave * (72.0/888.0) );
818     while ( tmp >= 360.0 ) { tmp -= 360.0; }
819     while ( tmp < 0.0 ) { tmp += 360.0; }
820     // cout << " obs = " << tmp << endl;
821     fgSetFloat( "/radios/kr-87/inputs/rotation-deg", tmp );
822
823     return true;
824 }
825
826
827 /////////////////////////////////////////////////////////////////////
828 // Write the lights
829 /////////////////////////////////////////////////////////////////////
830
831 bool FGATC610x::do_lights() {
832
833     // Marker beacons
834     ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
835     ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
836     ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
837
838     // ADF annunciators
839     ATC610xSetLamp( lamps_fd, 11, adf_ant_ann->getBoolValue() ); // ANT
840     ATC610xSetLamp( lamps_fd, 12, adf_adf_ann->getBoolValue() ); // ADF
841     ATC610xSetLamp( lamps_fd, 13, adf_bfo_ann->getBoolValue() ); // BFO
842     ATC610xSetLamp( lamps_fd, 14, adf_frq_ann->getBoolValue() ); // FRQ
843     ATC610xSetLamp( lamps_fd, 15, adf_flt_ann->getBoolValue() ); // FLT
844     ATC610xSetLamp( lamps_fd, 16, adf_et_ann->getBoolValue() ); // ET
845
846     // Transponder annunciators
847     ATC610xSetLamp( lamps_fd, 17, xpdr_fl_ann->getBoolValue() ); // FL
848     ATC610xSetLamp( lamps_fd, 18, xpdr_alt_ann->getBoolValue() ); // ALT
849     ATC610xSetLamp( lamps_fd, 19, xpdr_gnd_ann->getBoolValue() ); // GND
850     ATC610xSetLamp( lamps_fd, 20, xpdr_on_ann->getBoolValue() ); // ON
851     ATC610xSetLamp( lamps_fd, 21, xpdr_sby_ann->getBoolValue() ); // SBY
852     ATC610xSetLamp( lamps_fd, 22, xpdr_reply_ann->getBoolValue() ); // R
853
854     return true;
855 }
856
857
858 /////////////////////////////////////////////////////////////////////
859 // Read radio switches 
860 /////////////////////////////////////////////////////////////////////
861
862 bool FGATC610x::do_radio_switches() {
863     double freq, coarse_freq, fine_freq, value;
864     int diff;
865
866     ATC610xReadRadios( radios_fd, radio_switch_data );
867
868     // DME Switch
869     dme_switch = (radio_switch_data[7] >> 4) & 0x03;
870     if ( dme_switch == 0 ) {
871         // off
872         fgSetInt( "/instrumentation/dme/switch-position", 0 );
873     } else if ( dme_switch == 2 ) {
874         // nav1
875         fgSetInt( "/instrumentation/dme/switch-position", 1 );
876         fgSetString( "/instrumentation/dme/frequencies/source",
877                      "/radios/nav[0]/frequencies/selected-mhz" );
878         freq = fgGetFloat( "/radios/nav[0]/frequencies/selected-mhz", true );
879         fgSetFloat( "/instrumentation/dme/frequencies/selected-mhz", freq );
880     } else if ( dme_switch == 1 ) {
881         // nav2
882         fgSetInt( "/instrumentation/dme/switch-position", 3 );
883         fgSetString( "/instrumentation/dme/frequencies/source",
884                      "/radios/nav[1]/frequencies/selected-mhz" );
885         freq = fgGetFloat( "/radios/nav[1]/frequencies/selected-mhz", true );
886         fgSetFloat( "/instrumentation/dme/frequencies/selected-mhz", freq );
887     }
888
889     // NavCom1 Power
890     fgSetBool( "/radios/comm[0]/inputs/power-btn",
891                radio_switch_data[7] & 0x01 );
892
893     if ( navcom1_has_power() && comm1_servicable->getBoolValue() ) {
894         // Com1 Swap
895         int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
896         static int last_com1_swap;
897         if ( com1_swap && (last_com1_swap != com1_swap) ) {
898             float tmp = com1_freq->getFloatValue();
899             fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
900                         com1_stby_freq->getFloatValue() );
901             fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
902         }
903         last_com1_swap = com1_swap;
904     }
905
906     // NavCom2 Power
907     fgSetBool( "/radios/comm[1]/inputs/power-btn",
908                radio_switch_data[15] & 0x01 );
909
910     if ( navcom2_has_power() && comm2_servicable->getBoolValue() ) {
911         // Com2 Swap
912         int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
913         static int last_com2_swap;
914         if ( com2_swap && (last_com2_swap != com2_swap) ) {
915             float tmp = com2_freq->getFloatValue();
916             fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
917                         com2_stby_freq->getFloatValue() );
918             fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
919         }
920         last_com2_swap = com2_swap;
921     }
922
923     if ( navcom1_has_power() && nav1_servicable->getBoolValue() ) {
924         // Nav1 Swap
925         int nav1_swap = radio_switch_data[11] & 0x01;
926         static int last_nav1_swap;
927         if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
928             float tmp = nav1_freq->getFloatValue();
929             fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
930                         nav1_stby_freq->getFloatValue() );
931             fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
932         }
933         last_nav1_swap = nav1_swap;
934     }
935
936     if ( navcom2_has_power() && nav2_servicable->getBoolValue() ) {
937         // Nav2 Swap
938         int nav2_swap = !(radio_switch_data[19] & 0x01);
939         static int last_nav2_swap;
940         if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
941             float tmp = nav2_freq->getFloatValue();
942             fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
943                         nav2_stby_freq->getFloatValue() );
944             fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
945         }
946         last_nav2_swap = nav2_swap;
947     }
948
949     if ( navcom1_has_power() && comm1_servicable->getBoolValue() ) {
950         // Com1 Tuner
951         int com1_tuner_fine = ((radio_switch_data[5] >> 4) & 0x0f) - 1;
952         int com1_tuner_coarse = (radio_switch_data[5] & 0x0f) - 1;
953         static int last_com1_tuner_fine = com1_tuner_fine;
954         static int last_com1_tuner_coarse = com1_tuner_coarse;
955
956         freq = com1_stby_freq->getFloatValue();
957         coarse_freq = (int)freq;
958         fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
959
960         if ( com1_tuner_fine != last_com1_tuner_fine ) {
961             diff = com1_tuner_fine - last_com1_tuner_fine;
962             if ( abs(diff) > 4 ) {
963                 // roll over
964                 if ( com1_tuner_fine < last_com1_tuner_fine ) {
965                     // going up
966                     diff = 12 - last_com1_tuner_fine + com1_tuner_fine;
967                 } else {
968                     // going down
969                     diff = com1_tuner_fine - 12 - last_com1_tuner_fine;
970                 }
971             }
972             fine_freq += diff;
973         }
974         while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
975         while ( fine_freq < 0.0 )  { fine_freq += 40.0; }
976
977         if ( com1_tuner_coarse != last_com1_tuner_coarse ) {
978             diff = com1_tuner_coarse - last_com1_tuner_coarse;
979             if ( abs(diff) > 4 ) {
980                 // roll over
981                 if ( com1_tuner_coarse < last_com1_tuner_coarse ) {
982                     // going up
983                     diff = 12 - last_com1_tuner_coarse + com1_tuner_coarse;
984                 } else {
985                     // going down
986                     diff = com1_tuner_coarse - 12 - last_com1_tuner_coarse;
987                 }
988             }
989             coarse_freq += diff;
990         }
991         if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
992         if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
993
994         last_com1_tuner_fine = com1_tuner_fine;
995         last_com1_tuner_coarse = com1_tuner_coarse;
996
997         fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", 
998                     coarse_freq + fine_freq / 40.0 );
999     }
1000
1001     if ( navcom2_has_power() && comm2_servicable->getBoolValue() ) {
1002         // Com2 Tuner
1003         int com2_tuner_fine = ((radio_switch_data[13] >> 4) & 0x0f) - 1;
1004         int com2_tuner_coarse = (radio_switch_data[13] & 0x0f) - 1;
1005         static int last_com2_tuner_fine = com2_tuner_fine;
1006         static int last_com2_tuner_coarse = com2_tuner_coarse;
1007
1008         freq = com2_stby_freq->getFloatValue();
1009         coarse_freq = (int)freq;
1010         fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
1011
1012         if ( com2_tuner_fine != last_com2_tuner_fine ) {
1013             diff = com2_tuner_fine - last_com2_tuner_fine;
1014             if ( abs(diff) > 4 ) {
1015                 // roll over
1016                 if ( com2_tuner_fine < last_com2_tuner_fine ) {
1017                     // going up
1018                     diff = 12 - last_com2_tuner_fine + com2_tuner_fine;
1019                 } else {
1020                     // going down
1021                     diff = com2_tuner_fine - 12 - last_com2_tuner_fine;
1022                 }
1023             }
1024             fine_freq += diff;
1025         }
1026         while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
1027         while ( fine_freq < 0.0 )  { fine_freq += 40.0; }
1028
1029         if ( com2_tuner_coarse != last_com2_tuner_coarse ) {
1030             diff = com2_tuner_coarse - last_com2_tuner_coarse;
1031             if ( abs(diff) > 4 ) {
1032                 // roll over
1033                 if ( com2_tuner_coarse < last_com2_tuner_coarse ) {
1034                     // going up
1035                     diff = 12 - last_com2_tuner_coarse + com2_tuner_coarse;
1036                 } else {
1037                     // going down
1038                     diff = com2_tuner_coarse - 12 - last_com2_tuner_coarse;
1039                 }
1040             }
1041             coarse_freq += diff;
1042         }
1043         if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
1044         if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
1045
1046         last_com2_tuner_fine = com2_tuner_fine;
1047         last_com2_tuner_coarse = com2_tuner_coarse;
1048
1049         fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz",
1050                     coarse_freq + fine_freq / 40.0 );
1051     }
1052
1053     if ( navcom1_has_power() && nav1_servicable->getBoolValue() ) {
1054         // Nav1 Tuner
1055         int nav1_tuner_fine = ((radio_switch_data[9] >> 4) & 0x0f) - 1;
1056         int nav1_tuner_coarse = (radio_switch_data[9] & 0x0f) - 1;
1057         static int last_nav1_tuner_fine = nav1_tuner_fine;
1058         static int last_nav1_tuner_coarse = nav1_tuner_coarse;
1059
1060         freq = nav1_stby_freq->getFloatValue();
1061         coarse_freq = (int)freq;
1062         fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
1063
1064         if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
1065             diff = nav1_tuner_fine - last_nav1_tuner_fine;
1066             if ( abs(diff) > 4 ) {
1067                 // roll over
1068                 if ( nav1_tuner_fine < last_nav1_tuner_fine ) {
1069                     // going up
1070                     diff = 12 - last_nav1_tuner_fine + nav1_tuner_fine;
1071                 } else {
1072                     // going down
1073                     diff = nav1_tuner_fine - 12 - last_nav1_tuner_fine;
1074                 }
1075             }
1076             fine_freq += diff;
1077         }
1078         while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
1079         while ( fine_freq < 0.0 )  { fine_freq += 20.0; }
1080
1081         if ( nav1_tuner_coarse != last_nav1_tuner_coarse ) {
1082             diff = nav1_tuner_coarse - last_nav1_tuner_coarse;
1083             if ( abs(diff) > 4 ) {
1084                 // roll over
1085                 if ( nav1_tuner_coarse < last_nav1_tuner_coarse ) {
1086                     // going up
1087                     diff = 12 - last_nav1_tuner_coarse + nav1_tuner_coarse;
1088                 } else {
1089                     // going down
1090                     diff = nav1_tuner_coarse - 12 - last_nav1_tuner_coarse;
1091                 }
1092             }
1093             coarse_freq += diff;
1094         }
1095         if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
1096         if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
1097
1098         last_nav1_tuner_fine = nav1_tuner_fine;
1099         last_nav1_tuner_coarse = nav1_tuner_coarse;
1100
1101         fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz",
1102                     coarse_freq + fine_freq / 20.0 );
1103     }
1104
1105     if ( navcom2_has_power() && nav2_servicable->getBoolValue() ) {
1106         // Nav2 Tuner
1107         int nav2_tuner_fine = ((radio_switch_data[17] >> 4) & 0x0f) - 1;
1108         int nav2_tuner_coarse = (radio_switch_data[17] & 0x0f) - 1;
1109         static int last_nav2_tuner_fine = nav2_tuner_fine;
1110         static int last_nav2_tuner_coarse = nav2_tuner_coarse;
1111
1112         freq = nav2_stby_freq->getFloatValue();
1113         coarse_freq = (int)freq;
1114         fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
1115
1116         if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
1117             diff = nav2_tuner_fine - last_nav2_tuner_fine;
1118             if ( abs(diff) > 4 ) {
1119                 // roll over
1120                 if ( nav2_tuner_fine < last_nav2_tuner_fine ) {
1121                     // going up
1122                     diff = 12 - last_nav2_tuner_fine + nav2_tuner_fine;
1123                 } else {
1124                     // going down
1125                     diff = nav2_tuner_fine - 12 - last_nav2_tuner_fine;
1126                 }
1127             }
1128             fine_freq += diff;
1129         }
1130         while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
1131         while ( fine_freq < 0.0 )  { fine_freq += 20.0; }
1132
1133         if ( nav2_tuner_coarse != last_nav2_tuner_coarse ) {
1134             diff = nav2_tuner_coarse - last_nav2_tuner_coarse;
1135             if ( abs(diff) > 4 ) {
1136                 // roll over
1137                 if ( nav2_tuner_coarse < last_nav2_tuner_coarse ) {
1138                     // going up
1139                     diff = 12 - last_nav2_tuner_coarse + nav2_tuner_coarse;
1140                 } else {
1141                     // going down
1142                     diff = nav2_tuner_coarse - 12 - last_nav2_tuner_coarse;
1143                 }
1144             }
1145             coarse_freq += diff;
1146         }
1147         if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
1148         if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
1149
1150         last_nav2_tuner_fine = nav2_tuner_fine;
1151         last_nav2_tuner_coarse = nav2_tuner_coarse;
1152
1153         fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", 
1154                     coarse_freq + fine_freq / 20.0);
1155     }
1156
1157     // ADF Tuner
1158     
1159     int adf_tuner_fine = ((radio_switch_data[21] >> 4) & 0x0f) - 1;
1160     int adf_tuner_coarse = (radio_switch_data[21] & 0x0f) - 1;
1161     static int last_adf_tuner_fine = adf_tuner_fine;
1162     static int last_adf_tuner_coarse = adf_tuner_coarse;
1163
1164     if ( adf_has_power() && adf_servicable->getBoolValue() ) {
1165         // cout << "adf_stby_mode = " << adf_stby_mode->getIntValue() << endl;
1166         if ( adf_count_mode->getIntValue() == 2 ) {
1167             // tune count down timer
1168             value = adf_elapsed_timer->getDoubleValue();
1169         } else {
1170             // tune frequency
1171             if ( adf_stby_mode->getIntValue() == 1 ) {
1172                 value = adf_freq->getFloatValue();
1173             } else {
1174                 value = adf_stby_freq->getFloatValue();
1175             }
1176         }
1177
1178         if ( adf_tuner_fine != last_adf_tuner_fine ) {
1179             diff = adf_tuner_fine - last_adf_tuner_fine;
1180             if ( abs(diff) > 4 ) {
1181                 // roll over
1182                 if ( adf_tuner_fine < last_adf_tuner_fine ) {
1183                     // going up
1184                     diff = 12 - last_adf_tuner_fine + adf_tuner_fine;
1185                 } else {
1186                     // going down
1187                     diff = adf_tuner_fine - 12 - last_adf_tuner_fine;
1188                 }
1189             }
1190             value += diff;
1191         }
1192
1193         if ( adf_tuner_coarse != last_adf_tuner_coarse ) {
1194             diff = adf_tuner_coarse - last_adf_tuner_coarse;
1195             if ( abs(diff) > 4 ) {
1196                 // roll over
1197                 if ( adf_tuner_coarse < last_adf_tuner_coarse ) {
1198                     // going up
1199                     diff = 12 - last_adf_tuner_coarse + adf_tuner_coarse;
1200                 } else {
1201                     // going down
1202                     diff = adf_tuner_coarse - 12 - last_adf_tuner_coarse;
1203                 }
1204             }
1205             if ( adf_count_mode->getIntValue() == 2 ) {
1206                 value += 60 * diff;
1207             } else {
1208                 value += 25 * diff;
1209             }
1210         }
1211         if ( adf_count_mode->getIntValue() == 2 ) {
1212             if ( value < 0 ) { value += 3600; }
1213             if ( value > 3599 ) { value -= 3600; }
1214         } else {
1215             if ( value < 200 ) { value += 1600; }
1216             if ( value > 1799 ) { value -= 1600; }
1217         }
1218  
1219         if ( adf_count_mode->getIntValue() == 2 ) {
1220             fgSetFloat( "/radios/kr-87/outputs/elapsed-timer", value );
1221         } else {
1222             if ( adf_stby_mode->getIntValue() == 1 ) {
1223                 fgSetFloat( "/radios/kr-87/outputs/selected-khz", value );
1224             } else {
1225                 fgSetFloat( "/radios/kr-87/outputs/standby-khz", value );
1226             }
1227         }
1228     }
1229     last_adf_tuner_fine = adf_tuner_fine;
1230     last_adf_tuner_coarse = adf_tuner_coarse;
1231
1232
1233     // ADF buttons 
1234     fgSetInt( "/radios/kr-87/inputs/adf-btn",
1235               !(radio_switch_data[23] & 0x01) );
1236     fgSetInt( "/radios/kr-87/inputs/bfo-btn",
1237               !(radio_switch_data[23] >> 1 & 0x01) );
1238     fgSetInt( "/radios/kr-87/inputs/frq-btn",
1239               !(radio_switch_data[23] >> 2 & 0x01) );
1240     fgSetInt( "/radios/kr-87/inputs/flt-et-btn",
1241               !(radio_switch_data[23] >> 3 & 0x01) );
1242     fgSetInt( "/radios/kr-87/inputs/set-rst-btn",
1243               !(radio_switch_data[23] >> 4 & 0x01) );
1244     fgSetInt( "/radios/kr-87/inputs/power-btn",
1245               radio_switch_data[23] >> 5 & 0x01 );
1246     /* cout << "adf = " << !(radio_switch_data[23] & 0x01)
1247          << " bfo = " << !(radio_switch_data[23] >> 1 & 0x01)
1248          << " frq = " << !(radio_switch_data[23] >> 2 & 0x01)
1249          << " flt/et = " << !(radio_switch_data[23] >> 3 & 0x01)
1250          << " set/rst = " << !(radio_switch_data[23] >> 4 & 0x01)
1251          << endl; */
1252
1253     // Transponder Tuner
1254     int i;
1255     int digit_tuner[4];
1256     digit_tuner[0] = radio_switch_data[25] & 0x0f;
1257     digit_tuner[1] = ( radio_switch_data[25] >> 4 ) & 0x0f;
1258     digit_tuner[2] = radio_switch_data[29] & 0x0f;
1259     digit_tuner[3] = ( radio_switch_data[29] >> 4 ) & 0x0f;
1260
1261     static int last_digit_tuner[4];
1262     static bool first_time = true;
1263     if ( first_time ) {
1264         first_time = false;
1265         for ( i = 0; i < 4; ++i ) {
1266             last_digit_tuner[i] = digit_tuner[i];
1267         }
1268     }
1269
1270     if ( xpdr_has_power() && xpdr_servicable->getBoolValue() ) {
1271         int id_code = xpdr_id_code->getIntValue();
1272         int digit[4];
1273         int place = 1000;
1274         for ( i = 0; i < 4; ++i ) {
1275             digit[i] = id_code / place;
1276             id_code -= digit[i] * place;
1277             place /= 10;
1278         }
1279
1280         for ( i = 0; i < 4; ++i ) {
1281             if ( digit_tuner[i] != last_digit_tuner[i] ) {
1282                 diff = digit_tuner[i] - last_digit_tuner[i];
1283                 if ( abs(diff) > 4 ) {
1284                     // roll over
1285                     if ( digit_tuner[i] < last_digit_tuner[i] ) {
1286                         // going up
1287                         diff = 15 - last_digit_tuner[i] + digit_tuner[i];
1288                     } else {
1289                         // going down
1290                         diff = digit_tuner[i] - 15 - last_digit_tuner[i];
1291                     }
1292                 }
1293                 digit[i] += diff;
1294             }
1295             while ( digit[i] >= 8 ) { digit[i] -= 8; }
1296             while ( digit[i] < 0 )  { digit[i] += 8; }
1297         }
1298
1299         fgSetInt( "/radios/kt-70/inputs/digit1", digit[0] );
1300         fgSetInt( "/radios/kt-70/inputs/digit2", digit[1] );
1301         fgSetInt( "/radios/kt-70/inputs/digit3", digit[2] );
1302         fgSetInt( "/radios/kt-70/inputs/digit4", digit[3] );
1303     }
1304     for ( i = 0; i < 4; ++i ) {
1305         last_digit_tuner[i] = digit_tuner[i];
1306     }
1307
1308     int tmp = 0;
1309     for ( i = 0; i < 5; ++i ) {
1310         if ( radio_switch_data[27] >> i & 0x01 ) {
1311             tmp = i + 1;
1312         }
1313     }
1314     fgSetInt( "/radios/kt-70/inputs/func-knob", tmp );
1315     fgSetInt( "/radios/kt-70/inputs/ident-btn",
1316               !(radio_switch_data[27] >> 5 & 0x01) );
1317
1318     // Audio panel switches
1319     fgSetInt( "/radios/nav[0]/audio-btn",
1320               (radio_switch_data[3] & 0x01) );
1321     fgSetInt( "/radios/nav[1]/audio-btn",
1322               (radio_switch_data[3] >> 2 & 0x01) );
1323     fgSetInt( "/radios/kr-87/inputs/audio-btn",
1324               (radio_switch_data[3] >> 4 & 0x01) );
1325     fgSetInt( "/radios/marker-beacon/audio-btn",
1326               (radio_switch_data[3] >> 6 & 0x01) );
1327
1328     return true;
1329 }
1330
1331
1332 /////////////////////////////////////////////////////////////////////
1333 // Update the radio display 
1334 /////////////////////////////////////////////////////////////////////
1335
1336 bool FGATC610x::do_radio_display() {
1337
1338     char digits[10];
1339     int i;
1340
1341     if ( dme_has_power() && dme_servicable->getBoolValue() ) {
1342         if ( dme_in_range->getBoolValue() ) {
1343             // DME minutes
1344             float minutes = dme_min->getFloatValue();
1345             if ( minutes > 999 ) {
1346                 minutes = 999.0;
1347             }
1348             snprintf(digits, 7, "%03.0f", minutes);
1349             for ( i = 0; i < 6; ++i ) {
1350                 digits[i] -= '0';
1351             }
1352             radio_display_data[0] = digits[1] << 4 | digits[2];
1353             radio_display_data[1] = 0xf0 | digits[0];
1354         
1355             // DME knots
1356             float knots = dme_kt->getFloatValue();
1357             if ( knots > 999 ) {
1358                 knots = 999.0;
1359             }
1360             snprintf(digits, 7, "%03.0f", knots);
1361             for ( i = 0; i < 6; ++i ) {
1362                 digits[i] -= '0';
1363             }
1364             radio_display_data[2] = digits[1] << 4 | digits[2];
1365             radio_display_data[3] = 0xf0 | digits[0];
1366
1367             // DME distance (nm)
1368             float nm = dme_nm->getFloatValue();
1369             if ( nm > 99 ) {
1370                 nm = 99.0;
1371             }
1372             snprintf(digits, 7, "%04.1f", nm);
1373             for ( i = 0; i < 6; ++i ) {
1374                 digits[i] -= '0';
1375             }
1376             radio_display_data[4] = digits[1] << 4 | digits[3];
1377             radio_display_data[5] = 0x00 | digits[0];
1378             // the 0x00 in the upper nibble of the 6th byte of each
1379             // display turns on the decimal point
1380         } else {
1381             // out of range
1382             radio_display_data[0] = 0xbb;
1383             radio_display_data[1] = 0xfb;
1384             radio_display_data[2] = 0xbb;
1385             radio_display_data[3] = 0xfb;
1386             radio_display_data[4] = 0xbb;
1387             radio_display_data[5] = 0x0b;
1388         }
1389     } else {
1390         // blank dem display
1391         for ( i = 0; i < 6; ++i ) {
1392             radio_display_data[i] = 0xff;
1393         }
1394     }
1395
1396     if ( navcom1_has_power() && comm1_servicable->getBoolValue() ) {
1397         // Com1 standby frequency
1398         float com1_stby = com1_stby_freq->getFloatValue();
1399         if ( fabs(com1_stby) > 999.99 ) {
1400             com1_stby = 0.0;
1401         }
1402         snprintf(digits, 7, "%06.3f", com1_stby);
1403         for ( i = 0; i < 6; ++i ) {
1404             digits[i] -= '0';
1405         }
1406         radio_display_data[6] = digits[4] << 4 | digits[5];
1407         radio_display_data[7] = digits[1] << 4 | digits[2];
1408         radio_display_data[8] = 0xf0 | digits[0];
1409
1410         // Com1 in use frequency
1411         float com1 = com1_freq->getFloatValue();
1412         if ( fabs(com1) > 999.99 ) {
1413             com1 = 0.0;
1414         }
1415         snprintf(digits, 7, "%06.3f", com1);
1416         for ( i = 0; i < 6; ++i ) {
1417             digits[i] -= '0';
1418         }
1419         radio_display_data[9] = digits[4] << 4 | digits[5];
1420         radio_display_data[10] = digits[1] << 4 | digits[2];
1421         radio_display_data[11] = 0x00 | digits[0];
1422         // the 0x00 in the upper nibble of the 6th byte of each display
1423         // turns on the decimal point
1424     } else {
1425         radio_display_data[6] = 0xff;
1426         radio_display_data[7] = 0xff;
1427         radio_display_data[8] = 0xff;
1428         radio_display_data[9] = 0xff;
1429         radio_display_data[10] = 0xff;
1430         radio_display_data[11] = 0xff;
1431     }
1432
1433     if ( navcom2_has_power() && comm2_servicable->getBoolValue() ) {
1434         // Com2 standby frequency
1435         float com2_stby = com2_stby_freq->getFloatValue();
1436         if ( fabs(com2_stby) > 999.99 ) {
1437             com2_stby = 0.0;
1438         }
1439         snprintf(digits, 7, "%06.3f", com2_stby);
1440         for ( i = 0; i < 6; ++i ) {
1441             digits[i] -= '0';
1442         }
1443         radio_display_data[18] = digits[4] << 4 | digits[5];
1444         radio_display_data[19] = digits[1] << 4 | digits[2];
1445         radio_display_data[20] = 0xf0 | digits[0];
1446
1447         // Com2 in use frequency
1448         float com2 = com2_freq->getFloatValue();
1449         if ( fabs(com2) > 999.99 ) {
1450         com2 = 0.0;
1451         }
1452         snprintf(digits, 7, "%06.3f", com2);
1453         for ( i = 0; i < 6; ++i ) {
1454             digits[i] -= '0';
1455         }
1456         radio_display_data[21] = digits[4] << 4 | digits[5];
1457         radio_display_data[22] = digits[1] << 4 | digits[2];
1458         radio_display_data[23] = 0x00 | digits[0];
1459         // the 0x00 in the upper nibble of the 6th byte of each display
1460         // turns on the decimal point
1461     } else {
1462         radio_display_data[18] = 0xff;
1463         radio_display_data[19] = 0xff;
1464         radio_display_data[20] = 0xff;
1465         radio_display_data[21] = 0xff;
1466         radio_display_data[22] = 0xff;
1467         radio_display_data[23] = 0xff;
1468     }
1469
1470     if ( navcom1_has_power() && nav1_servicable->getBoolValue() ) {
1471         // Nav1 standby frequency
1472         float nav1_stby = nav1_stby_freq->getFloatValue();
1473         if ( fabs(nav1_stby) > 999.99 ) {
1474         nav1_stby = 0.0;
1475         }
1476         snprintf(digits, 7, "%06.2f", nav1_stby);
1477         for ( i = 0; i < 6; ++i ) {
1478             digits[i] -= '0';
1479         }
1480         radio_display_data[12] = digits[4] << 4 | digits[5];
1481         radio_display_data[13] = digits[1] << 4 | digits[2];
1482         radio_display_data[14] = 0xf0 | digits[0];
1483
1484         // Nav1 in use frequency
1485         float nav1 = nav1_freq->getFloatValue();
1486         if ( fabs(nav1) > 999.99 ) {
1487             nav1 = 0.0;
1488         }
1489         snprintf(digits, 7, "%06.2f", nav1);
1490         for ( i = 0; i < 6; ++i ) {
1491             digits[i] -= '0';
1492         }
1493         radio_display_data[15] = digits[4] << 4 | digits[5];
1494         radio_display_data[16] = digits[1] << 4 | digits[2];
1495         radio_display_data[17] = 0x00 | digits[0];
1496         // the 0x00 in the upper nibble of the 6th byte of each display
1497         // turns on the decimal point
1498     } else {
1499         radio_display_data[12] = 0xff;
1500         radio_display_data[13] = 0xff;
1501         radio_display_data[14] = 0xff;
1502         radio_display_data[15] = 0xff;
1503         radio_display_data[16] = 0xff;
1504         radio_display_data[17] = 0xff;
1505     }
1506
1507     if ( navcom2_has_power() && nav2_servicable->getBoolValue() ) {
1508         // Nav2 standby frequency
1509         float nav2_stby = nav2_stby_freq->getFloatValue();
1510         if ( fabs(nav2_stby) > 999.99 ) {
1511             nav2_stby = 0.0;
1512         }
1513         snprintf(digits, 7, "%06.2f", nav2_stby);
1514         for ( i = 0; i < 6; ++i ) {
1515             digits[i] -= '0';
1516         }
1517         radio_display_data[24] = digits[4] << 4 | digits[5];
1518         radio_display_data[25] = digits[1] << 4 | digits[2];
1519         radio_display_data[26] = 0xf0 | digits[0];
1520
1521         // Nav2 in use frequency
1522         float nav2 = nav2_freq->getFloatValue();
1523         if ( fabs(nav2) > 999.99 ) {
1524             nav2 = 0.0;
1525         }
1526         snprintf(digits, 7, "%06.2f", nav2);
1527         for ( i = 0; i < 6; ++i ) {
1528             digits[i] -= '0';
1529         }
1530         radio_display_data[27] = digits[4] << 4 | digits[5];
1531         radio_display_data[28] = digits[1] << 4 | digits[2];
1532         radio_display_data[29] = 0x00 | digits[0];
1533         // the 0x00 in the upper nibble of the 6th byte of each display
1534         // turns on the decimal point
1535     } else {
1536         radio_display_data[24] = 0xff;
1537         radio_display_data[25] = 0xff;
1538         radio_display_data[26] = 0xff;
1539         radio_display_data[27] = 0xff;
1540         radio_display_data[28] = 0xff;
1541         radio_display_data[29] = 0xff;
1542     }
1543
1544     // ADF standby frequency / timer
1545     if ( adf_has_power() && adf_servicable->getBoolValue() ) {
1546         if ( adf_stby_mode->getIntValue() == 0 ) {
1547             // frequency
1548             float adf_stby = adf_stby_freq->getFloatValue();
1549             if ( fabs(adf_stby) > 1799 ) {
1550                 adf_stby = 1799;
1551             }
1552             snprintf(digits, 7, "%04.0f", adf_stby);
1553             for ( i = 0; i < 6; ++i ) {
1554                 digits[i] -= '0';
1555             }
1556             radio_display_data[30] = digits[3] << 4 | 0x0f;
1557             radio_display_data[31] = digits[1] << 4 | digits[2];
1558             if ( digits[0] == 0 ) {
1559                 radio_display_data[32] = 0xff;
1560             } else {
1561                 radio_display_data[32] = 0xf0 | digits[0];
1562             }
1563         } else {
1564             // timer
1565             double time;
1566             int hours, min, sec;
1567             if ( adf_timer_mode->getIntValue() == 0 ) {
1568                 time = adf_flight_timer->getDoubleValue();
1569             } else {
1570                 time = adf_elapsed_timer->getDoubleValue();
1571             }
1572             // cout << time << endl;
1573             hours = (int)(time / 3600.0);
1574             time -= hours * 3600.00;
1575             min = (int)(time / 60.0);
1576             time -= min * 60.0;
1577             sec = (int)time;
1578             int big, little;
1579             if ( hours > 0 ) {
1580                 big = hours;
1581                 if ( big > 99 ) {
1582                     big = 99;
1583                 }
1584                 little = min;
1585             } else {
1586                 big = min;
1587                 little = sec;
1588             }
1589             if ( big > 99 ) {
1590                 big = 99;
1591             }
1592             // cout << big << ":" << little << endl;
1593             snprintf(digits, 7, "%02d%02d", big, little);
1594             for ( i = 0; i < 6; ++i ) {
1595                 digits[i] -= '0';
1596             }
1597             radio_display_data[30] = digits[2] << 4 | digits[3];
1598             radio_display_data[31] = digits[0] << 4 | digits[1];
1599             radio_display_data[32] = 0xff;
1600         }
1601
1602         // ADF in use frequency
1603         float adf = adf_freq->getFloatValue();
1604         if ( fabs(adf) > 1799 ) {
1605             adf = 1799;
1606         }
1607         snprintf(digits, 7, "%04.0f", adf);
1608         for ( i = 0; i < 6; ++i ) {
1609             digits[i] -= '0';
1610         }
1611         radio_display_data[33] = digits[2] << 4 | digits[3];
1612         if ( digits[0] == 0 ) {
1613             radio_display_data[34] = 0xf0 | digits[1];
1614         } else {
1615             radio_display_data[34] = digits[0] << 4 | digits[1];
1616         }
1617         if ( adf_stby_mode->getIntValue() == 0 ) {
1618           radio_display_data[35] = 0xff;
1619         } else {
1620           radio_display_data[35] = 0x0f;
1621         }
1622     } else {
1623         radio_display_data[30] = 0xff;
1624         radio_display_data[31] = 0xff;
1625         radio_display_data[32] = 0xff;
1626         radio_display_data[33] = 0xff;
1627         radio_display_data[34] = 0xff;
1628         radio_display_data[35] = 0xff;
1629     }
1630     
1631     // Transponder code and flight level
1632     if ( xpdr_has_power() && xpdr_servicable->getBoolValue() ) {
1633         if ( xpdr_func_knob->getIntValue() == 2 ) {
1634             // test mode
1635             radio_display_data[36] = 8 << 4 | 8;
1636             radio_display_data[37] = 8 << 4 | 8;
1637             radio_display_data[38] = 0xff;
1638             radio_display_data[39] = 8 << 4 | 0x0f;
1639             radio_display_data[40] = 8 << 4 | 8;
1640         } else {
1641             // other on modes
1642             int id_code = xpdr_id_code->getIntValue();
1643             int place = 1000;
1644             for ( i = 0; i < 4; ++i ) {
1645                 digits[i] = id_code / place;
1646                 id_code -= digits[i] * place;
1647                 place /= 10;
1648             }
1649             radio_display_data[36] = digits[2] << 4 | digits[3];
1650             radio_display_data[37] = digits[0] << 4 | digits[1];
1651             radio_display_data[38] = 0xff;
1652
1653             if ( xpdr_func_knob->getIntValue() == 3 ||
1654                  xpdr_func_knob->getIntValue() == 5 )
1655             {
1656                 // do flight level display
1657                 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
1658                 for ( i = 0; i < 6; ++i ) {
1659                     digits[i] -= '0';
1660                 }
1661                 radio_display_data[39] = digits[2] << 4 | 0x0f;
1662                 radio_display_data[40] = digits[0] << 4 | digits[1];
1663             } else {
1664                 // blank flight level display
1665                 radio_display_data[39] = 0xff;
1666                 radio_display_data[40] = 0xff;
1667             }
1668         }
1669     } else {
1670         // off
1671         radio_display_data[36] = 0xff;
1672         radio_display_data[37] = 0xff;
1673         radio_display_data[38] = 0xff;
1674         radio_display_data[39] = 0xff;
1675         radio_display_data[40] = 0xff;
1676     }
1677
1678     ATC610xSetRadios( radios_fd, radio_display_data );
1679
1680     return true;
1681 }
1682
1683
1684 /////////////////////////////////////////////////////////////////////
1685 // Drive the stepper motors
1686 /////////////////////////////////////////////////////////////////////
1687
1688 bool FGATC610x::do_steppers() {
1689     float diff = mag_compass->getFloatValue() - compass_position;
1690     while ( diff < -180.0 ) { diff += 360.0; }
1691     while ( diff >  180.0 ) { diff -= 360.0; }
1692
1693     int steps = (int)(diff * 4);
1694     // cout << "steps = " << steps << endl;
1695     if ( steps > 4 ) { steps = 4; }
1696     if ( steps < -4 ) { steps = -4; }
1697
1698     if ( abs(steps) > 0 ) {
1699         unsigned char cmd = 0x80;       // stepper command
1700         if ( steps > 0 ) {
1701             cmd |= 0x20;                // go up
1702         } else {
1703             cmd |= 0x00;                // go down
1704         }
1705         cmd |= abs(steps);
1706
1707         // sync compass_position with hardware position
1708         compass_position += (float)steps / 4.0;
1709
1710         ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1711     }
1712
1713     return true;
1714 }
1715
1716
1717 /////////////////////////////////////////////////////////////////////
1718 // Read the switch positions
1719 /////////////////////////////////////////////////////////////////////
1720
1721 // decode the packed switch data
1722 static void update_switch_matrix(
1723         int board,
1724         unsigned char switch_data[ATC_SWITCH_BYTES],
1725         int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
1726 {
1727     for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
1728         unsigned char switches = switch_data[row];
1729
1730         for( int column = 0; column < ATC_NUM_COLS; ++column ) {
1731             switch_matrix[board][column][row] = switches & 1;
1732             switches = switches >> 1;
1733         }                       
1734     }
1735 }                     
1736
1737 bool FGATC610x::do_switches() {
1738     ATC610xReadSwitches( switches_fd, switch_data );
1739
1740     // unpack the switch data
1741     int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
1742     update_switch_matrix( board, switch_data, switch_matrix );
1743
1744     // master switches
1745     fgSetBool( "/controls/switches/master-bat", switch_matrix[board][5][1] );
1746     fgSetBool( "/controls/switches/master-alt", switch_matrix[board][4][1] );
1747     fgSetBool( "/controls/switches/master-avionics",
1748                switch_matrix[board][0][3] );
1749
1750     // magnetos and starter switch
1751     int magnetos = 0;
1752     bool starter = false;
1753     if ( switch_matrix[board][3][1] == 1 ) {
1754         magnetos = 3;
1755         starter = true;
1756     } else if ( switch_matrix[board][2][1] == 1 ) {
1757         magnetos = 3;
1758         starter = false;
1759     } else if ( switch_matrix[board][1][1] == 1 ) {
1760         magnetos = 2;
1761         starter = false;
1762     } else if ( switch_matrix[board][0][1] == 1 ) {
1763         magnetos = 1;
1764         starter = false;
1765     } else {
1766         magnetos = 0;
1767         starter = false;
1768     }
1769
1770     // do a bit of filtering on the magneto/starter switch and the
1771     // flap lever because these are not well debounced in hardware
1772     static int mag1, mag2, mag3;
1773     mag3 = mag2;
1774     mag2 = mag1;
1775     mag1 = magnetos;
1776     if ( mag1 == mag2 && mag2 == mag3 ) {
1777         fgSetInt( "/controls/engines/engine[0]/magnetos", magnetos );
1778     }
1779     static bool start1, start2, start3;
1780     start3 = start2;
1781     start2 = start1;
1782     start1 = starter;
1783     if ( start1 == start2 && start2 == start3 ) {
1784         fgSetBool( "/controls/engines/engine[0]/starter", starter );
1785     }
1786
1787     // other toggle switches
1788     fgSetBool( "/controls/engines/engine[0]/fuel-pump",
1789                switch_matrix[board][0][2] );
1790     fgSetBool( "/controls/switches/flashing-beacon",
1791                switch_matrix[board][1][2] );
1792     fgSetBool( "/controls/switches/landing-light", switch_matrix[board][2][2] );
1793     fgSetBool( "/controls/switches/taxi-lights", switch_matrix[board][3][2] );
1794     fgSetBool( "/controls/switches/nav-lights",
1795                switch_matrix[board][4][2] );
1796     fgSetBool( "/controls/switches/strobe-lights", switch_matrix[board][5][2] );
1797     fgSetBool( "/controls/switches/pitot-heat", switch_matrix[board][6][2] );
1798
1799     // flaps
1800     float flaps = 0.0;
1801     if ( switch_matrix[board][6][3] ) {
1802         flaps = 1.0;
1803     } else if ( switch_matrix[board][5][3] ) {
1804         flaps = 2.0 / 3.0;
1805     } else if ( switch_matrix[board][4][3] ) {
1806         flaps = 1.0 / 3.0;
1807     } else if ( !switch_matrix[board][4][3] ) {
1808         flaps = 0.0;
1809     }
1810
1811     // do a bit of filtering on the magneto/starter switch and the
1812     // flap lever because these are not well debounced in hardware
1813     static float flap1, flap2, flap3;
1814     flap3 = flap2;
1815     flap2 = flap1;
1816     flap1 = flaps;
1817     if ( flap1 == flap2 && flap2 == flap3 ) {
1818         fgSetFloat( "/controls/flight/flaps", flaps );
1819     }
1820
1821     // fuel selector (also filtered)
1822     int fuel = 0;
1823     if ( switch_matrix[board][2][3] ) {
1824         // both
1825         fuel = 3;
1826     } else if ( switch_matrix[board][1][3] ) {
1827         // left
1828         fuel = 1;
1829     } else if ( switch_matrix[board][3][3] ) {
1830         // right
1831         fuel = 2;
1832     } else {
1833         // fuel cutoff
1834         fuel = 0;
1835     }
1836
1837     static int fuel1, fuel2, fuel3;
1838     fuel3 = fuel2;
1839     fuel2 = fuel1;
1840     fuel1 = fuel;
1841     if ( fuel1 == fuel2 && fuel2 == fuel3 ) {
1842         fgSetBool( "/controls/fuel/tank[0]/fuel-selector", (fuel & 0x01) > 0 );
1843         fgSetBool( "/controls/fuel/tank[1]/fuel-selector", (fuel & 0x02) > 0 );
1844     }
1845
1846     // circuit breakers
1847 #ifdef ATC_SUPPORT_CIRCUIT_BREAKERS_NOT_THE_DEFAULT
1848     fgSetBool( "/controls/circuit-breakers/cabin-lights-pwr",
1849                switch_matrix[board][0][0] );
1850     fgSetBool( "/controls/circuit-breakers/instr-ignition-switch",
1851                switch_matrix[board][1][0] );
1852     fgSetBool( "/controls/circuit-breakers/flaps",
1853                switch_matrix[board][2][0] );
1854     fgSetBool( "/controls/circuit-breakers/avn-bus-1",
1855                switch_matrix[board][3][0] );
1856     fgSetBool( "/controls/circuit-breakers/avn-bus-2",
1857                switch_matrix[board][4][0] );
1858     fgSetBool( "/controls/circuit-breakers/turn-coordinator",
1859                switch_matrix[board][5][0] );
1860     fgSetBool( "/controls/circuit-breakers/instrument-lights",
1861                switch_matrix[board][6][0] );
1862     fgSetBool( "/controls/circuit-breakers/annunciators",
1863                switch_matrix[board][7][0] );
1864 #else
1865     fgSetBool( "/controls/circuit-breakers/cabin-lights-pwr", true );
1866     fgSetBool( "/controls/circuit-breakers/instr-ignition-switch", true );
1867     fgSetBool( "/controls/circuit-breakers/flaps", true );
1868     fgSetBool( "/controls/circuit-breakers/avn-bus-1", true );
1869     fgSetBool( "/controls/circuit-breakers/avn-bus-2", true );
1870     fgSetBool( "/controls/circuit-breakers/turn-coordinator", true );
1871     fgSetBool( "/controls/circuit-breakers/instrument-lights", true );
1872     fgSetBool( "/controls/circuit-breakers/annunciators", true );
1873 #endif
1874
1875     fgSetDouble( "/controls/gear/parking-brake",
1876                  switch_matrix[board][7][3] );
1877     fgSetDouble( "/radios/marker-beacon/power-btn",
1878                  switch_matrix[board][6][1] );
1879
1880     return true;
1881 }
1882
1883
1884 bool FGATC610x::process() {
1885     // Lock the hardware, skip if it's not ready yet
1886     if ( ATC610xLock( lock_fd ) > 0 ) {
1887
1888         do_analog_in();
1889         do_lights();
1890         do_radio_switches();
1891         do_radio_display();
1892         do_steppers();
1893         do_switches();
1894         
1895         ATC610xRelease( lock_fd );
1896
1897         return true;
1898     } else {
1899         return false;
1900     }
1901 }
1902
1903
1904 bool FGATC610x::close() {
1905
1906     return true;
1907 }