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