]> git.mxchange.org Git - flightgear.git/blob - src/Network/atc610x.cxx
Add support for cygwin
[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 )
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 #if defined( _MSC_VER )
56 #  define snprintf _snprintf
57 #endif
58
59 // Lock the ATC 610 hardware
60 static int ATC610xLock( int fd ) {
61     // rewind
62     lseek( fd, 0, SEEK_SET );
63
64     char tmp[2];
65     int result = read( fd, tmp, 1 );
66     if ( result != 1 ) {
67         SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
68     }
69
70     return result;
71 }
72
73
74 // Write a radios command
75 static int ATC610xRelease( int fd ) {
76     // rewind
77     lseek( fd, 0, SEEK_SET );
78
79     char tmp[2];
80     tmp[0] = tmp[1] = 0;
81     int result = write( fd, tmp, 1 );
82
83     if ( result != 1 ) {
84         SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
85     }
86
87     return result;
88 }
89
90
91 // Read analog inputs
92 static void ATC610xReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
93     // rewind
94     lseek( fd, 0, SEEK_SET );
95
96     int result = read( fd, analog_in_bytes, ATC_ANAL_IN_BYTES );
97     if ( result != ATC_ANAL_IN_BYTES ) {
98         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
99         exit( -1 );
100     }
101 }
102
103
104 // Write a radios command
105 static int ATC610xSetRadios( int fd,
106                              unsigned char data[ATC_RADIO_DISPLAY_BYTES] )
107 {
108     // rewind
109     lseek( fd, 0, SEEK_SET );
110
111     int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
112
113     if ( result != ATC_RADIO_DISPLAY_BYTES ) {
114         SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
115     }
116
117     return result;
118 }
119
120
121 // Read status of last radios written to
122 static void ATC610xReadRadios( int fd, unsigned char *switch_data ) {
123     // rewind
124     lseek( fd, 0, SEEK_SET );
125
126     int result = read( fd, switch_data, ATC_RADIO_SWITCH_BYTES );
127     if ( result != ATC_RADIO_SWITCH_BYTES ) {
128         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
129         exit( -1 );
130     }
131 }
132
133 // Write a stepper command
134 static int ATC610xSetStepper( int fd, unsigned char channel,
135                               unsigned char value )
136 {
137     // rewind
138     lseek( fd, 0, SEEK_SET );
139
140     // Write the value
141     unsigned char buf[3];
142     buf[0] = channel;
143     buf[1] = value;
144     buf[2] = 0;
145     int result = write( fd, buf, 2 );
146     if ( result != 2 ) {
147         SG_LOG( SG_IO, SG_INFO, "Write failed" );
148     }
149     SG_LOG( SG_IO, SG_DEBUG,
150             "Sent cmd = " << (int)channel << " value = " << (int)value );
151     return result;
152 }
153
154
155 // Read status of last stepper written to
156 static unsigned char ATC610xReadStepper( int fd ) {
157     int result;
158
159     // rewind
160     lseek( fd, 0, SEEK_SET );
161
162     // Write the value
163     unsigned char buf[2];
164     result = read( fd, buf, 1 );
165     if ( result != 1 ) {
166         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
167         exit( -1 );
168     }
169     SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
170
171     return buf[0];
172 }
173
174
175 // Read switch inputs
176 static void ATC610xReadSwitches( int fd, unsigned char *switch_bytes ) {
177     // rewind
178     lseek( fd, 0, SEEK_SET );
179
180     int result = read( fd, switch_bytes, ATC_SWITCH_BYTES );
181     if ( result != ATC_SWITCH_BYTES ) {
182         SG_LOG( SG_IO, SG_ALERT, "Read failed" );
183         exit( -1 );
184     }
185 }
186
187
188 // Open and initialize ATC 610x hardware
189 bool FGATC610x::open() {
190     if ( is_enabled() ) {
191         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
192                 << "is already in use, ignoring" );
193         return false;
194     }
195
196     SG_LOG( SG_IO, SG_ALERT,
197             "Initializing ATC 610x hardware, please wait ..." );
198
199     set_hz( 30 );               // default to processing requests @ 30Hz
200     set_enabled( true );
201
202     board = 0;                  // 610x uses a single board number = 0
203
204     snprintf( lock_file, 256, "/proc/atc610x/board%d/lock", board );
205     snprintf( analog_in_file, 256, "/proc/atc610x/board%d/analog_in", board );
206     snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
207     snprintf( stepper_file, 256, "/proc/atc610x/board%d/steppers", board );
208     snprintf( switches_file, 256, "/proc/atc610x/board%d/switches", board );
209
210     /////////////////////////////////////////////////////////////////////
211     // Open the /proc files
212     /////////////////////////////////////////////////////////////////////
213
214     lock_fd = ::open( lock_file, O_RDWR );
215     if ( lock_fd == -1 ) {
216         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
217         char msg[256];
218         snprintf( msg, 256, "Error opening %s", lock_file );
219         perror( msg );
220         exit( -1 );
221     }
222
223     analog_in_fd = ::open( analog_in_file, O_RDONLY );
224     if ( analog_in_fd == -1 ) {
225         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
226         char msg[256];
227         snprintf( msg, 256, "Error opening %s", analog_in_file );
228         perror( msg );
229         exit( -1 );
230     }
231
232     radios_fd = ::open( radios_file, O_RDWR );
233     if ( radios_fd == -1 ) {
234         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
235         char msg[256];
236         snprintf( msg, 256, "Error opening %s", radios_file );
237         perror( msg );
238         exit( -1 );
239     }
240
241     stepper_fd = ::open( stepper_file, O_RDWR );
242     if ( stepper_fd == -1 ) {
243         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
244         char msg[256];
245         snprintf( msg, 256, "Error opening %s", stepper_file );
246         perror( msg );
247         exit( -1 );
248     }
249
250     switches_fd = ::open( switches_file, O_RDONLY );
251     if ( switches_fd == -1 ) {
252         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
253         char msg[256];
254         snprintf( msg, 256, "Error opening %s", switches_file );
255         perror( msg );
256         exit( -1 );
257     }
258
259     /////////////////////////////////////////////////////////////////////
260     // Home the compass stepper motor
261     /////////////////////////////////////////////////////////////////////
262
263     SG_LOG( SG_IO, SG_ALERT,
264             "  - Homing the compass stepper motor" );
265
266     // Lock the hardware, keep trying until we succeed
267     while ( ATC610xLock( lock_fd ) <= 0 );
268
269     // Send the stepper home command
270     ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
271
272     // Release the hardware
273     ATC610xRelease( lock_fd );
274
275     bool home = false;
276     while ( ! home ) {
277         SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
278
279         while ( ATC610xLock( lock_fd ) <= 0 );
280
281         unsigned char result = ATC610xReadStepper( stepper_fd );
282         if ( result == 0 ) {
283             home = true;
284         }
285
286         ATC610xRelease( lock_fd );
287
288 #if defined( _MSC_VER )
289         ulMilliSecondSleep(33);
290 #elif defined (WIN32) && !defined(__CYGWIN__)
291         Sleep (33);
292 #else
293         usleep(33);
294 #endif
295     }
296
297     compass_position = 0.0;
298
299     /////////////////////////////////////////////////////////////////////
300     // Blank the radio display
301     /////////////////////////////////////////////////////////////////////
302
303     SG_LOG( SG_IO, SG_ALERT,
304             "  - Clearing the radios displays." );
305
306     // Prepair the data
307     unsigned char value = 0xff;
308     for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
309         radio_display_data[channel] = value;
310     }
311
312     // Lock the hardware, keep trying until we succeed
313     while ( ATC610xLock( lock_fd ) <= 0 );
314
315     // Set radio display
316     ATC610xSetRadios( radios_fd, radio_display_data );
317
318     ATC610xRelease( lock_fd );
319
320     /////////////////////////////////////////////////////////////////////
321     // Finished initing hardware
322     /////////////////////////////////////////////////////////////////////
323
324     SG_LOG( SG_IO, SG_ALERT,
325             "Done initializing ATC 610x hardware." );
326
327     /////////////////////////////////////////////////////////////////////
328     // Connect up to property values
329     /////////////////////////////////////////////////////////////////////
330
331     mag_compass = fgGetNode( "/steam/mag-compass-deg", true );
332
333     dme_min = fgGetNode( "/radios/dme/ete-min", true );
334     dme_kt = fgGetNode( "/radios/dme/speed-kt", true );
335     dme_nm = fgGetNode( "/radios/dme/distance-nm", true );
336
337     com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
338     com1_stby_freq
339         = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
340     com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
341     com2_stby_freq
342         = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
343
344     nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
345     nav1_stby_freq
346         = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
347
348     nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
349     nav2_stby_freq
350         = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
351
352     adf_freq = fgGetNode( "/radios/adf/frequencies/selected-khz", true );
353     adf_stby_freq = fgGetNode( "/radios/adf/frequencies/standby-khz", true );
354
355     return true;
356 }
357
358
359 /////////////////////////////////////////////////////////////////////
360 // Read analog inputs
361 /////////////////////////////////////////////////////////////////////
362
363 #define ATC_AILERON_CENTER 535
364 #define ATC_ELEVATOR_TRIM_CENTER 512
365 #define ATC_ELEVATOR_CENTER 543
366
367 bool FGATC610x::do_analog_in() {
368     // Read raw data in byte form
369     ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
370
371     // Convert to integer values
372     for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
373         unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
374         unsigned char lo = analog_in_bytes[2 * channel + 1];
375         analog_in_data[channel] = hi * 256 + lo;
376
377         // printf("%02x %02x ", hi, lo );
378         // printf("%04d ", value );
379     }
380
381     float tmp, tmp1, tmp2;
382
383     // aileron
384     tmp = (float)(analog_in_data[0] - ATC_AILERON_CENTER) / 256.0f;
385     fgSetFloat( "/controls/aileron", tmp );
386     // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
387
388     // elevator
389     tmp = (float)(analog_in_data[4] - ATC_ELEVATOR_TRIM_CENTER) / 512.0f;
390     fgSetFloat( "/controls/elevator-trim", tmp );
391     // cout << "trim = " << analog_in_data[4] << " = " << tmp;
392
393     // trim
394     tmp = (float)(ATC_ELEVATOR_CENTER - analog_in_data[5]) / 100.0f;
395     fgSetFloat( "/controls/elevator", tmp );
396     // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
397
398     // mixture
399     tmp = (float)analog_in_data[7] / 680.0f;
400     fgSetFloat( "/controls/mixture[0]", tmp );
401
402     // throttle
403     tmp = (float)analog_in_data[8] / 690.0f;
404     fgSetFloat( "/controls/throttle[0]", tmp );
405
406     // nav1 volume
407     tmp = (float)analog_in_data[25] / 1024.0f;
408     fgSetFloat( "/radios/nav[0]/volume", tmp );
409
410     // nav2 volume
411     tmp = (float)analog_in_data[24] / 1024.0f;
412     fgSetFloat( "/radios/nav[1]/volume", tmp );
413
414     // adf volume
415     tmp = (float)analog_in_data[26] / 1024.0f;
416     fgSetFloat( "/radios/adf/volume", tmp );
417
418     // nav2 obs tuner
419     tmp = (float)analog_in_data[29] * 360.0f / 1024.0f;
420     fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
421
422     // nav1 obs tuner
423     tmp1 = (float)analog_in_data[30] * 360.0f / 1024.0f;
424     tmp2 = (float)analog_in_data[31] * 360.0f / 1024.0f;
425     fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp1 );
426
427     return true;
428 }
429
430
431 /////////////////////////////////////////////////////////////////////
432 // Read radio switches 
433 /////////////////////////////////////////////////////////////////////
434
435 bool FGATC610x::do_radio_switches() {
436     float freq, inc;
437
438     ATC610xReadRadios( radios_fd, radio_switch_data );
439
440     // DME Switch
441     dme_switch = (radio_switch_data[7] >> 4) & 0x03;
442     if ( dme_switch == 0 ) {
443         // off
444         fgSetInt( "/radios/dme/switch-position", 0 );
445     } else if ( dme_switch == 2 ) {
446         // nav1
447         fgSetInt( "/radios/dme/switch-position", 1 );
448     } else if ( dme_switch == 1 ) {
449         // nav2
450         fgSetInt( "/radios/dme/switch-position", 3 );
451     }
452
453     // Com1 Swap
454     int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
455     static int last_com1_swap;
456     if ( com1_swap && (last_com1_swap != com1_swap) ) {
457         float tmp = com1_freq->getFloatValue();
458         fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
459                    com1_stby_freq->getFloatValue() );
460         fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
461     }
462     last_com1_swap = com1_swap;
463
464     // Com2 Swap
465     int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
466     static int last_com2_swap;
467     if ( com2_swap && (last_com2_swap != com2_swap) ) {
468         float tmp = com2_freq->getFloatValue();
469         fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
470                    com2_stby_freq->getFloatValue() );
471         fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
472     }
473     last_com2_swap = com2_swap;
474
475     // Nav1 Swap
476     int nav1_swap = radio_switch_data[11] & 0x01;
477     static int last_nav1_swap;
478     if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
479         float tmp = nav1_freq->getFloatValue();
480         fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
481                    nav1_stby_freq->getFloatValue() );
482         fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
483     }
484     last_nav1_swap = nav1_swap;
485
486     // Nav2 Swap
487     int nav2_swap = !(radio_switch_data[19] & 0x01);
488     static int last_nav2_swap;
489     if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
490         float tmp = nav2_freq->getFloatValue();
491         fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
492                    nav2_stby_freq->getFloatValue() );
493         fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
494     }
495     last_nav2_swap = nav2_swap;
496
497     // Com1 Tuner
498     int com1_tuner_fine = (radio_switch_data[5] >> 4) & 0x0f;
499     int com1_tuner_course = radio_switch_data[5] & 0x0f;
500     // cout << "com1 = " << com1_tuner_fine << " " << com1_tuner_course << endl;
501     static int last_com1_tuner_fine = com1_tuner_fine;
502     static int last_com1_tuner_course = com1_tuner_course;
503     inc = 0.0;
504     if ( com1_tuner_fine != last_com1_tuner_fine ) {
505         if ( com1_tuner_fine == 0x0c && last_com1_tuner_fine == 0x01 ) {
506             inc = -0.025;
507         } else if ( com1_tuner_fine == 0x01 && last_com1_tuner_fine == 0x0c ) {
508             inc = -0.025;
509         } else if ( com1_tuner_fine > last_com1_tuner_fine ) {
510             inc = 0.025;
511         } else {
512             inc = -0.025;
513         }
514     }
515     if ( com1_tuner_course != last_com1_tuner_course ) {
516         if ( com1_tuner_course == 0x0c && last_com1_tuner_course == 0x01 ) {
517             inc = -1.0;
518         } else if ( com1_tuner_course == 0x01
519                     && last_com1_tuner_course == 0x0c ) {
520             inc = -1.0;
521         } else if ( com1_tuner_course > last_com1_tuner_course ) {
522             inc = 1.0;
523         } else {
524             inc = -1.0;
525         }
526     }
527     last_com1_tuner_fine = com1_tuner_fine;
528     last_com1_tuner_course = com1_tuner_course;
529
530     freq = com1_stby_freq->getFloatValue() + inc;
531     if ( freq < 0.0 ) {
532         freq = 140.0;
533     }
534     if ( freq > 140.0 ) {
535         freq = 0.0;
536     }
537     fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", freq );
538
539     // Com2 Tuner
540     int com2_tuner_fine = (radio_switch_data[13] >> 4) & 0x0f;
541     int com2_tuner_course = radio_switch_data[13] & 0x0f;
542     static int last_com2_tuner_fine = com2_tuner_fine;
543     static int last_com2_tuner_course = com2_tuner_course;
544     inc = 0.0;
545     if ( com2_tuner_fine != last_com2_tuner_fine ) {
546         if ( com2_tuner_fine == 0x0c && last_com2_tuner_fine == 0x01 ) {
547             inc = -0.025;
548         } else if ( com2_tuner_fine == 0x01 && last_com2_tuner_fine == 0x0c ) {
549             inc = -0.025;
550         } else if ( com2_tuner_fine > last_com2_tuner_fine ) {
551             inc = 0.025;
552         } else {
553             inc = -0.025;
554         }
555     }
556     if ( com2_tuner_course != last_com2_tuner_course ) {
557         if ( com2_tuner_course == 0x0c && last_com2_tuner_course == 0x01 ) {
558             inc = -1.0;
559         } else if ( com2_tuner_course == 0x01
560                     && last_com2_tuner_course == 0x0c ) {
561             inc = -1.0;
562         } else if ( com2_tuner_course > last_com2_tuner_course ) {
563             inc = 1.0;
564         } else {
565             inc = -1.0;
566         }
567     }
568     last_com2_tuner_fine = com2_tuner_fine;
569     last_com2_tuner_course = com2_tuner_course;
570
571     freq = com2_stby_freq->getFloatValue() + inc;
572     if ( freq < 0.0 ) {
573         freq = 140.0;
574     }
575     if ( freq > 140.0 ) {
576         freq = 0.0;
577     }
578     fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", freq );
579
580     // Nav1 Tuner
581     int nav1_tuner_fine = (radio_switch_data[9] >> 4) & 0x0f;
582     int nav1_tuner_course = radio_switch_data[9] & 0x0f;
583     static int last_nav1_tuner_fine = nav1_tuner_fine;
584     static int last_nav1_tuner_course = nav1_tuner_course;
585     inc = 0.0;
586     if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
587         if ( nav1_tuner_fine == 0x0c && last_nav1_tuner_fine == 0x01 ) {
588             inc = -0.05;
589         } else if ( nav1_tuner_fine == 0x01 && last_nav1_tuner_fine == 0x0c ) {
590             inc = -0.05;
591         } else if ( nav1_tuner_fine > last_nav1_tuner_fine ) {
592             inc = 0.05;
593         } else {
594             inc = -0.05;
595         }
596     }
597     if ( nav1_tuner_course != last_nav1_tuner_course ) {
598         if ( nav1_tuner_course == 0x0c && last_nav1_tuner_course == 0x01 ) {
599             inc = -1.0;
600         } else if ( nav1_tuner_course == 0x01
601                     && last_nav1_tuner_course == 0x0c ) {
602             inc = -1.0;
603         } else if ( nav1_tuner_course > last_nav1_tuner_course ) {
604             inc = 1.0;
605         } else {
606             inc = -1.0;
607         }
608     }
609     last_nav1_tuner_fine = nav1_tuner_fine;
610     last_nav1_tuner_course = nav1_tuner_course;
611
612     freq = nav1_stby_freq->getFloatValue() + inc;
613     if ( freq < 108.0 ) {
614         freq = 117.95;
615     }
616     if ( freq > 117.95 ) {
617         freq = 108.0;
618     }
619     fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", freq );
620
621     // Nav2 Tuner
622     int nav2_tuner_fine = (radio_switch_data[17] >> 4) & 0x0f;
623     int nav2_tuner_course = radio_switch_data[17] & 0x0f;
624     static int last_nav2_tuner_fine = nav2_tuner_fine;
625     static int last_nav2_tuner_course = nav2_tuner_course;
626     inc = 0.0;
627     if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
628         if ( nav2_tuner_fine == 0x0c && last_nav2_tuner_fine == 0x01 ) {
629             inc = -0.05;
630         } else if ( nav2_tuner_fine == 0x01 && last_nav2_tuner_fine == 0x0c ) {
631             inc = -0.05;
632         } else if ( nav2_tuner_fine > last_nav2_tuner_fine ) {
633             inc = 0.05;
634         } else {
635             inc = -0.05;
636         }
637     }
638     if ( nav2_tuner_course != last_nav2_tuner_course ) {
639         if ( nav2_tuner_course == 0x0c && last_nav2_tuner_course == 0x01 ) {
640             inc = -1.0;
641         } else if ( nav2_tuner_course == 0x01
642                     && last_nav2_tuner_course == 0x0c ) {
643             inc = -1.0;
644         } else if ( nav2_tuner_course > last_nav2_tuner_course ) {
645             inc = 1.0;
646         } else {
647             inc = -1.0;
648         }
649     }
650     last_nav2_tuner_fine = nav2_tuner_fine;
651     last_nav2_tuner_course = nav2_tuner_course;
652
653     freq = nav2_stby_freq->getFloatValue() + inc;
654     if ( freq < 108.0 ) {
655         freq = 117.95;
656     }
657     if ( freq > 117.95 ) {
658         freq = 108.0;
659     }
660     fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", freq );
661
662     // ADF Tuner
663     int adf_tuner_fine = (radio_switch_data[21] >> 4) & 0x0f;
664     int adf_tuner_course = radio_switch_data[21] & 0x0f;
665     // cout << "adf = " << adf_tuner_fine << " " << adf_tuner_course << endl;
666     static int last_adf_tuner_fine = adf_tuner_fine;
667     static int last_adf_tuner_course = adf_tuner_course;
668     inc = 0.0;
669     if ( adf_tuner_fine != last_adf_tuner_fine ) {
670         if ( adf_tuner_fine == 0x0c && last_adf_tuner_fine == 0x01 ) {
671             inc = -1.0;
672         } else if ( adf_tuner_fine == 0x01 && last_adf_tuner_fine == 0x0c ) {
673             inc = -1.0;
674         } else if ( adf_tuner_fine > last_adf_tuner_fine ) {
675             inc = 1.0;
676         } else {
677             inc = -1.0;
678         }
679     }
680     if ( adf_tuner_course != last_adf_tuner_course ) {
681         if ( adf_tuner_course == 0x0c && last_adf_tuner_course == 0x01 ) {
682             inc = -25.0;
683         } else if ( adf_tuner_course == 0x01
684                     && last_adf_tuner_course == 0x0c ) {
685             inc = -25.0;
686         } else if ( adf_tuner_course > last_adf_tuner_course ) {
687             inc = 25.0;
688         } else {
689             inc = -25.0;
690         }
691     }
692     last_adf_tuner_fine = adf_tuner_fine;
693     last_adf_tuner_course = adf_tuner_course;
694
695     freq = adf_freq->getFloatValue() + inc;
696     if ( freq < 100.0 ) {
697         freq = 1299;
698     }
699     if ( freq > 1299 ) {
700         freq = 100.0;
701     }
702     fgSetFloat( "/radios/adf/frequencies/selected-khz", freq );
703
704     return true;
705 }
706
707
708 /////////////////////////////////////////////////////////////////////
709 // Update the radio display 
710 /////////////////////////////////////////////////////////////////////
711
712 bool FGATC610x::do_radio_display() {
713
714     char digits[10];
715     int i;
716
717     if ( dme_switch != 0 ) {
718         // DME minutes
719         float minutes = dme_min->getFloatValue();
720         if ( minutes > 999 ) {
721             minutes = 999.0;
722         }
723         sprintf(digits, "%03.0f", minutes);
724         for ( i = 0; i < 6; ++i ) {
725             digits[i] -= '0';
726         }
727         radio_display_data[0] = digits[1] << 4 | digits[2];
728         radio_display_data[1] = 0xf0 | digits[0];
729         
730         // DME knots
731         float knots = dme_kt->getFloatValue();
732         if ( knots > 999 ) {
733             knots = 999.0;
734         }
735         sprintf(digits, "%03.0f", knots);
736         for ( i = 0; i < 6; ++i ) {
737             digits[i] -= '0';
738         }
739         radio_display_data[2] = digits[1] << 4 | digits[2];
740         radio_display_data[3] = 0xf0 | digits[0];
741
742         // DME distance (nm)
743         float nm = dme_nm->getFloatValue();
744         if ( nm > 99 ) {
745             nm = 99.0;
746         }
747         sprintf(digits, "%04.1f", nm);
748         for ( i = 0; i < 6; ++i ) {
749             digits[i] -= '0';
750         }
751         radio_display_data[4] = digits[1] << 4 | digits[3];
752         radio_display_data[5] = 0x00 | digits[0];
753         // the 0x00 in the upper nibble of the 6th byte of each
754         // display turns on the decimal point
755     } else {
756         // blank dem display
757         for ( i = 0; i < 6; ++i ) {
758             radio_display_data[i] = 0xff;
759         }
760     }
761
762     // Com1 standby frequency
763     float com1_stby = com1_stby_freq->getFloatValue();
764     if ( fabs(com1_stby) > 999.99 ) {
765         com1_stby = 0.0;
766     }
767     sprintf(digits, "%06.3f", com1_stby);
768     for ( i = 0; i < 6; ++i ) {
769         digits[i] -= '0';
770     }
771     radio_display_data[6] = digits[4] << 4 | digits[5];
772     radio_display_data[7] = digits[1] << 4 | digits[2];
773     radio_display_data[8] = 0xf0 | digits[0];
774
775     // Com1 in use frequency
776     float com1 = com1_freq->getFloatValue();
777     if ( fabs(com1) > 999.99 ) {
778         com1 = 0.0;
779     }
780     sprintf(digits, "%06.3f", com1);
781     for ( i = 0; i < 6; ++i ) {
782         digits[i] -= '0';
783     }
784     radio_display_data[9] = digits[4] << 4 | digits[5];
785     radio_display_data[10] = digits[1] << 4 | digits[2];
786     radio_display_data[11] = 0x00 | digits[0];
787     // the 0x00 in the upper nibble of the 6th byte of each display
788     // turns on the decimal point
789
790     // Com2 standby frequency
791     float com2_stby = com2_stby_freq->getFloatValue();
792     if ( fabs(com2_stby) > 999.99 ) {
793         com2_stby = 0.0;
794     }
795     sprintf(digits, "%06.3f", com2_stby);
796     for ( i = 0; i < 6; ++i ) {
797         digits[i] -= '0';
798     }
799     radio_display_data[18] = digits[4] << 4 | digits[5];
800     radio_display_data[19] = digits[1] << 4 | digits[2];
801     radio_display_data[20] = 0xf0 | digits[0];
802
803     // Com2 in use frequency
804     float com2 = com2_freq->getFloatValue();
805     if ( fabs(com2) > 999.99 ) {
806         com2 = 0.0;
807     }
808     sprintf(digits, "%06.3f", com2);
809     for ( i = 0; i < 6; ++i ) {
810         digits[i] -= '0';
811     }
812     radio_display_data[21] = digits[4] << 4 | digits[5];
813     radio_display_data[22] = digits[1] << 4 | digits[2];
814     radio_display_data[23] = 0x00 | digits[0];
815     // the 0x00 in the upper nibble of the 6th byte of each display
816     // turns on the decimal point
817
818     // Nav1 standby frequency
819     float nav1_stby = nav1_stby_freq->getFloatValue();
820     if ( fabs(nav1_stby) > 999.99 ) {
821         nav1_stby = 0.0;
822     }
823     sprintf(digits, "%06.2f", nav1_stby);
824     for ( i = 0; i < 6; ++i ) {
825         digits[i] -= '0';
826     }
827     radio_display_data[12] = digits[4] << 4 | digits[5];
828     radio_display_data[13] = digits[1] << 4 | digits[2];
829     radio_display_data[14] = 0xf0 | digits[0];
830
831     // Nav1 in use frequency
832     float nav1 = nav1_freq->getFloatValue();
833     if ( fabs(nav1) > 999.99 ) {
834         nav1 = 0.0;
835     }
836     sprintf(digits, "%06.2f", nav1);
837     for ( i = 0; i < 6; ++i ) {
838         digits[i] -= '0';
839     }
840     radio_display_data[15] = digits[4] << 4 | digits[5];
841     radio_display_data[16] = digits[1] << 4 | digits[2];
842     radio_display_data[17] = 0x00 | digits[0];
843     // the 0x00 in the upper nibble of the 6th byte of each display
844     // turns on the decimal point
845
846     // Nav2 standby frequency
847     float nav2_stby = nav2_stby_freq->getFloatValue();
848     if ( fabs(nav2_stby) > 999.99 ) {
849         nav2_stby = 0.0;
850     }
851     sprintf(digits, "%06.2f", nav2_stby);
852     for ( i = 0; i < 6; ++i ) {
853         digits[i] -= '0';
854     }
855     radio_display_data[24] = digits[4] << 4 | digits[5];
856     radio_display_data[25] = digits[1] << 4 | digits[2];
857     radio_display_data[26] = 0xf0 | digits[0];
858
859     // Nav2 in use frequency
860     float nav2 = nav2_freq->getFloatValue();
861     if ( fabs(nav2) > 999.99 ) {
862         nav2 = 0.0;
863     }
864     sprintf(digits, "%06.2f", nav2);
865     for ( i = 0; i < 6; ++i ) {
866         digits[i] -= '0';
867     }
868     radio_display_data[27] = digits[4] << 4 | digits[5];
869     radio_display_data[28] = digits[1] << 4 | digits[2];
870     radio_display_data[29] = 0x00 | digits[0];
871     // the 0x00 in the upper nibble of the 6th byte of each display
872     // turns on the decimal point
873
874     // ADF standby frequency
875     float adf_stby = adf_stby_freq->getFloatValue();
876     if ( fabs(adf_stby) > 999.99 ) {
877         adf_stby = 0.0;
878     }
879     sprintf(digits, "%03.0f", adf_stby);
880     for ( i = 0; i < 6; ++i ) {
881         digits[i] -= '0';
882     }
883     radio_display_data[30] = digits[2] << 4 | 0x0f;
884     radio_display_data[31] = digits[0] << 4 | digits[1];
885
886     // ADF in use frequency
887     float adf = adf_freq->getFloatValue();
888     if ( fabs(adf) > 999.99 ) {
889         adf = 0.0;
890     }
891     sprintf(digits, "%03.0f", adf);
892     for ( i = 0; i < 6; ++i ) {
893         digits[i] -= '0';
894     }
895     radio_display_data[33] = digits[1] << 4 | digits[2];
896     radio_display_data[34] = 0xf0 | digits[0];
897
898     ATC610xSetRadios( radios_fd, radio_display_data );
899
900     return true;
901 }
902
903
904 /////////////////////////////////////////////////////////////////////
905 // Drive the stepper motors
906 /////////////////////////////////////////////////////////////////////
907
908 bool FGATC610x::do_steppers() {
909     float diff = mag_compass->getFloatValue() - compass_position;
910     while ( diff < -180.0 ) { diff += 360.0; }
911     while ( diff >  180.0 ) { diff -= 360.0; }
912
913     int steps = (int)(diff * 4);
914     // cout << "steps = " << steps << endl;
915     if ( steps > 4 ) { steps = 4; }
916     if ( steps < -4 ) { steps = -4; }
917
918     if ( abs(steps) > 0 ) {
919         unsigned char cmd = 0x80;       // stepper command
920         if ( steps > 0 ) {
921             cmd |= 0x20;                // go up
922         } else {
923             cmd |= 0x00;                // go down
924         }
925         cmd |= abs(steps);
926
927         // sync compass_position with hardware position
928         compass_position += (float)steps / 4.0;
929
930         ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
931     }
932
933     return true;
934 }
935
936
937 /////////////////////////////////////////////////////////////////////
938 // Read the switch positions
939 /////////////////////////////////////////////////////////////////////
940
941 // decode the packed switch data
942 static void update_switch_matrix(
943         int board,
944         unsigned char switch_data[ATC_SWITCH_BYTES],
945         int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
946 {
947     for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
948         unsigned char switches = switch_data[row];
949
950         for( int column = 0; column < ATC_NUM_COLS; ++column ) {
951             switch_matrix[board][column][row] = switches & 1;
952             switches = switches >> 1;
953         }                       
954     }
955 }                     
956
957 bool FGATC610x::do_switches() {
958     ATC610xReadSwitches( switches_fd, switch_data );
959
960     // unpack the switch data
961     int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
962     update_switch_matrix( board, switch_data, switch_matrix );
963
964     // magnetos and starter switch
965     if ( switch_matrix[board][3][1] == 1 ) {
966         fgSetInt( "/controls/magnetos[0]", 3 );
967         fgSetBool( "/controls/starter[0]", true );
968     } else if ( switch_matrix[board][2][1] == 1 ) {
969         fgSetInt( "/controls/magnetos[0]", 3 );
970         fgSetBool( "/controls/starter[0]", false );
971     } else if ( switch_matrix[board][1][1] == 1 ) {
972         fgSetInt( "/controls/magnetos[0]", 2 );
973         fgSetBool( "/controls/starter[0]", false );
974     } else if ( switch_matrix[board][0][1] == 1 ) {
975         fgSetInt( "/controls/magnetos[0]", 1 );
976         fgSetBool( "/controls/starter[0]", false );
977     } else {
978         fgSetInt( "/controls/magnetos[0]", 0 );
979         fgSetBool( "/controls/starter[0]", false );
980     }
981
982     // flaps
983     if ( switch_matrix[board][6][3] == 1 ) {
984         fgSetFloat( "/controls/flaps", 1.0 );
985     } else if ( switch_matrix[board][5][3] == 1 ) {
986         fgSetFloat( "/controls/flaps", 0.66 );
987     } else if ( switch_matrix[board][4][3] == 1 ) {
988         fgSetFloat( "/controls/flaps", 0.33 );
989     } else if ( switch_matrix[board][4][3] == 0 ) {
990         fgSetFloat( "/controls/flaps", 0.0 );
991     }
992
993     return true;
994 }
995
996
997 bool FGATC610x::process() {
998
999     // Lock the hardware, skip if it's not ready yet
1000     if ( ATC610xLock( lock_fd ) > 0 ) {
1001
1002         do_analog_in();
1003         do_radio_switches();
1004         do_radio_display();
1005         do_steppers();
1006         do_switches();
1007         
1008         ATC610xRelease( lock_fd );
1009
1010         return true;
1011     } else {
1012         return false;
1013     }
1014 }
1015
1016
1017 bool FGATC610x::close() {
1018
1019     return true;
1020 }