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>
47 #include <simgear/misc/sg_path.hxx>
49 #include <Main/fg_props.hxx>
50 #include <Main/globals.hxx>
52 #include "atc610x.hxx"
56 // Lock the ATC 610 hardware
57 static int ATC610xLock( int fd ) {
59 lseek( fd, 0, SEEK_SET );
62 int result = read( fd, tmp, 1 );
64 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
71 // Write a radios command
72 static int ATC610xRelease( int fd ) {
74 lseek( fd, 0, SEEK_SET );
78 int result = write( fd, tmp, 1 );
81 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
89 static void ATC610xReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
91 lseek( fd, 0, SEEK_SET );
93 int result = read( fd, analog_in_bytes, ATC_ANAL_IN_BYTES );
94 if ( result != ATC_ANAL_IN_BYTES ) {
95 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
101 // Write a radios command
102 static int ATC610xSetRadios( int fd,
103 unsigned char data[ATC_RADIO_DISPLAY_BYTES] )
106 lseek( fd, 0, SEEK_SET );
108 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
110 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
111 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
118 // Read status of last radios written to
119 static void ATC610xReadRadios( int fd, unsigned char *switch_data ) {
121 lseek( fd, 0, SEEK_SET );
123 int result = read( fd, switch_data, ATC_RADIO_SWITCH_BYTES );
124 if ( result != ATC_RADIO_SWITCH_BYTES ) {
125 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
130 // Write a stepper command
131 static int ATC610xSetStepper( int fd, unsigned char channel,
132 unsigned char value )
135 lseek( fd, 0, SEEK_SET );
138 unsigned char buf[3];
142 int result = write( fd, buf, 2 );
144 SG_LOG( SG_IO, SG_INFO, "Write failed" );
146 SG_LOG( SG_IO, SG_DEBUG,
147 "Sent cmd = " << (int)channel << " value = " << (int)value );
152 // Read status of last stepper written to
153 static unsigned char ATC610xReadStepper( int fd ) {
157 lseek( fd, 0, SEEK_SET );
160 unsigned char buf[2];
161 result = read( fd, buf, 1 );
163 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
166 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
172 // Read switch inputs
173 static void ATC610xReadSwitches( int fd, unsigned char *switch_bytes ) {
175 lseek( fd, 0, SEEK_SET );
177 int result = read( fd, switch_bytes, ATC_SWITCH_BYTES );
178 if ( result != ATC_SWITCH_BYTES ) {
179 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
185 // Turn a lamp on or off
186 void ATC610xSetLamp( int fd, int channel, bool value ) {
187 // lamp channels 0-63 are written to LampPort0, channels 64-127
188 // are written to LampPort1
190 // bits 0-6 are the lamp address
191 // bit 7 is the value (on/off)
196 unsigned char buf[3];
200 result = write( fd, buf, 2 );
202 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
208 void FGATC610x::init_config() {
209 #if defined( unix ) || defined( __CYGWIN__ )
210 // Next check home directory for .fgfsrc.hostname file
211 char *envp = ::getenv( "HOME" );
212 if ( envp != NULL ) {
213 SGPath atc610x_config( envp );
214 atc610x_config.append( ".fgfs-atc610x.xml" );
215 readProperties( atc610x_config.str(), globals->get_props() );
221 // Open and initialize ATC 610x hardware
222 bool FGATC610x::open() {
223 if ( is_enabled() ) {
224 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
225 << "is already in use, ignoring" );
229 // This loads the config parameters generated by "simcal"
232 SG_LOG( SG_IO, SG_ALERT,
233 "Initializing ATC 610x hardware, please wait ..." );
235 set_hz( 30 ); // default to processing requests @ 30Hz
238 board = 0; // 610x uses a single board number = 0
240 snprintf( lock_file, 256, "/proc/atc610x/board%d/lock", board );
241 snprintf( analog_in_file, 256, "/proc/atc610x/board%d/analog_in", board );
242 snprintf( lamps_file, 256, "/proc/atc610x/board%d/lamps", board );
243 snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
244 snprintf( stepper_file, 256, "/proc/atc610x/board%d/steppers", board );
245 snprintf( switches_file, 256, "/proc/atc610x/board%d/switches", board );
247 /////////////////////////////////////////////////////////////////////
248 // Open the /proc files
249 /////////////////////////////////////////////////////////////////////
251 lock_fd = ::open( lock_file, O_RDWR );
252 if ( lock_fd == -1 ) {
253 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
255 snprintf( msg, 256, "Error opening %s", lock_file );
260 analog_in_fd = ::open( analog_in_file, O_RDONLY );
261 if ( analog_in_fd == -1 ) {
262 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
264 snprintf( msg, 256, "Error opening %s", analog_in_file );
269 lamps_fd = ::open( lamps_file, O_WRONLY );
270 if ( lamps_fd == -1 ) {
271 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
273 snprintf( msg, 256, "Error opening %s", lamps_file );
278 radios_fd = ::open( radios_file, O_RDWR );
279 if ( radios_fd == -1 ) {
280 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
282 snprintf( msg, 256, "Error opening %s", radios_file );
287 stepper_fd = ::open( stepper_file, O_RDWR );
288 if ( stepper_fd == -1 ) {
289 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
291 snprintf( msg, 256, "Error opening %s", stepper_file );
296 switches_fd = ::open( switches_file, O_RDONLY );
297 if ( switches_fd == -1 ) {
298 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
300 snprintf( msg, 256, "Error opening %s", switches_file );
305 /////////////////////////////////////////////////////////////////////
306 // Home the compass stepper motor
307 /////////////////////////////////////////////////////////////////////
309 SG_LOG( SG_IO, SG_ALERT,
310 " - Homing the compass stepper motor" );
312 // Lock the hardware, keep trying until we succeed
313 while ( ATC610xLock( lock_fd ) <= 0 );
315 // Send the stepper home command
316 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, ATC_STEPPER_HOME );
318 // Release the hardware
319 ATC610xRelease( lock_fd );
321 SG_LOG( SG_IO, SG_ALERT,
322 " - Waiting for compass to come home." );
325 int timeout = 900; // about 30 seconds
327 while ( ! home && timeout > 0 ) {
328 if ( timeout % 150 == 0 ) {
329 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
331 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
334 while ( ATC610xLock( lock_fd ) <= 0 );
336 unsigned char result = ATC610xReadStepper( stepper_fd );
341 ATC610xRelease( lock_fd );
343 #if defined( _MSC_VER )
344 ulMilliSecondSleep(33);
345 #elif defined (WIN32) && !defined(__CYGWIN__)
354 compass_position = 0.0;
356 /////////////////////////////////////////////////////////////////////
357 // Blank the radio display
358 /////////////////////////////////////////////////////////////////////
360 SG_LOG( SG_IO, SG_ALERT,
361 " - Clearing the radios displays." );
364 unsigned char value = 0xff;
365 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
366 radio_display_data[channel] = value;
369 // Lock the hardware, keep trying until we succeed
370 while ( ATC610xLock( lock_fd ) <= 0 );
373 ATC610xSetRadios( radios_fd, radio_display_data );
375 ATC610xRelease( lock_fd );
377 /////////////////////////////////////////////////////////////////////
379 /////////////////////////////////////////////////////////////////////
381 for ( int i = 0; i < 128; ++i ) {
382 ATC610xSetLamp( lamps_fd, i, false );
385 /////////////////////////////////////////////////////////////////////
386 // Finished initing hardware
387 /////////////////////////////////////////////////////////////////////
389 SG_LOG( SG_IO, SG_ALERT,
390 "Done initializing ATC 610x hardware." );
392 /////////////////////////////////////////////////////////////////////
393 // Connect up to property values
394 /////////////////////////////////////////////////////////////////////
396 mag_compass = fgGetNode( "/steam/mag-compass-deg", true );
398 dme_min = fgGetNode( "/radios/dme/ete-min", true );
399 dme_kt = fgGetNode( "/radios/dme/speed-kt", true );
400 dme_nm = fgGetNode( "/radios/dme/distance-nm", true );
402 adf_bus_power = fgGetNode( "/systems/electrical/outputs/adf", true );
403 dme_bus_power = fgGetNode( "/systems/electrical/outputs/dme", true );
404 navcom1_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[0]",
406 navcom2_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[1]",
408 xpdr_bus_power = fgGetNode( "/systems/electrical/outputs/transponder",
411 navcom1_power_btn = fgGetNode( "/radios/comm[0]/inputs/power-btn", true );
412 navcom2_power_btn = fgGetNode( "/radios/comm[1]/inputs/power-btn", true );
414 com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
416 = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
418 com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
420 = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
422 nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
424 = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
426 nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
428 = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
430 adf_power_btn = fgGetNode( "/radios/kr-87/inputs/power-btn", true );
431 adf_vol = fgGetNode( "/radios/kr-87/inputs/volume", true );
432 adf_adf_btn = fgGetNode( "/radios/kr-87/inputs/adf-btn", true );
433 adf_bfo_btn = fgGetNode( "/radios/kr-87/inputs/bfo-btn", true );
434 adf_freq = fgGetNode( "/radios/kr-87/outputs/selected-khz", true );
435 adf_stby_freq = fgGetNode( "/radios/kr-87/outputs/standby-khz", true );
436 adf_stby_mode = fgGetNode( "/radios/kr-87/modes/stby", true );
437 adf_timer_mode = fgGetNode( "/radios/kr-87/modes/timer", true );
438 adf_count_mode = fgGetNode( "/radios/kr-87/modes/count", true );
439 adf_flight_timer = fgGetNode( "/radios/kr-87/outputs/flight-timer", true );
440 adf_elapsed_timer = fgGetNode( "/radios/kr-87/outputs/elapsed-timer",
442 adf_ant_ann = fgGetNode( "/radios/kr-87/annunciators/ant", true );
443 adf_adf_ann = fgGetNode( "/radios/kr-87/annunciators/adf", true );
444 adf_bfo_ann = fgGetNode( "/radios/kr-87/annunciators/bfo", true );
445 adf_frq_ann = fgGetNode( "/radios/kr-87/annunciators/frq", true );
446 adf_flt_ann = fgGetNode( "/radios/kr-87/annunciators/flt", true );
447 adf_et_ann = fgGetNode( "/radios/kr-87/annunciators/et", true );
449 inner = fgGetNode( "/radios/marker-beacon/inner", true );
450 middle = fgGetNode( "/radios/marker-beacon/middle", true );
451 outer = fgGetNode( "/radios/marker-beacon/outer", true );
453 xpdr_ident_btn = fgGetNode( "/radios/kt-70/inputs/ident-btn", true );
454 xpdr_digit1 = fgGetNode( "/radios/kt-70/inputs/digit1", true );
455 xpdr_digit2 = fgGetNode( "/radios/kt-70/inputs/digit2", true );
456 xpdr_digit3 = fgGetNode( "/radios/kt-70/inputs/digit3", true );
457 xpdr_digit4 = fgGetNode( "/radios/kt-70/inputs/digit4", true );
458 xpdr_func_knob = fgGetNode( "/radios/kt-70/inputs/func-knob", true );
459 xpdr_id_code = fgGetNode( "/radios/kt-70/outputs/id-code", true );
460 xpdr_flight_level = fgGetNode( "/radios/kt-70/outputs/flight-level", true );
461 xpdr_fl_ann = fgGetNode( "/radios/kt-70/annunciators/fl", true );
462 xpdr_alt_ann = fgGetNode( "/radios/kt-70/annunciators/alt", true );
463 xpdr_gnd_ann = fgGetNode( "/radios/kt-70/annunciators/gnd", true );
464 xpdr_on_ann = fgGetNode( "/radios/kt-70/annunciators/on", true );
465 xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
466 xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
468 elevator_center = fgGetNode( "/input/atc610x/elevator/center", 0 );
469 elevator_min = fgGetNode( "/input/atc610x/elevator/min", 0 );
470 elevator_max = fgGetNode( "/input/atc610x/elevator/max", 0 );
472 ailerons_center = fgGetNode( "/input/atc610x/ailerons/center", 0 );
473 ailerons_min = fgGetNode( "/input/atc610x/ailerons/min", 0 );
474 ailerons_max = fgGetNode( "/input/atc610x/ailerons/max", 0 );
476 rudder_center = fgGetNode( "/input/atc610x/rudder/center", 0 );
477 rudder_min = fgGetNode( "/input/atc610x/rudder/min", 0 );
478 rudder_max = fgGetNode( "/input/atc610x/rudder/max", 0 );
480 throttle_min = fgGetNode( "/input/atc610x/throttle/min", 0 );
481 throttle_max = fgGetNode( "/input/atc610x/throttle/max", 0 );
483 mixture_min = fgGetNode( "/input/atc610x/mixture/min", 0 );
484 mixture_max = fgGetNode( "/input/atc610x/mixture/max", 0 );
486 trim_center = fgGetNode( "/input/atc610x/trim/center", 0 );
487 trim_min = fgGetNode( "/input/atc610x/trim/min", 0 );
488 trim_max = fgGetNode( "/input/atc610x/trim/max", 0 );
490 nav1vol_min = fgGetNode( "/input/atc610x/nav1vol/min", 0 );
491 nav1vol_max = fgGetNode( "/input/atc610x/nav1vol/max", 0 );
493 nav2vol_min = fgGetNode( "/input/atc610x/nav2vol/min", 0 );
494 nav2vol_max = fgGetNode( "/input/atc610x/nav2vol/max", 0 );
500 /////////////////////////////////////////////////////////////////////
501 // Read analog inputs
502 /////////////////////////////////////////////////////////////////////
504 // scale a number between min and max (with center defined) to a scale
506 static double scale( int center, int min, int max, int value ) {
507 // cout << center << " " << min << " " << max << " " << value << " ";
511 if ( value <= center ) {
512 range = center - min;
513 result = (value - center) / range;
515 range = max - center;
516 result = (value - center) / range;
519 if ( result < -1.0 ) result = -1.0;
520 if ( result > 1.0 ) result = 1.0;
522 // cout << result << endl;
528 // scale a number between min and max to a scale from 0.0 to 1.0
529 static double scale( int min, int max, int value ) {
530 // cout << center << " " << min << " " << max << " " << value << " ";
535 result = (value - min) / range;
537 if ( result < 0.0 ) result = 0.0;
538 if ( result > 1.0 ) result = 1.0;
540 // cout << result << endl;
546 bool FGATC610x::do_analog_in() {
547 // Read raw data in byte form
548 ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
550 // Convert to integer values
551 for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
552 unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
553 unsigned char lo = analog_in_bytes[2 * channel + 1];
554 analog_in_data[channel] = hi * 256 + lo;
556 // printf("%02x %02x ", hi, lo );
557 // printf("%04d ", value );
560 float tmp, tmp1, tmp2;
563 tmp = scale( ailerons_center->getIntValue(), ailerons_min->getIntValue(),
564 ailerons_max->getIntValue(), analog_in_data[0] );
565 fgSetFloat( "/controls/aileron", tmp );
566 // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
569 tmp = -scale( elevator_center->getIntValue(), elevator_min->getIntValue(),
570 elevator_max->getIntValue(), analog_in_data[5] );
571 fgSetFloat( "/controls/elevator", tmp );
572 // cout << "trim = " << analog_in_data[4] << " = " << tmp;
575 tmp = scale( trim_center->getIntValue(), trim_min->getIntValue(),
576 trim_max->getIntValue(), analog_in_data[4] );
577 fgSetFloat( "/controls/elevator-trim", tmp );
578 // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
581 tmp = scale( mixture_min->getIntValue(), mixture_max->getIntValue(),
583 fgSetFloat( "/controls/mixture[0]", tmp );
584 fgSetFloat( "/controls/mixture[1]", tmp );
587 tmp = scale( throttle_min->getIntValue(), throttle_max->getIntValue(),
589 fgSetFloat( "/controls/throttle[0]", tmp );
590 fgSetFloat( "/controls/throttle[1]", tmp );
591 // cout << "throttle = " << tmp << endl;
595 tmp = scale( rudder_center->getIntValue(), rudder_min->getIntValue(),
596 rudder_max->getIntValue(), analog_in_data[10] );
597 fgSetFloat( "/controls/rudder", -tmp );
601 tmp = (float)analog_in_data[25] / 1024.0f;
602 fgSetFloat( "/radios/nav[0]/volume", tmp );
605 tmp = (float)analog_in_data[24] / 1024.0f;
606 fgSetFloat( "/radios/nav[1]/volume", tmp );
609 tmp = (float)analog_in_data[26] / 1024.0f;
610 fgSetFloat( "/radios/kr-87/inputs/volume", tmp );
613 tmp = (float)analog_in_data[29] * 360.0f / 1024.0f;
614 fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
617 tmp1 = (float)analog_in_data[30] * 360.0f / 1024.0f;
618 tmp2 = (float)analog_in_data[31] * 360.0f / 1024.0f;
619 fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp1 );
625 /////////////////////////////////////////////////////////////////////
627 /////////////////////////////////////////////////////////////////////
629 bool FGATC610x::do_lights() {
632 ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
633 ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
634 ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
637 ATC610xSetLamp( lamps_fd, 11, adf_ant_ann->getBoolValue() ); // ANT
638 ATC610xSetLamp( lamps_fd, 12, adf_adf_ann->getBoolValue() ); // ADF
639 ATC610xSetLamp( lamps_fd, 13, adf_bfo_ann->getBoolValue() ); // BFO
640 ATC610xSetLamp( lamps_fd, 14, adf_frq_ann->getBoolValue() ); // FRQ
641 ATC610xSetLamp( lamps_fd, 15, adf_flt_ann->getBoolValue() ); // FLT
642 ATC610xSetLamp( lamps_fd, 16, adf_et_ann->getBoolValue() ); // ET
644 // Transponder annunciators
645 ATC610xSetLamp( lamps_fd, 17, xpdr_fl_ann->getBoolValue() ); // FL
646 ATC610xSetLamp( lamps_fd, 18, xpdr_alt_ann->getBoolValue() ); // ALT
647 ATC610xSetLamp( lamps_fd, 19, xpdr_gnd_ann->getBoolValue() ); // GND
648 ATC610xSetLamp( lamps_fd, 20, xpdr_on_ann->getBoolValue() ); // ON
649 ATC610xSetLamp( lamps_fd, 21, xpdr_sby_ann->getBoolValue() ); // SBY
650 ATC610xSetLamp( lamps_fd, 22, xpdr_reply_ann->getBoolValue() ); // R
656 /////////////////////////////////////////////////////////////////////
657 // Read radio switches
658 /////////////////////////////////////////////////////////////////////
660 bool FGATC610x::do_radio_switches() {
661 double freq, coarse_freq, fine_freq, value;
664 ATC610xReadRadios( radios_fd, radio_switch_data );
667 dme_switch = (radio_switch_data[7] >> 4) & 0x03;
668 if ( dme_switch == 0 ) {
670 fgSetInt( "/radios/dme/switch-position", 0 );
671 } else if ( dme_switch == 2 ) {
673 fgSetInt( "/radios/dme/switch-position", 1 );
674 } else if ( dme_switch == 1 ) {
676 fgSetInt( "/radios/dme/switch-position", 3 );
680 fgSetBool( "/radios/comm[0]/inputs/power-btn",
681 radio_switch_data[7] & 0x01 );
683 if ( navcom1_has_power() ) {
685 int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
686 static int last_com1_swap;
687 if ( com1_swap && (last_com1_swap != com1_swap) ) {
688 float tmp = com1_freq->getFloatValue();
689 fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
690 com1_stby_freq->getFloatValue() );
691 fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
693 last_com1_swap = com1_swap;
697 fgSetBool( "/radios/comm[1]/inputs/power-btn",
698 radio_switch_data[15] & 0x01 );
700 if ( navcom2_has_power() ) {
702 int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
703 static int last_com2_swap;
704 if ( com2_swap && (last_com2_swap != com2_swap) ) {
705 float tmp = com2_freq->getFloatValue();
706 fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
707 com2_stby_freq->getFloatValue() );
708 fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
710 last_com2_swap = com2_swap;
713 if ( navcom1_has_power() ) {
715 int nav1_swap = radio_switch_data[11] & 0x01;
716 static int last_nav1_swap;
717 if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
718 float tmp = nav1_freq->getFloatValue();
719 fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
720 nav1_stby_freq->getFloatValue() );
721 fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
723 last_nav1_swap = nav1_swap;
726 if ( navcom2_has_power() ) {
728 int nav2_swap = !(radio_switch_data[19] & 0x01);
729 static int last_nav2_swap;
730 if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
731 float tmp = nav2_freq->getFloatValue();
732 fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
733 nav2_stby_freq->getFloatValue() );
734 fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
736 last_nav2_swap = nav2_swap;
739 if ( navcom1_has_power() ) {
741 int com1_tuner_fine = ((radio_switch_data[5] >> 4) & 0x0f) - 1;
742 int com1_tuner_coarse = (radio_switch_data[5] & 0x0f) - 1;
743 static int last_com1_tuner_fine = com1_tuner_fine;
744 static int last_com1_tuner_coarse = com1_tuner_coarse;
746 freq = com1_stby_freq->getFloatValue();
747 coarse_freq = (int)freq;
748 fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
750 if ( com1_tuner_fine != last_com1_tuner_fine ) {
751 diff = com1_tuner_fine - last_com1_tuner_fine;
752 if ( abs(diff) > 4 ) {
754 if ( com1_tuner_fine < last_com1_tuner_fine ) {
756 diff = 12 - last_com1_tuner_fine + com1_tuner_fine;
759 diff = com1_tuner_fine - 12 - last_com1_tuner_fine;
764 while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
765 while ( fine_freq < 0.0 ) { fine_freq += 40.0; }
767 if ( com1_tuner_coarse != last_com1_tuner_coarse ) {
768 diff = com1_tuner_coarse - last_com1_tuner_coarse;
769 if ( abs(diff) > 4 ) {
771 if ( com1_tuner_coarse < last_com1_tuner_coarse ) {
773 diff = 12 - last_com1_tuner_coarse + com1_tuner_coarse;
776 diff = com1_tuner_coarse - 12 - last_com1_tuner_coarse;
781 if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
782 if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
784 last_com1_tuner_fine = com1_tuner_fine;
785 last_com1_tuner_coarse = com1_tuner_coarse;
787 fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz",
788 coarse_freq + fine_freq / 40.0 );
791 if ( navcom2_has_power() ) {
793 int com2_tuner_fine = ((radio_switch_data[13] >> 4) & 0x0f) - 1;
794 int com2_tuner_coarse = (radio_switch_data[13] & 0x0f) - 1;
795 static int last_com2_tuner_fine = com2_tuner_fine;
796 static int last_com2_tuner_coarse = com2_tuner_coarse;
798 freq = com2_stby_freq->getFloatValue();
799 coarse_freq = (int)freq;
800 fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
802 if ( com2_tuner_fine != last_com2_tuner_fine ) {
803 diff = com2_tuner_fine - last_com2_tuner_fine;
804 if ( abs(diff) > 4 ) {
806 if ( com2_tuner_fine < last_com2_tuner_fine ) {
808 diff = 12 - last_com2_tuner_fine + com2_tuner_fine;
811 diff = com2_tuner_fine - 12 - last_com2_tuner_fine;
816 while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
817 while ( fine_freq < 0.0 ) { fine_freq += 40.0; }
819 if ( com2_tuner_coarse != last_com2_tuner_coarse ) {
820 diff = com2_tuner_coarse - last_com2_tuner_coarse;
821 if ( abs(diff) > 4 ) {
823 if ( com2_tuner_coarse < last_com2_tuner_coarse ) {
825 diff = 12 - last_com2_tuner_coarse + com2_tuner_coarse;
828 diff = com2_tuner_coarse - 12 - last_com2_tuner_coarse;
833 if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
834 if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
836 last_com2_tuner_fine = com2_tuner_fine;
837 last_com2_tuner_coarse = com2_tuner_coarse;
839 fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz",
840 coarse_freq + fine_freq / 40.0 );
843 if ( navcom1_has_power() ) {
845 int nav1_tuner_fine = ((radio_switch_data[9] >> 4) & 0x0f) - 1;
846 int nav1_tuner_coarse = (radio_switch_data[9] & 0x0f) - 1;
847 static int last_nav1_tuner_fine = nav1_tuner_fine;
848 static int last_nav1_tuner_coarse = nav1_tuner_coarse;
850 freq = nav1_stby_freq->getFloatValue();
851 coarse_freq = (int)freq;
852 fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
854 if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
855 diff = nav1_tuner_fine - last_nav1_tuner_fine;
856 if ( abs(diff) > 4 ) {
858 if ( nav1_tuner_fine < last_nav1_tuner_fine ) {
860 diff = 12 - last_nav1_tuner_fine + nav1_tuner_fine;
863 diff = nav1_tuner_fine - 12 - last_nav1_tuner_fine;
868 while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
869 while ( fine_freq < 0.0 ) { fine_freq += 20.0; }
871 if ( nav1_tuner_coarse != last_nav1_tuner_coarse ) {
872 diff = nav1_tuner_coarse - last_nav1_tuner_coarse;
873 if ( abs(diff) > 4 ) {
875 if ( nav1_tuner_coarse < last_nav1_tuner_coarse ) {
877 diff = 12 - last_nav1_tuner_coarse + nav1_tuner_coarse;
880 diff = nav1_tuner_coarse - 12 - last_nav1_tuner_coarse;
885 if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
886 if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
888 last_nav1_tuner_fine = nav1_tuner_fine;
889 last_nav1_tuner_coarse = nav1_tuner_coarse;
891 fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz",
892 coarse_freq + fine_freq / 20.0 );
895 if ( navcom2_has_power() ) {
897 int nav2_tuner_fine = ((radio_switch_data[17] >> 4) & 0x0f) - 1;
898 int nav2_tuner_coarse = (radio_switch_data[17] & 0x0f) - 1;
899 static int last_nav2_tuner_fine = nav2_tuner_fine;
900 static int last_nav2_tuner_coarse = nav2_tuner_coarse;
902 freq = nav2_stby_freq->getFloatValue();
903 coarse_freq = (int)freq;
904 fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
906 if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
907 diff = nav2_tuner_fine - last_nav2_tuner_fine;
908 if ( abs(diff) > 4 ) {
910 if ( nav2_tuner_fine < last_nav2_tuner_fine ) {
912 diff = 12 - last_nav2_tuner_fine + nav2_tuner_fine;
915 diff = nav2_tuner_fine - 12 - last_nav2_tuner_fine;
920 while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
921 while ( fine_freq < 0.0 ) { fine_freq += 20.0; }
923 if ( nav2_tuner_coarse != last_nav2_tuner_coarse ) {
924 diff = nav2_tuner_coarse - last_nav2_tuner_coarse;
925 if ( abs(diff) > 4 ) {
927 if ( nav2_tuner_coarse < last_nav2_tuner_coarse ) {
929 diff = 12 - last_nav2_tuner_coarse + nav2_tuner_coarse;
932 diff = nav2_tuner_coarse - 12 - last_nav2_tuner_coarse;
937 if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
938 if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
940 last_nav2_tuner_fine = nav2_tuner_fine;
941 last_nav2_tuner_coarse = nav2_tuner_coarse;
943 fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz",
944 coarse_freq + fine_freq / 20.0);
949 int adf_tuner_fine = ((radio_switch_data[21] >> 4) & 0x0f) - 1;
950 int adf_tuner_coarse = (radio_switch_data[21] & 0x0f) - 1;
951 static int last_adf_tuner_fine = adf_tuner_fine;
952 static int last_adf_tuner_coarse = adf_tuner_coarse;
954 if ( adf_has_power() ) {
955 // cout << "adf_stby_mode = " << adf_stby_mode->getIntValue() << endl;
956 if ( adf_count_mode->getIntValue() == 2 ) {
957 // tune count down timer
958 value = adf_elapsed_timer->getDoubleValue();
961 if ( adf_stby_mode->getIntValue() == 1 ) {
962 value = adf_freq->getFloatValue();
964 value = adf_stby_freq->getFloatValue();
968 if ( adf_tuner_fine != last_adf_tuner_fine ) {
969 diff = adf_tuner_fine - last_adf_tuner_fine;
970 if ( abs(diff) > 4 ) {
972 if ( adf_tuner_fine < last_adf_tuner_fine ) {
974 diff = 12 - last_adf_tuner_fine + adf_tuner_fine;
977 diff = adf_tuner_fine - 12 - last_adf_tuner_fine;
983 if ( adf_tuner_coarse != last_adf_tuner_coarse ) {
984 diff = adf_tuner_coarse - last_adf_tuner_coarse;
985 if ( abs(diff) > 4 ) {
987 if ( adf_tuner_coarse < last_adf_tuner_coarse ) {
989 diff = 12 - last_adf_tuner_coarse + adf_tuner_coarse;
992 diff = adf_tuner_coarse - 12 - last_adf_tuner_coarse;
995 if ( adf_count_mode->getIntValue() == 2 ) {
1001 if ( adf_count_mode->getIntValue() == 2 ) {
1002 if ( value < 0 ) { value += 3600; }
1003 if ( value > 3599 ) { value -= 3600; }
1005 if ( value < 200 ) { value += 1600; }
1006 if ( value > 1799 ) { value -= 1600; }
1009 if ( adf_count_mode->getIntValue() == 2 ) {
1010 fgSetFloat( "/radios/kr-87/outputs/elapsed-timer", value );
1012 if ( adf_stby_mode->getIntValue() == 1 ) {
1013 fgSetFloat( "/radios/kr-87/outputs/selected-khz", value );
1015 fgSetFloat( "/radios/kr-87/outputs/standby-khz", value );
1019 last_adf_tuner_fine = adf_tuner_fine;
1020 last_adf_tuner_coarse = adf_tuner_coarse;
1024 fgSetInt( "/radios/kr-87/inputs/adf-btn",
1025 !(radio_switch_data[23] & 0x01) );
1026 fgSetInt( "/radios/kr-87/inputs/bfo-btn",
1027 !(radio_switch_data[23] >> 1 & 0x01) );
1028 fgSetInt( "/radios/kr-87/inputs/frq-btn",
1029 (radio_switch_data[23] >> 2 & 0x01) );
1030 fgSetInt( "/radios/kr-87/inputs/flt-et-btn",
1031 !(radio_switch_data[23] >> 3 & 0x01) );
1032 fgSetInt( "/radios/kr-87/inputs/set-rst-btn",
1033 !(radio_switch_data[23] >> 4 & 0x01) );
1034 fgSetInt( "/radios/kr-87/inputs/power-btn",
1035 radio_switch_data[23] >> 5 & 0x01 );
1036 /* cout << "adf = " << !(radio_switch_data[23] & 0x01)
1037 << " bfo = " << !(radio_switch_data[23] >> 1 & 0x01)
1038 << " stby = " << !(radio_switch_data[23] >> 2 & 0x01)
1039 << " timer = " << !(radio_switch_data[23] >> 3 & 0x01)
1040 << " set/rst = " << !(radio_switch_data[23] >> 4 & 0x01)
1043 // Transponder Tuner
1046 digit_tuner[0] = radio_switch_data[25] & 0x0f;
1047 digit_tuner[1] = ( radio_switch_data[25] >> 4 ) & 0x0f;
1048 digit_tuner[2] = radio_switch_data[29] & 0x0f;
1049 digit_tuner[3] = ( radio_switch_data[29] >> 4 ) & 0x0f;
1051 static int last_digit_tuner[4];
1052 static bool first_time = true;
1055 for ( i = 0; i < 4; ++i ) {
1056 last_digit_tuner[i] = digit_tuner[i];
1060 if ( xpdr_has_power() ) {
1061 int id_code = xpdr_id_code->getIntValue();
1064 for ( i = 0; i < 4; ++i ) {
1065 digit[i] = id_code / place;
1066 id_code -= digit[i] * place;
1070 for ( i = 0; i < 4; ++i ) {
1071 if ( digit_tuner[i] != last_digit_tuner[i] ) {
1072 diff = digit_tuner[i] - last_digit_tuner[i];
1073 if ( abs(diff) > 4 ) {
1075 if ( digit_tuner[i] < last_digit_tuner[i] ) {
1077 diff = 15 - last_digit_tuner[i] + digit_tuner[i];
1080 diff = digit_tuner[i] - 15 - last_digit_tuner[i];
1085 while ( digit[i] >= 8 ) { digit[i] -= 8; }
1086 while ( digit[i] < 0 ) { digit[i] += 8; }
1089 fgSetInt( "/radios/kt-70/inputs/digit1", digit[0] );
1090 fgSetInt( "/radios/kt-70/inputs/digit2", digit[1] );
1091 fgSetInt( "/radios/kt-70/inputs/digit3", digit[2] );
1092 fgSetInt( "/radios/kt-70/inputs/digit4", digit[3] );
1094 for ( i = 0; i < 4; ++i ) {
1095 last_digit_tuner[i] = digit_tuner[i];
1099 for ( i = 0; i < 5; ++i ) {
1100 if ( radio_switch_data[27] >> i & 0x01 ) {
1104 fgSetInt( "/radios/kt-70/inputs/func-knob", tmp );
1105 fgSetInt( "/radios/kt-70/inputs/ident-btn",
1106 !(radio_switch_data[27] >> 5 & 0x01) );
1112 /////////////////////////////////////////////////////////////////////
1113 // Update the radio display
1114 /////////////////////////////////////////////////////////////////////
1116 bool FGATC610x::do_radio_display() {
1121 if ( dme_has_power() ) {
1123 float minutes = dme_min->getFloatValue();
1124 if ( minutes > 999 ) {
1127 snprintf(digits, 7, "%03.0f", minutes);
1128 for ( i = 0; i < 6; ++i ) {
1131 radio_display_data[0] = digits[1] << 4 | digits[2];
1132 radio_display_data[1] = 0xf0 | digits[0];
1135 float knots = dme_kt->getFloatValue();
1136 if ( knots > 999 ) {
1139 snprintf(digits, 7, "%03.0f", knots);
1140 for ( i = 0; i < 6; ++i ) {
1143 radio_display_data[2] = digits[1] << 4 | digits[2];
1144 radio_display_data[3] = 0xf0 | digits[0];
1146 // DME distance (nm)
1147 float nm = dme_nm->getFloatValue();
1151 snprintf(digits, 7, "%04.1f", nm);
1152 for ( i = 0; i < 6; ++i ) {
1155 radio_display_data[4] = digits[1] << 4 | digits[3];
1156 radio_display_data[5] = 0x00 | digits[0];
1157 // the 0x00 in the upper nibble of the 6th byte of each
1158 // display turns on the decimal point
1160 // blank dem display
1161 for ( i = 0; i < 6; ++i ) {
1162 radio_display_data[i] = 0xff;
1166 if ( navcom1_has_power() ) {
1167 // Com1 standby frequency
1168 float com1_stby = com1_stby_freq->getFloatValue();
1169 if ( fabs(com1_stby) > 999.99 ) {
1172 snprintf(digits, 7, "%06.3f", com1_stby);
1173 for ( i = 0; i < 6; ++i ) {
1176 radio_display_data[6] = digits[4] << 4 | digits[5];
1177 radio_display_data[7] = digits[1] << 4 | digits[2];
1178 radio_display_data[8] = 0xf0 | digits[0];
1180 // Com1 in use frequency
1181 float com1 = com1_freq->getFloatValue();
1182 if ( fabs(com1) > 999.99 ) {
1185 snprintf(digits, 7, "%06.3f", com1);
1186 for ( i = 0; i < 6; ++i ) {
1189 radio_display_data[9] = digits[4] << 4 | digits[5];
1190 radio_display_data[10] = digits[1] << 4 | digits[2];
1191 radio_display_data[11] = 0x00 | digits[0];
1192 // the 0x00 in the upper nibble of the 6th byte of each display
1193 // turns on the decimal point
1195 radio_display_data[6] = 0xff;
1196 radio_display_data[7] = 0xff;
1197 radio_display_data[8] = 0xff;
1198 radio_display_data[9] = 0xff;
1199 radio_display_data[10] = 0xff;
1200 radio_display_data[11] = 0xff;
1203 if ( navcom2_has_power() ) {
1204 // Com2 standby frequency
1205 float com2_stby = com2_stby_freq->getFloatValue();
1206 if ( fabs(com2_stby) > 999.99 ) {
1209 snprintf(digits, 7, "%06.3f", com2_stby);
1210 for ( i = 0; i < 6; ++i ) {
1213 radio_display_data[18] = digits[4] << 4 | digits[5];
1214 radio_display_data[19] = digits[1] << 4 | digits[2];
1215 radio_display_data[20] = 0xf0 | digits[0];
1217 // Com2 in use frequency
1218 float com2 = com2_freq->getFloatValue();
1219 if ( fabs(com2) > 999.99 ) {
1222 snprintf(digits, 7, "%06.3f", com2);
1223 for ( i = 0; i < 6; ++i ) {
1226 radio_display_data[21] = digits[4] << 4 | digits[5];
1227 radio_display_data[22] = digits[1] << 4 | digits[2];
1228 radio_display_data[23] = 0x00 | digits[0];
1229 // the 0x00 in the upper nibble of the 6th byte of each display
1230 // turns on the decimal point
1232 radio_display_data[18] = 0xff;
1233 radio_display_data[19] = 0xff;
1234 radio_display_data[20] = 0xff;
1235 radio_display_data[21] = 0xff;
1236 radio_display_data[22] = 0xff;
1237 radio_display_data[23] = 0xff;
1240 if ( navcom1_has_power() ) {
1241 // Nav1 standby frequency
1242 float nav1_stby = nav1_stby_freq->getFloatValue();
1243 if ( fabs(nav1_stby) > 999.99 ) {
1246 snprintf(digits, 7, "%06.2f", nav1_stby);
1247 for ( i = 0; i < 6; ++i ) {
1250 radio_display_data[12] = digits[4] << 4 | digits[5];
1251 radio_display_data[13] = digits[1] << 4 | digits[2];
1252 radio_display_data[14] = 0xf0 | digits[0];
1254 // Nav1 in use frequency
1255 float nav1 = nav1_freq->getFloatValue();
1256 if ( fabs(nav1) > 999.99 ) {
1259 snprintf(digits, 7, "%06.2f", nav1);
1260 for ( i = 0; i < 6; ++i ) {
1263 radio_display_data[15] = digits[4] << 4 | digits[5];
1264 radio_display_data[16] = digits[1] << 4 | digits[2];
1265 radio_display_data[17] = 0x00 | digits[0];
1266 // the 0x00 in the upper nibble of the 6th byte of each display
1267 // turns on the decimal point
1269 radio_display_data[12] = 0xff;
1270 radio_display_data[13] = 0xff;
1271 radio_display_data[14] = 0xff;
1272 radio_display_data[15] = 0xff;
1273 radio_display_data[16] = 0xff;
1274 radio_display_data[17] = 0xff;
1277 if ( navcom2_has_power() ) {
1278 // Nav2 standby frequency
1279 float nav2_stby = nav2_stby_freq->getFloatValue();
1280 if ( fabs(nav2_stby) > 999.99 ) {
1283 snprintf(digits, 7, "%06.2f", nav2_stby);
1284 for ( i = 0; i < 6; ++i ) {
1287 radio_display_data[24] = digits[4] << 4 | digits[5];
1288 radio_display_data[25] = digits[1] << 4 | digits[2];
1289 radio_display_data[26] = 0xf0 | digits[0];
1291 // Nav2 in use frequency
1292 float nav2 = nav2_freq->getFloatValue();
1293 if ( fabs(nav2) > 999.99 ) {
1296 snprintf(digits, 7, "%06.2f", nav2);
1297 for ( i = 0; i < 6; ++i ) {
1300 radio_display_data[27] = digits[4] << 4 | digits[5];
1301 radio_display_data[28] = digits[1] << 4 | digits[2];
1302 radio_display_data[29] = 0x00 | digits[0];
1303 // the 0x00 in the upper nibble of the 6th byte of each display
1304 // turns on the decimal point
1306 radio_display_data[24] = 0xff;
1307 radio_display_data[25] = 0xff;
1308 radio_display_data[26] = 0xff;
1309 radio_display_data[27] = 0xff;
1310 radio_display_data[28] = 0xff;
1311 radio_display_data[29] = 0xff;
1314 // ADF standby frequency / timer
1315 if ( adf_has_power() ) {
1316 if ( adf_stby_mode->getIntValue() == 0 ) {
1318 float adf_stby = adf_stby_freq->getFloatValue();
1319 if ( fabs(adf_stby) > 1799 ) {
1322 snprintf(digits, 7, "%04.0f", adf_stby);
1323 for ( i = 0; i < 6; ++i ) {
1326 radio_display_data[30] = digits[3] << 4 | 0x0f;
1327 radio_display_data[31] = digits[1] << 4 | digits[2];
1328 if ( digits[0] == 0 ) {
1329 radio_display_data[32] = 0xff;
1331 radio_display_data[32] = 0xf0 | digits[0];
1336 int hours, min, sec;
1337 if ( adf_timer_mode->getIntValue() == 0 ) {
1338 time = adf_flight_timer->getDoubleValue();
1340 time = adf_elapsed_timer->getDoubleValue();
1342 // cout << time << endl;
1343 hours = (int)(time / 3600.0);
1344 time -= hours * 3600.00;
1345 min = (int)(time / 60.0);
1362 // cout << big << ":" << little << endl;
1363 snprintf(digits, 7, "%02d%02d", big, little);
1364 for ( i = 0; i < 6; ++i ) {
1367 radio_display_data[30] = digits[3] << 4 | 0x0f;
1368 radio_display_data[31] = digits[1] << 4 | digits[2];
1369 radio_display_data[32] = 0xf0 | digits[0];
1372 // ADF in use frequency
1373 float adf = adf_freq->getFloatValue();
1374 if ( fabs(adf) > 1799 ) {
1377 snprintf(digits, 7, "%04.0f", adf);
1378 for ( i = 0; i < 6; ++i ) {
1381 radio_display_data[33] = digits[2] << 4 | digits[3];
1382 if ( digits[0] == 0 ) {
1383 radio_display_data[34] = 0xf0 | digits[1];
1385 radio_display_data[34] = digits[0] << 4 | digits[1];
1387 if ( adf_stby_mode->getIntValue() == 0 ) {
1388 radio_display_data[35] = 0xff;
1390 radio_display_data[35] = 0x0f;
1393 radio_display_data[30] = 0xff;
1394 radio_display_data[31] = 0xff;
1395 radio_display_data[32] = 0xff;
1396 radio_display_data[33] = 0xff;
1397 radio_display_data[34] = 0xff;
1398 radio_display_data[35] = 0xff;
1401 // Transponder code and flight level
1402 if ( xpdr_has_power() ) {
1403 if ( xpdr_func_knob->getIntValue() == 2 ) {
1405 radio_display_data[36] = 8 << 4 | 8;
1406 radio_display_data[37] = 8 << 4 | 8;
1407 radio_display_data[38] = 0xff;
1408 radio_display_data[39] = 8 << 4 | 0x0f;
1409 radio_display_data[40] = 8 << 4 | 8;
1412 int id_code = xpdr_id_code->getIntValue();
1414 for ( i = 0; i < 4; ++i ) {
1415 digits[i] = id_code / place;
1416 id_code -= digits[i] * place;
1419 radio_display_data[36] = digits[2] << 4 | digits[3];
1420 radio_display_data[37] = digits[0] << 4 | digits[1];
1421 radio_display_data[38] = 0xff;
1423 if ( xpdr_func_knob->getIntValue() == 3 ||
1424 xpdr_func_knob->getIntValue() == 5 )
1426 // do flight level display
1427 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
1428 for ( i = 0; i < 6; ++i ) {
1431 radio_display_data[39] = digits[2] << 4 | 0x0f;
1432 radio_display_data[40] = digits[0] << 4 | digits[1];
1434 // blank flight level display
1435 radio_display_data[39] = 0xff;
1436 radio_display_data[40] = 0xff;
1441 radio_display_data[36] = 0xff;
1442 radio_display_data[37] = 0xff;
1443 radio_display_data[38] = 0xff;
1444 radio_display_data[39] = 0xff;
1445 radio_display_data[40] = 0xff;
1448 ATC610xSetRadios( radios_fd, radio_display_data );
1454 /////////////////////////////////////////////////////////////////////
1455 // Drive the stepper motors
1456 /////////////////////////////////////////////////////////////////////
1458 bool FGATC610x::do_steppers() {
1459 float diff = mag_compass->getFloatValue() - compass_position;
1460 while ( diff < -180.0 ) { diff += 360.0; }
1461 while ( diff > 180.0 ) { diff -= 360.0; }
1463 int steps = (int)(diff * 4);
1464 // cout << "steps = " << steps << endl;
1465 if ( steps > 4 ) { steps = 4; }
1466 if ( steps < -4 ) { steps = -4; }
1468 if ( abs(steps) > 0 ) {
1469 unsigned char cmd = 0x80; // stepper command
1471 cmd |= 0x20; // go up
1473 cmd |= 0x00; // go down
1477 // sync compass_position with hardware position
1478 compass_position += (float)steps / 4.0;
1480 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1487 /////////////////////////////////////////////////////////////////////
1488 // Read the switch positions
1489 /////////////////////////////////////////////////////////////////////
1491 // decode the packed switch data
1492 static void update_switch_matrix(
1494 unsigned char switch_data[ATC_SWITCH_BYTES],
1495 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
1497 for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
1498 unsigned char switches = switch_data[row];
1500 for( int column = 0; column < ATC_NUM_COLS; ++column ) {
1501 switch_matrix[board][column][row] = switches & 1;
1502 switches = switches >> 1;
1507 bool FGATC610x::do_switches() {
1508 ATC610xReadSwitches( switches_fd, switch_data );
1510 // unpack the switch data
1511 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
1512 update_switch_matrix( board, switch_data, switch_matrix );
1515 fgSetBool( "/controls/switches/master-bat", switch_matrix[board][4][1] );
1516 fgSetBool( "/controls/switches/master-alt", switch_matrix[board][5][1] );
1517 fgSetBool( "/controls/switches/master-avionics",
1518 switch_matrix[board][0][3] );
1520 // magnetos and starter switch
1522 bool starter = false;
1523 if ( switch_matrix[board][3][1] == 1 ) {
1526 } else if ( switch_matrix[board][2][1] == 1 ) {
1529 } else if ( switch_matrix[board][1][1] == 1 ) {
1532 } else if ( switch_matrix[board][0][1] == 1 ) {
1540 // do a bit of filtering on the magneto/starter switch and the
1541 // flap lever because these are not well debounced in hardware
1542 static int mag1, mag2, mag3;
1546 if ( mag1 == mag2 && mag2 == mag3 ) {
1547 fgSetInt( "/controls/magnetos[0]", magnetos );
1549 static bool start1, start2, start3;
1553 if ( start1 == start2 && start2 == start3 ) {
1554 fgSetBool( "/controls/starter[0]", starter );
1557 // other toggle switches
1558 fgSetBool( "/controls/fuel-pump[0]", switch_matrix[board][0][2] );
1559 fgSetBool( "/controls/switches/flashing-beacon",
1560 switch_matrix[board][1][2] );
1561 fgSetBool( "/controls/switches/landing-light", switch_matrix[board][2][2] );
1562 fgSetBool( "/controls/switches/taxi-lights", switch_matrix[board][3][2] );
1563 fgSetBool( "/controls/switches/nav-lights",
1564 switch_matrix[board][4][2] );
1565 fgSetBool( "/controls/switches/strobe-lights", switch_matrix[board][5][2] );
1566 fgSetBool( "/controls/switches/pitot-heat", switch_matrix[board][6][2] );
1570 if ( switch_matrix[board][6][3] ) {
1572 } else if ( switch_matrix[board][5][3] ) {
1574 } else if ( switch_matrix[board][4][3] ) {
1576 } else if ( !switch_matrix[board][4][3] ) {
1580 // do a bit of filtering on the magneto/starter switch and the
1581 // flap lever because these are not well debounced in hardware
1582 static float flap1, flap2, flap3;
1586 if ( flap1 == flap2 && flap2 == flap3 ) {
1587 fgSetFloat( "/controls/flaps", flaps );
1590 // fuel selector (also filtered)
1592 if ( switch_matrix[board][2][3] ) {
1595 } else if ( switch_matrix[board][1][3] ) {
1598 } else if ( switch_matrix[board][3][3] ) {
1606 static int fuel1, fuel2, fuel3;
1610 if ( fuel1 == fuel2 && fuel2 == fuel3 ) {
1611 fgSetBool( "/controls/fuel-selector[0]", (fuel & 0x01) > 0 );
1612 fgSetBool( "/controls/fuel-selector[1]", (fuel & 0x02) > 0 );
1616 fgSetBool( "/controls/circuit-breakers/cabin-lights-pwr",
1617 switch_matrix[board][0][0] );
1618 fgSetBool( "/controls/circuit-breakers/instr-ignition-switch",
1619 switch_matrix[board][1][0] );
1620 fgSetBool( "/controls/circuit-breakers/flaps",
1621 switch_matrix[board][2][0] );
1622 fgSetBool( "/controls/circuit-breakers/avn-bus-1",
1623 switch_matrix[board][3][0] );
1624 fgSetBool( "/controls/circuit-breakers/avn-bus-2",
1625 switch_matrix[board][4][0] );
1626 fgSetBool( "/controls/circuit-breakers/turn-coordinator",
1627 switch_matrix[board][5][0] );
1628 fgSetBool( "/controls/circuit-breakers/instrument-lights",
1629 switch_matrix[board][6][0] );
1630 fgSetBool( "/controls/circuit-breakers/annunciators",
1631 switch_matrix[board][7][0] );
1637 bool FGATC610x::process() {
1638 // Lock the hardware, skip if it's not ready yet
1639 if ( ATC610xLock( lock_fd ) > 0 ) {
1643 do_radio_switches();
1648 ATC610xRelease( lock_fd );
1657 bool FGATC610x::close() {