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