]> git.mxchange.org Git - flightgear.git/blob - src/Network/atc610x.cxx
Moved "scenery" from being declaried in scenery.cxx to being declared
[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 #if defined( _MSC_VER ) || defined(__MINGW32__)
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     SG_LOG( SG_IO, SG_ALERT,
276             "  - Waiting for compass to come home." );
277
278     bool home = false;
279     int timeout = 900;          // about 30 seconds
280     while ( ! home ) {
281         if ( timeout % 150 == 0 ) {
282             SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
283         } else {
284             SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
285         }
286
287         while ( ATC610xLock( lock_fd ) <= 0 );
288
289         unsigned char result = ATC610xReadStepper( stepper_fd );
290         if ( result == 0 ) {
291             home = true;
292         }
293
294         ATC610xRelease( lock_fd );
295
296 #if defined( _MSC_VER )
297         ulMilliSecondSleep(33);
298 #elif defined (WIN32) && !defined(__CYGWIN__)
299         Sleep (33);
300 #else
301         usleep(33);
302 #endif
303
304         --timeout;
305     }
306
307     compass_position = 0.0;
308
309     /////////////////////////////////////////////////////////////////////
310     // Blank the radio display
311     /////////////////////////////////////////////////////////////////////
312
313     SG_LOG( SG_IO, SG_ALERT,
314             "  - Clearing the radios displays." );
315
316     // Prepair the data
317     unsigned char value = 0xff;
318     for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
319         radio_display_data[channel] = value;
320     }
321
322     // Lock the hardware, keep trying until we succeed
323     while ( ATC610xLock( lock_fd ) <= 0 );
324
325     // Set radio display
326     ATC610xSetRadios( radios_fd, radio_display_data );
327
328     ATC610xRelease( lock_fd );
329
330     /////////////////////////////////////////////////////////////////////
331     // Finished initing hardware
332     /////////////////////////////////////////////////////////////////////
333
334     SG_LOG( SG_IO, SG_ALERT,
335             "Done initializing ATC 610x hardware." );
336
337     /////////////////////////////////////////////////////////////////////
338     // Connect up to property values
339     /////////////////////////////////////////////////////////////////////
340
341     mag_compass = fgGetNode( "/steam/mag-compass-deg", true );
342
343     dme_min = fgGetNode( "/radios/dme/ete-min", true );
344     dme_kt = fgGetNode( "/radios/dme/speed-kt", true );
345     dme_nm = fgGetNode( "/radios/dme/distance-nm", true );
346
347     com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
348     com1_stby_freq
349         = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
350     com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
351     com2_stby_freq
352         = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
353
354     nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
355     nav1_stby_freq
356         = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
357
358     nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
359     nav2_stby_freq
360         = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
361
362     adf_freq = fgGetNode( "/radios/adf/frequencies/selected-khz", true );
363     adf_stby_freq = fgGetNode( "/radios/adf/frequencies/standby-khz", true );
364
365     return true;
366 }
367
368
369 /////////////////////////////////////////////////////////////////////
370 // Read analog inputs
371 /////////////////////////////////////////////////////////////////////
372
373 #define ATC_AILERON_CENTER 535
374 #define ATC_ELEVATOR_TRIM_CENTER 512
375 #define ATC_ELEVATOR_CENTER 543
376
377 bool FGATC610x::do_analog_in() {
378     // Read raw data in byte form
379     ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
380
381     // Convert to integer values
382     for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
383         unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
384         unsigned char lo = analog_in_bytes[2 * channel + 1];
385         analog_in_data[channel] = hi * 256 + lo;
386
387         // printf("%02x %02x ", hi, lo );
388         // printf("%04d ", value );
389     }
390
391     float tmp, tmp1, tmp2;
392
393     // aileron
394     tmp = (float)(analog_in_data[0] - ATC_AILERON_CENTER) / 256.0f;
395     fgSetFloat( "/controls/aileron", tmp );
396     // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
397
398     // elevator
399     tmp = (float)(analog_in_data[4] - ATC_ELEVATOR_TRIM_CENTER) / 512.0f;
400     fgSetFloat( "/controls/elevator-trim", tmp );
401     // cout << "trim = " << analog_in_data[4] << " = " << tmp;
402
403     // trim
404     tmp = (float)(ATC_ELEVATOR_CENTER - analog_in_data[5]) / 100.0f;
405     fgSetFloat( "/controls/elevator", tmp );
406     // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
407
408     // mixture
409     tmp = (float)analog_in_data[7] / 680.0f;
410     fgSetFloat( "/controls/mixture[0]", tmp );
411
412     // throttle
413     tmp = (float)analog_in_data[8] / 690.0f;
414     fgSetFloat( "/controls/throttle[0]", tmp );
415
416     // nav1 volume
417     tmp = (float)analog_in_data[25] / 1024.0f;
418     fgSetFloat( "/radios/nav[0]/volume", tmp );
419
420     // nav2 volume
421     tmp = (float)analog_in_data[24] / 1024.0f;
422     fgSetFloat( "/radios/nav[1]/volume", tmp );
423
424     // adf volume
425     tmp = (float)analog_in_data[26] / 1024.0f;
426     fgSetFloat( "/radios/adf/volume", tmp );
427
428     // nav2 obs tuner
429     tmp = (float)analog_in_data[29] * 360.0f / 1024.0f;
430     fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
431
432     // nav1 obs tuner
433     tmp1 = (float)analog_in_data[30] * 360.0f / 1024.0f;
434     tmp2 = (float)analog_in_data[31] * 360.0f / 1024.0f;
435     fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp1 );
436
437     return true;
438 }
439
440
441 /////////////////////////////////////////////////////////////////////
442 // Read radio switches 
443 /////////////////////////////////////////////////////////////////////
444
445 bool FGATC610x::do_radio_switches() {
446     float freq, inc;
447
448     ATC610xReadRadios( radios_fd, radio_switch_data );
449
450     // DME Switch
451     dme_switch = (radio_switch_data[7] >> 4) & 0x03;
452     if ( dme_switch == 0 ) {
453         // off
454         fgSetInt( "/radios/dme/switch-position", 0 );
455     } else if ( dme_switch == 2 ) {
456         // nav1
457         fgSetInt( "/radios/dme/switch-position", 1 );
458     } else if ( dme_switch == 1 ) {
459         // nav2
460         fgSetInt( "/radios/dme/switch-position", 3 );
461     }
462
463     // Com1 Swap
464     int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
465     static int last_com1_swap;
466     if ( com1_swap && (last_com1_swap != com1_swap) ) {
467         float tmp = com1_freq->getFloatValue();
468         fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
469                    com1_stby_freq->getFloatValue() );
470         fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
471     }
472     last_com1_swap = com1_swap;
473
474     // Com2 Swap
475     int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
476     static int last_com2_swap;
477     if ( com2_swap && (last_com2_swap != com2_swap) ) {
478         float tmp = com2_freq->getFloatValue();
479         fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
480                    com2_stby_freq->getFloatValue() );
481         fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
482     }
483     last_com2_swap = com2_swap;
484
485     // Nav1 Swap
486     int nav1_swap = radio_switch_data[11] & 0x01;
487     static int last_nav1_swap;
488     if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
489         float tmp = nav1_freq->getFloatValue();
490         fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
491                    nav1_stby_freq->getFloatValue() );
492         fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
493     }
494     last_nav1_swap = nav1_swap;
495
496     // Nav2 Swap
497     int nav2_swap = !(radio_switch_data[19] & 0x01);
498     static int last_nav2_swap;
499     if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
500         float tmp = nav2_freq->getFloatValue();
501         fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
502                    nav2_stby_freq->getFloatValue() );
503         fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
504     }
505     last_nav2_swap = nav2_swap;
506
507     // Com1 Tuner
508     int com1_tuner_fine = (radio_switch_data[5] >> 4) & 0x0f;
509     int com1_tuner_course = radio_switch_data[5] & 0x0f;
510     // cout << "com1 = " << com1_tuner_fine << " " << com1_tuner_course << endl;
511     static int last_com1_tuner_fine = com1_tuner_fine;
512     static int last_com1_tuner_course = com1_tuner_course;
513     inc = 0.0;
514     if ( com1_tuner_fine != last_com1_tuner_fine ) {
515         if ( com1_tuner_fine == 0x0c && last_com1_tuner_fine == 0x01 ) {
516             inc = -0.025;
517         } else if ( com1_tuner_fine == 0x01 && last_com1_tuner_fine == 0x0c ) {
518             inc = -0.025;
519         } else if ( com1_tuner_fine > last_com1_tuner_fine ) {
520             inc = 0.025;
521         } else {
522             inc = -0.025;
523         }
524     }
525     if ( com1_tuner_course != last_com1_tuner_course ) {
526         if ( com1_tuner_course == 0x0c && last_com1_tuner_course == 0x01 ) {
527             inc = -1.0;
528         } else if ( com1_tuner_course == 0x01
529                     && last_com1_tuner_course == 0x0c ) {
530             inc = -1.0;
531         } else if ( com1_tuner_course > last_com1_tuner_course ) {
532             inc = 1.0;
533         } else {
534             inc = -1.0;
535         }
536     }
537     last_com1_tuner_fine = com1_tuner_fine;
538     last_com1_tuner_course = com1_tuner_course;
539
540     freq = com1_stby_freq->getFloatValue() + inc;
541     if ( freq < 0.0 ) {
542         freq = 140.0;
543     }
544     if ( freq > 140.0 ) {
545         freq = 0.0;
546     }
547     fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", freq );
548
549     // Com2 Tuner
550     int com2_tuner_fine = (radio_switch_data[13] >> 4) & 0x0f;
551     int com2_tuner_course = radio_switch_data[13] & 0x0f;
552     static int last_com2_tuner_fine = com2_tuner_fine;
553     static int last_com2_tuner_course = com2_tuner_course;
554     inc = 0.0;
555     if ( com2_tuner_fine != last_com2_tuner_fine ) {
556         if ( com2_tuner_fine == 0x0c && last_com2_tuner_fine == 0x01 ) {
557             inc = -0.025;
558         } else if ( com2_tuner_fine == 0x01 && last_com2_tuner_fine == 0x0c ) {
559             inc = -0.025;
560         } else if ( com2_tuner_fine > last_com2_tuner_fine ) {
561             inc = 0.025;
562         } else {
563             inc = -0.025;
564         }
565     }
566     if ( com2_tuner_course != last_com2_tuner_course ) {
567         if ( com2_tuner_course == 0x0c && last_com2_tuner_course == 0x01 ) {
568             inc = -1.0;
569         } else if ( com2_tuner_course == 0x01
570                     && last_com2_tuner_course == 0x0c ) {
571             inc = -1.0;
572         } else if ( com2_tuner_course > last_com2_tuner_course ) {
573             inc = 1.0;
574         } else {
575             inc = -1.0;
576         }
577     }
578     last_com2_tuner_fine = com2_tuner_fine;
579     last_com2_tuner_course = com2_tuner_course;
580
581     freq = com2_stby_freq->getFloatValue() + inc;
582     if ( freq < 0.0 ) {
583         freq = 140.0;
584     }
585     if ( freq > 140.0 ) {
586         freq = 0.0;
587     }
588     fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", freq );
589
590     // Nav1 Tuner
591     int nav1_tuner_fine = (radio_switch_data[9] >> 4) & 0x0f;
592     int nav1_tuner_course = radio_switch_data[9] & 0x0f;
593     static int last_nav1_tuner_fine = nav1_tuner_fine;
594     static int last_nav1_tuner_course = nav1_tuner_course;
595     inc = 0.0;
596     if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
597         if ( nav1_tuner_fine == 0x0c && last_nav1_tuner_fine == 0x01 ) {
598             inc = -0.05;
599         } else if ( nav1_tuner_fine == 0x01 && last_nav1_tuner_fine == 0x0c ) {
600             inc = -0.05;
601         } else if ( nav1_tuner_fine > last_nav1_tuner_fine ) {
602             inc = 0.05;
603         } else {
604             inc = -0.05;
605         }
606     }
607     if ( nav1_tuner_course != last_nav1_tuner_course ) {
608         if ( nav1_tuner_course == 0x0c && last_nav1_tuner_course == 0x01 ) {
609             inc = -1.0;
610         } else if ( nav1_tuner_course == 0x01
611                     && last_nav1_tuner_course == 0x0c ) {
612             inc = -1.0;
613         } else if ( nav1_tuner_course > last_nav1_tuner_course ) {
614             inc = 1.0;
615         } else {
616             inc = -1.0;
617         }
618     }
619     last_nav1_tuner_fine = nav1_tuner_fine;
620     last_nav1_tuner_course = nav1_tuner_course;
621
622     freq = nav1_stby_freq->getFloatValue() + inc;
623     if ( freq < 108.0 ) {
624         freq = 117.95;
625     }
626     if ( freq > 117.95 ) {
627         freq = 108.0;
628     }
629     fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", freq );
630
631     // Nav2 Tuner
632     int nav2_tuner_fine = (radio_switch_data[17] >> 4) & 0x0f;
633     int nav2_tuner_course = radio_switch_data[17] & 0x0f;
634     static int last_nav2_tuner_fine = nav2_tuner_fine;
635     static int last_nav2_tuner_course = nav2_tuner_course;
636     inc = 0.0;
637     if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
638         if ( nav2_tuner_fine == 0x0c && last_nav2_tuner_fine == 0x01 ) {
639             inc = -0.05;
640         } else if ( nav2_tuner_fine == 0x01 && last_nav2_tuner_fine == 0x0c ) {
641             inc = -0.05;
642         } else if ( nav2_tuner_fine > last_nav2_tuner_fine ) {
643             inc = 0.05;
644         } else {
645             inc = -0.05;
646         }
647     }
648     if ( nav2_tuner_course != last_nav2_tuner_course ) {
649         if ( nav2_tuner_course == 0x0c && last_nav2_tuner_course == 0x01 ) {
650             inc = -1.0;
651         } else if ( nav2_tuner_course == 0x01
652                     && last_nav2_tuner_course == 0x0c ) {
653             inc = -1.0;
654         } else if ( nav2_tuner_course > last_nav2_tuner_course ) {
655             inc = 1.0;
656         } else {
657             inc = -1.0;
658         }
659     }
660     last_nav2_tuner_fine = nav2_tuner_fine;
661     last_nav2_tuner_course = nav2_tuner_course;
662
663     freq = nav2_stby_freq->getFloatValue() + inc;
664     if ( freq < 108.0 ) {
665         freq = 117.95;
666     }
667     if ( freq > 117.95 ) {
668         freq = 108.0;
669     }
670     fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", freq );
671
672     // ADF Tuner
673     int adf_tuner_fine = (radio_switch_data[21] >> 4) & 0x0f;
674     int adf_tuner_course = radio_switch_data[21] & 0x0f;
675     // cout << "adf = " << adf_tuner_fine << " " << adf_tuner_course << endl;
676     static int last_adf_tuner_fine = adf_tuner_fine;
677     static int last_adf_tuner_course = adf_tuner_course;
678     inc = 0.0;
679     if ( adf_tuner_fine != last_adf_tuner_fine ) {
680         if ( adf_tuner_fine == 0x0c && last_adf_tuner_fine == 0x01 ) {
681             inc = -1.0;
682         } else if ( adf_tuner_fine == 0x01 && last_adf_tuner_fine == 0x0c ) {
683             inc = -1.0;
684         } else if ( adf_tuner_fine > last_adf_tuner_fine ) {
685             inc = 1.0;
686         } else {
687             inc = -1.0;
688         }
689     }
690     if ( adf_tuner_course != last_adf_tuner_course ) {
691         if ( adf_tuner_course == 0x0c && last_adf_tuner_course == 0x01 ) {
692             inc = -25.0;
693         } else if ( adf_tuner_course == 0x01
694                     && last_adf_tuner_course == 0x0c ) {
695             inc = -25.0;
696         } else if ( adf_tuner_course > last_adf_tuner_course ) {
697             inc = 25.0;
698         } else {
699             inc = -25.0;
700         }
701     }
702     last_adf_tuner_fine = adf_tuner_fine;
703     last_adf_tuner_course = adf_tuner_course;
704
705     freq = adf_freq->getFloatValue() + inc;
706     if ( freq < 100.0 ) {
707         freq = 1299;
708     }
709     if ( freq > 1299 ) {
710         freq = 100.0;
711     }
712     fgSetFloat( "/radios/adf/frequencies/selected-khz", freq );
713
714     return true;
715 }
716
717
718 /////////////////////////////////////////////////////////////////////
719 // Update the radio display 
720 /////////////////////////////////////////////////////////////////////
721
722 bool FGATC610x::do_radio_display() {
723
724     char digits[10];
725     int i;
726
727     if ( dme_switch != 0 ) {
728         // DME minutes
729         float minutes = dme_min->getFloatValue();
730         if ( minutes > 999 ) {
731             minutes = 999.0;
732         }
733         sprintf(digits, "%03.0f", minutes);
734         for ( i = 0; i < 6; ++i ) {
735             digits[i] -= '0';
736         }
737         radio_display_data[0] = digits[1] << 4 | digits[2];
738         radio_display_data[1] = 0xf0 | digits[0];
739         
740         // DME knots
741         float knots = dme_kt->getFloatValue();
742         if ( knots > 999 ) {
743             knots = 999.0;
744         }
745         sprintf(digits, "%03.0f", knots);
746         for ( i = 0; i < 6; ++i ) {
747             digits[i] -= '0';
748         }
749         radio_display_data[2] = digits[1] << 4 | digits[2];
750         radio_display_data[3] = 0xf0 | digits[0];
751
752         // DME distance (nm)
753         float nm = dme_nm->getFloatValue();
754         if ( nm > 99 ) {
755             nm = 99.0;
756         }
757         sprintf(digits, "%04.1f", nm);
758         for ( i = 0; i < 6; ++i ) {
759             digits[i] -= '0';
760         }
761         radio_display_data[4] = digits[1] << 4 | digits[3];
762         radio_display_data[5] = 0x00 | digits[0];
763         // the 0x00 in the upper nibble of the 6th byte of each
764         // display turns on the decimal point
765     } else {
766         // blank dem display
767         for ( i = 0; i < 6; ++i ) {
768             radio_display_data[i] = 0xff;
769         }
770     }
771
772     // Com1 standby frequency
773     float com1_stby = com1_stby_freq->getFloatValue();
774     if ( fabs(com1_stby) > 999.99 ) {
775         com1_stby = 0.0;
776     }
777     sprintf(digits, "%06.3f", com1_stby);
778     for ( i = 0; i < 6; ++i ) {
779         digits[i] -= '0';
780     }
781     radio_display_data[6] = digits[4] << 4 | digits[5];
782     radio_display_data[7] = digits[1] << 4 | digits[2];
783     radio_display_data[8] = 0xf0 | digits[0];
784
785     // Com1 in use frequency
786     float com1 = com1_freq->getFloatValue();
787     if ( fabs(com1) > 999.99 ) {
788         com1 = 0.0;
789     }
790     sprintf(digits, "%06.3f", com1);
791     for ( i = 0; i < 6; ++i ) {
792         digits[i] -= '0';
793     }
794     radio_display_data[9] = digits[4] << 4 | digits[5];
795     radio_display_data[10] = digits[1] << 4 | digits[2];
796     radio_display_data[11] = 0x00 | digits[0];
797     // the 0x00 in the upper nibble of the 6th byte of each display
798     // turns on the decimal point
799
800     // Com2 standby frequency
801     float com2_stby = com2_stby_freq->getFloatValue();
802     if ( fabs(com2_stby) > 999.99 ) {
803         com2_stby = 0.0;
804     }
805     sprintf(digits, "%06.3f", com2_stby);
806     for ( i = 0; i < 6; ++i ) {
807         digits[i] -= '0';
808     }
809     radio_display_data[18] = digits[4] << 4 | digits[5];
810     radio_display_data[19] = digits[1] << 4 | digits[2];
811     radio_display_data[20] = 0xf0 | digits[0];
812
813     // Com2 in use frequency
814     float com2 = com2_freq->getFloatValue();
815     if ( fabs(com2) > 999.99 ) {
816         com2 = 0.0;
817     }
818     sprintf(digits, "%06.3f", com2);
819     for ( i = 0; i < 6; ++i ) {
820         digits[i] -= '0';
821     }
822     radio_display_data[21] = digits[4] << 4 | digits[5];
823     radio_display_data[22] = digits[1] << 4 | digits[2];
824     radio_display_data[23] = 0x00 | digits[0];
825     // the 0x00 in the upper nibble of the 6th byte of each display
826     // turns on the decimal point
827
828     // Nav1 standby frequency
829     float nav1_stby = nav1_stby_freq->getFloatValue();
830     if ( fabs(nav1_stby) > 999.99 ) {
831         nav1_stby = 0.0;
832     }
833     sprintf(digits, "%06.2f", nav1_stby);
834     for ( i = 0; i < 6; ++i ) {
835         digits[i] -= '0';
836     }
837     radio_display_data[12] = digits[4] << 4 | digits[5];
838     radio_display_data[13] = digits[1] << 4 | digits[2];
839     radio_display_data[14] = 0xf0 | digits[0];
840
841     // Nav1 in use frequency
842     float nav1 = nav1_freq->getFloatValue();
843     if ( fabs(nav1) > 999.99 ) {
844         nav1 = 0.0;
845     }
846     sprintf(digits, "%06.2f", nav1);
847     for ( i = 0; i < 6; ++i ) {
848         digits[i] -= '0';
849     }
850     radio_display_data[15] = digits[4] << 4 | digits[5];
851     radio_display_data[16] = digits[1] << 4 | digits[2];
852     radio_display_data[17] = 0x00 | digits[0];
853     // the 0x00 in the upper nibble of the 6th byte of each display
854     // turns on the decimal point
855
856     // Nav2 standby frequency
857     float nav2_stby = nav2_stby_freq->getFloatValue();
858     if ( fabs(nav2_stby) > 999.99 ) {
859         nav2_stby = 0.0;
860     }
861     sprintf(digits, "%06.2f", nav2_stby);
862     for ( i = 0; i < 6; ++i ) {
863         digits[i] -= '0';
864     }
865     radio_display_data[24] = digits[4] << 4 | digits[5];
866     radio_display_data[25] = digits[1] << 4 | digits[2];
867     radio_display_data[26] = 0xf0 | digits[0];
868
869     // Nav2 in use frequency
870     float nav2 = nav2_freq->getFloatValue();
871     if ( fabs(nav2) > 999.99 ) {
872         nav2 = 0.0;
873     }
874     sprintf(digits, "%06.2f", nav2);
875     for ( i = 0; i < 6; ++i ) {
876         digits[i] -= '0';
877     }
878     radio_display_data[27] = digits[4] << 4 | digits[5];
879     radio_display_data[28] = digits[1] << 4 | digits[2];
880     radio_display_data[29] = 0x00 | digits[0];
881     // the 0x00 in the upper nibble of the 6th byte of each display
882     // turns on the decimal point
883
884     // ADF standby frequency
885     float adf_stby = adf_stby_freq->getFloatValue();
886     if ( fabs(adf_stby) > 999.99 ) {
887         adf_stby = 0.0;
888     }
889     sprintf(digits, "%03.0f", adf_stby);
890     for ( i = 0; i < 6; ++i ) {
891         digits[i] -= '0';
892     }
893     radio_display_data[30] = digits[2] << 4 | 0x0f;
894     radio_display_data[31] = digits[0] << 4 | digits[1];
895
896     // ADF in use frequency
897     float adf = adf_freq->getFloatValue();
898     if ( fabs(adf) > 999.99 ) {
899         adf = 0.0;
900     }
901     sprintf(digits, "%03.0f", adf);
902     for ( i = 0; i < 6; ++i ) {
903         digits[i] -= '0';
904     }
905     radio_display_data[33] = digits[1] << 4 | digits[2];
906     radio_display_data[34] = 0xf0 | digits[0];
907
908     ATC610xSetRadios( radios_fd, radio_display_data );
909
910     return true;
911 }
912
913
914 /////////////////////////////////////////////////////////////////////
915 // Drive the stepper motors
916 /////////////////////////////////////////////////////////////////////
917
918 bool FGATC610x::do_steppers() {
919     float diff = mag_compass->getFloatValue() - compass_position;
920     while ( diff < -180.0 ) { diff += 360.0; }
921     while ( diff >  180.0 ) { diff -= 360.0; }
922
923     int steps = (int)(diff * 4);
924     // cout << "steps = " << steps << endl;
925     if ( steps > 4 ) { steps = 4; }
926     if ( steps < -4 ) { steps = -4; }
927
928     if ( abs(steps) > 0 ) {
929         unsigned char cmd = 0x80;       // stepper command
930         if ( steps > 0 ) {
931             cmd |= 0x20;                // go up
932         } else {
933             cmd |= 0x00;                // go down
934         }
935         cmd |= abs(steps);
936
937         // sync compass_position with hardware position
938         compass_position += (float)steps / 4.0;
939
940         ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
941     }
942
943     return true;
944 }
945
946
947 /////////////////////////////////////////////////////////////////////
948 // Read the switch positions
949 /////////////////////////////////////////////////////////////////////
950
951 // decode the packed switch data
952 static void update_switch_matrix(
953         int board,
954         unsigned char switch_data[ATC_SWITCH_BYTES],
955         int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
956 {
957     for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
958         unsigned char switches = switch_data[row];
959
960         for( int column = 0; column < ATC_NUM_COLS; ++column ) {
961             switch_matrix[board][column][row] = switches & 1;
962             switches = switches >> 1;
963         }                       
964     }
965 }                     
966
967 bool FGATC610x::do_switches() {
968     ATC610xReadSwitches( switches_fd, switch_data );
969
970     // unpack the switch data
971     int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
972     update_switch_matrix( board, switch_data, switch_matrix );
973
974     // magnetos and starter switch
975     if ( switch_matrix[board][3][1] == 1 ) {
976         fgSetInt( "/controls/magnetos[0]", 3 );
977         fgSetBool( "/controls/starter[0]", true );
978     } else if ( switch_matrix[board][2][1] == 1 ) {
979         fgSetInt( "/controls/magnetos[0]", 3 );
980         fgSetBool( "/controls/starter[0]", false );
981     } else if ( switch_matrix[board][1][1] == 1 ) {
982         fgSetInt( "/controls/magnetos[0]", 2 );
983         fgSetBool( "/controls/starter[0]", false );
984     } else if ( switch_matrix[board][0][1] == 1 ) {
985         fgSetInt( "/controls/magnetos[0]", 1 );
986         fgSetBool( "/controls/starter[0]", false );
987     } else {
988         fgSetInt( "/controls/magnetos[0]", 0 );
989         fgSetBool( "/controls/starter[0]", false );
990     }
991
992     // flaps
993     if ( switch_matrix[board][6][3] == 1 ) {
994         fgSetFloat( "/controls/flaps", 1.0 );
995     } else if ( switch_matrix[board][5][3] == 1 ) {
996         fgSetFloat( "/controls/flaps", 0.66 );
997     } else if ( switch_matrix[board][4][3] == 1 ) {
998         fgSetFloat( "/controls/flaps", 0.33 );
999     } else if ( switch_matrix[board][4][3] == 0 ) {
1000         fgSetFloat( "/controls/flaps", 0.0 );
1001     }
1002
1003     return true;
1004 }
1005
1006
1007 bool FGATC610x::process() {
1008
1009     // Lock the hardware, skip if it's not ready yet
1010     if ( ATC610xLock( lock_fd ) > 0 ) {
1011
1012         do_analog_in();
1013         do_radio_switches();
1014         do_radio_display();
1015         do_steppers();
1016         do_switches();
1017         
1018         ATC610xRelease( lock_fd );
1019
1020         return true;
1021     } else {
1022         return false;
1023     }
1024 }
1025
1026
1027 bool FGATC610x::close() {
1028
1029     return true;
1030 }