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