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