1 // atc610x.cxx -- FGFS interface to ATC 610x hardware
3 // Written by Curtis Olson, started January 2002
5 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
28 #include <simgear/compiler.h>
30 #include <stdlib.h> // atoi() atof() abs()
31 #include <sys/types.h>
34 #include <stdio.h> //snprintf
35 #if defined( _MSC_VER ) || defined(__MINGW32__)
36 # include <io.h> //lseek, read, write
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>
48 #include <Main/fg_props.hxx>
49 #include <Main/globals.hxx>
51 #include "atc610x.hxx"
55 // Lock the ATC 610 hardware
56 static int ATC610xLock( int fd ) {
58 lseek( fd, 0, SEEK_SET );
61 int result = read( fd, tmp, 1 );
63 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
70 // Write a radios command
71 static int ATC610xRelease( int fd ) {
73 lseek( fd, 0, SEEK_SET );
77 int result = write( fd, tmp, 1 );
80 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
88 static void ATC610xReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
90 lseek( fd, 0, SEEK_SET );
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" );
100 // Write a radios command
101 static int ATC610xSetRadios( int fd,
102 unsigned char data[ATC_RADIO_DISPLAY_BYTES] )
105 lseek( fd, 0, SEEK_SET );
107 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
109 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
110 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
117 // Read status of last radios written to
118 static void ATC610xReadRadios( int fd, unsigned char *switch_data ) {
120 lseek( fd, 0, SEEK_SET );
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" );
129 // Write a stepper command
130 static int ATC610xSetStepper( int fd, unsigned char channel,
131 unsigned char value )
134 lseek( fd, 0, SEEK_SET );
137 unsigned char buf[3];
141 int result = write( fd, buf, 2 );
143 SG_LOG( SG_IO, SG_INFO, "Write failed" );
145 SG_LOG( SG_IO, SG_DEBUG,
146 "Sent cmd = " << (int)channel << " value = " << (int)value );
151 // Read status of last stepper written to
152 static unsigned char ATC610xReadStepper( int fd ) {
156 lseek( fd, 0, SEEK_SET );
159 unsigned char buf[2];
160 result = read( fd, buf, 1 );
162 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
165 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
171 // Read switch inputs
172 static void ATC610xReadSwitches( int fd, unsigned char *switch_bytes ) {
174 lseek( fd, 0, SEEK_SET );
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" );
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
189 // bits 0-6 are the lamp address
190 // bit 7 is the value (on/off)
195 unsigned char buf[3];
199 result = write( fd, buf, 2 );
201 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
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" );
215 SG_LOG( SG_IO, SG_ALERT,
216 "Initializing ATC 610x hardware, please wait ..." );
218 set_hz( 30 ); // default to processing requests @ 30Hz
221 board = 0; // 610x uses a single board number = 0
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 );
230 /////////////////////////////////////////////////////////////////////
231 // Open the /proc files
232 /////////////////////////////////////////////////////////////////////
234 lock_fd = ::open( lock_file, O_RDWR );
235 if ( lock_fd == -1 ) {
236 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
238 snprintf( msg, 256, "Error opening %s", lock_file );
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 );
247 snprintf( msg, 256, "Error opening %s", analog_in_file );
252 lamps_fd = ::open( lamps_file, O_WRONLY );
253 if ( lamps_fd == -1 ) {
254 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
256 snprintf( msg, 256, "Error opening %s", lamps_file );
261 radios_fd = ::open( radios_file, O_RDWR );
262 if ( radios_fd == -1 ) {
263 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
265 snprintf( msg, 256, "Error opening %s", radios_file );
270 stepper_fd = ::open( stepper_file, O_RDWR );
271 if ( stepper_fd == -1 ) {
272 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
274 snprintf( msg, 256, "Error opening %s", stepper_file );
279 switches_fd = ::open( switches_file, O_RDONLY );
280 if ( switches_fd == -1 ) {
281 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
283 snprintf( msg, 256, "Error opening %s", switches_file );
288 /////////////////////////////////////////////////////////////////////
289 // Home the compass stepper motor
290 /////////////////////////////////////////////////////////////////////
292 SG_LOG( SG_IO, SG_ALERT,
293 " - Homing the compass stepper motor" );
295 // Lock the hardware, keep trying until we succeed
296 while ( ATC610xLock( lock_fd ) <= 0 );
298 // Send the stepper home command
299 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
301 // Release the hardware
302 ATC610xRelease( lock_fd );
304 SG_LOG( SG_IO, SG_ALERT,
305 " - Waiting for compass to come home." );
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 );
313 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
316 while ( ATC610xLock( lock_fd ) <= 0 );
318 unsigned char result = ATC610xReadStepper( stepper_fd );
323 ATC610xRelease( lock_fd );
325 #if defined( _MSC_VER )
326 ulMilliSecondSleep(33);
327 #elif defined (WIN32) && !defined(__CYGWIN__)
336 compass_position = 0.0;
338 /////////////////////////////////////////////////////////////////////
339 // Blank the radio display
340 /////////////////////////////////////////////////////////////////////
342 SG_LOG( SG_IO, SG_ALERT,
343 " - Clearing the radios displays." );
346 unsigned char value = 0xff;
347 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
348 radio_display_data[channel] = value;
351 // Lock the hardware, keep trying until we succeed
352 while ( ATC610xLock( lock_fd ) <= 0 );
355 ATC610xSetRadios( radios_fd, radio_display_data );
357 ATC610xRelease( lock_fd );
359 /////////////////////////////////////////////////////////////////////
361 /////////////////////////////////////////////////////////////////////
363 for ( int i = 0; i < 128; ++i ) {
364 ATC610xSetLamp( lamps_fd, i, false );
367 /////////////////////////////////////////////////////////////////////
368 // Finished initing hardware
369 /////////////////////////////////////////////////////////////////////
371 SG_LOG( SG_IO, SG_ALERT,
372 "Done initializing ATC 610x hardware." );
374 /////////////////////////////////////////////////////////////////////
375 // Connect up to property values
376 /////////////////////////////////////////////////////////////////////
378 mag_compass = fgGetNode( "/steam/mag-compass-deg", true );
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 );
384 com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
386 = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
387 com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
389 = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
391 nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
393 = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
395 nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
397 = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
399 adf_freq = fgGetNode( "/radios/adf/frequencies/selected-khz", true );
400 adf_stby_freq = fgGetNode( "/radios/adf/frequencies/standby-khz", true );
402 inner = fgGetNode( "/radios/marker-beacon/inner", true );
403 middle = fgGetNode( "/radios/marker-beacon/middle", true );
404 outer = fgGetNode( "/radios/marker-beacon/outer", true );
410 /////////////////////////////////////////////////////////////////////
411 // Read analog inputs
412 /////////////////////////////////////////////////////////////////////
414 #define ATC_AILERON_CENTER 535
415 #define ATC_ELEVATOR_TRIM_CENTER 512
416 #define ATC_ELEVATOR_CENTER 543
418 bool FGATC610x::do_analog_in() {
419 // Read raw data in byte form
420 ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
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;
428 // printf("%02x %02x ", hi, lo );
429 // printf("%04d ", value );
432 float tmp, tmp1, tmp2;
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;
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;
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;
450 tmp = (float)analog_in_data[7] / 680.0f;
451 fgSetFloat( "/controls/mixture[0]", tmp );
454 tmp = (float)analog_in_data[8] / 690.0f;
455 fgSetFloat( "/controls/throttle[0]", tmp );
458 tmp = (float)analog_in_data[25] / 1024.0f;
459 fgSetFloat( "/radios/nav[0]/volume", tmp );
462 tmp = (float)analog_in_data[24] / 1024.0f;
463 fgSetFloat( "/radios/nav[1]/volume", tmp );
466 tmp = (float)analog_in_data[26] / 1024.0f;
467 fgSetFloat( "/radios/adf/volume", tmp );
470 tmp = (float)analog_in_data[29] * 360.0f / 1024.0f;
471 fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
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 );
482 /////////////////////////////////////////////////////////////////////
484 /////////////////////////////////////////////////////////////////////
486 bool FGATC610x::do_lights() {
488 ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
489 ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
490 ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
496 /////////////////////////////////////////////////////////////////////
497 // Read radio switches
498 /////////////////////////////////////////////////////////////////////
500 bool FGATC610x::do_radio_switches() {
501 float freq, coarse_freq, fine_freq;
504 ATC610xReadRadios( radios_fd, radio_switch_data );
507 dme_switch = (radio_switch_data[7] >> 4) & 0x03;
508 if ( dme_switch == 0 ) {
510 fgSetInt( "/radios/dme/switch-position", 0 );
511 } else if ( dme_switch == 2 ) {
513 fgSetInt( "/radios/dme/switch-position", 1 );
514 } else if ( dme_switch == 1 ) {
516 fgSetInt( "/radios/dme/switch-position", 3 );
520 int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
521 static int last_com1_swap;
522 if ( com1_swap && (last_com1_swap != com1_swap) ) {
523 float tmp = com1_freq->getFloatValue();
524 fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
525 com1_stby_freq->getFloatValue() );
526 fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
528 last_com1_swap = com1_swap;
531 int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
532 static int last_com2_swap;
533 if ( com2_swap && (last_com2_swap != com2_swap) ) {
534 float tmp = com2_freq->getFloatValue();
535 fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
536 com2_stby_freq->getFloatValue() );
537 fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
539 last_com2_swap = com2_swap;
542 int nav1_swap = radio_switch_data[11] & 0x01;
543 static int last_nav1_swap;
544 if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
545 float tmp = nav1_freq->getFloatValue();
546 fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
547 nav1_stby_freq->getFloatValue() );
548 fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
550 last_nav1_swap = nav1_swap;
553 int nav2_swap = !(radio_switch_data[19] & 0x01);
554 static int last_nav2_swap;
555 if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
556 float tmp = nav2_freq->getFloatValue();
557 fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
558 nav2_stby_freq->getFloatValue() );
559 fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
561 last_nav2_swap = nav2_swap;
564 int com1_tuner_fine = ((radio_switch_data[5] >> 4) & 0x0f) - 1;
565 int com1_tuner_coarse = (radio_switch_data[5] & 0x0f) - 1;
566 static int last_com1_tuner_fine = com1_tuner_fine;
567 static int last_com1_tuner_coarse = com1_tuner_coarse;
569 freq = com1_stby_freq->getFloatValue();
570 coarse_freq = (int)freq;
571 fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
573 if ( com1_tuner_fine != last_com1_tuner_fine ) {
574 diff = com1_tuner_fine - last_com1_tuner_fine;
575 if ( abs(diff) > 4 ) {
577 if ( com1_tuner_fine < last_com1_tuner_fine ) {
579 diff = 12 - last_com1_tuner_fine + com1_tuner_fine;
582 diff = com1_tuner_fine - 12 - last_com1_tuner_fine;
587 while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
588 while ( fine_freq < 0.0 ) { fine_freq += 40.0; }
590 if ( com1_tuner_coarse != last_com1_tuner_coarse ) {
591 diff = com1_tuner_coarse - last_com1_tuner_coarse;
592 if ( abs(diff) > 4 ) {
594 if ( com1_tuner_coarse < last_com1_tuner_coarse ) {
596 diff = 12 - last_com1_tuner_coarse + com1_tuner_coarse;
599 diff = com1_tuner_coarse - 12 - last_com1_tuner_coarse;
604 if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
605 if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
607 last_com1_tuner_fine = com1_tuner_fine;
608 last_com1_tuner_coarse = com1_tuner_coarse;
610 fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz",
611 coarse_freq + fine_freq / 40.0 );
614 int com2_tuner_fine = ((radio_switch_data[13] >> 4) & 0x0f) - 1;
615 int com2_tuner_coarse = (radio_switch_data[13] & 0x0f) - 1;
616 static int last_com2_tuner_fine = com2_tuner_fine;
617 static int last_com2_tuner_coarse = com2_tuner_coarse;
619 freq = com2_stby_freq->getFloatValue();
620 coarse_freq = (int)freq;
621 fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
623 if ( com2_tuner_fine != last_com2_tuner_fine ) {
624 diff = com2_tuner_fine - last_com2_tuner_fine;
625 if ( abs(diff) > 4 ) {
627 if ( com2_tuner_fine < last_com2_tuner_fine ) {
629 diff = 12 - last_com2_tuner_fine + com2_tuner_fine;
632 diff = com2_tuner_fine - 12 - last_com2_tuner_fine;
637 while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
638 while ( fine_freq < 0.0 ) { fine_freq += 40.0; }
640 if ( com2_tuner_coarse != last_com2_tuner_coarse ) {
641 diff = com2_tuner_coarse - last_com2_tuner_coarse;
642 if ( abs(diff) > 4 ) {
644 if ( com2_tuner_coarse < last_com2_tuner_coarse ) {
646 diff = 12 - last_com2_tuner_coarse + com2_tuner_coarse;
649 diff = com2_tuner_coarse - 12 - last_com2_tuner_coarse;
654 if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
655 if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
657 last_com2_tuner_fine = com2_tuner_fine;
658 last_com2_tuner_coarse = com2_tuner_coarse;
660 fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz",
661 coarse_freq + fine_freq / 40.0 );
664 int nav1_tuner_fine = ((radio_switch_data[9] >> 4) & 0x0f) - 1;
665 int nav1_tuner_coarse = (radio_switch_data[9] & 0x0f) - 1;
666 static int last_nav1_tuner_fine = nav1_tuner_fine;
667 static int last_nav1_tuner_coarse = nav1_tuner_coarse;
669 freq = nav1_stby_freq->getFloatValue();
670 coarse_freq = (int)freq;
671 fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
673 if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
674 diff = nav1_tuner_fine - last_nav1_tuner_fine;
675 if ( abs(diff) > 4 ) {
677 if ( nav1_tuner_fine < last_nav1_tuner_fine ) {
679 diff = 12 - last_nav1_tuner_fine + nav1_tuner_fine;
682 diff = nav1_tuner_fine - 12 - last_nav1_tuner_fine;
687 while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
688 while ( fine_freq < 0.0 ) { fine_freq += 20.0; }
690 if ( nav1_tuner_coarse != last_nav1_tuner_coarse ) {
691 diff = nav1_tuner_coarse - last_nav1_tuner_coarse;
692 if ( abs(diff) > 4 ) {
694 if ( nav1_tuner_coarse < last_nav1_tuner_coarse ) {
696 diff = 12 - last_nav1_tuner_coarse + nav1_tuner_coarse;
699 diff = nav1_tuner_coarse - 12 - last_nav1_tuner_coarse;
704 if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
705 if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
707 last_nav1_tuner_fine = nav1_tuner_fine;
708 last_nav1_tuner_coarse = nav1_tuner_coarse;
710 fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz",
711 coarse_freq + fine_freq / 20.0 );
714 int nav2_tuner_fine = ((radio_switch_data[17] >> 4) & 0x0f) - 1;
715 int nav2_tuner_coarse = (radio_switch_data[17] & 0x0f) - 1;
716 static int last_nav2_tuner_fine = nav2_tuner_fine;
717 static int last_nav2_tuner_coarse = nav2_tuner_coarse;
719 freq = nav2_stby_freq->getFloatValue();
720 coarse_freq = (int)freq;
721 fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
723 if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
724 diff = nav2_tuner_fine - last_nav2_tuner_fine;
725 if ( abs(diff) > 4 ) {
727 if ( nav2_tuner_fine < last_nav2_tuner_fine ) {
729 diff = 12 - last_nav2_tuner_fine + nav2_tuner_fine;
732 diff = nav2_tuner_fine - 12 - last_nav2_tuner_fine;
737 while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
738 while ( fine_freq < 0.0 ) { fine_freq += 20.0; }
740 if ( nav2_tuner_coarse != last_nav2_tuner_coarse ) {
741 diff = nav2_tuner_coarse - last_nav2_tuner_coarse;
742 if ( abs(diff) > 4 ) {
744 if ( nav2_tuner_coarse < last_nav2_tuner_coarse ) {
746 diff = 12 - last_nav2_tuner_coarse + nav2_tuner_coarse;
749 diff = nav2_tuner_coarse - 12 - last_nav2_tuner_coarse;
754 if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
755 if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
757 last_nav2_tuner_fine = nav2_tuner_fine;
758 last_nav2_tuner_coarse = nav2_tuner_coarse;
760 fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz",
761 coarse_freq + fine_freq / 20.0);
764 int adf_tuner_fine = ((radio_switch_data[21] >> 4) & 0x0f) - 1;
765 int adf_tuner_coarse = (radio_switch_data[21] & 0x0f) - 1;
766 static int last_adf_tuner_fine = adf_tuner_fine;
767 static int last_adf_tuner_coarse = adf_tuner_coarse;
769 freq = adf_stby_freq->getFloatValue();
771 if ( adf_tuner_fine != last_adf_tuner_fine ) {
772 diff = adf_tuner_fine - last_adf_tuner_fine;
773 if ( abs(diff) > 4 ) {
775 if ( adf_tuner_fine < last_adf_tuner_fine ) {
777 diff = 12 - last_adf_tuner_fine + adf_tuner_fine;
780 diff = adf_tuner_fine - 12 - last_adf_tuner_fine;
786 if ( adf_tuner_coarse != last_adf_tuner_coarse ) {
787 diff = adf_tuner_coarse - last_adf_tuner_coarse;
788 if ( abs(diff) > 4 ) {
790 if ( adf_tuner_coarse < last_adf_tuner_coarse ) {
792 diff = 12 - last_adf_tuner_coarse + adf_tuner_coarse;
795 diff = adf_tuner_coarse - 12 - last_adf_tuner_coarse;
800 if ( freq < 100 ) { freq += 1200; }
801 if ( freq > 1299 ) { freq -= 1200; }
803 last_adf_tuner_fine = adf_tuner_fine;
804 last_adf_tuner_coarse = adf_tuner_coarse;
806 fgSetFloat( "/radios/adf/frequencies/selected-khz", freq );
812 /////////////////////////////////////////////////////////////////////
813 // Update the radio display
814 /////////////////////////////////////////////////////////////////////
816 bool FGATC610x::do_radio_display() {
821 if ( dme_switch != 0 ) {
823 float minutes = dme_min->getFloatValue();
824 if ( minutes > 999 ) {
827 sprintf(digits, "%03.0f", minutes);
828 for ( i = 0; i < 6; ++i ) {
831 radio_display_data[0] = digits[1] << 4 | digits[2];
832 radio_display_data[1] = 0xf0 | digits[0];
835 float knots = dme_kt->getFloatValue();
839 sprintf(digits, "%03.0f", knots);
840 for ( i = 0; i < 6; ++i ) {
843 radio_display_data[2] = digits[1] << 4 | digits[2];
844 radio_display_data[3] = 0xf0 | digits[0];
847 float nm = dme_nm->getFloatValue();
851 sprintf(digits, "%04.1f", nm);
852 for ( i = 0; i < 6; ++i ) {
855 radio_display_data[4] = digits[1] << 4 | digits[3];
856 radio_display_data[5] = 0x00 | digits[0];
857 // the 0x00 in the upper nibble of the 6th byte of each
858 // display turns on the decimal point
861 for ( i = 0; i < 6; ++i ) {
862 radio_display_data[i] = 0xff;
866 // Com1 standby frequency
867 float com1_stby = com1_stby_freq->getFloatValue();
868 if ( fabs(com1_stby) > 999.99 ) {
871 sprintf(digits, "%06.3f", com1_stby);
872 for ( i = 0; i < 6; ++i ) {
875 radio_display_data[6] = digits[4] << 4 | digits[5];
876 radio_display_data[7] = digits[1] << 4 | digits[2];
877 radio_display_data[8] = 0xf0 | digits[0];
879 // Com1 in use frequency
880 float com1 = com1_freq->getFloatValue();
881 if ( fabs(com1) > 999.99 ) {
884 sprintf(digits, "%06.3f", com1);
885 for ( i = 0; i < 6; ++i ) {
888 radio_display_data[9] = digits[4] << 4 | digits[5];
889 radio_display_data[10] = digits[1] << 4 | digits[2];
890 radio_display_data[11] = 0x00 | digits[0];
891 // the 0x00 in the upper nibble of the 6th byte of each display
892 // turns on the decimal point
894 // Com2 standby frequency
895 float com2_stby = com2_stby_freq->getFloatValue();
896 if ( fabs(com2_stby) > 999.99 ) {
899 sprintf(digits, "%06.3f", com2_stby);
900 for ( i = 0; i < 6; ++i ) {
903 radio_display_data[18] = digits[4] << 4 | digits[5];
904 radio_display_data[19] = digits[1] << 4 | digits[2];
905 radio_display_data[20] = 0xf0 | digits[0];
907 // Com2 in use frequency
908 float com2 = com2_freq->getFloatValue();
909 if ( fabs(com2) > 999.99 ) {
912 sprintf(digits, "%06.3f", com2);
913 for ( i = 0; i < 6; ++i ) {
916 radio_display_data[21] = digits[4] << 4 | digits[5];
917 radio_display_data[22] = digits[1] << 4 | digits[2];
918 radio_display_data[23] = 0x00 | digits[0];
919 // the 0x00 in the upper nibble of the 6th byte of each display
920 // turns on the decimal point
922 // Nav1 standby frequency
923 float nav1_stby = nav1_stby_freq->getFloatValue();
924 if ( fabs(nav1_stby) > 999.99 ) {
927 sprintf(digits, "%06.2f", nav1_stby);
928 for ( i = 0; i < 6; ++i ) {
931 radio_display_data[12] = digits[4] << 4 | digits[5];
932 radio_display_data[13] = digits[1] << 4 | digits[2];
933 radio_display_data[14] = 0xf0 | digits[0];
935 // Nav1 in use frequency
936 float nav1 = nav1_freq->getFloatValue();
937 if ( fabs(nav1) > 999.99 ) {
940 sprintf(digits, "%06.2f", nav1);
941 for ( i = 0; i < 6; ++i ) {
944 radio_display_data[15] = digits[4] << 4 | digits[5];
945 radio_display_data[16] = digits[1] << 4 | digits[2];
946 radio_display_data[17] = 0x00 | digits[0];
947 // the 0x00 in the upper nibble of the 6th byte of each display
948 // turns on the decimal point
950 // Nav2 standby frequency
951 float nav2_stby = nav2_stby_freq->getFloatValue();
952 if ( fabs(nav2_stby) > 999.99 ) {
955 sprintf(digits, "%06.2f", nav2_stby);
956 for ( i = 0; i < 6; ++i ) {
959 radio_display_data[24] = digits[4] << 4 | digits[5];
960 radio_display_data[25] = digits[1] << 4 | digits[2];
961 radio_display_data[26] = 0xf0 | digits[0];
963 // Nav2 in use frequency
964 float nav2 = nav2_freq->getFloatValue();
965 if ( fabs(nav2) > 999.99 ) {
968 sprintf(digits, "%06.2f", nav2);
969 for ( i = 0; i < 6; ++i ) {
972 radio_display_data[27] = digits[4] << 4 | digits[5];
973 radio_display_data[28] = digits[1] << 4 | digits[2];
974 radio_display_data[29] = 0x00 | digits[0];
975 // the 0x00 in the upper nibble of the 6th byte of each display
976 // turns on the decimal point
978 // ADF standby frequency
979 float adf_stby = adf_stby_freq->getFloatValue();
980 if ( fabs(adf_stby) > 999.99 ) {
983 sprintf(digits, "%03.0f", adf_stby);
984 for ( i = 0; i < 6; ++i ) {
987 radio_display_data[30] = digits[2] << 4 | 0x0f;
988 radio_display_data[31] = digits[0] << 4 | digits[1];
990 // ADF in use frequency
991 float adf = adf_freq->getFloatValue();
992 if ( fabs(adf) > 999.99 ) {
995 sprintf(digits, "%03.0f", adf);
996 for ( i = 0; i < 6; ++i ) {
999 radio_display_data[33] = digits[1] << 4 | digits[2];
1000 radio_display_data[34] = 0xf0 | digits[0];
1002 ATC610xSetRadios( radios_fd, radio_display_data );
1008 /////////////////////////////////////////////////////////////////////
1009 // Drive the stepper motors
1010 /////////////////////////////////////////////////////////////////////
1012 bool FGATC610x::do_steppers() {
1013 float diff = mag_compass->getFloatValue() - compass_position;
1014 while ( diff < -180.0 ) { diff += 360.0; }
1015 while ( diff > 180.0 ) { diff -= 360.0; }
1017 int steps = (int)(diff * 4);
1018 // cout << "steps = " << steps << endl;
1019 if ( steps > 4 ) { steps = 4; }
1020 if ( steps < -4 ) { steps = -4; }
1022 if ( abs(steps) > 0 ) {
1023 unsigned char cmd = 0x80; // stepper command
1025 cmd |= 0x20; // go up
1027 cmd |= 0x00; // go down
1031 // sync compass_position with hardware position
1032 compass_position += (float)steps / 4.0;
1034 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1041 /////////////////////////////////////////////////////////////////////
1042 // Read the switch positions
1043 /////////////////////////////////////////////////////////////////////
1045 // decode the packed switch data
1046 static void update_switch_matrix(
1048 unsigned char switch_data[ATC_SWITCH_BYTES],
1049 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
1051 for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
1052 unsigned char switches = switch_data[row];
1054 for( int column = 0; column < ATC_NUM_COLS; ++column ) {
1055 switch_matrix[board][column][row] = switches & 1;
1056 switches = switches >> 1;
1061 bool FGATC610x::do_switches() {
1062 ATC610xReadSwitches( switches_fd, switch_data );
1064 // unpack the switch data
1065 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
1066 update_switch_matrix( board, switch_data, switch_matrix );
1068 // magnetos and starter switch
1070 bool starter = false;
1071 if ( switch_matrix[board][3][1] == 1 ) {
1074 } else if ( switch_matrix[board][2][1] == 1 ) {
1077 } else if ( switch_matrix[board][1][1] == 1 ) {
1080 } else if ( switch_matrix[board][0][1] == 1 ) {
1090 if ( switch_matrix[board][6][3] == 1 ) {
1092 } else if ( switch_matrix[board][5][3] == 1 ) {
1094 } else if ( switch_matrix[board][4][3] == 1 ) {
1096 } else if ( switch_matrix[board][4][3] == 0 ) {
1100 // do a bit of filtering on the magneto/starter switch and the
1101 // flap lever because these are not well debounced in hardware
1102 static int mag1, mag2, mag3;
1106 if ( mag1 == mag2 && mag2 == mag3 ) {
1107 fgSetInt( "/controls/magnetos[0]", magnetos );
1110 static bool start1, start2, start3;
1114 if ( start1 == start2 && start2 == start3 ) {
1115 fgSetBool( "/controls/starter[0]", starter );
1118 static float flap1, flap2, flap3;
1122 if ( flap1 == flap2 && flap2 == flap3 ) {
1123 fgSetFloat( "/controls/flaps", flaps );
1130 bool FGATC610x::process() {
1132 // Lock the hardware, skip if it's not ready yet
1133 if ( ATC610xLock( lock_fd ) > 0 ) {
1137 do_radio_switches();
1142 ATC610xRelease( lock_fd );
1151 bool FGATC610x::close() {