1 // atc610x.cxx -- FGFS interface to ATC 610x hardware
3 // Written by Curtis Olson, started January 2002
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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/sg_path.hxx>
47 #include <simgear/props/props.hxx>
49 #include <Scripting/NasalSys.hxx>
50 #include <Main/fg_props.hxx>
51 #include <Main/globals.hxx>
53 #include "atc610x.hxx"
58 // Lock the ATC 610 hardware
59 static int ATC610xLock( int fd ) {
61 lseek( fd, 0, SEEK_SET );
64 int result = read( fd, tmp, 1 );
66 SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
73 // Write a radios command
74 static int ATC610xRelease( int fd ) {
76 lseek( fd, 0, SEEK_SET );
80 int result = write( fd, tmp, 1 );
83 SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
90 // Write a radios command
91 static int ATC610xSetRadios( int fd,
92 unsigned char data[ATC_RADIO_DISPLAY_BYTES] )
95 lseek( fd, 0, SEEK_SET );
97 int result = write( fd, data, ATC_RADIO_DISPLAY_BYTES );
99 if ( result != ATC_RADIO_DISPLAY_BYTES ) {
100 SG_LOG( SG_IO, SG_DEBUG, "Write failed" );
107 // Write a stepper command
108 static int ATC610xSetStepper( int fd, unsigned char channel,
109 unsigned char value )
112 lseek( fd, 0, SEEK_SET );
115 unsigned char buf[3];
119 int result = write( fd, buf, 2 );
121 SG_LOG( SG_IO, SG_INFO, "Write failed" );
123 SG_LOG( SG_IO, SG_DEBUG,
124 "Sent cmd = " << (int)channel << " value = " << (int)value );
129 // Read status of last stepper written to
130 static unsigned char ATC610xReadStepper( int fd ) {
134 lseek( fd, 0, SEEK_SET );
137 unsigned char buf[2];
138 result = read( fd, buf, 1 );
140 SG_LOG( SG_IO, SG_ALERT, "Read failed" );
143 SG_LOG( SG_IO, SG_DEBUG, "Read result = " << (int)buf[0] );
149 // Turn a lamp on or off
150 void ATC610xSetLamp( int fd, int channel, bool value ) {
151 // lamp channels 0-63 are written to LampPort0, channels 64-127
152 // are written to LampPort1
154 // bits 0-6 are the lamp address
155 // bit 7 is the value (on/off)
160 unsigned char buf[3];
164 result = write( fd, buf, 2 );
166 SG_LOG( SG_IO, SG_ALERT, "Write failed" );
172 void FGATC610x::init_config() {
173 #if defined( unix ) || defined( __CYGWIN__ )
174 // Next check home directory for .fgfsrc.hostname file
175 char *envp = ::getenv( "HOME" );
176 if ( envp != NULL ) {
177 SGPath atc610x_config( envp );
178 atc610x_config.append( ".fgfs-atc610x.xml" );
179 readProperties( atc610x_config.str(), globals->get_props() );
185 // Open and initialize ATC 610x hardware
186 bool FGATC610x::open() {
187 if ( is_enabled() ) {
188 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
189 << "is already in use, ignoring" );
193 SG_LOG( SG_IO, SG_ALERT,
194 "Initializing ATC 610x hardware, please wait ..." );
196 // This loads the config parameters generated by "simcal"
199 if ( input0_path.str().length() ) {
200 input0 = new FGATCInput( 0, input0_path );
203 if ( input1_path.str().length() ) {
204 input1 = new FGATCInput( 1, input1_path );
208 set_hz( 30 ); // default to processing requests @ 30Hz
211 board = 0; // 610x uses a single board number = 0
213 snprintf( lock_file, 256, "/proc/atc610x/board%d/lock", board );
214 snprintf( lamps_file, 256, "/proc/atc610x/board%d/lamps", board );
215 snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
216 snprintf( stepper_file, 256, "/proc/atc610x/board%d/steppers", board );
218 /////////////////////////////////////////////////////////////////////
219 // Open the /proc files
220 /////////////////////////////////////////////////////////////////////
222 lock_fd = ::open( lock_file, O_RDWR );
223 if ( lock_fd == -1 ) {
224 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
226 snprintf( msg, 256, "Error opening %s", lock_file );
231 lamps_fd = ::open( lamps_file, O_WRONLY );
232 if ( lamps_fd == -1 ) {
233 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
235 snprintf( msg, 256, "Error opening %s", lamps_file );
240 radios_fd = ::open( radios_file, O_RDWR );
241 if ( radios_fd == -1 ) {
242 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
244 snprintf( msg, 256, "Error opening %s", radios_file );
249 stepper_fd = ::open( stepper_file, O_RDWR );
250 if ( stepper_fd == -1 ) {
251 SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
253 snprintf( msg, 256, "Error opening %s", stepper_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 );
275 SG_LOG( SG_IO, SG_ALERT,
276 " - Waiting for compass to come home." );
279 int timeout = 900; // about 30 seconds
281 while ( ! home && timeout > 0 ) {
282 if ( timeout % 150 == 0 ) {
283 SG_LOG( SG_IO, SG_INFO, "waiting for compass = " << timeout );
285 SG_LOG( SG_IO, SG_DEBUG, "Checking if compass home ..." );
288 while ( ATC610xLock( lock_fd ) <= 0 );
290 unsigned char result = ATC610xReadStepper( stepper_fd );
295 ATC610xRelease( lock_fd );
297 #if defined( _MSC_VER )
298 ulMilliSecondSleep(33);
299 #elif defined (WIN32) && !defined(__CYGWIN__)
308 compass_position = 0.0;
310 /////////////////////////////////////////////////////////////////////
311 // Blank the radio display
312 /////////////////////////////////////////////////////////////////////
314 SG_LOG( SG_IO, SG_ALERT,
315 " - Clearing the radios displays." );
318 unsigned char value = 0xff;
319 for ( int channel = 0; channel < ATC_RADIO_DISPLAY_BYTES; ++channel ) {
320 radio_display_data[channel] = value;
323 // Lock the hardware, keep trying until we succeed
324 while ( ATC610xLock( lock_fd ) <= 0 );
327 ATC610xSetRadios( radios_fd, radio_display_data );
329 ATC610xRelease( lock_fd );
331 /////////////////////////////////////////////////////////////////////
333 /////////////////////////////////////////////////////////////////////
335 for ( int i = 0; i < 128; ++i ) {
336 ATC610xSetLamp( lamps_fd, i, false );
339 /////////////////////////////////////////////////////////////////////
340 // Finished initing hardware
341 /////////////////////////////////////////////////////////////////////
343 SG_LOG( SG_IO, SG_ALERT,
344 "Done initializing ATC 610x hardware." );
346 /////////////////////////////////////////////////////////////////////
347 // Connect up to property values
348 /////////////////////////////////////////////////////////////////////
351 = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg",
354 dme_min = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
355 dme_kt = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt",
357 dme_nm = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
358 dme_in_range = fgGetNode( "/instrumentation/dme/in-range", true );
360 adf_bus_power = fgGetNode( "/systems/electrical/outputs/adf", true );
361 dme_bus_power = fgGetNode( "/systems/electrical/outputs/dme", true );
362 navcom1_bus_power = fgGetNode( "/systems/electrical/outputs/nav[0]",
364 navcom2_bus_power = fgGetNode( "/systems/electrical/outputs/nav[1]",
366 xpdr_bus_power = fgGetNode( "/systems/electrical/outputs/transponder",
369 navcom1_power_btn = fgGetNode( "/instrumentation/comm[0]/inputs/power-btn", true );
370 navcom2_power_btn = fgGetNode( "/instrumentation/comm[1]/inputs/power-btn", true );
372 com1_freq = fgGetNode( "/instrumentation/comm[0]/frequencies/selected-mhz", true );
374 = fgGetNode( "/instrumentation/comm[0]/frequencies/standby-mhz", true );
376 com2_freq = fgGetNode( "/instrumentation/comm[1]/frequencies/selected-mhz", true );
378 = fgGetNode( "/instrumentation/comm[1]/frequencies/standby-mhz", true );
380 nav1_freq = fgGetNode( "/instrumentation/nav[0]/frequencies/selected-mhz", true );
382 = fgGetNode( "/instrumentation/nav[0]/frequencies/standby-mhz", true );
383 nav1_obs = fgGetNode( "/instrumentation/nav[0]/radials/selected-deg", true );
385 nav2_freq = fgGetNode( "/instrumentation/nav[1]/frequencies/selected-mhz", true );
387 = fgGetNode( "/instrumentation/nav[1]/frequencies/standby-mhz", true );
388 nav2_obs = fgGetNode( "/instrumentation/nav[1]/radials/selected-deg", true );
391 = fgGetNode( "/instrumentation/kr-87/inputs/power-btn", true );
392 adf_vol = fgGetNode( "/instrumentation/kr-87/inputs/volume", true );
393 adf_adf_btn = fgGetNode( "/instrumentation/kr-87/inputs/adf-btn", true );
394 adf_bfo_btn = fgGetNode( "/instrumentation/kr-87/inputs/bfo-btn", true );
395 adf_freq = fgGetNode( "/instrumentation/kr-87/outputs/selected-khz", true );
397 = fgGetNode( "/instrumentation/kr-87/outputs/standby-khz", true );
398 adf_stby_mode = fgGetNode( "/instrumentation/kr-87/modes/stby", true );
399 adf_timer_mode = fgGetNode( "/instrumentation/kr-87/modes/timer", true );
400 adf_count_mode = fgGetNode( "/instrumentation/kr-87/modes/count", true );
402 = fgGetNode( "/instrumentation/kr-87/outputs/flight-timer", true );
404 = fgGetNode( "/instrumentation/kr-87/outputs/elapsed-timer",
406 adf_ant_ann = fgGetNode( "/instrumentation/kr-87/annunciators/ant", true );
407 adf_adf_ann = fgGetNode( "/instrumentation/kr-87/annunciators/adf", true );
408 adf_bfo_ann = fgGetNode( "/instrumentation/kr-87/annunciators/bfo", true );
409 adf_frq_ann = fgGetNode( "/instrumentation/kr-87/annunciators/frq", true );
410 adf_flt_ann = fgGetNode( "/instrumentation/kr-87/annunciators/flt", true );
411 adf_et_ann = fgGetNode( "/instrumentation/kr-87/annunciators/et", true );
413 inner = fgGetNode( "/instrumentation/marker-beacon/inner", true );
414 middle = fgGetNode( "/instrumentation/marker-beacon/middle", true );
415 outer = fgGetNode( "/instrumentation/marker-beacon/outer", true );
418 = fgGetNode( "/instrumentation/kt-70/inputs/ident-btn", true );
419 xpdr_digit1 = fgGetNode( "/instrumentation/kt-70/inputs/digit1", true );
420 xpdr_digit2 = fgGetNode( "/instrumentation/kt-70/inputs/digit2", true );
421 xpdr_digit3 = fgGetNode( "/instrumentation/kt-70/inputs/digit3", true );
422 xpdr_digit4 = fgGetNode( "/instrumentation/kt-70/inputs/digit4", true );
424 = fgGetNode( "/instrumentation/kt-70/inputs/func-knob", true );
425 xpdr_id_code = fgGetNode( "/instrumentation/kt-70/outputs/id-code", true );
427 = fgGetNode( "/instrumentation/kt-70/outputs/flight-level", true );
428 xpdr_fl_ann = fgGetNode( "/instrumentation/kt-70/annunciators/fl", true );
429 xpdr_alt_ann = fgGetNode( "/instrumentation/kt-70/annunciators/alt", true );
430 xpdr_gnd_ann = fgGetNode( "/instrumentation/kt-70/annunciators/gnd", true );
431 xpdr_on_ann = fgGetNode( "/instrumentation/kt-70/annunciators/on", true );
432 xpdr_sby_ann = fgGetNode( "/instrumentation/kt-70/annunciators/sby", true );
434 = fgGetNode( "/instrumentation/kt-70/annunciators/reply", true );
437 = fgGetNode( "/instrumentation/attitude-indicator/horizon-offset-deg",
439 alt_press = fgGetNode( "/instrumentation/altimeter/setting-inhg", true );
440 adf_hdg = fgGetNode( "/instrumentation/kr-87/inputs/rotation-deg", true );
441 hdg_bug = fgGetNode( "/autopilot/settings/heading-bug-deg", true );
443 elevator_center = fgGetNode( "/input/atc610x/elevator/center", true );
444 elevator_min = fgGetNode( "/input/atc610x/elevator/min", true );
445 elevator_max = fgGetNode( "/input/atc610x/elevator/max", true );
447 ailerons_center = fgGetNode( "/input/atc610x/ailerons/center", true );
448 ailerons_min = fgGetNode( "/input/atc610x/ailerons/min", true );
449 ailerons_max = fgGetNode( "/input/atc610x/ailerons/max", true );
451 rudder_center = fgGetNode( "/input/atc610x/rudder/center", true );
452 rudder_min = fgGetNode( "/input/atc610x/rudder/min", true );
453 rudder_max = fgGetNode( "/input/atc610x/rudder/max", true );
455 brake_left_min = fgGetNode( "/input/atc610x/brake-left/min", true );
456 brake_left_max = fgGetNode( "/input/atc610x/brake-left/max", true );
458 brake_right_min = fgGetNode( "/input/atc610x/brake-right/min", true );
459 brake_right_max = fgGetNode( "/input/atc610x/brake-right/max", true );
461 throttle_min = fgGetNode( "/input/atc610x/throttle/min", true );
462 throttle_max = fgGetNode( "/input/atc610x/throttle/max", true );
464 mixture_min = fgGetNode( "/input/atc610x/mixture/min", true );
465 mixture_max = fgGetNode( "/input/atc610x/mixture/max", true );
467 trim_center = fgGetNode( "/input/atc610x/trim/center", true );
468 trim_min = fgGetNode( "/input/atc610x/trim/min", true );
469 trim_max = fgGetNode( "/input/atc610x/trim/max", true );
471 nav1vol_min = fgGetNode( "/input/atc610x/nav1vol/min", true );
472 nav1vol_max = fgGetNode( "/input/atc610x/nav1vol/max", true );
474 nav2vol_min = fgGetNode( "/input/atc610x/nav2vol/min", true );
475 nav2vol_max = fgGetNode( "/input/atc610x/nav2vol/max", true );
477 ignore_flight_controls
478 = fgGetNode( "/input/atc610x/ignore-flight-controls", true );
481 = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
483 = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
484 nav1_serviceable = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
485 nav2_serviceable = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
486 adf_serviceable = fgGetNode( "/instrumentation/adf/serviceable", true );
488 = fgGetNode( "/instrumentation/kt-70/inputs/serviceable", true );
489 dme_serviceable = fgGetNode( "/instrumentation/dme/serviceable", true );
492 = fgGetNode( "/input/atc-board/radio-switches/raw/dme-switch-position");
494 // default to having everything serviceable
495 comm1_serviceable->setBoolValue( true );
496 comm2_serviceable->setBoolValue( true );
497 nav1_serviceable->setBoolValue( true );
498 nav2_serviceable->setBoolValue( true );
499 adf_serviceable->setBoolValue( true );
500 xpdr_serviceable->setBoolValue( true );
501 dme_serviceable->setBoolValue( true );
507 /////////////////////////////////////////////////////////////////////
509 /////////////////////////////////////////////////////////////////////
511 bool FGATC610x::do_lights() {
514 ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
515 ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
516 ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
519 ATC610xSetLamp( lamps_fd, 11, adf_ant_ann->getBoolValue() ); // ANT
520 ATC610xSetLamp( lamps_fd, 12, adf_adf_ann->getBoolValue() ); // ADF
521 ATC610xSetLamp( lamps_fd, 13, adf_bfo_ann->getBoolValue() ); // BFO
522 ATC610xSetLamp( lamps_fd, 14, adf_frq_ann->getBoolValue() ); // FRQ
523 ATC610xSetLamp( lamps_fd, 15, adf_flt_ann->getBoolValue() ); // FLT
524 ATC610xSetLamp( lamps_fd, 16, adf_et_ann->getBoolValue() ); // ET
526 // Transponder annunciators
527 ATC610xSetLamp( lamps_fd, 17, xpdr_fl_ann->getBoolValue() ); // FL
528 ATC610xSetLamp( lamps_fd, 18, xpdr_alt_ann->getBoolValue() ); // ALT
529 ATC610xSetLamp( lamps_fd, 19, xpdr_gnd_ann->getBoolValue() ); // GND
530 ATC610xSetLamp( lamps_fd, 20, xpdr_on_ann->getBoolValue() ); // ON
531 ATC610xSetLamp( lamps_fd, 21, xpdr_sby_ann->getBoolValue() ); // SBY
532 ATC610xSetLamp( lamps_fd, 22, xpdr_reply_ann->getBoolValue() ); // R
538 /////////////////////////////////////////////////////////////////////
539 // Update the radio display
540 /////////////////////////////////////////////////////////////////////
542 bool FGATC610x::do_radio_display() {
547 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
548 if ( dme_in_range->getBoolValue() ) {
550 float minutes = dme_min->getFloatValue();
551 if ( minutes > 999 ) {
554 snprintf(digits, 7, "%03.0f", minutes);
555 for ( i = 0; i < 6; ++i ) {
558 radio_display_data[0] = digits[1] << 4 | digits[2];
559 radio_display_data[1] = 0xf0 | digits[0];
562 float knots = dme_kt->getFloatValue();
566 snprintf(digits, 7, "%03.0f", knots);
567 for ( i = 0; i < 6; ++i ) {
570 radio_display_data[2] = digits[1] << 4 | digits[2];
571 radio_display_data[3] = 0xf0 | digits[0];
574 float nm = dme_nm->getFloatValue();
578 snprintf(digits, 7, "%04.1f", nm);
579 for ( i = 0; i < 6; ++i ) {
582 radio_display_data[4] = digits[1] << 4 | digits[3];
583 radio_display_data[5] = 0x00 | digits[0];
584 // the 0x00 in the upper nibble of the 6th byte of each
585 // display turns on the decimal point
588 radio_display_data[0] = 0xbb;
589 radio_display_data[1] = 0xfb;
590 radio_display_data[2] = 0xbb;
591 radio_display_data[3] = 0xfb;
592 radio_display_data[4] = 0xbb;
593 radio_display_data[5] = 0x0b;
597 for ( i = 0; i < 6; ++i ) {
598 radio_display_data[i] = 0xff;
602 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
603 // Com1 standby frequency
604 float com1_stby = com1_stby_freq->getFloatValue();
605 if ( fabs(com1_stby) > 999.99 ) {
608 snprintf(digits, 7, "%06.3f", com1_stby);
609 for ( i = 0; i < 6; ++i ) {
612 radio_display_data[6] = digits[4] << 4 | digits[5];
613 radio_display_data[7] = digits[1] << 4 | digits[2];
614 radio_display_data[8] = 0xf0 | digits[0];
616 // Com1 in use frequency
617 float com1 = com1_freq->getFloatValue();
618 if ( fabs(com1) > 999.99 ) {
621 snprintf(digits, 7, "%06.3f", com1);
622 for ( i = 0; i < 6; ++i ) {
625 radio_display_data[9] = digits[4] << 4 | digits[5];
626 radio_display_data[10] = digits[1] << 4 | digits[2];
627 radio_display_data[11] = 0x00 | digits[0];
628 // the 0x00 in the upper nibble of the 6th byte of each display
629 // turns on the decimal point
631 radio_display_data[6] = 0xff;
632 radio_display_data[7] = 0xff;
633 radio_display_data[8] = 0xff;
634 radio_display_data[9] = 0xff;
635 radio_display_data[10] = 0xff;
636 radio_display_data[11] = 0xff;
639 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
640 // Com2 standby frequency
641 float com2_stby = com2_stby_freq->getFloatValue();
642 if ( fabs(com2_stby) > 999.99 ) {
645 snprintf(digits, 7, "%06.3f", com2_stby);
646 for ( i = 0; i < 6; ++i ) {
649 radio_display_data[18] = digits[4] << 4 | digits[5];
650 radio_display_data[19] = digits[1] << 4 | digits[2];
651 radio_display_data[20] = 0xf0 | digits[0];
653 // Com2 in use frequency
654 float com2 = com2_freq->getFloatValue();
655 if ( fabs(com2) > 999.99 ) {
658 snprintf(digits, 7, "%06.3f", com2);
659 for ( i = 0; i < 6; ++i ) {
662 radio_display_data[21] = digits[4] << 4 | digits[5];
663 radio_display_data[22] = digits[1] << 4 | digits[2];
664 radio_display_data[23] = 0x00 | digits[0];
665 // the 0x00 in the upper nibble of the 6th byte of each display
666 // turns on the decimal point
668 radio_display_data[18] = 0xff;
669 radio_display_data[19] = 0xff;
670 radio_display_data[20] = 0xff;
671 radio_display_data[21] = 0xff;
672 radio_display_data[22] = 0xff;
673 radio_display_data[23] = 0xff;
676 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
677 // Nav1 standby frequency
678 float nav1_stby = nav1_stby_freq->getFloatValue();
679 if ( fabs(nav1_stby) > 999.99 ) {
682 snprintf(digits, 7, "%06.2f", nav1_stby);
683 for ( i = 0; i < 6; ++i ) {
686 radio_display_data[12] = digits[4] << 4 | digits[5];
687 radio_display_data[13] = digits[1] << 4 | digits[2];
688 radio_display_data[14] = 0xf0 | digits[0];
690 // Nav1 in use frequency
691 float nav1 = nav1_freq->getFloatValue();
692 if ( fabs(nav1) > 999.99 ) {
695 snprintf(digits, 7, "%06.2f", nav1);
696 for ( i = 0; i < 6; ++i ) {
699 radio_display_data[15] = digits[4] << 4 | digits[5];
700 radio_display_data[16] = digits[1] << 4 | digits[2];
701 radio_display_data[17] = 0x00 | digits[0];
702 // the 0x00 in the upper nibble of the 6th byte of each display
703 // turns on the decimal point
705 radio_display_data[12] = 0xff;
706 radio_display_data[13] = 0xff;
707 radio_display_data[14] = 0xff;
708 radio_display_data[15] = 0xff;
709 radio_display_data[16] = 0xff;
710 radio_display_data[17] = 0xff;
713 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
714 // Nav2 standby frequency
715 float nav2_stby = nav2_stby_freq->getFloatValue();
716 if ( fabs(nav2_stby) > 999.99 ) {
719 snprintf(digits, 7, "%06.2f", nav2_stby);
720 for ( i = 0; i < 6; ++i ) {
723 radio_display_data[24] = digits[4] << 4 | digits[5];
724 radio_display_data[25] = digits[1] << 4 | digits[2];
725 radio_display_data[26] = 0xf0 | digits[0];
727 // Nav2 in use frequency
728 float nav2 = nav2_freq->getFloatValue();
729 if ( fabs(nav2) > 999.99 ) {
732 snprintf(digits, 7, "%06.2f", nav2);
733 for ( i = 0; i < 6; ++i ) {
736 radio_display_data[27] = digits[4] << 4 | digits[5];
737 radio_display_data[28] = digits[1] << 4 | digits[2];
738 radio_display_data[29] = 0x00 | digits[0];
739 // the 0x00 in the upper nibble of the 6th byte of each display
740 // turns on the decimal point
742 radio_display_data[24] = 0xff;
743 radio_display_data[25] = 0xff;
744 radio_display_data[26] = 0xff;
745 radio_display_data[27] = 0xff;
746 radio_display_data[28] = 0xff;
747 radio_display_data[29] = 0xff;
750 // ADF standby frequency / timer
751 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
752 if ( adf_stby_mode->getIntValue() == 0 ) {
754 float adf_stby = adf_stby_freq->getFloatValue();
755 if ( fabs(adf_stby) > 1799 ) {
758 snprintf(digits, 7, "%04.0f", adf_stby);
759 for ( i = 0; i < 6; ++i ) {
762 radio_display_data[30] = digits[3] << 4 | 0x0f;
763 radio_display_data[31] = digits[1] << 4 | digits[2];
764 if ( digits[0] == 0 ) {
765 radio_display_data[32] = 0xff;
767 radio_display_data[32] = 0xf0 | digits[0];
773 if ( adf_timer_mode->getIntValue() == 0 ) {
774 time = adf_flight_timer->getDoubleValue();
776 time = adf_elapsed_timer->getDoubleValue();
778 // cout << time << endl;
779 hours = (int)(time / 3600.0);
780 time -= hours * 3600.00;
781 min = (int)(time / 60.0);
798 // cout << big << ":" << little << endl;
799 snprintf(digits, 7, "%02d%02d", big, little);
800 for ( i = 0; i < 6; ++i ) {
803 radio_display_data[30] = digits[2] << 4 | digits[3];
804 radio_display_data[31] = digits[0] << 4 | digits[1];
805 radio_display_data[32] = 0xff;
808 // ADF in use frequency
809 float adf = adf_freq->getFloatValue();
810 if ( fabs(adf) > 1799 ) {
813 snprintf(digits, 7, "%04.0f", adf);
814 for ( i = 0; i < 6; ++i ) {
817 radio_display_data[33] = digits[2] << 4 | digits[3];
818 if ( digits[0] == 0 ) {
819 radio_display_data[34] = 0xf0 | digits[1];
821 radio_display_data[34] = digits[0] << 4 | digits[1];
823 if ( adf_stby_mode->getIntValue() == 0 ) {
824 radio_display_data[35] = 0xff;
826 radio_display_data[35] = 0x0f;
829 radio_display_data[30] = 0xff;
830 radio_display_data[31] = 0xff;
831 radio_display_data[32] = 0xff;
832 radio_display_data[33] = 0xff;
833 radio_display_data[34] = 0xff;
834 radio_display_data[35] = 0xff;
837 // Transponder code and flight level
838 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
839 if ( xpdr_func_knob->getIntValue() == 2 ) {
841 radio_display_data[36] = 8 << 4 | 8;
842 radio_display_data[37] = 8 << 4 | 8;
843 radio_display_data[38] = 0xff;
844 radio_display_data[39] = 8 << 4 | 0x0f;
845 radio_display_data[40] = 8 << 4 | 8;
848 int id_code = xpdr_id_code->getIntValue();
850 for ( i = 0; i < 4; ++i ) {
851 digits[i] = id_code / place;
852 id_code -= digits[i] * place;
855 radio_display_data[36] = digits[2] << 4 | digits[3];
856 radio_display_data[37] = digits[0] << 4 | digits[1];
857 radio_display_data[38] = 0xff;
859 if ( xpdr_func_knob->getIntValue() == 3 ||
860 xpdr_func_knob->getIntValue() == 5 )
862 // do flight level display
863 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
864 for ( i = 0; i < 6; ++i ) {
867 radio_display_data[39] = digits[2] << 4 | 0x0f;
868 radio_display_data[40] = digits[0] << 4 | digits[1];
870 // blank flight level display
871 radio_display_data[39] = 0xff;
872 radio_display_data[40] = 0xff;
877 radio_display_data[36] = 0xff;
878 radio_display_data[37] = 0xff;
879 radio_display_data[38] = 0xff;
880 radio_display_data[39] = 0xff;
881 radio_display_data[40] = 0xff;
884 ATC610xSetRadios( radios_fd, radio_display_data );
890 /////////////////////////////////////////////////////////////////////
891 // Drive the stepper motors
892 /////////////////////////////////////////////////////////////////////
894 bool FGATC610x::do_steppers() {
895 float diff = mag_compass->getFloatValue() - compass_position;
896 while ( diff < -180.0 ) { diff += 360.0; }
897 while ( diff > 180.0 ) { diff -= 360.0; }
899 int steps = (int)(diff * 4);
900 // cout << "steps = " << steps << endl;
901 if ( steps > 4 ) { steps = 4; }
902 if ( steps < -4 ) { steps = -4; }
904 if ( abs(steps) > 0 ) {
905 unsigned char cmd = 0x80; // stepper command
907 cmd |= 0x20; // go up
909 cmd |= 0x00; // go down
913 // sync compass_position with hardware position
914 compass_position += (float)steps / 4.0;
916 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
923 bool FGATC610x::process() {
925 // Lock the hardware, skip if it's not ready yet
926 if ( ATC610xLock( lock_fd ) > 0 ) {
928 // process the ATC inputs
929 if ( input0 != NULL ) {
932 if ( input1 != NULL ) {
936 // run our custom nasal script. This is a layer above the raw
937 // hardware inputs. It handles situations where there isn't a
938 // direct 1-1 linear mapping between ATC functionality and FG
939 // functionality, and handles situations where FG expects more
940 // functionality from the interface than the ATC hardware can
943 FGNasalSys *n = (FGNasalSys*)globals->get_subsystem("nasal");
944 bool result = n->parseAndRun( "atcsim.do_hardware()" );
946 SG_LOG( SG_GENERAL, SG_ALERT,
947 "do_atcflightsim_hardware() failed!" );
954 ATC610xRelease( lock_fd );
963 bool FGATC610x::close() {