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_on_off_vol = fgGetNode( "/radios/kr-87/inputs/on-off-volume", true );
400 adf_adf_btn = fgGetNode( "/radios/kr-87/inputs/adf-btn", true );
401 adf_bfo_btn = fgGetNode( "/radios/kr-87/inputs/bfo-btn", true );
402 adf_freq = fgGetNode( "/radios/kr-87/outputs/selected-khz", true );
403 adf_stby_freq = fgGetNode( "/radios/kr-87/outputs/standby-khz", true );
404 adf_stby_mode = fgGetNode( "/radios/kr-87/modes/stby", true );
405 adf_timer_mode = fgGetNode( "/radios/kr-87/modes/timer", true );
406 adf_count_mode = fgGetNode( "/radios/kr-87/modes/count", true );
407 adf_flight_timer = fgGetNode( "/radios/kr-87/outputs/flight-timer", true );
408 adf_elapsed_timer = fgGetNode( "/radios/kr-87/outputs/elapsed-timer",
410 adf_ant_ann = fgGetNode( "/radios/kr-87/annunciators/ant", true );
411 adf_adf_ann = fgGetNode( "/radios/kr-87/annunciators/adf", true );
412 adf_bfo_ann = fgGetNode( "/radios/kr-87/annunciators/bfo", true );
413 adf_frq_ann = fgGetNode( "/radios/kr-87/annunciators/frq", true );
414 adf_flt_ann = fgGetNode( "/radios/kr-87/annunciators/flt", true );
415 adf_et_ann = fgGetNode( "/radios/kr-87/annunciators/et", true );
417 inner = fgGetNode( "/radios/marker-beacon/inner", true );
418 middle = fgGetNode( "/radios/marker-beacon/middle", true );
419 outer = fgGetNode( "/radios/marker-beacon/outer", true );
421 xpdr_ident_btn = fgGetNode( "/radios/kt-70/inputs/ident-btn", true );
422 xpdr_digit1 = fgGetNode( "/radios/kt-70/inputs/digit1", true );
423 xpdr_digit2 = fgGetNode( "/radios/kt-70/inputs/digit2", true );
424 xpdr_digit3 = fgGetNode( "/radios/kt-70/inputs/digit3", true );
425 xpdr_digit4 = fgGetNode( "/radios/kt-70/inputs/digit4", true );
426 xpdr_func_knob = fgGetNode( "/radios/kt-70/inputs/func-knob", true );
427 xpdr_id_code = fgGetNode( "/radios/kt-70/outputs/id-code", true );
428 xpdr_flight_level = fgGetNode( "/radios/kt-70/outputs/flight-level", true );
429 xpdr_fl_ann = fgGetNode( "/radios/kt-70/annunciators/fl", true );
430 xpdr_alt_ann = fgGetNode( "/radios/kt-70/annunciators/alt", true );
431 xpdr_gnd_ann = fgGetNode( "/radios/kt-70/annunciators/gnd", true );
432 xpdr_on_ann = fgGetNode( "/radios/kt-70/annunciators/on", true );
433 xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
434 xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
440 /////////////////////////////////////////////////////////////////////
441 // Read analog inputs
442 /////////////////////////////////////////////////////////////////////
444 #define ATC_AILERON_CENTER 535
445 #define ATC_ELEVATOR_TRIM_CENTER 512
446 #define ATC_ELEVATOR_CENTER 543
447 #define ATC_RUDDER_CENTER 519
449 bool FGATC610x::do_analog_in() {
450 // Read raw data in byte form
451 ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
453 // Convert to integer values
454 for ( int channel = 0; channel < ATC_ANAL_IN_VALUES; ++channel ) {
455 unsigned char hi = analog_in_bytes[2 * channel] & 0x03;
456 unsigned char lo = analog_in_bytes[2 * channel + 1];
457 analog_in_data[channel] = hi * 256 + lo;
459 // printf("%02x %02x ", hi, lo );
460 // printf("%04d ", value );
463 float tmp, tmp1, tmp2;
466 tmp = (float)(analog_in_data[0] - ATC_AILERON_CENTER) / 256.0f;
467 fgSetFloat( "/controls/aileron", tmp );
468 // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
471 tmp = (float)(analog_in_data[4] - ATC_ELEVATOR_TRIM_CENTER) / 512.0f;
472 fgSetFloat( "/controls/elevator-trim", tmp );
473 // cout << "trim = " << analog_in_data[4] << " = " << tmp;
476 tmp = (float)(ATC_ELEVATOR_CENTER - analog_in_data[5]) / 100.0f;
477 fgSetFloat( "/controls/elevator", tmp );
478 // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
481 tmp = (float)analog_in_data[7] / 680.0f;
482 fgSetFloat( "/controls/mixture[0]", tmp );
483 fgSetFloat( "/controls/mixture[1]", tmp );
486 tmp = ((float)analog_in_data[8] - 141.0) / 632.0f;
487 fgSetFloat( "/controls/throttle[0]", tmp );
488 fgSetFloat( "/controls/throttle[1]", tmp );
491 tmp = (float)(ATC_RUDDER_CENTER - analog_in_data[10]) / 145.0f;
492 fgSetFloat( "/controls/rudder", tmp );
495 tmp = (float)analog_in_data[25] / 1024.0f;
496 fgSetFloat( "/radios/nav[0]/volume", tmp );
499 tmp = (float)analog_in_data[24] / 1024.0f;
500 fgSetFloat( "/radios/nav[1]/volume", tmp );
503 tmp = (float)analog_in_data[26] / 1024.0f;
504 fgSetFloat( "/radios/kr-87/inputs/on-off-volume", tmp );
507 tmp = (float)analog_in_data[29] * 360.0f / 1024.0f;
508 fgSetFloat( "/radios/nav[1]/radials/selected-deg", tmp );
511 tmp1 = (float)analog_in_data[30] * 360.0f / 1024.0f;
512 tmp2 = (float)analog_in_data[31] * 360.0f / 1024.0f;
513 fgSetFloat( "/radios/nav[0]/radials/selected-deg", tmp1 );
519 /////////////////////////////////////////////////////////////////////
521 /////////////////////////////////////////////////////////////////////
523 bool FGATC610x::do_lights() {
526 ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
527 ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
528 ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
531 ATC610xSetLamp( lamps_fd, 11, adf_ant_ann->getBoolValue() ); // ANT
532 ATC610xSetLamp( lamps_fd, 12, adf_adf_ann->getBoolValue() ); // ADF
533 ATC610xSetLamp( lamps_fd, 13, adf_bfo_ann->getBoolValue() ); // BFO
534 ATC610xSetLamp( lamps_fd, 14, adf_frq_ann->getBoolValue() ); // FRQ
535 ATC610xSetLamp( lamps_fd, 15, adf_flt_ann->getBoolValue() ); // FLT
536 ATC610xSetLamp( lamps_fd, 16, adf_et_ann->getBoolValue() ); // ET
538 // Transponder annunciators
539 ATC610xSetLamp( lamps_fd, 17, xpdr_fl_ann->getBoolValue() ); // FL
540 ATC610xSetLamp( lamps_fd, 18, xpdr_alt_ann->getBoolValue() ); // ALT
541 ATC610xSetLamp( lamps_fd, 19, xpdr_gnd_ann->getBoolValue() ); // GND
542 ATC610xSetLamp( lamps_fd, 20, xpdr_on_ann->getBoolValue() ); // ON
543 ATC610xSetLamp( lamps_fd, 21, xpdr_sby_ann->getBoolValue() ); // SBY
544 ATC610xSetLamp( lamps_fd, 22, xpdr_reply_ann->getBoolValue() ); // R
550 /////////////////////////////////////////////////////////////////////
551 // Read radio switches
552 /////////////////////////////////////////////////////////////////////
554 bool FGATC610x::do_radio_switches() {
555 double freq, coarse_freq, fine_freq, value;
558 ATC610xReadRadios( radios_fd, radio_switch_data );
561 dme_switch = (radio_switch_data[7] >> 4) & 0x03;
562 if ( dme_switch == 0 ) {
564 fgSetInt( "/radios/dme/switch-position", 0 );
565 } else if ( dme_switch == 2 ) {
567 fgSetInt( "/radios/dme/switch-position", 1 );
568 } else if ( dme_switch == 1 ) {
570 fgSetInt( "/radios/dme/switch-position", 3 );
574 int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
575 static int last_com1_swap;
576 if ( com1_swap && (last_com1_swap != com1_swap) ) {
577 float tmp = com1_freq->getFloatValue();
578 fgSetFloat( "/radios/comm[0]/frequencies/selected-mhz",
579 com1_stby_freq->getFloatValue() );
580 fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz", tmp );
582 last_com1_swap = com1_swap;
585 int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
586 static int last_com2_swap;
587 if ( com2_swap && (last_com2_swap != com2_swap) ) {
588 float tmp = com2_freq->getFloatValue();
589 fgSetFloat( "/radios/comm[1]/frequencies/selected-mhz",
590 com2_stby_freq->getFloatValue() );
591 fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz", tmp );
593 last_com2_swap = com2_swap;
596 int nav1_swap = radio_switch_data[11] & 0x01;
597 static int last_nav1_swap;
598 if ( nav1_swap && (last_nav1_swap != nav1_swap) ) {
599 float tmp = nav1_freq->getFloatValue();
600 fgSetFloat( "/radios/nav[0]/frequencies/selected-mhz",
601 nav1_stby_freq->getFloatValue() );
602 fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz", tmp );
604 last_nav1_swap = nav1_swap;
607 int nav2_swap = !(radio_switch_data[19] & 0x01);
608 static int last_nav2_swap;
609 if ( nav2_swap && (last_nav2_swap != nav2_swap) ) {
610 float tmp = nav2_freq->getFloatValue();
611 fgSetFloat( "/radios/nav[1]/frequencies/selected-mhz",
612 nav2_stby_freq->getFloatValue() );
613 fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz", tmp );
615 last_nav2_swap = nav2_swap;
618 int com1_tuner_fine = ((radio_switch_data[5] >> 4) & 0x0f) - 1;
619 int com1_tuner_coarse = (radio_switch_data[5] & 0x0f) - 1;
620 static int last_com1_tuner_fine = com1_tuner_fine;
621 static int last_com1_tuner_coarse = com1_tuner_coarse;
623 freq = com1_stby_freq->getFloatValue();
624 coarse_freq = (int)freq;
625 fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
627 if ( com1_tuner_fine != last_com1_tuner_fine ) {
628 diff = com1_tuner_fine - last_com1_tuner_fine;
629 if ( abs(diff) > 4 ) {
631 if ( com1_tuner_fine < last_com1_tuner_fine ) {
633 diff = 12 - last_com1_tuner_fine + com1_tuner_fine;
636 diff = com1_tuner_fine - 12 - last_com1_tuner_fine;
641 while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
642 while ( fine_freq < 0.0 ) { fine_freq += 40.0; }
644 if ( com1_tuner_coarse != last_com1_tuner_coarse ) {
645 diff = com1_tuner_coarse - last_com1_tuner_coarse;
646 if ( abs(diff) > 4 ) {
648 if ( com1_tuner_coarse < last_com1_tuner_coarse ) {
650 diff = 12 - last_com1_tuner_coarse + com1_tuner_coarse;
653 diff = com1_tuner_coarse - 12 - last_com1_tuner_coarse;
658 if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
659 if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
661 last_com1_tuner_fine = com1_tuner_fine;
662 last_com1_tuner_coarse = com1_tuner_coarse;
664 fgSetFloat( "/radios/comm[0]/frequencies/standby-mhz",
665 coarse_freq + fine_freq / 40.0 );
668 int com2_tuner_fine = ((radio_switch_data[13] >> 4) & 0x0f) - 1;
669 int com2_tuner_coarse = (radio_switch_data[13] & 0x0f) - 1;
670 static int last_com2_tuner_fine = com2_tuner_fine;
671 static int last_com2_tuner_coarse = com2_tuner_coarse;
673 freq = com2_stby_freq->getFloatValue();
674 coarse_freq = (int)freq;
675 fine_freq = (int)((freq - coarse_freq) * 40 + 0.5);
677 if ( com2_tuner_fine != last_com2_tuner_fine ) {
678 diff = com2_tuner_fine - last_com2_tuner_fine;
679 if ( abs(diff) > 4 ) {
681 if ( com2_tuner_fine < last_com2_tuner_fine ) {
683 diff = 12 - last_com2_tuner_fine + com2_tuner_fine;
686 diff = com2_tuner_fine - 12 - last_com2_tuner_fine;
691 while ( fine_freq >= 40.0 ) { fine_freq -= 40.0; }
692 while ( fine_freq < 0.0 ) { fine_freq += 40.0; }
694 if ( com2_tuner_coarse != last_com2_tuner_coarse ) {
695 diff = com2_tuner_coarse - last_com2_tuner_coarse;
696 if ( abs(diff) > 4 ) {
698 if ( com2_tuner_coarse < last_com2_tuner_coarse ) {
700 diff = 12 - last_com2_tuner_coarse + com2_tuner_coarse;
703 diff = com2_tuner_coarse - 12 - last_com2_tuner_coarse;
708 if ( coarse_freq < 118.0 ) { coarse_freq += 19.0; }
709 if ( coarse_freq > 136.0 ) { coarse_freq -= 19.0; }
711 last_com2_tuner_fine = com2_tuner_fine;
712 last_com2_tuner_coarse = com2_tuner_coarse;
714 fgSetFloat( "/radios/comm[1]/frequencies/standby-mhz",
715 coarse_freq + fine_freq / 40.0 );
718 int nav1_tuner_fine = ((radio_switch_data[9] >> 4) & 0x0f) - 1;
719 int nav1_tuner_coarse = (radio_switch_data[9] & 0x0f) - 1;
720 static int last_nav1_tuner_fine = nav1_tuner_fine;
721 static int last_nav1_tuner_coarse = nav1_tuner_coarse;
723 freq = nav1_stby_freq->getFloatValue();
724 coarse_freq = (int)freq;
725 fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
727 if ( nav1_tuner_fine != last_nav1_tuner_fine ) {
728 diff = nav1_tuner_fine - last_nav1_tuner_fine;
729 if ( abs(diff) > 4 ) {
731 if ( nav1_tuner_fine < last_nav1_tuner_fine ) {
733 diff = 12 - last_nav1_tuner_fine + nav1_tuner_fine;
736 diff = nav1_tuner_fine - 12 - last_nav1_tuner_fine;
741 while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
742 while ( fine_freq < 0.0 ) { fine_freq += 20.0; }
744 if ( nav1_tuner_coarse != last_nav1_tuner_coarse ) {
745 diff = nav1_tuner_coarse - last_nav1_tuner_coarse;
746 if ( abs(diff) > 4 ) {
748 if ( nav1_tuner_coarse < last_nav1_tuner_coarse ) {
750 diff = 12 - last_nav1_tuner_coarse + nav1_tuner_coarse;
753 diff = nav1_tuner_coarse - 12 - last_nav1_tuner_coarse;
758 if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
759 if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
761 last_nav1_tuner_fine = nav1_tuner_fine;
762 last_nav1_tuner_coarse = nav1_tuner_coarse;
764 fgSetFloat( "/radios/nav[0]/frequencies/standby-mhz",
765 coarse_freq + fine_freq / 20.0 );
768 int nav2_tuner_fine = ((radio_switch_data[17] >> 4) & 0x0f) - 1;
769 int nav2_tuner_coarse = (radio_switch_data[17] & 0x0f) - 1;
770 static int last_nav2_tuner_fine = nav2_tuner_fine;
771 static int last_nav2_tuner_coarse = nav2_tuner_coarse;
773 freq = nav2_stby_freq->getFloatValue();
774 coarse_freq = (int)freq;
775 fine_freq = (int)((freq - coarse_freq) * 20 + 0.5);
777 if ( nav2_tuner_fine != last_nav2_tuner_fine ) {
778 diff = nav2_tuner_fine - last_nav2_tuner_fine;
779 if ( abs(diff) > 4 ) {
781 if ( nav2_tuner_fine < last_nav2_tuner_fine ) {
783 diff = 12 - last_nav2_tuner_fine + nav2_tuner_fine;
786 diff = nav2_tuner_fine - 12 - last_nav2_tuner_fine;
791 while ( fine_freq >= 20.0 ) { fine_freq -= 20.0; }
792 while ( fine_freq < 0.0 ) { fine_freq += 20.0; }
794 if ( nav2_tuner_coarse != last_nav2_tuner_coarse ) {
795 diff = nav2_tuner_coarse - last_nav2_tuner_coarse;
796 if ( abs(diff) > 4 ) {
798 if ( nav2_tuner_coarse < last_nav2_tuner_coarse ) {
800 diff = 12 - last_nav2_tuner_coarse + nav2_tuner_coarse;
803 diff = nav2_tuner_coarse - 12 - last_nav2_tuner_coarse;
808 if ( coarse_freq < 108.0 ) { coarse_freq += 10.0; }
809 if ( coarse_freq > 117.0 ) { coarse_freq -= 10.0; }
811 last_nav2_tuner_fine = nav2_tuner_fine;
812 last_nav2_tuner_coarse = nav2_tuner_coarse;
814 fgSetFloat( "/radios/nav[1]/frequencies/standby-mhz",
815 coarse_freq + fine_freq / 20.0);
818 int adf_tuner_fine = ((radio_switch_data[21] >> 4) & 0x0f) - 1;
819 int adf_tuner_coarse = (radio_switch_data[21] & 0x0f) - 1;
820 static int last_adf_tuner_fine = adf_tuner_fine;
821 static int last_adf_tuner_coarse = adf_tuner_coarse;
823 // cout << "adf_stby_mode = " << adf_stby_mode->getIntValue() << endl;
824 if ( adf_count_mode->getIntValue() == 2 ) {
825 // tune count down timer
826 value = adf_elapsed_timer->getDoubleValue();
829 if ( adf_stby_mode->getIntValue() == 1 ) {
830 value = adf_freq->getFloatValue();
832 value = adf_stby_freq->getFloatValue();
836 if ( adf_tuner_fine != last_adf_tuner_fine ) {
837 diff = adf_tuner_fine - last_adf_tuner_fine;
838 if ( abs(diff) > 4 ) {
840 if ( adf_tuner_fine < last_adf_tuner_fine ) {
842 diff = 12 - last_adf_tuner_fine + adf_tuner_fine;
845 diff = adf_tuner_fine - 12 - last_adf_tuner_fine;
851 if ( adf_tuner_coarse != last_adf_tuner_coarse ) {
852 diff = adf_tuner_coarse - last_adf_tuner_coarse;
853 if ( abs(diff) > 4 ) {
855 if ( adf_tuner_coarse < last_adf_tuner_coarse ) {
857 diff = 12 - last_adf_tuner_coarse + adf_tuner_coarse;
860 diff = adf_tuner_coarse - 12 - last_adf_tuner_coarse;
863 if ( adf_count_mode->getIntValue() == 2 ) {
869 if ( adf_count_mode->getIntValue() == 2 ) {
870 if ( value < 0 ) { value += 3600; }
871 if ( value > 3599 ) { value -= 3600; }
873 if ( value < 200 ) { value += 1600; }
874 if ( value > 1799 ) { value -= 1600; }
877 last_adf_tuner_fine = adf_tuner_fine;
878 last_adf_tuner_coarse = adf_tuner_coarse;
880 if ( adf_count_mode->getIntValue() == 2 ) {
881 fgSetFloat( "/radios/kr-87/outputs/elapsed-timer", value );
883 if ( adf_stby_mode->getIntValue() == 1 ) {
884 fgSetFloat( "/radios/kr-87/outputs/selected-khz", value );
886 fgSetFloat( "/radios/kr-87/outputs/standby-khz", value );
891 fgSetInt( "/radios/kr-87/inputs/adf-btn",
892 !(radio_switch_data[23] & 0x01) );
893 fgSetInt( "/radios/kr-87/inputs/bfo-btn",
894 !(radio_switch_data[23] >> 1 & 0x01) );
895 fgSetInt( "/radios/kr-87/inputs/frq-btn",
896 (radio_switch_data[23] >> 2 & 0x01) );
897 fgSetInt( "/radios/kr-87/inputs/flt-et-btn",
898 !(radio_switch_data[23] >> 3 & 0x01) );
899 fgSetInt( "/radios/kr-87/inputs/set-rst-btn",
900 !(radio_switch_data[23] >> 4 & 0x01) );
901 /* cout << "adf = " << !(radio_switch_data[23] & 0x01)
902 << " bfo = " << !(radio_switch_data[23] >> 1 & 0x01)
903 << " stby = " << !(radio_switch_data[23] >> 2 & 0x01)
904 << " timer = " << !(radio_switch_data[23] >> 3 & 0x01)
905 << " set/rst = " << !(radio_switch_data[23] >> 4 & 0x01)
912 digit_tuner[0] = radio_switch_data[25] & 0x0f;
913 digit_tuner[1] = ( radio_switch_data[25] >> 4 ) & 0x0f;
914 digit_tuner[2] = radio_switch_data[29] & 0x0f;
915 digit_tuner[3] = ( radio_switch_data[29] >> 4 ) & 0x0f;
916 static bool first_time = true;
917 static int last_digit_tuner[4];
920 for ( i = 0; i < 4; ++i ) {
921 last_digit_tuner[i] = digit_tuner[i];
925 int id_code = xpdr_id_code->getIntValue();
928 for ( i = 0; i < 4; ++i ) {
929 digit[i] = id_code / place;
930 id_code -= digit[i] * place;
934 for ( i = 0; i < 4; ++i ) {
935 if ( digit_tuner[i] != last_digit_tuner[i] ) {
936 diff = digit_tuner[i] - last_digit_tuner[i];
937 if ( abs(diff) > 4 ) {
939 if ( digit_tuner[i] < last_digit_tuner[i] ) {
941 diff = 15 - last_digit_tuner[i] + digit_tuner[i];
944 diff = digit_tuner[i] - 15 - last_digit_tuner[i];
949 while ( digit[i] >= 8 ) { digit[i] -= 8; }
950 while ( digit[i] < 0 ) { digit[i] += 8; }
951 last_digit_tuner[i] = digit_tuner[i];
954 fgSetInt( "/radios/kt-70/inputs/digit1", digit[0] );
955 fgSetInt( "/radios/kt-70/inputs/digit2", digit[1] );
956 fgSetInt( "/radios/kt-70/inputs/digit3", digit[2] );
957 fgSetInt( "/radios/kt-70/inputs/digit4", digit[3] );
960 for ( i = 0; i < 5; ++i ) {
961 if ( radio_switch_data[27] >> i & 0x01 ) {
965 fgSetInt( "/radios/kt-70/inputs/func-knob", tmp );
966 fgSetInt( "/radios/kt-70/inputs/ident-btn",
967 !(radio_switch_data[27] >> 5 & 0x01) );
973 /////////////////////////////////////////////////////////////////////
974 // Update the radio display
975 /////////////////////////////////////////////////////////////////////
977 bool FGATC610x::do_radio_display() {
982 if ( dme_switch != 0 ) {
984 float minutes = dme_min->getFloatValue();
985 if ( minutes > 999 ) {
988 snprintf(digits, 7, "%03.0f", minutes);
989 for ( i = 0; i < 6; ++i ) {
992 radio_display_data[0] = digits[1] << 4 | digits[2];
993 radio_display_data[1] = 0xf0 | digits[0];
996 float knots = dme_kt->getFloatValue();
1000 snprintf(digits, 7, "%03.0f", knots);
1001 for ( i = 0; i < 6; ++i ) {
1004 radio_display_data[2] = digits[1] << 4 | digits[2];
1005 radio_display_data[3] = 0xf0 | digits[0];
1007 // DME distance (nm)
1008 float nm = dme_nm->getFloatValue();
1012 snprintf(digits, 7, "%04.1f", nm);
1013 for ( i = 0; i < 6; ++i ) {
1016 radio_display_data[4] = digits[1] << 4 | digits[3];
1017 radio_display_data[5] = 0x00 | digits[0];
1018 // the 0x00 in the upper nibble of the 6th byte of each
1019 // display turns on the decimal point
1021 // blank dem display
1022 for ( i = 0; i < 6; ++i ) {
1023 radio_display_data[i] = 0xff;
1027 // Com1 standby frequency
1028 float com1_stby = com1_stby_freq->getFloatValue();
1029 if ( fabs(com1_stby) > 999.99 ) {
1032 snprintf(digits, 7, "%06.3f", com1_stby);
1033 for ( i = 0; i < 6; ++i ) {
1036 radio_display_data[6] = digits[4] << 4 | digits[5];
1037 radio_display_data[7] = digits[1] << 4 | digits[2];
1038 radio_display_data[8] = 0xf0 | digits[0];
1040 // Com1 in use frequency
1041 float com1 = com1_freq->getFloatValue();
1042 if ( fabs(com1) > 999.99 ) {
1045 snprintf(digits, 7, "%06.3f", com1);
1046 for ( i = 0; i < 6; ++i ) {
1049 radio_display_data[9] = digits[4] << 4 | digits[5];
1050 radio_display_data[10] = digits[1] << 4 | digits[2];
1051 radio_display_data[11] = 0x00 | digits[0];
1052 // the 0x00 in the upper nibble of the 6th byte of each display
1053 // turns on the decimal point
1055 // Com2 standby frequency
1056 float com2_stby = com2_stby_freq->getFloatValue();
1057 if ( fabs(com2_stby) > 999.99 ) {
1060 snprintf(digits, 7, "%06.3f", com2_stby);
1061 for ( i = 0; i < 6; ++i ) {
1064 radio_display_data[18] = digits[4] << 4 | digits[5];
1065 radio_display_data[19] = digits[1] << 4 | digits[2];
1066 radio_display_data[20] = 0xf0 | digits[0];
1068 // Com2 in use frequency
1069 float com2 = com2_freq->getFloatValue();
1070 if ( fabs(com2) > 999.99 ) {
1073 snprintf(digits, 7, "%06.3f", com2);
1074 for ( i = 0; i < 6; ++i ) {
1077 radio_display_data[21] = digits[4] << 4 | digits[5];
1078 radio_display_data[22] = digits[1] << 4 | digits[2];
1079 radio_display_data[23] = 0x00 | digits[0];
1080 // the 0x00 in the upper nibble of the 6th byte of each display
1081 // turns on the decimal point
1083 // Nav1 standby frequency
1084 float nav1_stby = nav1_stby_freq->getFloatValue();
1085 if ( fabs(nav1_stby) > 999.99 ) {
1088 snprintf(digits, 7, "%06.2f", nav1_stby);
1089 for ( i = 0; i < 6; ++i ) {
1092 radio_display_data[12] = digits[4] << 4 | digits[5];
1093 radio_display_data[13] = digits[1] << 4 | digits[2];
1094 radio_display_data[14] = 0xf0 | digits[0];
1096 // Nav1 in use frequency
1097 float nav1 = nav1_freq->getFloatValue();
1098 if ( fabs(nav1) > 999.99 ) {
1101 snprintf(digits, 7, "%06.2f", nav1);
1102 for ( i = 0; i < 6; ++i ) {
1105 radio_display_data[15] = digits[4] << 4 | digits[5];
1106 radio_display_data[16] = digits[1] << 4 | digits[2];
1107 radio_display_data[17] = 0x00 | digits[0];
1108 // the 0x00 in the upper nibble of the 6th byte of each display
1109 // turns on the decimal point
1111 // Nav2 standby frequency
1112 float nav2_stby = nav2_stby_freq->getFloatValue();
1113 if ( fabs(nav2_stby) > 999.99 ) {
1116 snprintf(digits, 7, "%06.2f", nav2_stby);
1117 for ( i = 0; i < 6; ++i ) {
1120 radio_display_data[24] = digits[4] << 4 | digits[5];
1121 radio_display_data[25] = digits[1] << 4 | digits[2];
1122 radio_display_data[26] = 0xf0 | digits[0];
1124 // Nav2 in use frequency
1125 float nav2 = nav2_freq->getFloatValue();
1126 if ( fabs(nav2) > 999.99 ) {
1129 snprintf(digits, 7, "%06.2f", nav2);
1130 for ( i = 0; i < 6; ++i ) {
1133 radio_display_data[27] = digits[4] << 4 | digits[5];
1134 radio_display_data[28] = digits[1] << 4 | digits[2];
1135 radio_display_data[29] = 0x00 | digits[0];
1136 // the 0x00 in the upper nibble of the 6th byte of each display
1137 // turns on the decimal point
1139 // ADF standby frequency / timer
1140 if ( adf_on_off_vol->getDoubleValue() >= 0.01 ) {
1141 if ( adf_stby_mode->getIntValue() == 0 ) {
1143 float adf_stby = adf_stby_freq->getFloatValue();
1144 if ( fabs(adf_stby) > 1799 ) {
1147 snprintf(digits, 7, "%04.0f", adf_stby);
1148 for ( i = 0; i < 6; ++i ) {
1151 radio_display_data[30] = digits[3] << 4 | 0x0f;
1152 radio_display_data[31] = digits[1] << 4 | digits[2];
1153 if ( digits[0] == 0 ) {
1154 radio_display_data[32] = 0xff;
1156 radio_display_data[32] = 0xf0 | digits[0];
1161 int hours, min, sec;
1162 if ( adf_timer_mode->getIntValue() == 0 ) {
1163 time = adf_flight_timer->getDoubleValue();
1165 time = adf_elapsed_timer->getDoubleValue();
1167 // cout << time << endl;
1168 hours = (int)(time / 3600.0);
1169 time -= hours * 3600.00;
1170 min = (int)(time / 60.0);
1187 // cout << big << ":" << little << endl;
1188 snprintf(digits, 7, "%02d%02d", big, little);
1189 for ( i = 0; i < 6; ++i ) {
1192 radio_display_data[30] = digits[3] << 4 | 0x0f;
1193 radio_display_data[31] = digits[1] << 4 | digits[2];
1194 radio_display_data[32] = 0xf0 | digits[0];
1197 // ADF in use frequency
1198 float adf = adf_freq->getFloatValue();
1199 if ( fabs(adf) > 1799 ) {
1202 snprintf(digits, 7, "%04.0f", adf);
1203 for ( i = 0; i < 6; ++i ) {
1206 radio_display_data[33] = digits[2] << 4 | digits[3];
1207 if ( digits[0] == 0 ) {
1208 radio_display_data[34] = 0xf0 | digits[1];
1210 radio_display_data[34] = digits[0] << 4 | digits[1];
1213 radio_display_data[30] = 0xff;
1214 radio_display_data[31] = 0xff;
1215 radio_display_data[32] = 0xff;
1216 radio_display_data[33] = 0xff;
1217 radio_display_data[34] = 0xff;
1220 // Transponder code and flight level
1221 if ( xpdr_func_knob->getIntValue() == 2 ) {
1223 radio_display_data[36] = 8 << 4 | 8;
1224 radio_display_data[37] = 8 << 4 | 8;
1225 radio_display_data[38] = 0xff;
1226 radio_display_data[39] = 8 << 4 | 0x0f;
1227 radio_display_data[40] = 8 << 4 | 8;
1228 } else if ( xpdr_func_knob->getIntValue() > 0 ) {
1230 int id_code = xpdr_id_code->getIntValue();
1232 for ( i = 0; i < 4; ++i ) {
1233 digits[i] = id_code / place;
1234 id_code -= digits[i] * place;
1237 radio_display_data[36] = digits[2] << 4 | digits[3];
1238 radio_display_data[37] = digits[0] << 4 | digits[1];
1239 radio_display_data[38] = 0xff;
1241 if ( xpdr_func_knob->getIntValue() == 3 ||
1242 xpdr_func_knob->getIntValue() == 5 )
1244 // do flight level display
1245 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
1246 for ( i = 0; i < 6; ++i ) {
1249 radio_display_data[39] = digits[2] << 4 | 0x0f;
1250 radio_display_data[40] = digits[0] << 4 | digits[1];
1252 // blank flight level display
1253 radio_display_data[39] = 0xff;
1254 radio_display_data[40] = 0xff;
1258 radio_display_data[36] = 0xff;
1259 radio_display_data[37] = 0xff;
1260 radio_display_data[38] = 0xff;
1261 radio_display_data[39] = 0xff;
1262 radio_display_data[40] = 0xff;
1265 ATC610xSetRadios( radios_fd, radio_display_data );
1271 /////////////////////////////////////////////////////////////////////
1272 // Drive the stepper motors
1273 /////////////////////////////////////////////////////////////////////
1275 bool FGATC610x::do_steppers() {
1276 float diff = mag_compass->getFloatValue() - compass_position;
1277 while ( diff < -180.0 ) { diff += 360.0; }
1278 while ( diff > 180.0 ) { diff -= 360.0; }
1280 int steps = (int)(diff * 4);
1281 // cout << "steps = " << steps << endl;
1282 if ( steps > 4 ) { steps = 4; }
1283 if ( steps < -4 ) { steps = -4; }
1285 if ( abs(steps) > 0 ) {
1286 unsigned char cmd = 0x80; // stepper command
1288 cmd |= 0x20; // go up
1290 cmd |= 0x00; // go down
1294 // sync compass_position with hardware position
1295 compass_position += (float)steps / 4.0;
1297 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
1304 /////////////////////////////////////////////////////////////////////
1305 // Read the switch positions
1306 /////////////////////////////////////////////////////////////////////
1308 // decode the packed switch data
1309 static void update_switch_matrix(
1311 unsigned char switch_data[ATC_SWITCH_BYTES],
1312 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES] )
1314 for ( int row = 0; row < ATC_SWITCH_BYTES; ++row ) {
1315 unsigned char switches = switch_data[row];
1317 for( int column = 0; column < ATC_NUM_COLS; ++column ) {
1318 switch_matrix[board][column][row] = switches & 1;
1319 switches = switches >> 1;
1324 bool FGATC610x::do_switches() {
1325 ATC610xReadSwitches( switches_fd, switch_data );
1327 // unpack the switch data
1328 int switch_matrix[2][ATC_NUM_COLS][ATC_SWITCH_BYTES];
1329 update_switch_matrix( board, switch_data, switch_matrix );
1331 // magnetos and starter switch
1333 bool starter = false;
1334 if ( switch_matrix[board][3][1] == 1 ) {
1337 } else if ( switch_matrix[board][2][1] == 1 ) {
1340 } else if ( switch_matrix[board][1][1] == 1 ) {
1343 } else if ( switch_matrix[board][0][1] == 1 ) {
1351 // do a bit of filtering on the magneto/starter switch and the
1352 // flap lever because these are not well debounced in hardware
1353 static int mag1, mag2, mag3;
1357 if ( mag1 == mag2 && mag2 == mag3 ) {
1358 fgSetInt( "/controls/magnetos[0]", magnetos );
1360 static bool start1, start2, start3;
1364 if ( start1 == start2 && start2 == start3 ) {
1365 fgSetBool( "/controls/starter[0]", starter );
1370 if ( switch_matrix[board][6][3] ) {
1372 } else if ( switch_matrix[board][5][3] ) {
1374 } else if ( switch_matrix[board][4][3] ) {
1376 } else if ( !switch_matrix[board][4][3] ) {
1380 // do a bit of filtering on the magneto/starter switch and the
1381 // flap lever because these are not well debounced in hardware
1382 static float flap1, flap2, flap3;
1386 if ( flap1 == flap2 && flap2 == flap3 ) {
1387 fgSetFloat( "/controls/flaps", flaps );
1390 // fuel selector (also filtered)
1392 if ( switch_matrix[board][2][3] ) {
1395 } else if ( switch_matrix[board][1][3] ) {
1398 } else if ( switch_matrix[board][3][3] ) {
1406 static int fuel1, fuel2, fuel3;
1410 if ( fuel1 == fuel2 && fuel2 == fuel3 ) {
1411 fgSetBool( "/controls/fuel-selector[0]", (fuel & 0x01) > 0 );
1412 fgSetBool( "/controls/fuel-selector[1]", (fuel & 0x02) > 0 );
1419 bool FGATC610x::process() {
1420 // Lock the hardware, skip if it's not ready yet
1421 if ( ATC610xLock( lock_fd ) > 0 ) {
1425 do_radio_switches();
1430 ATC610xRelease( lock_fd );
1439 bool FGATC610x::close() {