]> git.mxchange.org Git - flightgear.git/blob - src/Network/atc610x.cxx
Patches from Erik Hofman for SGI compatibility:
[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
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( "/steam/mag-compass-deg", true );
398
399     dme_min = fgGetNode( "/radios/dme/ete-min", true );
400     dme_kt = fgGetNode( "/radios/dme/speed-kt", true );
401     dme_nm = fgGetNode( "/radios/dme/distance-nm", true );
402
403     adf_bus_power = fgGetNode( "/systems/electrical/outputs/adf", true );
404     dme_bus_power = fgGetNode( "/systems/electrical/outputs/dme", true );
405     navcom1_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[0]",
406                                    true );
407     navcom2_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[1]",
408                                    true );
409     xpdr_bus_power = fgGetNode( "/systems/electrical/outputs/transponder",
410                                  true );
411
412     navcom1_power_btn = fgGetNode( "/radios/comm[0]/inputs/power-btn", true );
413     navcom2_power_btn = fgGetNode( "/radios/comm[1]/inputs/power-btn", true );
414
415     com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
416     com1_stby_freq
417         = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
418
419     com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
420     com2_stby_freq
421         = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
422
423     nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
424     nav1_stby_freq
425         = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
426     nav1_obs = fgGetNode( "/radios/nav[0]/radials/selected-deg", true );
427
428     nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
429     nav2_stby_freq
430         = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
431     nav2_obs = fgGetNode( "/radios/nav[1]/radials/selected-deg", true );
432
433     adf_power_btn = fgGetNode( "/radios/kr-87/inputs/power-btn", true );
434     adf_vol = fgGetNode( "/radios/kr-87/inputs/volume", true );
435     adf_adf_btn = fgGetNode( "/radios/kr-87/inputs/adf-btn", true );
436     adf_bfo_btn = fgGetNode( "/radios/kr-87/inputs/bfo-btn", true );
437     adf_freq = fgGetNode( "/radios/kr-87/outputs/selected-khz", true );
438     adf_stby_freq = fgGetNode( "/radios/kr-87/outputs/standby-khz", true );
439     adf_stby_mode = fgGetNode( "/radios/kr-87/modes/stby", true );
440     adf_timer_mode = fgGetNode( "/radios/kr-87/modes/timer", true );
441     adf_count_mode = fgGetNode( "/radios/kr-87/modes/count", true );
442     adf_flight_timer = fgGetNode( "/radios/kr-87/outputs/flight-timer", true );
443     adf_elapsed_timer = fgGetNode( "/radios/kr-87/outputs/elapsed-timer",
444                                    true );
445     adf_ant_ann = fgGetNode( "/radios/kr-87/annunciators/ant", true );
446     adf_adf_ann = fgGetNode( "/radios/kr-87/annunciators/adf", true );
447     adf_bfo_ann = fgGetNode( "/radios/kr-87/annunciators/bfo", true );
448     adf_frq_ann = fgGetNode( "/radios/kr-87/annunciators/frq", true );
449     adf_flt_ann = fgGetNode( "/radios/kr-87/annunciators/flt", true );
450     adf_et_ann = fgGetNode( "/radios/kr-87/annunciators/et", true );
451
452     inner = fgGetNode( "/radios/marker-beacon/inner", true );
453     middle = fgGetNode( "/radios/marker-beacon/middle", true );
454     outer = fgGetNode( "/radios/marker-beacon/outer", true );
455
456     xpdr_ident_btn = fgGetNode( "/radios/kt-70/inputs/ident-btn", true );
457     xpdr_digit1 = fgGetNode( "/radios/kt-70/inputs/digit1", true );
458     xpdr_digit2 = fgGetNode( "/radios/kt-70/inputs/digit2", true );
459     xpdr_digit3 = fgGetNode( "/radios/kt-70/inputs/digit3", true );
460     xpdr_digit4 = fgGetNode( "/radios/kt-70/inputs/digit4", true );
461     xpdr_func_knob = fgGetNode( "/radios/kt-70/inputs/func-knob", true );
462     xpdr_id_code = fgGetNode( "/radios/kt-70/outputs/id-code", true );
463     xpdr_flight_level = fgGetNode( "/radios/kt-70/outputs/flight-level", true );
464     xpdr_fl_ann = fgGetNode( "/radios/kt-70/annunciators/fl", true );
465     xpdr_alt_ann = fgGetNode( "/radios/kt-70/annunciators/alt", true );
466     xpdr_gnd_ann = fgGetNode( "/radios/kt-70/annunciators/gnd", true );
467     xpdr_on_ann = fgGetNode( "/radios/kt-70/annunciators/on", true );
468     xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
469     xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
470
471     elevator_center = fgGetNode( "/input/atc610x/elevator/center", true );
472     elevator_min = fgGetNode( "/input/atc610x/elevator/min", true );
473     elevator_max = fgGetNode( "/input/atc610x/elevator/max", true );
474
475     ailerons_center = fgGetNode( "/input/atc610x/ailerons/center", true );
476     ailerons_min = fgGetNode( "/input/atc610x/ailerons/min", true );
477     ailerons_max = fgGetNode( "/input/atc610x/ailerons/max", true );
478
479     rudder_center = fgGetNode( "/input/atc610x/rudder/center", true );
480     rudder_min = fgGetNode( "/input/atc610x/rudder/min", true );
481     rudder_max = fgGetNode( "/input/atc610x/rudder/max", true );
482
483     throttle_min = fgGetNode( "/input/atc610x/throttle/min", true );
484     throttle_max = fgGetNode( "/input/atc610x/throttle/max", true );
485
486     mixture_min = fgGetNode( "/input/atc610x/mixture/min", true );
487     mixture_max = fgGetNode( "/input/atc610x/mixture/max", true );
488
489     trim_center = fgGetNode( "/input/atc610x/trim/center", true );
490     trim_min = fgGetNode( "/input/atc610x/trim/min", true );
491     trim_max = fgGetNode( "/input/atc610x/trim/max", true );
492
493     nav1vol_min = fgGetNode( "/input/atc610x/nav1vol/min", true );
494     nav1vol_max = fgGetNode( "/input/atc610x/nav1vol/max", true );
495
496     nav2vol_min = fgGetNode( "/input/atc610x/nav2vol/min", true );
497     nav2vol_max = fgGetNode( "/input/atc610x/nav2vol/max", true );
498
499     comm1_servicable = fgGetNode( "/instrumentation/comm[0]/servicable", true );
500     comm2_servicable = fgGetNode( "/instrumentation/comm[1]/servicable", true );
501     nav1_servicable = fgGetNode( "/instrumentation/nav[0]/servicable", true );
502     nav2_servicable = fgGetNode( "/instrumentation/nav[1]/servicable", true );
503     adf_servicable = fgGetNode( "/instrumentation/adf/servicable", true );
504     xpdr_servicable = fgGetNode( "/instrumentation/transponder/servicable",
505                                  true );
506     dme_servicable = fgGetNode( "/instrumentation/dme/servicable", true );
507
508     // default to having everything servicable
509     comm1_servicable->setBoolValue( true );
510     comm2_servicable->setBoolValue( true );
511     nav1_servicable->setBoolValue( true );
512     nav2_servicable->setBoolValue( true );
513     adf_servicable->setBoolValue( true );
514     xpdr_servicable->setBoolValue( true );
515     dme_servicable->setBoolValue( true );
516
517     return true;
518 }
519
520
521 /////////////////////////////////////////////////////////////////////
522 // Read analog inputs
523 /////////////////////////////////////////////////////////////////////
524
525 // scale a number between min and max (with center defined) to a scale
526 // from -1.0 to 1.0
527 static double scale( int center, int min, int max, int value ) {
528     // cout << center << " " << min << " " << max << " " << value << " ";
529     double result;
530     double range;
531
532     if ( value <= center ) {
533         range = center - min;
534         result = (value - center) / range;
535     } else {
536         range = max - center;
537         result = (value - center) / range;            
538     }
539
540     if ( result < -1.0 ) result = -1.0;
541     if ( result > 1.0 ) result = 1.0;
542
543     // cout << result << endl;
544
545     return result;
546 }
547
548
549 // scale a number between min and max to a scale from 0.0 to 1.0
550 static double scale( int min, int max, int value ) {
551     // cout << center << " " << min << " " << max << " " << value << " ";
552     double result;
553     double range;
554
555     range = max - min;
556     result = (value - min) / range;
557
558     if ( result < 0.0 ) result = 0.0;
559     if ( result > 1.0 ) result = 1.0;
560
561     // cout << result << endl;
562
563     return result;
564 }
565
566
567 bool FGATC610x::do_analog_in() {
568     // Read raw data in byte form
569     ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
570
571     // Convert to integer values
572     for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
573         unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
574         unsigned char lo = analog_in_bytes[2 * channel + 1];
575         analog_in_data[channel] = hi * 256 + lo;
576
577         // printf("%02x %02x ", hi, lo );
578         // printf("%04d ", value );
579     }
580
581     float tmp;
582
583     // aileron
584     tmp = scale( ailerons_center->getIntValue(), ailerons_min->getIntValue(),
585                  ailerons_max->getIntValue(), analog_in_data[0] );
586     fgSetFloat( "/controls/aileron", tmp );
587     // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
588
589     // elevator
590     tmp = -scale( elevator_center->getIntValue(), elevator_min->getIntValue(),
591                   elevator_max->getIntValue(), analog_in_data[5] );
592     fgSetFloat( "/controls/elevator", tmp );
593     // cout << "trim = " << analog_in_data[4] << " = " << tmp;
594
595     // elevator trim
596     tmp = scale( trim_center->getIntValue(), trim_min->getIntValue(),
597                  trim_max->getIntValue(), analog_in_data[4] );
598     fgSetFloat( "/controls/elevator-trim", tmp );
599     // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
600
601     // mixture
602     tmp = scale( mixture_min->getIntValue(), mixture_max->getIntValue(),
603                  analog_in_data[6] );
604     fgSetFloat( "/controls/mixture[0]", tmp );
605     fgSetFloat( "/controls/mixture[1]", tmp );
606
607     // throttle
608     tmp = scale( throttle_min->getIntValue(), throttle_max->getIntValue(),
609                  analog_in_data[8] );
610     fgSetFloat( "/controls/throttle[0]", tmp );
611     fgSetFloat( "/controls/throttle[1]", tmp );
612     // cout << "throttle = " << tmp << endl;
613
614     // rudder
615     /*
616     tmp = scale( rudder_center->getIntValue(), rudder_min->getIntValue(),
617                  rudder_max->getIntValue(), analog_in_data[10] );
618     fgSetFloat( "/controls/rudder", -tmp );
619     */
620
621     // nav1 volume
622     tmp = (float)analog_in_data[25] / 1024.0f;
623     fgSetFloat( "/radios/nav[0]/volume", tmp );
624
625     // nav2 volume
626     tmp = (float)analog_in_data[24] / 1024.0f;
627     fgSetFloat( "/radios/nav[1]/volume", tmp );
628
629     // adf volume
630     tmp = (float)analog_in_data[26] / 1024.0f;
631     fgSetFloat( "/radios/kr-87/inputs/volume", tmp );
632
633 #define FG_SECOND_TRY
634
635     // nav1 obs tuner
636     static int last_obs1 = analog_in_data[29];
637     static double diff1_ave = 0.0;
638     int diff1 = 0;
639
640 #if defined( FG_FIRST_TRY )
641     if ( analog_in_data[29] < 150 || analog_in_data[29] > 990 ) {
642         if ( last_obs1 > 512 && last_obs1 <= 990 ) {
643             diff1 = 1;
644         } else if ( last_obs1 >= 150 && last_obs1 <= 990 ) {
645             diff1 = -1;
646         }
647     } else if ( last_obs1 < 150 || last_obs1 > 990 ) {
648         if ( analog_in_data[29] > 512 && analog_in_data[29] <= 990 ) {
649             diff1 = -1;
650         } else if ( analog_in_data[29] >= 150 && analog_in_data[29] <= 990 ) {
651             diff1 = 1;
652         }
653     } else {
654         diff1 = analog_in_data[29] - last_obs1;
655     }
656 #elif defined( FG_SECOND_TRY )
657     if ( analog_in_data[29] < 20 ) {
658         if ( last_obs1 >= 110 && last_obs1 < 512 ) {
659             diff1 = -6;
660         } else if ( last_obs1 >= 512 ) {
661             diff1 = 6;
662         }
663         last_obs1 = analog_in_data[29];
664     } else if ( analog_in_data[29] < 110 ) {
665         // do nothing
666     } else if ( last_obs1 < 20 ) {
667         if ( analog_in_data[29] >= 110 && analog_in_data[29] < 512 ) {
668             diff1 = 6;
669         } else if ( analog_in_data[29] >= 512 ) {
670             diff1 = -6;
671         }
672         last_obs1 = analog_in_data[29];
673     } else {
674         diff1 = analog_in_data[29] - last_obs1;
675         if ( abs(diff1) > 200 ) {
676             // ignore
677             diff1 = 0;
678         }
679         last_obs1 = analog_in_data[29];
680     }
681 #elif defined( FG_FOURTH_TRY )
682     static bool ignore_next = false;
683     diff1 = analog_in_data[29] - last_obs1;
684     if ( abs(diff1) > 200 ) {
685         // ignore
686         diff1 = 0;
687         ignore_next = true;
688     } else if ( ignore_next ) {
689         diff1 = 0;
690         ignore_next = false;
691     }
692     last_obs1 = analog_in_data[29];
693 #endif
694
695     // cout << " diff1 = " << diff1 << endl;
696     if ( diff1 < -500 ) { diff1 += 1024; }
697     if ( diff1 > 500 ) { diff1 -= 1024; }
698
699     if ( fabs(diff1_ave - diff1) < 200 || fabs(diff1) < fabs(diff1_ave) ) {
700         diff1_ave = (2.0/3.0) * diff1_ave + (1.0/3.0) * diff1;
701     }
702
703     tmp = nav1_obs->getDoubleValue() + (diff1_ave * (72.0/914.0) );
704     while ( tmp >= 360.0 ) { tmp -= 360.0; }
705     while ( tmp < 0.0 ) { tmp += 360.0; }
706
707     fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp );
708
709     // nav2 obs tuner
710     static int last_obs2 = analog_in_data[30];
711     static double diff2_ave = 0.0;
712     int diff2 = 0;
713
714     // cout << "obs2 = " << analog_in_data[30] << " last obs2 = " << last_obs2;
715
716 #if defined( FG_FIRST_TRY )
717     if ( analog_in_data[30] < 150 || analog_in_data[30] > 990 ) {
718         if ( last_obs2 > 512 && last_obs2 <= 990 ) {
719             diff2 = 1;
720         } else if ( last_obs2 >= 150 && last_obs2 <= 990 ) {
721             diff2 = -1;
722         }
723     } else if ( last_obs2 < 150 || last_obs2 > 990 ) {
724         if ( analog_in_data[30] > 512 && analog_in_data[30] <= 990 ) {
725             diff2 = -1;
726         } else if ( analog_in_data[30] >= 150 && analog_in_data[30] <= 990 ) {
727             diff2 = 1;
728         }
729     } else {
730         diff2 = analog_in_data[30] - last_obs2;
731     }
732 #elif defined( FG_SECOND_TRY )
733     if ( analog_in_data[30] < 30 ) {
734         if ( last_obs2 >= 43 && last_obs2 < 480 ) {
735             diff2 = -6;
736         } else if ( last_obs2 >= 480 ) {
737             diff2 = 6;
738         }
739         last_obs2 = analog_in_data[30];
740     } else if ( analog_in_data[30] < 43 ) {
741         // do nothing
742     } else if ( last_obs2 < 30 ) {
743         if ( analog_in_data[30] >= 23 && analog_in_data[30] < 480 ) {
744             diff2 = 6;
745         } else if ( analog_in_data[30] >= 480 ) {
746             diff2 = -6;
747         }
748         last_obs2 = analog_in_data[30];
749     } else if ( analog_in_data[30] > 930 ) {
750         if ( last_obs2 <= 924 && last_obs2 > 480 ) {
751             diff2 = 6;
752         } else if ( last_obs2 <= 480 ) {
753             diff2 = -6;
754         }
755         last_obs2 = analog_in_data[30];
756     } else if ( analog_in_data[30] > 924 ) {
757         // do nothing
758     } else if ( last_obs2 > 930 ) {
759         if ( analog_in_data[30] <= 924 && analog_in_data[30] > 480 ) {
760             diff2 = -6;
761         } else if ( analog_in_data[30] <= 480 ) {
762             diff2 = 6;
763         }
764         last_obs2 = analog_in_data[30];
765     } else {
766         diff2 = analog_in_data[30] - last_obs2;
767         if ( abs(diff2) > 200 ) {
768             // ignore
769             diff2 = 0;
770         }
771         last_obs2 = analog_in_data[30];
772     }
773 #elif defined( FG_FOURTH_TRY )
774     static bool ignore_next = false;
775     diff2 = analog_in_data[30] - last_obs2;
776     if ( abs(diff2) > 200 ) {
777         // ignore
778         diff2 = 0;
779         ignore_next = true;
780     } else if ( ignore_next ) {
781         diff2 = 0;
782         ignore_next = false;
783     }
784     last_obs2 = analog_in_data[30];
785 #endif
786
787     // cout << " diff2 = " << diff2 << endl;
788     if ( diff2 < -500 ) { diff2 += 1024; }
789     if ( diff2 > 500 ) { diff2 -= 1024; }
790
791     if ( fabs(diff2_ave - diff2) < 200 || fabs(diff2) < fabs(diff2_ave) ) {
792         diff2_ave = (2.0/3.0) * diff2_ave + (1.0/3.0) * diff2;
793     }
794
795     tmp = nav2_obs->getDoubleValue() + (diff2_ave * (72.0/880.0) );
796     while ( tmp >= 360.0 ) { tmp -= 360.0; }
797     while ( tmp < 0.0 ) { tmp += 360.0; }
798     // cout << " obs = " << tmp << endl;
799     fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
800
801     return true;
802 }
803
804
805 /////////////////////////////////////////////////////////////////////
806 // Write the lights
807 /////////////////////////////////////////////////////////////////////
808
809 bool FGATC610x::do_lights() {
810
811     // Marker beacons
812     ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
813     ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
814     ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
815
816     // ADF annunciators
817     ATC610xSetLamp( lamps_fd, 11, adf_ant_ann->getBoolValue() ); // ANT
818     ATC610xSetLamp( lamps_fd, 12, adf_adf_ann->getBoolValue() ); // ADF
819     ATC610xSetLamp( lamps_fd, 13, adf_bfo_ann->getBoolValue() ); // BFO
820     ATC610xSetLamp( lamps_fd, 14, adf_frq_ann->getBoolValue() ); // FRQ
821     ATC610xSetLamp( lamps_fd, 15, adf_flt_ann->getBoolValue() ); // FLT
822     ATC610xSetLamp( lamps_fd, 16, adf_et_ann->getBoolValue() ); // ET
823
824     // Transponder annunciators
825     ATC610xSetLamp( lamps_fd, 17, xpdr_fl_ann->getBoolValue() ); // FL
826     ATC610xSetLamp( lamps_fd, 18, xpdr_alt_ann->getBoolValue() ); // ALT
827     ATC610xSetLamp( lamps_fd, 19, xpdr_gnd_ann->getBoolValue() ); // GND
828     ATC610xSetLamp( lamps_fd, 20, xpdr_on_ann->getBoolValue() ); // ON
829     ATC610xSetLamp( lamps_fd, 21, xpdr_sby_ann->getBoolValue() ); // SBY
830     ATC610xSetLamp( lamps_fd, 22, xpdr_reply_ann->getBoolValue() ); // R
831
832     return true;
833 }
834
835
836 /////////////////////////////////////////////////////////////////////
837 // Read radio switches 
838 /////////////////////////////////////////////////////////////////////
839
840 bool FGATC610x::do_radio_switches() {
841     double freq, coarse_freq, fine_freq, value;
842     int diff;
843
844     ATC610xReadRadios( radios_fd, radio_switch_data );
845
846     // DME Switch
847     dme_switch = (radio_switch_data[7] >> 4) & 0x03;
848     if ( dme_switch == 0 ) {
849         // off
850         fgSetInt( "/radios/dme/switch-position", 0 );
851     } else if ( dme_switch == 2 ) {
852         // nav1
853         fgSetInt( "/radios/dme/switch-position", 1 );
854     } else if ( dme_switch == 1 ) {
855         // nav2
856         fgSetInt( "/radios/dme/switch-position", 3 );
857     }
858
859     // NavCom1 Power
860     fgSetBool( "/radios/comm[0]/inputs/power-btn",
861                radio_switch_data[7] & 0x01 );
862
863     if ( navcom1_has_power() && comm1_servicable->getBoolValue() ) {
864         // Com1 Swap
865         int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
866         static int last_com1_swap;
867         if ( com1_swap && (last_com1_swap != com1_swap) ) {
868             float tmp = com1_freq->getFloatValue();
869             fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
870                         com1_stby_freq->getFloatValue() );
871             fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
872         }
873         last_com1_swap = com1_swap;
874     }
875
876     // NavCom2 Power
877     fgSetBool( "/radios/comm[1]/inputs/power-btn",
878                radio_switch_data[15] & 0x01 );
879
880     if ( navcom2_has_power() && comm2_servicable->getBoolValue() ) {
881         // Com2 Swap
882         int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
883         static int last_com2_swap;
884         if ( com2_swap && (last_com2_swap != com2_swap) ) {
885             float tmp = com2_freq->getFloatValue();
886             fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
887                         com2_stby_freq->getFloatValue() );
888             fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
889         }
890         last_com2_swap = com2_swap;
891     }
892
893     if ( navcom1_has_power() && nav1_servicable->getBoolValue() ) {
894         // Nav1 Swap
895         int nav1_swap = radio_switch_data[11] & 0x01;
896         static int last_nav1_swap;
897         if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
898             float tmp = nav1_freq->getFloatValue();
899             fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
900                         nav1_stby_freq->getFloatValue() );
901             fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
902         }
903         last_nav1_swap = nav1_swap;
904     }
905
906     if ( navcom2_has_power() && nav2_servicable->getBoolValue() ) {
907         // Nav2 Swap
908         int nav2_swap = !(radio_switch_data[19] & 0x01);
909         static int last_nav2_swap;
910         if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
911             float tmp = nav2_freq->getFloatValue();
912             fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
913                         nav2_stby_freq->getFloatValue() );
914             fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
915         }
916         last_nav2_swap = nav2_swap;
917     }
918
919     if ( navcom1_has_power() && comm1_servicable->getBoolValue() ) {
920         // Com1 Tuner
921         int com1_tuner_fine = ((radio_switch_data[5] >> 4) & 0x0f) - 1;
922         int com1_tuner_coarse = (radio_switch_data[5] & 0x0f) - 1;
923         static int last_com1_tuner_fine = com1_tuner_fine;
924         static int last_com1_tuner_coarse = com1_tuner_coarse;
925
926         freq = com1_stby_freq->getFloatValue();
927         coarse_freq = (int)freq;
928         fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
929
930         if ( com1_tuner_fine != last_com1_tuner_fine ) {
931             diff = com1_tuner_fine - last_com1_tuner_fine;
932             if ( abs(diff) > 4 ) {
933                 // roll over
934                 if ( com1_tuner_fine < last_com1_tuner_fine ) {
935                     // going up
936                     diff = 12 - last_com1_tuner_fine + com1_tuner_fine;
937                 } else {
938                     // going down
939                     diff = com1_tuner_fine - 12 - last_com1_tuner_fine;
940                 }
941             }
942             fine_freq += diff;
943         }
944         while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
945         while ( fine_freq < 0.0 )  { fine_freq += 40.0; }
946
947         if ( com1_tuner_coarse != last_com1_tuner_coarse ) {
948             diff = com1_tuner_coarse - last_com1_tuner_coarse;
949             if ( abs(diff) > 4 ) {
950                 // roll over
951                 if ( com1_tuner_coarse < last_com1_tuner_coarse ) {
952                     // going up
953                     diff = 12 - last_com1_tuner_coarse + com1_tuner_coarse;
954                 } else {
955                     // going down
956                     diff = com1_tuner_coarse - 12 - last_com1_tuner_coarse;
957                 }
958             }
959             coarse_freq += diff;
960         }
961         if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
962         if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
963
964         last_com1_tuner_fine = com1_tuner_fine;
965         last_com1_tuner_coarse = com1_tuner_coarse;
966
967         fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", 
968                     coarse_freq + fine_freq / 40.0 );
969     }
970
971     if ( navcom2_has_power() && comm2_servicable->getBoolValue() ) {
972         // Com2 Tuner
973         int com2_tuner_fine = ((radio_switch_data[13] >> 4) & 0x0f) - 1;
974         int com2_tuner_coarse = (radio_switch_data[13] & 0x0f) - 1;
975         static int last_com2_tuner_fine = com2_tuner_fine;
976         static int last_com2_tuner_coarse = com2_tuner_coarse;
977
978         freq = com2_stby_freq->getFloatValue();
979         coarse_freq = (int)freq;
980         fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
981
982         if ( com2_tuner_fine != last_com2_tuner_fine ) {
983             diff = com2_tuner_fine - last_com2_tuner_fine;
984             if ( abs(diff) > 4 ) {
985                 // roll over
986                 if ( com2_tuner_fine < last_com2_tuner_fine ) {
987                     // going up
988                     diff = 12 - last_com2_tuner_fine + com2_tuner_fine;
989                 } else {
990                     // going down
991                     diff = com2_tuner_fine - 12 - last_com2_tuner_fine;
992                 }
993             }
994             fine_freq += diff;
995         }
996         while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
997         while ( fine_freq < 0.0 )  { fine_freq += 40.0; }
998
999         if ( com2_tuner_coarse != last_com2_tuner_coarse ) {
1000             diff = com2_tuner_coarse - last_com2_tuner_coarse;
1001             if ( abs(diff) > 4 ) {
1002                 // roll over
1003                 if ( com2_tuner_coarse < last_com2_tuner_coarse ) {
1004                     // going up
1005                     diff = 12 - last_com2_tuner_coarse + com2_tuner_coarse;
1006                 } else {
1007                     // going down
1008                     diff = com2_tuner_coarse - 12 - last_com2_tuner_coarse;
1009                 }
1010             }
1011             coarse_freq += diff;
1012         }
1013         if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
1014         if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
1015
1016         last_com2_tuner_fine = com2_tuner_fine;
1017         last_com2_tuner_coarse = com2_tuner_coarse;
1018
1019         fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz",
1020                     coarse_freq + fine_freq / 40.0 );
1021     }
1022
1023     if ( navcom1_has_power() && nav1_servicable->getBoolValue() ) {
1024         // Nav1 Tuner
1025         int nav1_tuner_fine = ((radio_switch_data[9] >> 4) & 0x0f) - 1;
1026         int nav1_tuner_coarse = (radio_switch_data[9] & 0x0f) - 1;
1027         static int last_nav1_tuner_fine = nav1_tuner_fine;
1028         static int last_nav1_tuner_coarse = nav1_tuner_coarse;
1029
1030         freq = nav1_stby_freq->getFloatValue();
1031         coarse_freq = (int)freq;
1032         fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
1033
1034         if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
1035             diff = nav1_tuner_fine - last_nav1_tuner_fine;
1036             if ( abs(diff) > 4 ) {
1037                 // roll over
1038                 if ( nav1_tuner_fine < last_nav1_tuner_fine ) {
1039                     // going up
1040                     diff = 12 - last_nav1_tuner_fine + nav1_tuner_fine;
1041                 } else {
1042                     // going down
1043                     diff = nav1_tuner_fine - 12 - last_nav1_tuner_fine;
1044                 }
1045             }
1046             fine_freq += diff;
1047         }
1048         while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
1049         while ( fine_freq < 0.0 )  { fine_freq += 20.0; }
1050
1051         if ( nav1_tuner_coarse != last_nav1_tuner_coarse ) {
1052             diff = nav1_tuner_coarse - last_nav1_tuner_coarse;
1053             if ( abs(diff) > 4 ) {
1054                 // roll over
1055                 if ( nav1_tuner_coarse < last_nav1_tuner_coarse ) {
1056                     // going up
1057                     diff = 12 - last_nav1_tuner_coarse + nav1_tuner_coarse;
1058                 } else {
1059                     // going down
1060                     diff = nav1_tuner_coarse - 12 - last_nav1_tuner_coarse;
1061                 }
1062             }
1063             coarse_freq += diff;
1064         }
1065         if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
1066         if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
1067
1068         last_nav1_tuner_fine = nav1_tuner_fine;
1069         last_nav1_tuner_coarse = nav1_tuner_coarse;
1070
1071         fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz",
1072                     coarse_freq + fine_freq / 20.0 );
1073     }
1074
1075     if ( navcom2_has_power() && nav2_servicable->getBoolValue() ) {
1076         // Nav2 Tuner
1077         int nav2_tuner_fine = ((radio_switch_data[17] >> 4) & 0x0f) - 1;
1078         int nav2_tuner_coarse = (radio_switch_data[17] & 0x0f) - 1;
1079         static int last_nav2_tuner_fine = nav2_tuner_fine;
1080         static int last_nav2_tuner_coarse = nav2_tuner_coarse;
1081
1082         freq = nav2_stby_freq->getFloatValue();
1083         coarse_freq = (int)freq;
1084         fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
1085
1086         if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
1087             diff = nav2_tuner_fine - last_nav2_tuner_fine;
1088             if ( abs(diff) > 4 ) {
1089                 // roll over
1090                 if ( nav2_tuner_fine < last_nav2_tuner_fine ) {
1091                     // going up
1092                     diff = 12 - last_nav2_tuner_fine + nav2_tuner_fine;
1093                 } else {
1094                     // going down
1095                     diff = nav2_tuner_fine - 12 - last_nav2_tuner_fine;
1096                 }
1097             }
1098             fine_freq += diff;
1099         }
1100         while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
1101         while ( fine_freq < 0.0 )  { fine_freq += 20.0; }
1102
1103         if ( nav2_tuner_coarse != last_nav2_tuner_coarse ) {
1104             diff = nav2_tuner_coarse - last_nav2_tuner_coarse;
1105             if ( abs(diff) > 4 ) {
1106                 // roll over
1107                 if ( nav2_tuner_coarse < last_nav2_tuner_coarse ) {
1108                     // going up
1109                     diff = 12 - last_nav2_tuner_coarse + nav2_tuner_coarse;
1110                 } else {
1111                     // going down
1112                     diff = nav2_tuner_coarse - 12 - last_nav2_tuner_coarse;
1113                 }
1114             }
1115             coarse_freq += diff;
1116         }
1117         if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
1118         if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
1119
1120         last_nav2_tuner_fine = nav2_tuner_fine;
1121         last_nav2_tuner_coarse = nav2_tuner_coarse;
1122
1123         fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", 
1124                     coarse_freq + fine_freq / 20.0);
1125     }
1126
1127     // ADF Tuner
1128     
1129     int adf_tuner_fine = ((radio_switch_data[21] >> 4) & 0x0f) - 1;
1130     int adf_tuner_coarse = (radio_switch_data[21] & 0x0f) - 1;
1131     static int last_adf_tuner_fine = adf_tuner_fine;
1132     static int last_adf_tuner_coarse = adf_tuner_coarse;
1133
1134     if ( adf_has_power() && adf_servicable->getBoolValue() ) {
1135         // cout << "adf_stby_mode = " << adf_stby_mode->getIntValue() << endl;
1136         if ( adf_count_mode->getIntValue() == 2 ) {
1137             // tune count down timer
1138             value = adf_elapsed_timer->getDoubleValue();
1139         } else {
1140             // tune frequency
1141             if ( adf_stby_mode->getIntValue() == 1 ) {
1142                 value = adf_freq->getFloatValue();
1143             } else {
1144                 value = adf_stby_freq->getFloatValue();
1145             }
1146         }
1147
1148         if ( adf_tuner_fine != last_adf_tuner_fine ) {
1149             diff = adf_tuner_fine - last_adf_tuner_fine;
1150             if ( abs(diff) > 4 ) {
1151                 // roll over
1152                 if ( adf_tuner_fine < last_adf_tuner_fine ) {
1153                     // going up
1154                     diff = 12 - last_adf_tuner_fine + adf_tuner_fine;
1155                 } else {
1156                     // going down
1157                     diff = adf_tuner_fine - 12 - last_adf_tuner_fine;
1158                 }
1159             }
1160             value += diff;
1161         }
1162
1163         if ( adf_tuner_coarse != last_adf_tuner_coarse ) {
1164             diff = adf_tuner_coarse - last_adf_tuner_coarse;
1165             if ( abs(diff) > 4 ) {
1166                 // roll over
1167                 if ( adf_tuner_coarse < last_adf_tuner_coarse ) {
1168                     // going up
1169                     diff = 12 - last_adf_tuner_coarse + adf_tuner_coarse;
1170                 } else {
1171                     // going down
1172                     diff = adf_tuner_coarse - 12 - last_adf_tuner_coarse;
1173                 }
1174             }
1175             if ( adf_count_mode->getIntValue() == 2 ) {
1176                 value += 60 * diff;
1177             } else {
1178                 value += 25 * diff;
1179             }
1180         }
1181         if ( adf_count_mode->getIntValue() == 2 ) {
1182             if ( value < 0 ) { value += 3600; }
1183             if ( value > 3599 ) { value -= 3600; }
1184         } else {
1185             if ( value < 200 ) { value += 1600; }
1186             if ( value > 1799 ) { value -= 1600; }
1187         }
1188  
1189         if ( adf_count_mode->getIntValue() == 2 ) {
1190             fgSetFloat( "/radios/kr-87/outputs/elapsed-timer", value );
1191         } else {
1192             if ( adf_stby_mode->getIntValue() == 1 ) {
1193                 fgSetFloat( "/radios/kr-87/outputs/selected-khz", value );
1194             } else {
1195                 fgSetFloat( "/radios/kr-87/outputs/standby-khz", value );
1196             }
1197         }
1198     }
1199     last_adf_tuner_fine = adf_tuner_fine;
1200     last_adf_tuner_coarse = adf_tuner_coarse;
1201
1202
1203     // ADF buttons 
1204     fgSetInt( "/radios/kr-87/inputs/adf-btn",
1205               !(radio_switch_data[23] & 0x01) );
1206     fgSetInt( "/radios/kr-87/inputs/bfo-btn",
1207               !(radio_switch_data[23] >> 1 & 0x01) );
1208     fgSetInt( "/radios/kr-87/inputs/frq-btn",
1209               !(radio_switch_data[23] >> 2 & 0x01) );
1210     fgSetInt( "/radios/kr-87/inputs/flt-et-btn",
1211               !(radio_switch_data[23] >> 3 & 0x01) );
1212     fgSetInt( "/radios/kr-87/inputs/set-rst-btn",
1213               !(radio_switch_data[23] >> 4 & 0x01) );
1214     fgSetInt( "/radios/kr-87/inputs/power-btn",
1215               radio_switch_data[23] >> 5 & 0x01 );
1216     /* cout << "adf = " << !(radio_switch_data[23] & 0x01)
1217          << " bfo = " << !(radio_switch_data[23] >> 1 & 0x01)
1218          << " frq = " << !(radio_switch_data[23] >> 2 & 0x01)
1219          << " flt/et = " << !(radio_switch_data[23] >> 3 & 0x01)
1220          << " set/rst = " << !(radio_switch_data[23] >> 4 & 0x01)
1221          << endl; */
1222
1223     // Transponder Tuner
1224     int i;
1225     int digit_tuner[4];
1226     digit_tuner[0] = radio_switch_data[25] & 0x0f;
1227     digit_tuner[1] = ( radio_switch_data[25] >> 4 ) & 0x0f;
1228     digit_tuner[2] = radio_switch_data[29] & 0x0f;
1229     digit_tuner[3] = ( radio_switch_data[29] >> 4 ) & 0x0f;
1230
1231     static int last_digit_tuner[4];
1232     static bool first_time = true;
1233     if ( first_time ) {
1234         first_time = false;
1235         for ( i = 0; i < 4; ++i ) {
1236             last_digit_tuner[i] = digit_tuner[i];
1237         }
1238     }
1239
1240     if ( xpdr_has_power() && xpdr_servicable->getBoolValue() ) {
1241         int id_code = xpdr_id_code->getIntValue();
1242         int digit[4];
1243         int place = 1000;
1244         for ( i = 0; i < 4; ++i ) {
1245             digit[i] = id_code / place;
1246             id_code -= digit[i] * place;
1247             place /= 10;
1248         }
1249
1250         for ( i = 0; i < 4; ++i ) {
1251             if ( digit_tuner[i] != last_digit_tuner[i] ) {
1252                 diff = digit_tuner[i] - last_digit_tuner[i];
1253                 if ( abs(diff) > 4 ) {
1254                     // roll over
1255                     if ( digit_tuner[i] < last_digit_tuner[i] ) {
1256                         // going up
1257                         diff = 15 - last_digit_tuner[i] + digit_tuner[i];
1258                     } else {
1259                         // going down
1260                         diff = digit_tuner[i] - 15 - last_digit_tuner[i];
1261                     }
1262                 }
1263                 digit[i] += diff;
1264             }
1265             while ( digit[i] >= 8 ) { digit[i] -= 8; }
1266             while ( digit[i] < 0 )  { digit[i] += 8; }
1267         }
1268
1269         fgSetInt( "/radios/kt-70/inputs/digit1", digit[0] );
1270         fgSetInt( "/radios/kt-70/inputs/digit2", digit[1] );
1271         fgSetInt( "/radios/kt-70/inputs/digit3", digit[2] );
1272         fgSetInt( "/radios/kt-70/inputs/digit4", digit[3] );
1273     }
1274     for ( i = 0; i < 4; ++i ) {
1275         last_digit_tuner[i] = digit_tuner[i];
1276     }
1277
1278     int tmp = 0;
1279     for ( i = 0; i < 5; ++i ) {
1280         if ( radio_switch_data[27] >> i & 0x01 ) {
1281             tmp = i + 1;
1282         }
1283     }
1284     fgSetInt( "/radios/kt-70/inputs/func-knob", tmp );
1285     fgSetInt( "/radios/kt-70/inputs/ident-btn",
1286               !(radio_switch_data[27] >> 5 & 0x01) );
1287
1288     // Audio panel switches
1289     fgSetInt( "/radios/nav[0]/audio-btn",
1290               (radio_switch_data[3] & 0x01) );
1291     fgSetInt( "/radios/nav[1]/audio-btn",
1292               (radio_switch_data[3] >> 2 & 0x01) );
1293     fgSetInt( "/radios/kr-87/inputs/audio-btn",
1294               (radio_switch_data[3] >> 4 & 0x01) );
1295     fgSetInt( "/radios/marker-beacon/audio-btn",
1296               (radio_switch_data[3] >> 6 & 0x01) );
1297
1298     return true;
1299 }
1300
1301
1302 /////////////////////////////////////////////////////////////////////
1303 // Update the radio display 
1304 /////////////////////////////////////////////////////////////////////
1305
1306 bool FGATC610x::do_radio_display() {
1307
1308     char digits[10];
1309     int i;
1310
1311     if ( dme_has_power() && dme_servicable->getBoolValue() ) {
1312         // DME minutes
1313         float minutes = dme_min->getFloatValue();
1314         if ( minutes > 999 ) {
1315             minutes = 999.0;
1316         }
1317         snprintf(digits, 7, "%03.0f", minutes);
1318         for ( i = 0; i < 6; ++i ) {
1319             digits[i] -= '0';
1320         }
1321         radio_display_data[0] = digits[1] << 4 | digits[2];
1322         radio_display_data[1] = 0xf0 | digits[0];
1323         
1324         // DME knots
1325         float knots = dme_kt->getFloatValue();
1326         if ( knots > 999 ) {
1327             knots = 999.0;
1328         }
1329         snprintf(digits, 7, "%03.0f", knots);
1330         for ( i = 0; i < 6; ++i ) {
1331             digits[i] -= '0';
1332         }
1333         radio_display_data[2] = digits[1] << 4 | digits[2];
1334         radio_display_data[3] = 0xf0 | digits[0];
1335
1336         // DME distance (nm)
1337         float nm = dme_nm->getFloatValue();
1338         if ( nm > 99 ) {
1339             nm = 99.0;
1340         }
1341         snprintf(digits, 7, "%04.1f", nm);
1342         for ( i = 0; i < 6; ++i ) {
1343             digits[i] -= '0';
1344         }
1345         radio_display_data[4] = digits[1] << 4 | digits[3];
1346         radio_display_data[5] = 0x00 | digits[0];
1347         // the 0x00 in the upper nibble of the 6th byte of each
1348         // display turns on the decimal point
1349     } else {
1350         // blank dem display
1351         for ( i = 0; i < 6; ++i ) {
1352             radio_display_data[i] = 0xff;
1353         }
1354     }
1355
1356     if ( navcom1_has_power() && comm1_servicable->getBoolValue() ) {
1357         // Com1 standby frequency
1358         float com1_stby = com1_stby_freq->getFloatValue();
1359         if ( fabs(com1_stby) > 999.99 ) {
1360             com1_stby = 0.0;
1361         }
1362         snprintf(digits, 7, "%06.3f", com1_stby);
1363         for ( i = 0; i < 6; ++i ) {
1364             digits[i] -= '0';
1365         }
1366         radio_display_data[6] = digits[4] << 4 | digits[5];
1367         radio_display_data[7] = digits[1] << 4 | digits[2];
1368         radio_display_data[8] = 0xf0 | digits[0];
1369
1370         // Com1 in use frequency
1371         float com1 = com1_freq->getFloatValue();
1372         if ( fabs(com1) > 999.99 ) {
1373             com1 = 0.0;
1374         }
1375         snprintf(digits, 7, "%06.3f", com1);
1376         for ( i = 0; i < 6; ++i ) {
1377             digits[i] -= '0';
1378         }
1379         radio_display_data[9] = digits[4] << 4 | digits[5];
1380         radio_display_data[10] = digits[1] << 4 | digits[2];
1381         radio_display_data[11] = 0x00 | digits[0];
1382         // the 0x00 in the upper nibble of the 6th byte of each display
1383         // turns on the decimal point
1384     } else {
1385         radio_display_data[6] = 0xff;
1386         radio_display_data[7] = 0xff;
1387         radio_display_data[8] = 0xff;
1388         radio_display_data[9] = 0xff;
1389         radio_display_data[10] = 0xff;
1390         radio_display_data[11] = 0xff;
1391     }
1392
1393     if ( navcom2_has_power() && comm2_servicable->getBoolValue() ) {
1394         // Com2 standby frequency
1395         float com2_stby = com2_stby_freq->getFloatValue();
1396         if ( fabs(com2_stby) > 999.99 ) {
1397             com2_stby = 0.0;
1398         }
1399         snprintf(digits, 7, "%06.3f", com2_stby);
1400         for ( i = 0; i < 6; ++i ) {
1401             digits[i] -= '0';
1402         }
1403         radio_display_data[18] = digits[4] << 4 | digits[5];
1404         radio_display_data[19] = digits[1] << 4 | digits[2];
1405         radio_display_data[20] = 0xf0 | digits[0];
1406
1407         // Com2 in use frequency
1408         float com2 = com2_freq->getFloatValue();
1409         if ( fabs(com2) > 999.99 ) {
1410         com2 = 0.0;
1411         }
1412         snprintf(digits, 7, "%06.3f", com2);
1413         for ( i = 0; i < 6; ++i ) {
1414             digits[i] -= '0';
1415         }
1416         radio_display_data[21] = digits[4] << 4 | digits[5];
1417         radio_display_data[22] = digits[1] << 4 | digits[2];
1418         radio_display_data[23] = 0x00 | digits[0];
1419         // the 0x00 in the upper nibble of the 6th byte of each display
1420         // turns on the decimal point
1421     } else {
1422         radio_display_data[18] = 0xff;
1423         radio_display_data[19] = 0xff;
1424         radio_display_data[20] = 0xff;
1425         radio_display_data[21] = 0xff;
1426         radio_display_data[22] = 0xff;
1427         radio_display_data[23] = 0xff;
1428     }
1429
1430     if ( navcom1_has_power() && nav1_servicable->getBoolValue() ) {
1431         // Nav1 standby frequency
1432         float nav1_stby = nav1_stby_freq->getFloatValue();
1433         if ( fabs(nav1_stby) > 999.99 ) {
1434         nav1_stby = 0.0;
1435         }
1436         snprintf(digits, 7, "%06.2f", nav1_stby);
1437         for ( i = 0; i < 6; ++i ) {
1438             digits[i] -= '0';
1439         }
1440         radio_display_data[12] = digits[4] << 4 | digits[5];
1441         radio_display_data[13] = digits[1] << 4 | digits[2];
1442         radio_display_data[14] = 0xf0 | digits[0];
1443
1444         // Nav1 in use frequency
1445         float nav1 = nav1_freq->getFloatValue();
1446         if ( fabs(nav1) > 999.99 ) {
1447             nav1 = 0.0;
1448         }
1449         snprintf(digits, 7, "%06.2f", nav1);
1450         for ( i = 0; i < 6; ++i ) {
1451             digits[i] -= '0';
1452         }
1453         radio_display_data[15] = digits[4] << 4 | digits[5];
1454         radio_display_data[16] = digits[1] << 4 | digits[2];
1455         radio_display_data[17] = 0x00 | digits[0];
1456         // the 0x00 in the upper nibble of the 6th byte of each display
1457         // turns on the decimal point
1458     } else {
1459         radio_display_data[12] = 0xff;
1460         radio_display_data[13] = 0xff;
1461         radio_display_data[14] = 0xff;
1462         radio_display_data[15] = 0xff;
1463         radio_display_data[16] = 0xff;
1464         radio_display_data[17] = 0xff;
1465     }
1466
1467     if ( navcom2_has_power() && nav2_servicable->getBoolValue() ) {
1468         // Nav2 standby frequency
1469         float nav2_stby = nav2_stby_freq->getFloatValue();
1470         if ( fabs(nav2_stby) > 999.99 ) {
1471             nav2_stby = 0.0;
1472         }
1473         snprintf(digits, 7, "%06.2f", nav2_stby);
1474         for ( i = 0; i < 6; ++i ) {
1475             digits[i] -= '0';
1476         }
1477         radio_display_data[24] = digits[4] << 4 | digits[5];
1478         radio_display_data[25] = digits[1] << 4 | digits[2];
1479         radio_display_data[26] = 0xf0 | digits[0];
1480
1481         // Nav2 in use frequency
1482         float nav2 = nav2_freq->getFloatValue();
1483         if ( fabs(nav2) > 999.99 ) {
1484             nav2 = 0.0;
1485         }
1486         snprintf(digits, 7, "%06.2f", nav2);
1487         for ( i = 0; i < 6; ++i ) {
1488             digits[i] -= '0';
1489         }
1490         radio_display_data[27] = digits[4] << 4 | digits[5];
1491         radio_display_data[28] = digits[1] << 4 | digits[2];
1492         radio_display_data[29] = 0x00 | digits[0];
1493         // the 0x00 in the upper nibble of the 6th byte of each display
1494         // turns on the decimal point
1495     } else {
1496         radio_display_data[24] = 0xff;
1497         radio_display_data[25] = 0xff;
1498         radio_display_data[26] = 0xff;
1499         radio_display_data[27] = 0xff;
1500         radio_display_data[28] = 0xff;
1501         radio_display_data[29] = 0xff;
1502     }
1503
1504     // ADF standby frequency / timer
1505     if ( adf_has_power() && adf_servicable->getBoolValue() ) {
1506         if ( adf_stby_mode->getIntValue() == 0 ) {
1507             // frequency
1508             float adf_stby = adf_stby_freq->getFloatValue();
1509             if ( fabs(adf_stby) > 1799 ) {
1510                 adf_stby = 1799;
1511             }
1512             snprintf(digits, 7, "%04.0f", adf_stby);
1513             for ( i = 0; i < 6; ++i ) {
1514                 digits[i] -= '0';
1515             }
1516             radio_display_data[30] = digits[3] << 4 | 0x0f;
1517             radio_display_data[31] = digits[1] << 4 | digits[2];
1518             if ( digits[0] == 0 ) {
1519                 radio_display_data[32] = 0xff;
1520             } else {
1521                 radio_display_data[32] = 0xf0 | digits[0];
1522             }
1523         } else {
1524             // timer
1525             double time;
1526             int hours, min, sec;
1527             if ( adf_timer_mode->getIntValue() == 0 ) {
1528                 time = adf_flight_timer->getDoubleValue();
1529             } else {
1530                 time = adf_elapsed_timer->getDoubleValue();
1531             }
1532             // cout << time << endl;
1533             hours = (int)(time / 3600.0);
1534             time -= hours * 3600.00;
1535             min = (int)(time / 60.0);
1536             time -= min * 60.0;
1537             sec = (int)time;
1538             int big, little;
1539             if ( hours > 0 ) {
1540                 big = hours;
1541                 if ( big > 99 ) {
1542                     big = 99;
1543                 }
1544                 little = min;
1545             } else {
1546                 big = min;
1547                 little = sec;
1548             }
1549             if ( big > 99 ) {
1550                 big = 99;
1551             }
1552             // cout << big << ":" << little << endl;
1553             snprintf(digits, 7, "%02d%02d", big, little);
1554             for ( i = 0; i < 6; ++i ) {
1555                 digits[i] -= '0';
1556             }
1557             radio_display_data[30] = digits[2] << 4 | digits[3];
1558             radio_display_data[31] = digits[0] << 4 | digits[1];
1559             radio_display_data[32] = 0xff;
1560         }
1561
1562         // ADF in use frequency
1563         float adf = adf_freq->getFloatValue();
1564         if ( fabs(adf) > 1799 ) {
1565             adf = 1799;
1566         }
1567         snprintf(digits, 7, "%04.0f", adf);
1568         for ( i = 0; i < 6; ++i ) {
1569             digits[i] -= '0';
1570         }
1571         radio_display_data[33] = digits[2] << 4 | digits[3];
1572         if ( digits[0] == 0 ) {
1573             radio_display_data[34] = 0xf0 | digits[1];
1574         } else {
1575             radio_display_data[34] = digits[0] << 4 | digits[1];
1576         }
1577         if ( adf_stby_mode->getIntValue() == 0 ) {
1578           radio_display_data[35] = 0xff;
1579         } else {
1580           radio_display_data[35] = 0x0f;
1581         }
1582     } else {
1583         radio_display_data[30] = 0xff;
1584         radio_display_data[31] = 0xff;
1585         radio_display_data[32] = 0xff;
1586         radio_display_data[33] = 0xff;
1587         radio_display_data[34] = 0xff;
1588         radio_display_data[35] = 0xff;
1589     }
1590     
1591     // Transponder code and flight level
1592     if ( xpdr_has_power() && xpdr_servicable->getBoolValue() ) {
1593         if ( xpdr_func_knob->getIntValue() == 2 ) {
1594             // test mode
1595             radio_display_data[36] = 8 << 4 | 8;
1596             radio_display_data[37] = 8 << 4 | 8;
1597             radio_display_data[38] = 0xff;
1598             radio_display_data[39] = 8 << 4 | 0x0f;
1599             radio_display_data[40] = 8 << 4 | 8;
1600         } else {
1601             // other on modes
1602             int id_code = xpdr_id_code->getIntValue();
1603             int place = 1000;
1604             for ( i = 0; i < 4; ++i ) {
1605                 digits[i] = id_code / place;
1606                 id_code -= digits[i] * place;
1607                 place /= 10;
1608             }
1609             radio_display_data[36] = digits[2] << 4 | digits[3];
1610             radio_display_data[37] = digits[0] << 4 | digits[1];
1611             radio_display_data[38] = 0xff;
1612
1613             if ( xpdr_func_knob->getIntValue() == 3 ||
1614                  xpdr_func_knob->getIntValue() == 5 )
1615             {
1616                 // do flight level display
1617                 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
1618                 for ( i = 0; i < 6; ++i ) {
1619                     digits[i] -= '0';
1620                 }
1621                 radio_display_data[39] = digits[2] << 4 | 0x0f;
1622                 radio_display_data[40] = digits[0] << 4 | digits[1];
1623             } else {
1624                 // blank flight level display
1625                 radio_display_data[39] = 0xff;
1626                 radio_display_data[40] = 0xff;
1627             }
1628         }
1629     } else {
1630         // off
1631         radio_display_data[36] = 0xff;
1632         radio_display_data[37] = 0xff;
1633         radio_display_data[38] = 0xff;
1634         radio_display_data[39] = 0xff;
1635         radio_display_data[40] = 0xff;
1636     }
1637
1638     ATC610xSetRadios( radios_fd, radio_display_data );
1639
1640     return true;
1641 }
1642
1643
1644 /////////////////////////////////////////////////////////////////////
1645 // Drive the stepper motors
1646 /////////////////////////////////////////////////////////////////////
1647
1648 bool FGATC610x::do_steppers() {
1649     float diff = mag_compass->getFloatValue() - compass_position;
1650     while ( diff < -180.0 ) { diff += 360.0; }
1651     while ( diff >  180.0 ) { diff -= 360.0; }
1652
1653     int steps = (int)(diff * 4);
1654     // cout << "steps = " << steps << endl;
1655     if ( steps > 4 ) { steps = 4; }
1656     if ( steps < -4 ) { steps = -4; }
1657
1658     if ( abs(steps) > 0 ) {
1659         unsigned char cmd = 0x80;       // stepper command
1660         if ( steps > 0 ) {
1661             cmd |= 0x20;                // go up
1662         } else {
1663             cmd |= 0x00;                // go down
1664         }
1665         cmd |= abs(steps);
1666
1667         // sync compass_position with hardware position
1668         compass_position += (float)steps / 4.0;
1669
1670         ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1671     }
1672
1673     return true;
1674 }
1675
1676
1677 /////////////////////////////////////////////////////////////////////
1678 // Read the switch positions
1679 /////////////////////////////////////////////////////////////////////
1680
1681 // decode the packed switch data
1682 static void update_switch_matrix(
1683         int board,
1684         unsigned char switch_data[ATC_SWITCH_BYTES],
1685         int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
1686 {
1687     for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
1688         unsigned char switches = switch_data[row];
1689
1690         for( int column = 0; column < ATC_NUM_COLS; ++column ) {
1691             switch_matrix[board][column][row] = switches & 1;
1692             switches = switches >> 1;
1693         }                       
1694     }
1695 }                     
1696
1697 bool FGATC610x::do_switches() {
1698     ATC610xReadSwitches( switches_fd, switch_data );
1699
1700     // unpack the switch data
1701     int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
1702     update_switch_matrix( board, switch_data, switch_matrix );
1703
1704     // master switches
1705     fgSetBool( "/controls/switches/master-bat", switch_matrix[board][4][1] );
1706     fgSetBool( "/controls/switches/master-alt", switch_matrix[board][5][1] );
1707     fgSetBool( "/controls/switches/master-avionics",
1708                switch_matrix[board][0][3] );
1709
1710     // magnetos and starter switch
1711     int magnetos = 0;
1712     bool starter = false;
1713     if ( switch_matrix[board][3][1] == 1 ) {
1714         magnetos = 3;
1715         starter = true;
1716     } else if ( switch_matrix[board][2][1] == 1 ) {
1717         magnetos = 3;
1718         starter = false;
1719     } else if ( switch_matrix[board][1][1] == 1 ) {
1720         magnetos = 2;
1721         starter = false;
1722     } else if ( switch_matrix[board][0][1] == 1 ) {
1723         magnetos = 1;
1724         starter = false;
1725     } else {
1726         magnetos = 0;
1727         starter = false;
1728     }
1729
1730     // do a bit of filtering on the magneto/starter switch and the
1731     // flap lever because these are not well debounced in hardware
1732     static int mag1, mag2, mag3;
1733     mag3 = mag2;
1734     mag2 = mag1;
1735     mag1 = magnetos;
1736     if ( mag1 == mag2 && mag2 == mag3 ) {
1737         fgSetInt( "/controls/magnetos[0]", magnetos );
1738     }
1739     static bool start1, start2, start3;
1740     start3 = start2;
1741     start2 = start1;
1742     start1 = starter;
1743     if ( start1 == start2 && start2 == start3 ) {
1744         fgSetBool( "/controls/starter[0]", starter );
1745     }
1746
1747     // other toggle switches
1748     fgSetBool( "/controls/fuel-pump[0]", switch_matrix[board][0][2] );
1749     fgSetBool( "/controls/switches/flashing-beacon",
1750                switch_matrix[board][1][2] );
1751     fgSetBool( "/controls/switches/landing-light", switch_matrix[board][2][2] );
1752     fgSetBool( "/controls/switches/taxi-lights", switch_matrix[board][3][2] );
1753     fgSetBool( "/controls/switches/nav-lights",
1754                switch_matrix[board][4][2] );
1755     fgSetBool( "/controls/switches/strobe-lights", switch_matrix[board][5][2] );
1756     fgSetBool( "/controls/switches/pitot-heat", switch_matrix[board][6][2] );
1757
1758     // flaps
1759     float flaps = 0.0;
1760     if ( switch_matrix[board][6][3] ) {
1761         flaps = 1.0;
1762     } else if ( switch_matrix[board][5][3] ) {
1763         flaps = 2.0 / 3.0;
1764     } else if ( switch_matrix[board][4][3] ) {
1765         flaps = 1.0 / 3.0;
1766     } else if ( !switch_matrix[board][4][3] ) {
1767         flaps = 0.0;
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 float flap1, flap2, flap3;
1773     flap3 = flap2;
1774     flap2 = flap1;
1775     flap1 = flaps;
1776     if ( flap1 == flap2 && flap2 == flap3 ) {
1777         fgSetFloat( "/controls/flaps", flaps );
1778     }
1779
1780     // fuel selector (also filtered)
1781     int fuel = 0;
1782     if ( switch_matrix[board][2][3] ) {
1783         // both
1784         fuel = 3;
1785     } else if ( switch_matrix[board][1][3] ) {
1786         // left
1787         fuel = 1;
1788     } else if ( switch_matrix[board][3][3] ) {
1789         // right
1790         fuel = 2;
1791     } else {
1792         // fuel cutoff
1793         fuel = 0;
1794     }
1795
1796     static int fuel1, fuel2, fuel3;
1797     fuel3 = fuel2;
1798     fuel2 = fuel1;
1799     fuel1 = fuel;
1800     if ( fuel1 == fuel2 && fuel2 == fuel3 ) {
1801         fgSetBool( "/controls/fuel-selector[0]", (fuel & 0x01) > 0 );
1802         fgSetBool( "/controls/fuel-selector[1]", (fuel & 0x02) > 0 );
1803     }
1804
1805     // circuit breakers
1806     fgSetBool( "/controls/circuit-breakers/cabin-lights-pwr",
1807                switch_matrix[board][0][0] );
1808     fgSetBool( "/controls/circuit-breakers/instr-ignition-switch",
1809                switch_matrix[board][1][0] );
1810     fgSetBool( "/controls/circuit-breakers/flaps",
1811                switch_matrix[board][2][0] );
1812     fgSetBool( "/controls/circuit-breakers/avn-bus-1",
1813                switch_matrix[board][3][0] );
1814     fgSetBool( "/controls/circuit-breakers/avn-bus-2",
1815                switch_matrix[board][4][0] );
1816     fgSetBool( "/controls/circuit-breakers/turn-coordinator",
1817                switch_matrix[board][5][0] );
1818     fgSetBool( "/controls/circuit-breakers/instrument-lights",
1819                switch_matrix[board][6][0] );
1820     fgSetBool( "/controls/circuit-breakers/annunciators",
1821                switch_matrix[board][7][0] );
1822
1823     fgSetDouble( "/controls/parking-brake",
1824                  switch_matrix[board][7][3] );
1825     fgSetDouble( "/radios/marker-beacon/power-btn",
1826                  switch_matrix[board][6][1] );
1827
1828     return true;
1829 }
1830
1831
1832 bool FGATC610x::process() {
1833     // Lock the hardware, skip if it's not ready yet
1834     if ( ATC610xLock( lock_fd ) > 0 ) {
1835
1836         do_analog_in();
1837         do_lights();
1838         do_radio_switches();
1839         do_radio_display();
1840         do_steppers();
1841         do_switches();
1842         
1843         ATC610xRelease( lock_fd );
1844
1845         return true;
1846     } else {
1847         return false;
1848     }
1849 }
1850
1851
1852 bool FGATC610x::close() {
1853
1854     return true;
1855 }