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( _MSCVER_ )
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"
56 # define snprintf _snprintf
59 // Lock the ATC 610 hardware
60 static int ATC610xLock( int fd ) {
62 lseek( fd, 0, SEEK_SET );
65 int result = read( fd, tmp, 1 );
67 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
74 // Write a radios command
75 static int ATC610xRelease( int fd ) {
77 lseek( fd, 0, SEEK_SET );
81 int result = write( fd, tmp, 1 );
84 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
92 static void ATC610xReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
94 lseek( fd, 0, SEEK_SET );
96 int result = read( fd, analog_in_bytes, ATC_ANAL_IN_BYTES );
97 if ( result != ATC_ANAL_IN_BYTES ) {
98 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
104 // Write a radios command
105 static int ATC610xSetRadios( int fd,
106 unsigned char data[ATC_RADIO_DISPLAY_BYTES] )
109 lseek( fd, 0, SEEK_SET );
111 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
113 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
114 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
121 // Read status of last radios written to
122 static void ATC610xReadRadios( int fd, unsigned char *switch_data ) {
124 lseek( fd, 0, SEEK_SET );
126 int result = read( fd, switch_data, ATC_RADIO_SWITCH_BYTES );
127 if ( result != ATC_RADIO_SWITCH_BYTES ) {
128 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
133 // Write a stepper command
134 static int ATC610xSetStepper( int fd, unsigned char channel,
135 unsigned char value )
138 lseek( fd, 0, SEEK_SET );
141 unsigned char buf[3];
145 int result = write( fd, buf, 2 );
147 SG_LOG( SG_IO, SG_INFO, "Write failed" );
149 SG_LOG( SG_IO, SG_DEBUG,
150 "Sent cmd = " << (int)channel << " value = " << (int)value );
155 // Read status of last stepper written to
156 static unsigned char ATC610xReadStepper( int fd ) {
160 lseek( fd, 0, SEEK_SET );
163 unsigned char buf[2];
164 result = read( fd, buf, 1 );
166 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
169 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
175 // Read switch inputs
176 static void ATC610xReadSwitches( int fd, unsigned char *switch_bytes ) {
178 lseek( fd, 0, SEEK_SET );
180 int result = read( fd, switch_bytes, ATC_SWITCH_BYTES );
181 if ( result != ATC_SWITCH_BYTES ) {
182 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
188 // Open and initialize ATC 610x hardware
189 bool FGATC610x::open() {
190 if ( is_enabled() ) {
191 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
192 << "is already in use, ignoring" );
196 SG_LOG( SG_IO, SG_ALERT,
197 "Initializing ATC 610x hardware, please wait ..." );
199 set_hz( 30 ); // default to processing requests @ 30Hz
202 board = 0; // 610x uses a single board number = 0
204 snprintf( lock_file, 256, "/proc/atc610x/board%d/lock", board );
205 snprintf( analog_in_file, 256, "/proc/atc610x/board%d/analog_in", board );
206 snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
207 snprintf( stepper_file, 256, "/proc/atc610x/board%d/steppers", board );
208 snprintf( switches_file, 256, "/proc/atc610x/board%d/switches", board );
210 /////////////////////////////////////////////////////////////////////
211 // Open the /proc files
212 /////////////////////////////////////////////////////////////////////
214 lock_fd = ::open( lock_file, O_RDWR );
215 if ( lock_fd == -1 ) {
216 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
218 snprintf( msg, 256, "Error opening %s", lock_file );
223 analog_in_fd = ::open( analog_in_file, O_RDONLY );
224 if ( analog_in_fd == -1 ) {
225 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
227 snprintf( msg, 256, "Error opening %s", analog_in_file );
232 radios_fd = ::open( radios_file, O_RDWR );
233 if ( radios_fd == -1 ) {
234 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
236 snprintf( msg, 256, "Error opening %s", radios_file );
241 stepper_fd = ::open( stepper_file, O_RDWR );
242 if ( stepper_fd == -1 ) {
243 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
245 snprintf( msg, 256, "Error opening %s", stepper_file );
250 switches_fd = ::open( switches_file, O_RDONLY );
251 if ( switches_fd == -1 ) {
252 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
254 snprintf( msg, 256, "Error opening %s", switches_file );
259 /////////////////////////////////////////////////////////////////////
260 // Home the compass stepper motor
261 /////////////////////////////////////////////////////////////////////
263 SG_LOG( SG_IO, SG_ALERT,
264 " - Homing the compass stepper motor" );
266 // Lock the hardware, keep trying until we succeed
267 while ( ATC610xLock( lock_fd ) <= 0 );
269 // Send the stepper home command
270 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
272 // Release the hardware
273 ATC610xRelease( lock_fd );
277 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
279 while ( ATC610xLock( lock_fd ) <= 0 );
281 unsigned char result = ATC610xReadStepper( stepper_fd );
286 ATC610xRelease( lock_fd );
288 #if defined( _MSCVER_ )
289 ulMilliSecondSleep(33);
295 compass_position = 0.0;
297 /////////////////////////////////////////////////////////////////////
298 // Blank the radio display
299 /////////////////////////////////////////////////////////////////////
301 SG_LOG( SG_IO, SG_ALERT,
302 " - Clearing the radios displays." );
305 unsigned char value = 0xff;
306 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
307 radio_display_data[channel] = value;
310 // Lock the hardware, keep trying until we succeed
311 while ( ATC610xLock( lock_fd ) <= 0 );
314 ATC610xSetRadios( radios_fd, radio_display_data );
316 ATC610xRelease( lock_fd );
318 /////////////////////////////////////////////////////////////////////
319 // Finished initing hardware
320 /////////////////////////////////////////////////////////////////////
322 SG_LOG( SG_IO, SG_ALERT,
323 "Done initializing ATC 610x hardware." );
325 /////////////////////////////////////////////////////////////////////
326 // Connect up to property values
327 /////////////////////////////////////////////////////////////////////
329 mag_compass = fgGetNode( "/steam/mag-compass-deg", true );
331 dme_min = fgGetNode( "/radios/dme/ete-min", true );
332 dme_kt = fgGetNode( "/radios/dme/speed-kt", true );
333 dme_nm = fgGetNode( "/radios/dme/distance-nm", true );
335 com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
337 = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
338 com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
340 = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
342 nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
344 = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
346 nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
348 = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
350 adf_freq = fgGetNode( "/radios/adf/frequencies/selected-khz", true );
351 adf_stby_freq = fgGetNode( "/radios/adf/frequencies/standby-khz", true );
357 /////////////////////////////////////////////////////////////////////
358 // Read analog inputs
359 /////////////////////////////////////////////////////////////////////
361 #define ATC_AILERON_CENTER 535
362 #define ATC_ELEVATOR_TRIM_CENTER 512
363 #define ATC_ELEVATOR_CENTER 543
365 bool FGATC610x::do_analog_in() {
366 // Read raw data in byte form
367 ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
369 // Convert to integer values
370 for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
371 unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
372 unsigned char lo = analog_in_bytes[2 * channel + 1];
373 analog_in_data[channel] = hi * 256 + lo;
375 // printf("%02x %02x ", hi, lo );
376 // printf("%04d ", value );
379 float tmp, tmp1, tmp2;
382 tmp = (float)(analog_in_data[0] - ATC_AILERON_CENTER) / 256.0f;
383 fgSetFloat( "/controls/aileron", tmp );
384 // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
387 tmp = (float)(analog_in_data[4] - ATC_ELEVATOR_TRIM_CENTER) / 512.0f;
388 fgSetFloat( "/controls/elevator-trim", tmp );
389 // cout << "trim = " << analog_in_data[4] << " = " << tmp;
392 tmp = (float)(ATC_ELEVATOR_CENTER - analog_in_data[5]) / 100.0f;
393 fgSetFloat( "/controls/elevator", tmp );
394 // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
397 tmp = (float)analog_in_data[7] / 680.0f;
398 fgSetFloat( "/controls/mixture[0]", tmp );
401 tmp = (float)analog_in_data[8] / 690.0f;
402 fgSetFloat( "/controls/throttle[0]", tmp );
405 tmp = (float)analog_in_data[25] / 1024.0f;
406 fgSetFloat( "/radios/nav[0]/volume", tmp );
409 tmp = (float)analog_in_data[24] / 1024.0f;
410 fgSetFloat( "/radios/nav[1]/volume", tmp );
413 tmp = (float)analog_in_data[26] / 1024.0f;
414 fgSetFloat( "/radios/adf/volume", tmp );
417 tmp = (float)analog_in_data[29] * 360.0f / 1024.0f;
418 fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
421 tmp1 = (float)analog_in_data[30] * 360.0f / 1024.0f;
422 tmp2 = (float)analog_in_data[31] * 360.0f / 1024.0f;
423 fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp1 );
429 /////////////////////////////////////////////////////////////////////
430 // Read radio switches
431 /////////////////////////////////////////////////////////////////////
433 bool FGATC610x::do_radio_switches() {
436 ATC610xReadRadios( radios_fd, radio_switch_data );
439 dme_switch = (radio_switch_data[7] >> 4) & 0x03;
440 if ( dme_switch == 0 ) {
442 fgSetInt( "/radios/dme/switch-position", 0 );
443 } else if ( dme_switch == 2 ) {
445 fgSetInt( "/radios/dme/switch-position", 1 );
446 } else if ( dme_switch == 1 ) {
448 fgSetInt( "/radios/dme/switch-position", 3 );
452 int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
453 static int last_com1_swap;
454 if ( com1_swap && (last_com1_swap != com1_swap) ) {
455 float tmp = com1_freq->getFloatValue();
456 fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
457 com1_stby_freq->getFloatValue() );
458 fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
460 last_com1_swap = com1_swap;
463 int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
464 static int last_com2_swap;
465 if ( com2_swap && (last_com2_swap != com2_swap) ) {
466 float tmp = com2_freq->getFloatValue();
467 fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
468 com2_stby_freq->getFloatValue() );
469 fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
471 last_com2_swap = com2_swap;
474 int nav1_swap = radio_switch_data[11] & 0x01;
475 static int last_nav1_swap;
476 if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
477 float tmp = nav1_freq->getFloatValue();
478 fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
479 nav1_stby_freq->getFloatValue() );
480 fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
482 last_nav1_swap = nav1_swap;
485 int nav2_swap = !(radio_switch_data[19] & 0x01);
486 static int last_nav2_swap;
487 if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
488 float tmp = nav2_freq->getFloatValue();
489 fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
490 nav2_stby_freq->getFloatValue() );
491 fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
493 last_nav2_swap = nav2_swap;
496 int com1_tuner_fine = (radio_switch_data[5] >> 4) & 0x0f;
497 int com1_tuner_course = radio_switch_data[5] & 0x0f;
498 // cout << "com1 = " << com1_tuner_fine << " " << com1_tuner_course << endl;
499 static int last_com1_tuner_fine = com1_tuner_fine;
500 static int last_com1_tuner_course = com1_tuner_course;
502 if ( com1_tuner_fine != last_com1_tuner_fine ) {
503 if ( com1_tuner_fine == 0x0c && last_com1_tuner_fine == 0x01 ) {
505 } else if ( com1_tuner_fine == 0x01 && last_com1_tuner_fine == 0x0c ) {
507 } else if ( com1_tuner_fine > last_com1_tuner_fine ) {
513 if ( com1_tuner_course != last_com1_tuner_course ) {
514 if ( com1_tuner_course == 0x0c && last_com1_tuner_course == 0x01 ) {
516 } else if ( com1_tuner_course == 0x01
517 && last_com1_tuner_course == 0x0c ) {
519 } else if ( com1_tuner_course > last_com1_tuner_course ) {
525 last_com1_tuner_fine = com1_tuner_fine;
526 last_com1_tuner_course = com1_tuner_course;
528 freq = com1_stby_freq->getFloatValue() + inc;
532 if ( freq > 140.0 ) {
535 fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", freq );
538 int com2_tuner_fine = (radio_switch_data[13] >> 4) & 0x0f;
539 int com2_tuner_course = radio_switch_data[13] & 0x0f;
540 static int last_com2_tuner_fine = com2_tuner_fine;
541 static int last_com2_tuner_course = com2_tuner_course;
543 if ( com2_tuner_fine != last_com2_tuner_fine ) {
544 if ( com2_tuner_fine == 0x0c && last_com2_tuner_fine == 0x01 ) {
546 } else if ( com2_tuner_fine == 0x01 && last_com2_tuner_fine == 0x0c ) {
548 } else if ( com2_tuner_fine > last_com2_tuner_fine ) {
554 if ( com2_tuner_course != last_com2_tuner_course ) {
555 if ( com2_tuner_course == 0x0c && last_com2_tuner_course == 0x01 ) {
557 } else if ( com2_tuner_course == 0x01
558 && last_com2_tuner_course == 0x0c ) {
560 } else if ( com2_tuner_course > last_com2_tuner_course ) {
566 last_com2_tuner_fine = com2_tuner_fine;
567 last_com2_tuner_course = com2_tuner_course;
569 freq = com2_stby_freq->getFloatValue() + inc;
573 if ( freq > 140.0 ) {
576 fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", freq );
579 int nav1_tuner_fine = (radio_switch_data[9] >> 4) & 0x0f;
580 int nav1_tuner_course = radio_switch_data[9] & 0x0f;
581 static int last_nav1_tuner_fine = nav1_tuner_fine;
582 static int last_nav1_tuner_course = nav1_tuner_course;
584 if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
585 if ( nav1_tuner_fine == 0x0c && last_nav1_tuner_fine == 0x01 ) {
587 } else if ( nav1_tuner_fine == 0x01 && last_nav1_tuner_fine == 0x0c ) {
589 } else if ( nav1_tuner_fine > last_nav1_tuner_fine ) {
595 if ( nav1_tuner_course != last_nav1_tuner_course ) {
596 if ( nav1_tuner_course == 0x0c && last_nav1_tuner_course == 0x01 ) {
598 } else if ( nav1_tuner_course == 0x01
599 && last_nav1_tuner_course == 0x0c ) {
601 } else if ( nav1_tuner_course > last_nav1_tuner_course ) {
607 last_nav1_tuner_fine = nav1_tuner_fine;
608 last_nav1_tuner_course = nav1_tuner_course;
610 freq = nav1_stby_freq->getFloatValue() + inc;
611 if ( freq < 108.0 ) {
614 if ( freq > 117.95 ) {
617 fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", freq );
620 int nav2_tuner_fine = (radio_switch_data[17] >> 4) & 0x0f;
621 int nav2_tuner_course = radio_switch_data[17] & 0x0f;
622 static int last_nav2_tuner_fine = nav2_tuner_fine;
623 static int last_nav2_tuner_course = nav2_tuner_course;
625 if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
626 if ( nav2_tuner_fine == 0x0c && last_nav2_tuner_fine == 0x01 ) {
628 } else if ( nav2_tuner_fine == 0x01 && last_nav2_tuner_fine == 0x0c ) {
630 } else if ( nav2_tuner_fine > last_nav2_tuner_fine ) {
636 if ( nav2_tuner_course != last_nav2_tuner_course ) {
637 if ( nav2_tuner_course == 0x0c && last_nav2_tuner_course == 0x01 ) {
639 } else if ( nav2_tuner_course == 0x01
640 && last_nav2_tuner_course == 0x0c ) {
642 } else if ( nav2_tuner_course > last_nav2_tuner_course ) {
648 last_nav2_tuner_fine = nav2_tuner_fine;
649 last_nav2_tuner_course = nav2_tuner_course;
651 freq = nav2_stby_freq->getFloatValue() + inc;
652 if ( freq < 108.0 ) {
655 if ( freq > 117.95 ) {
658 fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", freq );
661 int adf_tuner_fine = (radio_switch_data[21] >> 4) & 0x0f;
662 int adf_tuner_course = radio_switch_data[21] & 0x0f;
663 // cout << "adf = " << adf_tuner_fine << " " << adf_tuner_course << endl;
664 static int last_adf_tuner_fine = adf_tuner_fine;
665 static int last_adf_tuner_course = adf_tuner_course;
667 if ( adf_tuner_fine != last_adf_tuner_fine ) {
668 if ( adf_tuner_fine == 0x0c && last_adf_tuner_fine == 0x01 ) {
670 } else if ( adf_tuner_fine == 0x01 && last_adf_tuner_fine == 0x0c ) {
672 } else if ( adf_tuner_fine > last_adf_tuner_fine ) {
678 if ( adf_tuner_course != last_adf_tuner_course ) {
679 if ( adf_tuner_course == 0x0c && last_adf_tuner_course == 0x01 ) {
681 } else if ( adf_tuner_course == 0x01
682 && last_adf_tuner_course == 0x0c ) {
684 } else if ( adf_tuner_course > last_adf_tuner_course ) {
690 last_adf_tuner_fine = adf_tuner_fine;
691 last_adf_tuner_course = adf_tuner_course;
693 freq = adf_freq->getFloatValue() + inc;
694 if ( freq < 100.0 ) {
700 fgSetFloat( "/radios/adf/frequencies/selected-khz", freq );
706 /////////////////////////////////////////////////////////////////////
707 // Update the radio display
708 /////////////////////////////////////////////////////////////////////
710 bool FGATC610x::do_radio_display() {
715 if ( dme_switch != 0 ) {
717 float minutes = dme_min->getFloatValue();
718 if ( minutes > 999 ) {
721 sprintf(digits, "%03.0f", minutes);
722 for ( i = 0; i < 6; ++i ) {
725 radio_display_data[0] = digits[1] << 4 | digits[2];
726 radio_display_data[1] = 0xf0 | digits[0];
729 float knots = dme_kt->getFloatValue();
733 sprintf(digits, "%03.0f", knots);
734 for ( i = 0; i < 6; ++i ) {
737 radio_display_data[2] = digits[1] << 4 | digits[2];
738 radio_display_data[3] = 0xf0 | digits[0];
741 float nm = dme_nm->getFloatValue();
745 sprintf(digits, "%04.1f", nm);
746 for ( i = 0; i < 6; ++i ) {
749 radio_display_data[4] = digits[1] << 4 | digits[3];
750 radio_display_data[5] = 0x00 | digits[0];
751 // the 0x00 in the upper nibble of the 6th byte of each
752 // display turns on the decimal point
755 for ( i = 0; i < 6; ++i ) {
756 radio_display_data[i] = 0xff;
760 // Com1 standby frequency
761 float com1_stby = com1_stby_freq->getFloatValue();
762 if ( fabs(com1_stby) > 999.99 ) {
765 sprintf(digits, "%06.3f", com1_stby);
766 for ( i = 0; i < 6; ++i ) {
769 radio_display_data[6] = digits[4] << 4 | digits[5];
770 radio_display_data[7] = digits[1] << 4 | digits[2];
771 radio_display_data[8] = 0xf0 | digits[0];
773 // Com1 in use frequency
774 float com1 = com1_freq->getFloatValue();
775 if ( fabs(com1) > 999.99 ) {
778 sprintf(digits, "%06.3f", com1);
779 for ( i = 0; i < 6; ++i ) {
782 radio_display_data[9] = digits[4] << 4 | digits[5];
783 radio_display_data[10] = digits[1] << 4 | digits[2];
784 radio_display_data[11] = 0x00 | digits[0];
785 // the 0x00 in the upper nibble of the 6th byte of each display
786 // turns on the decimal point
788 // Com2 standby frequency
789 float com2_stby = com2_stby_freq->getFloatValue();
790 if ( fabs(com2_stby) > 999.99 ) {
793 sprintf(digits, "%06.3f", com2_stby);
794 for ( i = 0; i < 6; ++i ) {
797 radio_display_data[18] = digits[4] << 4 | digits[5];
798 radio_display_data[19] = digits[1] << 4 | digits[2];
799 radio_display_data[20] = 0xf0 | digits[0];
801 // Com2 in use frequency
802 float com2 = com2_freq->getFloatValue();
803 if ( fabs(com2) > 999.99 ) {
806 sprintf(digits, "%06.3f", com2);
807 for ( i = 0; i < 6; ++i ) {
810 radio_display_data[21] = digits[4] << 4 | digits[5];
811 radio_display_data[22] = digits[1] << 4 | digits[2];
812 radio_display_data[23] = 0x00 | digits[0];
813 // the 0x00 in the upper nibble of the 6th byte of each display
814 // turns on the decimal point
816 // Nav1 standby frequency
817 float nav1_stby = nav1_stby_freq->getFloatValue();
818 if ( fabs(nav1_stby) > 999.99 ) {
821 sprintf(digits, "%06.2f", nav1_stby);
822 for ( i = 0; i < 6; ++i ) {
825 radio_display_data[12] = digits[4] << 4 | digits[5];
826 radio_display_data[13] = digits[1] << 4 | digits[2];
827 radio_display_data[14] = 0xf0 | digits[0];
829 // Nav1 in use frequency
830 float nav1 = nav1_freq->getFloatValue();
831 if ( fabs(nav1) > 999.99 ) {
834 sprintf(digits, "%06.2f", nav1);
835 for ( i = 0; i < 6; ++i ) {
838 radio_display_data[15] = digits[4] << 4 | digits[5];
839 radio_display_data[16] = digits[1] << 4 | digits[2];
840 radio_display_data[17] = 0x00 | digits[0];
841 // the 0x00 in the upper nibble of the 6th byte of each display
842 // turns on the decimal point
844 // Nav2 standby frequency
845 float nav2_stby = nav2_stby_freq->getFloatValue();
846 if ( fabs(nav2_stby) > 999.99 ) {
849 sprintf(digits, "%06.2f", nav2_stby);
850 for ( i = 0; i < 6; ++i ) {
853 radio_display_data[24] = digits[4] << 4 | digits[5];
854 radio_display_data[25] = digits[1] << 4 | digits[2];
855 radio_display_data[26] = 0xf0 | digits[0];
857 // Nav2 in use frequency
858 float nav2 = nav2_freq->getFloatValue();
859 if ( fabs(nav2) > 999.99 ) {
862 sprintf(digits, "%06.2f", nav2);
863 for ( i = 0; i < 6; ++i ) {
866 radio_display_data[27] = digits[4] << 4 | digits[5];
867 radio_display_data[28] = digits[1] << 4 | digits[2];
868 radio_display_data[29] = 0x00 | digits[0];
869 // the 0x00 in the upper nibble of the 6th byte of each display
870 // turns on the decimal point
872 // ADF standby frequency
873 float adf_stby = adf_stby_freq->getFloatValue();
874 if ( fabs(adf_stby) > 999.99 ) {
877 sprintf(digits, "%03.0f", adf_stby);
878 for ( i = 0; i < 6; ++i ) {
881 radio_display_data[30] = digits[2] << 4 | 0x0f;
882 radio_display_data[31] = digits[0] << 4 | digits[1];
884 // ADF in use frequency
885 float adf = adf_freq->getFloatValue();
886 if ( fabs(adf) > 999.99 ) {
889 sprintf(digits, "%03.0f", adf);
890 for ( i = 0; i < 6; ++i ) {
893 radio_display_data[33] = digits[1] << 4 | digits[2];
894 radio_display_data[34] = 0xf0 | digits[0];
896 ATC610xSetRadios( radios_fd, radio_display_data );
902 /////////////////////////////////////////////////////////////////////
903 // Drive the stepper motors
904 /////////////////////////////////////////////////////////////////////
906 bool FGATC610x::do_steppers() {
907 float diff = mag_compass->getFloatValue() - compass_position;
908 while ( diff < -180.0 ) { diff += 360.0; }
909 while ( diff > 180.0 ) { diff -= 360.0; }
911 int steps = (int)(diff * 4);
912 // cout << "steps = " << steps << endl;
913 if ( steps > 4 ) { steps = 4; }
914 if ( steps < -4 ) { steps = -4; }
916 if ( abs(steps) > 0 ) {
917 unsigned char cmd = 0x80; // stepper command
919 cmd |= 0x20; // go up
921 cmd |= 0x00; // go down
925 // sync compass_position with hardware position
926 compass_position += (float)steps / 4.0;
928 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
935 /////////////////////////////////////////////////////////////////////
936 // Read the switch positions
937 /////////////////////////////////////////////////////////////////////
939 // decode the packed switch data
940 static void update_switch_matrix(
942 unsigned char switch_data[ATC_SWITCH_BYTES],
943 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
945 for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
946 unsigned char switches = switch_data[row];
948 for( int column = 0; column < ATC_NUM_COLS; ++column ) {
949 switch_matrix[board][column][row] = switches & 1;
950 switches = switches >> 1;
955 bool FGATC610x::do_switches() {
956 ATC610xReadSwitches( switches_fd, switch_data );
958 // unpack the switch data
959 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
960 update_switch_matrix( board, switch_data, switch_matrix );
962 // magnetos and starter switch
963 if ( switch_matrix[board][3][1] == 1 ) {
964 fgSetInt( "/controls/magnetos[0]", 3 );
965 fgSetBool( "/controls/starter[0]", true );
966 } else if ( switch_matrix[board][2][1] == 1 ) {
967 fgSetInt( "/controls/magnetos[0]", 3 );
968 fgSetBool( "/controls/starter[0]", false );
969 } else if ( switch_matrix[board][1][1] == 1 ) {
970 fgSetInt( "/controls/magnetos[0]", 2 );
971 fgSetBool( "/controls/starter[0]", false );
972 } else if ( switch_matrix[board][0][1] == 1 ) {
973 fgSetInt( "/controls/magnetos[0]", 1 );
974 fgSetBool( "/controls/starter[0]", false );
976 fgSetInt( "/controls/magnetos[0]", 0 );
977 fgSetBool( "/controls/starter[0]", false );
981 if ( switch_matrix[board][6][3] == 1 ) {
982 fgSetFloat( "/controls/flaps", 1.0 );
983 } else if ( switch_matrix[board][5][3] == 1 ) {
984 fgSetFloat( "/controls/flaps", 0.66 );
985 } else if ( switch_matrix[board][4][3] == 1 ) {
986 fgSetFloat( "/controls/flaps", 0.33 );
987 } else if ( switch_matrix[board][4][3] == 0 ) {
988 fgSetFloat( "/controls/flaps", 0.0 );
995 bool FGATC610x::process() {
997 // Lock the hardware, skip if it's not ready yet
998 if ( ATC610xLock( lock_fd ) > 0 ) {
1001 do_radio_switches();
1006 ATC610xRelease( lock_fd );
1015 bool FGATC610x::close() {