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