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