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