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