]> git.mxchange.org Git - flightgear.git/blob - src/Network/atc610x.cxx
Updated hardware interface to match new KR 87 features.
[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/adf/on-off-volume", true );
400     adf_freq = fgGetNode( "/radios/adf/frequencies/selected-khz", true );
401     adf_stby_freq = fgGetNode( "/radios/adf/frequencies/standby-khz", true );
402     adf_stby_mode = fgGetNode( "/radios/adf/stby-mode", true );
403     adf_timer_mode = fgGetNode( "/radios/adf/timer-mode", true );
404     adf_count_mode = fgGetNode( "/radios/adf/count-mode", true );
405     adf_flight_timer = fgGetNode( "/radios/adf/flight-timer", true );
406     adf_elapsed_timer = fgGetNode( "/radios/adf/elapsed-timer", true );
407
408     inner = fgGetNode( "/radios/marker-beacon/inner", true );
409     middle = fgGetNode( "/radios/marker-beacon/middle", true );
410     outer = fgGetNode( "/radios/marker-beacon/outer", true );
411
412     return true;
413 }
414
415
416 /////////////////////////////////////////////////////////////////////
417 // Read analog inputs
418 /////////////////////////////////////////////////////////////////////
419
420 #define ATC_AILERON_CENTER 535
421 #define ATC_ELEVATOR_TRIM_CENTER 512
422 #define ATC_ELEVATOR_CENTER 543
423
424 bool FGATC610x::do_analog_in() {
425     // Read raw data in byte form
426     ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
427
428     // Convert to integer values
429     for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
430         unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
431         unsigned char lo = analog_in_bytes[2 * channel + 1];
432         analog_in_data[channel] = hi * 256 + lo;
433
434         // printf("%02x %02x ", hi, lo );
435         // printf("%04d ", value );
436     }
437
438     float tmp, tmp1, tmp2;
439
440     // aileron
441     tmp = (float)(analog_in_data[0] - ATC_AILERON_CENTER) / 256.0f;
442     fgSetFloat( "/controls/aileron", tmp );
443     // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
444
445     // elevator
446     tmp = (float)(analog_in_data[4] - ATC_ELEVATOR_TRIM_CENTER) / 512.0f;
447     fgSetFloat( "/controls/elevator-trim", tmp );
448     // cout << "trim = " << analog_in_data[4] << " = " << tmp;
449
450     // trim
451     tmp = (float)(ATC_ELEVATOR_CENTER - analog_in_data[5]) / 100.0f;
452     fgSetFloat( "/controls/elevator", tmp );
453     // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
454
455     // mixture
456     tmp = (float)analog_in_data[7] / 680.0f;
457     fgSetFloat( "/controls/mixture[0]", tmp );
458
459     // throttle
460     tmp = (float)analog_in_data[8] / 690.0f;
461     fgSetFloat( "/controls/throttle[0]", tmp );
462
463     // nav1 volume
464     tmp = (float)analog_in_data[25] / 1024.0f;
465     fgSetFloat( "/radios/nav[0]/volume", tmp );
466
467     // nav2 volume
468     tmp = (float)analog_in_data[24] / 1024.0f;
469     fgSetFloat( "/radios/nav[1]/volume", tmp );
470
471     // adf volume
472     tmp = (float)analog_in_data[26] / 1024.0f;
473     fgSetFloat( "/radios/adf/on-off-volume", tmp );
474
475     // nav2 obs tuner
476     tmp = (float)analog_in_data[29] * 360.0f / 1024.0f;
477     fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
478
479     // nav1 obs tuner
480     tmp1 = (float)analog_in_data[30] * 360.0f / 1024.0f;
481     tmp2 = (float)analog_in_data[31] * 360.0f / 1024.0f;
482     fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp1 );
483
484     return true;
485 }
486
487
488 /////////////////////////////////////////////////////////////////////
489 // Write the lights
490 /////////////////////////////////////////////////////////////////////
491
492 bool FGATC610x::do_lights() {
493
494     ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
495     ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
496     ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
497
498     return true;
499 }
500
501
502 /////////////////////////////////////////////////////////////////////
503 // Read radio switches 
504 /////////////////////////////////////////////////////////////////////
505
506 bool FGATC610x::do_radio_switches() {
507     double freq, coarse_freq, fine_freq, value;
508     int diff;
509
510     ATC610xReadRadios( radios_fd, radio_switch_data );
511
512     // DME Switch
513     dme_switch = (radio_switch_data[7] >> 4) & 0x03;
514     if ( dme_switch == 0 ) {
515         // off
516         fgSetInt( "/radios/dme/switch-position", 0 );
517     } else if ( dme_switch == 2 ) {
518         // nav1
519         fgSetInt( "/radios/dme/switch-position", 1 );
520     } else if ( dme_switch == 1 ) {
521         // nav2
522         fgSetInt( "/radios/dme/switch-position", 3 );
523     }
524
525     // Com1 Swap
526     int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
527     static int last_com1_swap;
528     if ( com1_swap && (last_com1_swap != com1_swap) ) {
529         float tmp = com1_freq->getFloatValue();
530         fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
531                    com1_stby_freq->getFloatValue() );
532         fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
533     }
534     last_com1_swap = com1_swap;
535
536     // Com2 Swap
537     int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
538     static int last_com2_swap;
539     if ( com2_swap && (last_com2_swap != com2_swap) ) {
540         float tmp = com2_freq->getFloatValue();
541         fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
542                    com2_stby_freq->getFloatValue() );
543         fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
544     }
545     last_com2_swap = com2_swap;
546
547     // Nav1 Swap
548     int nav1_swap = radio_switch_data[11] & 0x01;
549     static int last_nav1_swap;
550     if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
551         float tmp = nav1_freq->getFloatValue();
552         fgSetFloat( "/radios/nav[0]/freqencies/selected-mhz",
553                    nav1_stby_freq->getFloatValue() );
554         fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
555     }
556     last_nav1_swap = nav1_swap;
557
558     // Nav2 Swap
559     int nav2_swap = !(radio_switch_data[19] & 0x01);
560     static int last_nav2_swap;
561     if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
562         float tmp = nav2_freq->getFloatValue();
563         fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
564                    nav2_stby_freq->getFloatValue() );
565         fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
566     }
567     last_nav2_swap = nav2_swap;
568
569     // Com1 Tuner
570     int com1_tuner_fine = ((radio_switch_data[5] >> 4) & 0x0f) - 1;
571     int com1_tuner_coarse = (radio_switch_data[5] & 0x0f) - 1;
572     static int last_com1_tuner_fine = com1_tuner_fine;
573     static int last_com1_tuner_coarse = com1_tuner_coarse;
574
575     freq = com1_stby_freq->getFloatValue();
576     coarse_freq = (int)freq;
577     fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
578
579     if ( com1_tuner_fine != last_com1_tuner_fine ) {
580         diff = com1_tuner_fine - last_com1_tuner_fine;
581         if ( abs(diff) > 4 ) {
582             // roll over
583             if ( com1_tuner_fine < last_com1_tuner_fine ) {
584                 // going up
585                 diff = 12 - last_com1_tuner_fine + com1_tuner_fine;
586             } else {
587                 // going down
588                 diff = com1_tuner_fine - 12 - last_com1_tuner_fine;
589             }
590         }
591         fine_freq += diff;
592     }
593     while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
594     while ( fine_freq < 0.0 )  { fine_freq += 40.0; }
595
596     if ( com1_tuner_coarse != last_com1_tuner_coarse ) {
597         diff = com1_tuner_coarse - last_com1_tuner_coarse;
598         if ( abs(diff) > 4 ) {
599             // roll over
600             if ( com1_tuner_coarse < last_com1_tuner_coarse ) {
601                 // going up
602                 diff = 12 - last_com1_tuner_coarse + com1_tuner_coarse;
603             } else {
604                 // going down
605                 diff = com1_tuner_coarse - 12 - last_com1_tuner_coarse;
606             }
607         }
608         coarse_freq += diff;
609     }
610     if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
611     if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
612
613     last_com1_tuner_fine = com1_tuner_fine;
614     last_com1_tuner_coarse = com1_tuner_coarse;
615
616     fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", 
617                 coarse_freq + fine_freq / 40.0 );
618
619     // Com2 Tuner
620     int com2_tuner_fine = ((radio_switch_data[13] >> 4) & 0x0f) - 1;
621     int com2_tuner_coarse = (radio_switch_data[13] & 0x0f) - 1;
622     static int last_com2_tuner_fine = com2_tuner_fine;
623     static int last_com2_tuner_coarse = com2_tuner_coarse;
624
625     freq = com2_stby_freq->getFloatValue();
626     coarse_freq = (int)freq;
627     fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
628
629     if ( com2_tuner_fine != last_com2_tuner_fine ) {
630         diff = com2_tuner_fine - last_com2_tuner_fine;
631         if ( abs(diff) > 4 ) {
632             // roll over
633             if ( com2_tuner_fine < last_com2_tuner_fine ) {
634                 // going up
635                 diff = 12 - last_com2_tuner_fine + com2_tuner_fine;
636             } else {
637                 // going down
638                 diff = com2_tuner_fine - 12 - last_com2_tuner_fine;
639             }
640         }
641         fine_freq += diff;
642     }
643     while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
644     while ( fine_freq < 0.0 )  { fine_freq += 40.0; }
645
646     if ( com2_tuner_coarse != last_com2_tuner_coarse ) {
647         diff = com2_tuner_coarse - last_com2_tuner_coarse;
648         if ( abs(diff) > 4 ) {
649             // roll over
650             if ( com2_tuner_coarse < last_com2_tuner_coarse ) {
651                 // going up
652                 diff = 12 - last_com2_tuner_coarse + com2_tuner_coarse;
653             } else {
654                 // going down
655                 diff = com2_tuner_coarse - 12 - last_com2_tuner_coarse;
656             }
657         }
658         coarse_freq += diff;
659     }
660     if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
661     if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
662
663     last_com2_tuner_fine = com2_tuner_fine;
664     last_com2_tuner_coarse = com2_tuner_coarse;
665
666     fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz",
667                 coarse_freq + fine_freq / 40.0 );
668
669     // Nav1 Tuner
670     int nav1_tuner_fine = ((radio_switch_data[9] >> 4) & 0x0f) - 1;
671     int nav1_tuner_coarse = (radio_switch_data[9] & 0x0f) - 1;
672     static int last_nav1_tuner_fine = nav1_tuner_fine;
673     static int last_nav1_tuner_coarse = nav1_tuner_coarse;
674
675     freq = nav1_stby_freq->getFloatValue();
676     coarse_freq = (int)freq;
677     fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
678
679     if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
680         diff = nav1_tuner_fine - last_nav1_tuner_fine;
681         if ( abs(diff) > 4 ) {
682             // roll over
683             if ( nav1_tuner_fine < last_nav1_tuner_fine ) {
684                 // going up
685                 diff = 12 - last_nav1_tuner_fine + nav1_tuner_fine;
686             } else {
687                 // going down
688                 diff = nav1_tuner_fine - 12 - last_nav1_tuner_fine;
689             }
690         }
691         fine_freq += diff;
692     }
693     while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
694     while ( fine_freq < 0.0 )  { fine_freq += 20.0; }
695
696     if ( nav1_tuner_coarse != last_nav1_tuner_coarse ) {
697         diff = nav1_tuner_coarse - last_nav1_tuner_coarse;
698         if ( abs(diff) > 4 ) {
699             // roll over
700             if ( nav1_tuner_coarse < last_nav1_tuner_coarse ) {
701                 // going up
702                 diff = 12 - last_nav1_tuner_coarse + nav1_tuner_coarse;
703             } else {
704                 // going down
705                 diff = nav1_tuner_coarse - 12 - last_nav1_tuner_coarse;
706             }
707         }
708         coarse_freq += diff;
709     }
710     if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
711     if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
712
713     last_nav1_tuner_fine = nav1_tuner_fine;
714     last_nav1_tuner_coarse = nav1_tuner_coarse;
715
716     fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz",
717                 coarse_freq + fine_freq / 20.0 );
718
719     // Nav2 Tuner
720     int nav2_tuner_fine = ((radio_switch_data[17] >> 4) & 0x0f) - 1;
721     int nav2_tuner_coarse = (radio_switch_data[17] & 0x0f) - 1;
722     static int last_nav2_tuner_fine = nav2_tuner_fine;
723     static int last_nav2_tuner_coarse = nav2_tuner_coarse;
724
725     freq = nav2_stby_freq->getFloatValue();
726     coarse_freq = (int)freq;
727     fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
728
729     if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
730         diff = nav2_tuner_fine - last_nav2_tuner_fine;
731         if ( abs(diff) > 4 ) {
732             // roll over
733             if ( nav2_tuner_fine < last_nav2_tuner_fine ) {
734                 // going up
735                 diff = 12 - last_nav2_tuner_fine + nav2_tuner_fine;
736             } else {
737                 // going down
738                 diff = nav2_tuner_fine - 12 - last_nav2_tuner_fine;
739             }
740         }
741         fine_freq += diff;
742     }
743     while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
744     while ( fine_freq < 0.0 )  { fine_freq += 20.0; }
745
746     if ( nav2_tuner_coarse != last_nav2_tuner_coarse ) {
747         diff = nav2_tuner_coarse - last_nav2_tuner_coarse;
748         if ( abs(diff) > 4 ) {
749             // roll over
750             if ( nav2_tuner_coarse < last_nav2_tuner_coarse ) {
751                 // going up
752                 diff = 12 - last_nav2_tuner_coarse + nav2_tuner_coarse;
753             } else {
754                 // going down
755                 diff = nav2_tuner_coarse - 12 - last_nav2_tuner_coarse;
756             }
757         }
758         coarse_freq += diff;
759     }
760     if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
761     if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
762
763     last_nav2_tuner_fine = nav2_tuner_fine;
764     last_nav2_tuner_coarse = nav2_tuner_coarse;
765
766     fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", 
767                 coarse_freq + fine_freq / 20.0);
768
769     // ADF Tuner
770     int adf_tuner_fine = ((radio_switch_data[21] >> 4) & 0x0f) - 1;
771     int adf_tuner_coarse = (radio_switch_data[21] & 0x0f) - 1;
772     static int last_adf_tuner_fine = adf_tuner_fine;
773     static int last_adf_tuner_coarse = adf_tuner_coarse;
774
775     if ( adf_count_mode->getIntValue() == 2 ) {
776         // tune count down timer
777         value = adf_elapsed_timer->getDoubleValue();
778     } else {
779         // tune frequency
780         if ( adf_stby_mode->getIntValue() == 1 ) {
781             value = adf_freq->getFloatValue();
782         } else {
783             value = adf_stby_freq->getFloatValue();
784         }
785     }
786
787     if ( adf_tuner_fine != last_adf_tuner_fine ) {
788         diff = adf_tuner_fine - last_adf_tuner_fine;
789         if ( abs(diff) > 4 ) {
790             // roll over
791             if ( adf_tuner_fine < last_adf_tuner_fine ) {
792                 // going up
793                 diff = 12 - last_adf_tuner_fine + adf_tuner_fine;
794             } else {
795                 // going down
796                 diff = adf_tuner_fine - 12 - last_adf_tuner_fine;
797             }
798         }
799         value += diff;
800     }
801
802     if ( adf_tuner_coarse != last_adf_tuner_coarse ) {
803         diff = adf_tuner_coarse - last_adf_tuner_coarse;
804         if ( abs(diff) > 4 ) {
805             // roll over
806             if ( adf_tuner_coarse < last_adf_tuner_coarse ) {
807                 // going up
808                 diff = 12 - last_adf_tuner_coarse + adf_tuner_coarse;
809             } else {
810                 // going down
811                 diff = adf_tuner_coarse - 12 - last_adf_tuner_coarse;
812             }
813         }
814         if ( adf_count_mode->getIntValue() == 2 ) {
815             value += 60 * diff;
816         } else {
817             value += 25 * diff;
818         }
819     }
820     if ( adf_count_mode->getIntValue() == 2 ) {
821         if ( value < 0 ) { value += 3600; }
822         if ( value > 3599 ) { value -= 3600; }
823     } else {
824         if ( value < 200 ) { value += 1600; }
825         if ( value > 1799 ) { value -= 1600; }
826     }
827  
828     last_adf_tuner_fine = adf_tuner_fine;
829     last_adf_tuner_coarse = adf_tuner_coarse;
830
831     if ( adf_count_mode->getIntValue() == 2 ) {
832         fgSetFloat( "/radios/adf/elapsed-timer", value );
833     } else {
834         if ( adf_stby_mode->getIntValue() == 1 ) {
835             fgSetFloat( "/radios/adf/frequencies/selected-khz", value );
836         } else {
837             fgSetFloat( "/radios/adf/frequencies/standby-khz", value );
838         }
839     }
840
841     // ADF Modes 
842     fgSetInt( "/radios/adf/adf-btn", !(radio_switch_data[23] & 0x01) );
843     fgSetInt( "/radios/adf/bfo-btn", !(radio_switch_data[23] >> 1 & 0x01) );
844     fgSetInt( "/radios/adf/frq-btn", !(radio_switch_data[23] >> 2 & 0x01) );
845     fgSetInt( "/radios/adf/flt-et-btn", !(radio_switch_data[23] >> 3 & 0x01) );
846     fgSetInt( "/radios/adf/set-rst-btn", !(radio_switch_data[23] >> 4 & 0x01) );
847     /* cout << "adf = " << !(radio_switch_data[23] & 0x01)
848          << " bfo = " << !(radio_switch_data[23] >> 1 & 0x01)
849          << " stby = " << !(radio_switch_data[23] >> 2 & 0x01)
850          << " timer = " << !(radio_switch_data[23] >> 3 & 0x01)
851          << " set/rst = " << !(radio_switch_data[23] >> 4 & 0x01)
852          << endl; */
853
854     return true;
855 }
856
857
858 /////////////////////////////////////////////////////////////////////
859 // Update the radio display 
860 /////////////////////////////////////////////////////////////////////
861
862 bool FGATC610x::do_radio_display() {
863
864     char digits[10];
865     int i;
866
867     if ( dme_switch != 0 ) {
868         // DME minutes
869         float minutes = dme_min->getFloatValue();
870         if ( minutes > 999 ) {
871             minutes = 999.0;
872         }
873         sprintf(digits, "%03.0f", minutes);
874         for ( i = 0; i < 6; ++i ) {
875             digits[i] -= '0';
876         }
877         radio_display_data[0] = digits[1] << 4 | digits[2];
878         radio_display_data[1] = 0xf0 | digits[0];
879         
880         // DME knots
881         float knots = dme_kt->getFloatValue();
882         if ( knots > 999 ) {
883             knots = 999.0;
884         }
885         sprintf(digits, "%03.0f", knots);
886         for ( i = 0; i < 6; ++i ) {
887             digits[i] -= '0';
888         }
889         radio_display_data[2] = digits[1] << 4 | digits[2];
890         radio_display_data[3] = 0xf0 | digits[0];
891
892         // DME distance (nm)
893         float nm = dme_nm->getFloatValue();
894         if ( nm > 99 ) {
895             nm = 99.0;
896         }
897         sprintf(digits, "%04.1f", nm);
898         for ( i = 0; i < 6; ++i ) {
899             digits[i] -= '0';
900         }
901         radio_display_data[4] = digits[1] << 4 | digits[3];
902         radio_display_data[5] = 0x00 | digits[0];
903         // the 0x00 in the upper nibble of the 6th byte of each
904         // display turns on the decimal point
905     } else {
906         // blank dem display
907         for ( i = 0; i < 6; ++i ) {
908             radio_display_data[i] = 0xff;
909         }
910     }
911
912     // Com1 standby frequency
913     float com1_stby = com1_stby_freq->getFloatValue();
914     if ( fabs(com1_stby) > 999.99 ) {
915         com1_stby = 0.0;
916     }
917     sprintf(digits, "%06.3f", com1_stby);
918     for ( i = 0; i < 6; ++i ) {
919         digits[i] -= '0';
920     }
921     radio_display_data[6] = digits[4] << 4 | digits[5];
922     radio_display_data[7] = digits[1] << 4 | digits[2];
923     radio_display_data[8] = 0xf0 | digits[0];
924
925     // Com1 in use frequency
926     float com1 = com1_freq->getFloatValue();
927     if ( fabs(com1) > 999.99 ) {
928         com1 = 0.0;
929     }
930     sprintf(digits, "%06.3f", com1);
931     for ( i = 0; i < 6; ++i ) {
932         digits[i] -= '0';
933     }
934     radio_display_data[9] = digits[4] << 4 | digits[5];
935     radio_display_data[10] = digits[1] << 4 | digits[2];
936     radio_display_data[11] = 0x00 | digits[0];
937     // the 0x00 in the upper nibble of the 6th byte of each display
938     // turns on the decimal point
939
940     // Com2 standby frequency
941     float com2_stby = com2_stby_freq->getFloatValue();
942     if ( fabs(com2_stby) > 999.99 ) {
943         com2_stby = 0.0;
944     }
945     sprintf(digits, "%06.3f", com2_stby);
946     for ( i = 0; i < 6; ++i ) {
947         digits[i] -= '0';
948     }
949     radio_display_data[18] = digits[4] << 4 | digits[5];
950     radio_display_data[19] = digits[1] << 4 | digits[2];
951     radio_display_data[20] = 0xf0 | digits[0];
952
953     // Com2 in use frequency
954     float com2 = com2_freq->getFloatValue();
955     if ( fabs(com2) > 999.99 ) {
956         com2 = 0.0;
957     }
958     sprintf(digits, "%06.3f", com2);
959     for ( i = 0; i < 6; ++i ) {
960         digits[i] -= '0';
961     }
962     radio_display_data[21] = digits[4] << 4 | digits[5];
963     radio_display_data[22] = digits[1] << 4 | digits[2];
964     radio_display_data[23] = 0x00 | digits[0];
965     // the 0x00 in the upper nibble of the 6th byte of each display
966     // turns on the decimal point
967
968     // Nav1 standby frequency
969     float nav1_stby = nav1_stby_freq->getFloatValue();
970     if ( fabs(nav1_stby) > 999.99 ) {
971         nav1_stby = 0.0;
972     }
973     sprintf(digits, "%06.2f", nav1_stby);
974     for ( i = 0; i < 6; ++i ) {
975         digits[i] -= '0';
976     }
977     radio_display_data[12] = digits[4] << 4 | digits[5];
978     radio_display_data[13] = digits[1] << 4 | digits[2];
979     radio_display_data[14] = 0xf0 | digits[0];
980
981     // Nav1 in use frequency
982     float nav1 = nav1_freq->getFloatValue();
983     if ( fabs(nav1) > 999.99 ) {
984         nav1 = 0.0;
985     }
986     sprintf(digits, "%06.2f", nav1);
987     for ( i = 0; i < 6; ++i ) {
988         digits[i] -= '0';
989     }
990     radio_display_data[15] = digits[4] << 4 | digits[5];
991     radio_display_data[16] = digits[1] << 4 | digits[2];
992     radio_display_data[17] = 0x00 | digits[0];
993     // the 0x00 in the upper nibble of the 6th byte of each display
994     // turns on the decimal point
995
996     // Nav2 standby frequency
997     float nav2_stby = nav2_stby_freq->getFloatValue();
998     if ( fabs(nav2_stby) > 999.99 ) {
999         nav2_stby = 0.0;
1000     }
1001     sprintf(digits, "%06.2f", nav2_stby);
1002     for ( i = 0; i < 6; ++i ) {
1003         digits[i] -= '0';
1004     }
1005     radio_display_data[24] = digits[4] << 4 | digits[5];
1006     radio_display_data[25] = digits[1] << 4 | digits[2];
1007     radio_display_data[26] = 0xf0 | digits[0];
1008
1009     // Nav2 in use frequency
1010     float nav2 = nav2_freq->getFloatValue();
1011     if ( fabs(nav2) > 999.99 ) {
1012         nav2 = 0.0;
1013     }
1014     sprintf(digits, "%06.2f", nav2);
1015     for ( i = 0; i < 6; ++i ) {
1016         digits[i] -= '0';
1017     }
1018     radio_display_data[27] = digits[4] << 4 | digits[5];
1019     radio_display_data[28] = digits[1] << 4 | digits[2];
1020     radio_display_data[29] = 0x00 | digits[0];
1021     // the 0x00 in the upper nibble of the 6th byte of each display
1022     // turns on the decimal point
1023
1024     // ADF standby frequency / timer
1025     if ( adf_on_off_vol->getDoubleValue() >= 0.01 ) {
1026         if ( adf_stby_mode->getIntValue() == 0 ) {
1027             // frequency
1028             float adf_stby = adf_stby_freq->getFloatValue();
1029             if ( fabs(adf_stby) > 1799 ) {
1030                 adf_stby = 1799;
1031             }
1032             sprintf(digits, "%04.0f", adf_stby);
1033             for ( i = 0; i < 6; ++i ) {
1034                 digits[i] -= '0';
1035             }
1036             radio_display_data[30] = digits[3] << 4 | 0x0f;
1037             radio_display_data[31] = digits[1] << 4 | digits[2];
1038             if ( digits[0] == 0 ) {
1039                 radio_display_data[32] = 0xff;
1040             } else {
1041                 radio_display_data[32] = 0xf0 | digits[0];
1042             }
1043         } else {
1044             // timer
1045             double time;
1046             int hours, min, sec;
1047             if ( adf_timer_mode->getIntValue() == 0 ) {
1048                 time = adf_flight_timer->getDoubleValue();
1049             } else {
1050                 time = adf_elapsed_timer->getDoubleValue();
1051             }
1052             // cout << time << endl;
1053             hours = (int)(time / 3600.0);
1054             time -= hours * 3600.00;
1055             min = (int)(time / 60.0);
1056             time -= min * 60.0;
1057             sec = (int)time;
1058             int big, small;
1059             if ( hours > 0 ) {
1060                 big = hours;
1061                 if ( big > 99 ) {
1062                     big = 99;
1063                 }
1064                 small = min;
1065             } else {
1066                 big = min;
1067                 small = sec;
1068             }
1069             if ( big > 99 ) {
1070                 big = 99;
1071             }
1072             // cout << big << ":" << small << endl;
1073             sprintf(digits, "%02d%02d", big, small);
1074             for ( i = 0; i < 6; ++i ) {
1075                 digits[i] -= '0';
1076             }
1077             radio_display_data[30] = digits[3] << 4 | 0x0f;
1078             radio_display_data[31] = digits[1] << 4 | digits[2];
1079             radio_display_data[32] = 0xf0 | digits[0];
1080         }
1081
1082         // ADF in use frequency
1083         float adf = adf_freq->getFloatValue();
1084         if ( fabs(adf) > 1799 ) {
1085             adf = 1799;
1086         }
1087         sprintf(digits, "%04.0f", adf);
1088         for ( i = 0; i < 6; ++i ) {
1089             digits[i] -= '0';
1090         }
1091         radio_display_data[33] = digits[2] << 4 | digits[3];
1092         if ( digits[0] == 0 ) {
1093             radio_display_data[34] = 0xf0 | digits[1];
1094         } else {
1095             radio_display_data[34] = digits[0] << 4 | digits[1];
1096         }
1097     } else {
1098         radio_display_data[30] = 0xff;
1099         radio_display_data[31] = 0xff;
1100         radio_display_data[32] = 0xff;
1101         radio_display_data[33] = 0xff;
1102         radio_display_data[34] = 0xff;
1103     }
1104     
1105
1106     ATC610xSetRadios( radios_fd, radio_display_data );
1107
1108     return true;
1109 }
1110
1111
1112 /////////////////////////////////////////////////////////////////////
1113 // Drive the stepper motors
1114 /////////////////////////////////////////////////////////////////////
1115
1116 bool FGATC610x::do_steppers() {
1117     float diff = mag_compass->getFloatValue() - compass_position;
1118     while ( diff < -180.0 ) { diff += 360.0; }
1119     while ( diff >  180.0 ) { diff -= 360.0; }
1120
1121     int steps = (int)(diff * 4);
1122     // cout << "steps = " << steps << endl;
1123     if ( steps > 4 ) { steps = 4; }
1124     if ( steps < -4 ) { steps = -4; }
1125
1126     if ( abs(steps) > 0 ) {
1127         unsigned char cmd = 0x80;       // stepper command
1128         if ( steps > 0 ) {
1129             cmd |= 0x20;                // go up
1130         } else {
1131             cmd |= 0x00;                // go down
1132         }
1133         cmd |= abs(steps);
1134
1135         // sync compass_position with hardware position
1136         compass_position += (float)steps / 4.0;
1137
1138         ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1139     }
1140
1141     return true;
1142 }
1143
1144
1145 /////////////////////////////////////////////////////////////////////
1146 // Read the switch positions
1147 /////////////////////////////////////////////////////////////////////
1148
1149 // decode the packed switch data
1150 static void update_switch_matrix(
1151         int board,
1152         unsigned char switch_data[ATC_SWITCH_BYTES],
1153         int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
1154 {
1155     for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
1156         unsigned char switches = switch_data[row];
1157
1158         for( int column = 0; column < ATC_NUM_COLS; ++column ) {
1159             switch_matrix[board][column][row] = switches & 1;
1160             switches = switches >> 1;
1161         }                       
1162     }
1163 }                     
1164
1165 bool FGATC610x::do_switches() {
1166     ATC610xReadSwitches( switches_fd, switch_data );
1167
1168     // unpack the switch data
1169     int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
1170     update_switch_matrix( board, switch_data, switch_matrix );
1171
1172     // magnetos and starter switch
1173     int magnetos = 0;
1174     bool starter = false;
1175     if ( switch_matrix[board][3][1] == 1 ) {
1176         magnetos = 3;
1177         starter = true;
1178     } else if ( switch_matrix[board][2][1] == 1 ) {
1179         magnetos = 3;
1180         starter = false;
1181     } else if ( switch_matrix[board][1][1] == 1 ) {
1182         magnetos = 2;
1183         starter = false;
1184     } else if ( switch_matrix[board][0][1] == 1 ) {
1185         magnetos = 1;
1186         starter = false;
1187     } else {
1188         magnetos = 0;
1189         starter = false;
1190     }
1191
1192     // flaps
1193     float flaps = 0.0;
1194     if ( switch_matrix[board][6][3] == 1 ) {
1195         flaps = 1.0;
1196     } else if ( switch_matrix[board][5][3] == 1 ) {
1197         flaps = 2.0 / 3.0;
1198     } else if ( switch_matrix[board][4][3] == 1 ) {
1199         flaps = 1.0 / 3.0;
1200     } else if ( switch_matrix[board][4][3] == 0 ) {
1201         flaps = 0.0;
1202     }
1203
1204     // do a bit of filtering on the magneto/starter switch and the
1205     // flap lever because these are not well debounced in hardware
1206     static int mag1, mag2, mag3;
1207     mag3 = mag2;
1208     mag2 = mag1;
1209     mag1 = magnetos;
1210     if ( mag1 == mag2 && mag2 == mag3 ) {
1211         fgSetInt( "/controls/magnetos[0]", magnetos );
1212     }
1213
1214     static bool start1, start2, start3;
1215     start3 = start2;
1216     start2 = start1;
1217     start1 = starter;
1218     if ( start1 == start2 && start2 == start3 ) {
1219         fgSetBool( "/controls/starter[0]", starter );
1220     }
1221
1222     static float flap1, flap2, flap3;
1223     flap3 = flap2;
1224     flap2 = flap1;
1225     flap1 = flaps;
1226     if ( flap1 == flap2 && flap2 == flap3 ) {
1227         fgSetFloat( "/controls/flaps", flaps );
1228     }
1229
1230     return true;
1231 }
1232
1233
1234 bool FGATC610x::process() {
1235
1236     // Lock the hardware, skip if it's not ready yet
1237     if ( ATC610xLock( lock_fd ) > 0 ) {
1238
1239         do_analog_in();
1240         do_lights();
1241         do_radio_switches();
1242         do_radio_display();
1243         do_steppers();
1244         do_switches();
1245         
1246         ATC610xRelease( lock_fd );
1247
1248         return true;
1249     } else {
1250         return false;
1251     }
1252 }
1253
1254
1255 bool FGATC610x::close() {
1256
1257     return true;
1258 }