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/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 /////////////////////////////////////////////////////////////////////
350 mag_compass = fgGetNode( "/instrumentation/magnetic-compass/indicated-heading-deg", true );
352 dme_min = fgGetNode( "/instrumentation/dme/indicated-time-min", true );
353 dme_kt = fgGetNode( "/instrumentation/dme/indicated-ground-speed-kt",
355 dme_nm = fgGetNode( "/instrumentation/dme/indicated-distance-nm", true );
356 dme_in_range = fgGetNode( "/instrumentation/dme/in-range", true );
358 adf_bus_power = fgGetNode( "/systems/electrical/outputs/adf", true );
359 dme_bus_power = fgGetNode( "/systems/electrical/outputs/dme", true );
360 navcom1_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[0]",
362 navcom2_bus_power = fgGetNode( "/systems/electrical/outputs/navcom[1]",
364 xpdr_bus_power = fgGetNode( "/systems/electrical/outputs/transponder",
367 navcom1_power_btn = fgGetNode( "/radios/comm[0]/inputs/power-btn", true );
368 navcom2_power_btn = fgGetNode( "/radios/comm[1]/inputs/power-btn", true );
370 com1_freq = fgGetNode( "/radios/comm[0]/frequencies/selected-mhz", true );
372 = fgGetNode( "/radios/comm[0]/frequencies/standby-mhz", true );
374 com2_freq = fgGetNode( "/radios/comm[1]/frequencies/selected-mhz", true );
376 = fgGetNode( "/radios/comm[1]/frequencies/standby-mhz", true );
378 nav1_freq = fgGetNode( "/radios/nav[0]/frequencies/selected-mhz", true );
380 = fgGetNode( "/radios/nav[0]/frequencies/standby-mhz", true );
381 nav1_obs = fgGetNode( "/radios/nav[0]/radials/selected-deg", true );
383 nav2_freq = fgGetNode( "/radios/nav[1]/frequencies/selected-mhz", true );
385 = fgGetNode( "/radios/nav[1]/frequencies/standby-mhz", true );
386 nav2_obs = fgGetNode( "/radios/nav[1]/radials/selected-deg", true );
388 adf_power_btn = fgGetNode( "/radios/kr-87/inputs/power-btn", true );
389 adf_vol = fgGetNode( "/radios/kr-87/inputs/volume", true );
390 adf_adf_btn = fgGetNode( "/radios/kr-87/inputs/adf-btn", true );
391 adf_bfo_btn = fgGetNode( "/radios/kr-87/inputs/bfo-btn", true );
392 adf_freq = fgGetNode( "/radios/kr-87/outputs/selected-khz", true );
393 adf_stby_freq = fgGetNode( "/radios/kr-87/outputs/standby-khz", true );
394 adf_stby_mode = fgGetNode( "/radios/kr-87/modes/stby", true );
395 adf_timer_mode = fgGetNode( "/radios/kr-87/modes/timer", true );
396 adf_count_mode = fgGetNode( "/radios/kr-87/modes/count", true );
397 adf_flight_timer = fgGetNode( "/radios/kr-87/outputs/flight-timer", true );
398 adf_elapsed_timer = fgGetNode( "/radios/kr-87/outputs/elapsed-timer",
400 adf_ant_ann = fgGetNode( "/radios/kr-87/annunciators/ant", true );
401 adf_adf_ann = fgGetNode( "/radios/kr-87/annunciators/adf", true );
402 adf_bfo_ann = fgGetNode( "/radios/kr-87/annunciators/bfo", true );
403 adf_frq_ann = fgGetNode( "/radios/kr-87/annunciators/frq", true );
404 adf_flt_ann = fgGetNode( "/radios/kr-87/annunciators/flt", true );
405 adf_et_ann = fgGetNode( "/radios/kr-87/annunciators/et", true );
407 inner = fgGetNode( "/radios/marker-beacon/inner", true );
408 middle = fgGetNode( "/radios/marker-beacon/middle", true );
409 outer = fgGetNode( "/radios/marker-beacon/outer", true );
411 xpdr_ident_btn = fgGetNode( "/radios/kt-70/inputs/ident-btn", true );
412 xpdr_digit1 = fgGetNode( "/radios/kt-70/inputs/digit1", true );
413 xpdr_digit2 = fgGetNode( "/radios/kt-70/inputs/digit2", true );
414 xpdr_digit3 = fgGetNode( "/radios/kt-70/inputs/digit3", true );
415 xpdr_digit4 = fgGetNode( "/radios/kt-70/inputs/digit4", true );
416 xpdr_func_knob = fgGetNode( "/radios/kt-70/inputs/func-knob", true );
417 xpdr_id_code = fgGetNode( "/radios/kt-70/outputs/id-code", true );
418 xpdr_flight_level = fgGetNode( "/radios/kt-70/outputs/flight-level", true );
419 xpdr_fl_ann = fgGetNode( "/radios/kt-70/annunciators/fl", true );
420 xpdr_alt_ann = fgGetNode( "/radios/kt-70/annunciators/alt", true );
421 xpdr_gnd_ann = fgGetNode( "/radios/kt-70/annunciators/gnd", true );
422 xpdr_on_ann = fgGetNode( "/radios/kt-70/annunciators/on", true );
423 xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
424 xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
427 = fgGetNode( "/instrumentation/attitude-indicator/horizon-offset-deg",
429 alt_press = fgGetNode( "/instrumentation/altimeter/setting-inhg", true );
430 adf_hdg = fgGetNode( "/radios/kr-87/inputs/rotation-deg", true );
431 hdg_bug = fgGetNode( "/autopilot/settings/heading-bug-deg", true );
433 elevator_center = fgGetNode( "/input/atc610x/elevator/center", true );
434 elevator_min = fgGetNode( "/input/atc610x/elevator/min", true );
435 elevator_max = fgGetNode( "/input/atc610x/elevator/max", true );
437 ailerons_center = fgGetNode( "/input/atc610x/ailerons/center", true );
438 ailerons_min = fgGetNode( "/input/atc610x/ailerons/min", true );
439 ailerons_max = fgGetNode( "/input/atc610x/ailerons/max", true );
441 rudder_center = fgGetNode( "/input/atc610x/rudder/center", true );
442 rudder_min = fgGetNode( "/input/atc610x/rudder/min", true );
443 rudder_max = fgGetNode( "/input/atc610x/rudder/max", true );
445 brake_left_min = fgGetNode( "/input/atc610x/brake-left/min", true );
446 brake_left_max = fgGetNode( "/input/atc610x/brake-left/max", true );
448 brake_right_min = fgGetNode( "/input/atc610x/brake-right/min", true );
449 brake_right_max = fgGetNode( "/input/atc610x/brake-right/max", true );
451 throttle_min = fgGetNode( "/input/atc610x/throttle/min", true );
452 throttle_max = fgGetNode( "/input/atc610x/throttle/max", true );
454 mixture_min = fgGetNode( "/input/atc610x/mixture/min", true );
455 mixture_max = fgGetNode( "/input/atc610x/mixture/max", true );
457 trim_center = fgGetNode( "/input/atc610x/trim/center", true );
458 trim_min = fgGetNode( "/input/atc610x/trim/min", true );
459 trim_max = fgGetNode( "/input/atc610x/trim/max", true );
461 nav1vol_min = fgGetNode( "/input/atc610x/nav1vol/min", true );
462 nav1vol_max = fgGetNode( "/input/atc610x/nav1vol/max", true );
464 nav2vol_min = fgGetNode( "/input/atc610x/nav2vol/min", true );
465 nav2vol_max = fgGetNode( "/input/atc610x/nav2vol/max", true );
467 ignore_flight_controls
468 = fgGetNode( "/input/atc610x/ignore-flight-controls", true );
470 comm1_serviceable = fgGetNode( "/instrumentation/comm[0]/serviceable", true );
471 comm2_serviceable = fgGetNode( "/instrumentation/comm[1]/serviceable", true );
472 nav1_serviceable = fgGetNode( "/instrumentation/nav[0]/serviceable", true );
473 nav2_serviceable = fgGetNode( "/instrumentation/nav[1]/serviceable", true );
474 adf_serviceable = fgGetNode( "/instrumentation/adf/serviceable", true );
475 xpdr_serviceable = fgGetNode( "/radios/kt-70/inputs/serviceable",
477 dme_serviceable = fgGetNode( "/instrumentation/dme/serviceable", true );
480 = fgGetNode( "/input/atc-board/radio-switches/raw/dme-switch-position");
482 // default to having everything serviceable
483 comm1_serviceable->setBoolValue( true );
484 comm2_serviceable->setBoolValue( true );
485 nav1_serviceable->setBoolValue( true );
486 nav2_serviceable->setBoolValue( true );
487 adf_serviceable->setBoolValue( true );
488 xpdr_serviceable->setBoolValue( true );
489 dme_serviceable->setBoolValue( true );
495 /////////////////////////////////////////////////////////////////////
497 /////////////////////////////////////////////////////////////////////
499 bool FGATC610x::do_lights() {
502 ATC610xSetLamp( lamps_fd, 4, inner->getBoolValue() );
503 ATC610xSetLamp( lamps_fd, 5, middle->getBoolValue() );
504 ATC610xSetLamp( lamps_fd, 3, outer->getBoolValue() );
507 ATC610xSetLamp( lamps_fd, 11, adf_ant_ann->getBoolValue() ); // ANT
508 ATC610xSetLamp( lamps_fd, 12, adf_adf_ann->getBoolValue() ); // ADF
509 ATC610xSetLamp( lamps_fd, 13, adf_bfo_ann->getBoolValue() ); // BFO
510 ATC610xSetLamp( lamps_fd, 14, adf_frq_ann->getBoolValue() ); // FRQ
511 ATC610xSetLamp( lamps_fd, 15, adf_flt_ann->getBoolValue() ); // FLT
512 ATC610xSetLamp( lamps_fd, 16, adf_et_ann->getBoolValue() ); // ET
514 // Transponder annunciators
515 ATC610xSetLamp( lamps_fd, 17, xpdr_fl_ann->getBoolValue() ); // FL
516 ATC610xSetLamp( lamps_fd, 18, xpdr_alt_ann->getBoolValue() ); // ALT
517 ATC610xSetLamp( lamps_fd, 19, xpdr_gnd_ann->getBoolValue() ); // GND
518 ATC610xSetLamp( lamps_fd, 20, xpdr_on_ann->getBoolValue() ); // ON
519 ATC610xSetLamp( lamps_fd, 21, xpdr_sby_ann->getBoolValue() ); // SBY
520 ATC610xSetLamp( lamps_fd, 22, xpdr_reply_ann->getBoolValue() ); // R
526 /////////////////////////////////////////////////////////////////////
527 // Update the radio display
528 /////////////////////////////////////////////////////////////////////
530 bool FGATC610x::do_radio_display() {
535 if ( dme_has_power() && dme_serviceable->getBoolValue() ) {
536 if ( dme_in_range->getBoolValue() ) {
538 float minutes = dme_min->getFloatValue();
539 if ( minutes > 999 ) {
542 snprintf(digits, 7, "%03.0f", minutes);
543 for ( i = 0; i < 6; ++i ) {
546 radio_display_data[0] = digits[1] << 4 | digits[2];
547 radio_display_data[1] = 0xf0 | digits[0];
550 float knots = dme_kt->getFloatValue();
554 snprintf(digits, 7, "%03.0f", knots);
555 for ( i = 0; i < 6; ++i ) {
558 radio_display_data[2] = digits[1] << 4 | digits[2];
559 radio_display_data[3] = 0xf0 | digits[0];
562 float nm = dme_nm->getFloatValue();
566 snprintf(digits, 7, "%04.1f", nm);
567 for ( i = 0; i < 6; ++i ) {
570 radio_display_data[4] = digits[1] << 4 | digits[3];
571 radio_display_data[5] = 0x00 | digits[0];
572 // the 0x00 in the upper nibble of the 6th byte of each
573 // display turns on the decimal point
576 radio_display_data[0] = 0xbb;
577 radio_display_data[1] = 0xfb;
578 radio_display_data[2] = 0xbb;
579 radio_display_data[3] = 0xfb;
580 radio_display_data[4] = 0xbb;
581 radio_display_data[5] = 0x0b;
585 for ( i = 0; i < 6; ++i ) {
586 radio_display_data[i] = 0xff;
590 if ( navcom1_has_power() && comm1_serviceable->getBoolValue() ) {
591 // Com1 standby frequency
592 float com1_stby = com1_stby_freq->getFloatValue();
593 if ( fabs(com1_stby) > 999.99 ) {
596 snprintf(digits, 7, "%06.3f", com1_stby);
597 for ( i = 0; i < 6; ++i ) {
600 radio_display_data[6] = digits[4] << 4 | digits[5];
601 radio_display_data[7] = digits[1] << 4 | digits[2];
602 radio_display_data[8] = 0xf0 | digits[0];
604 // Com1 in use frequency
605 float com1 = com1_freq->getFloatValue();
606 if ( fabs(com1) > 999.99 ) {
609 snprintf(digits, 7, "%06.3f", com1);
610 for ( i = 0; i < 6; ++i ) {
613 radio_display_data[9] = digits[4] << 4 | digits[5];
614 radio_display_data[10] = digits[1] << 4 | digits[2];
615 radio_display_data[11] = 0x00 | digits[0];
616 // the 0x00 in the upper nibble of the 6th byte of each display
617 // turns on the decimal point
619 radio_display_data[6] = 0xff;
620 radio_display_data[7] = 0xff;
621 radio_display_data[8] = 0xff;
622 radio_display_data[9] = 0xff;
623 radio_display_data[10] = 0xff;
624 radio_display_data[11] = 0xff;
627 if ( navcom2_has_power() && comm2_serviceable->getBoolValue() ) {
628 // Com2 standby frequency
629 float com2_stby = com2_stby_freq->getFloatValue();
630 if ( fabs(com2_stby) > 999.99 ) {
633 snprintf(digits, 7, "%06.3f", com2_stby);
634 for ( i = 0; i < 6; ++i ) {
637 radio_display_data[18] = digits[4] << 4 | digits[5];
638 radio_display_data[19] = digits[1] << 4 | digits[2];
639 radio_display_data[20] = 0xf0 | digits[0];
641 // Com2 in use frequency
642 float com2 = com2_freq->getFloatValue();
643 if ( fabs(com2) > 999.99 ) {
646 snprintf(digits, 7, "%06.3f", com2);
647 for ( i = 0; i < 6; ++i ) {
650 radio_display_data[21] = digits[4] << 4 | digits[5];
651 radio_display_data[22] = digits[1] << 4 | digits[2];
652 radio_display_data[23] = 0x00 | digits[0];
653 // the 0x00 in the upper nibble of the 6th byte of each display
654 // turns on the decimal point
656 radio_display_data[18] = 0xff;
657 radio_display_data[19] = 0xff;
658 radio_display_data[20] = 0xff;
659 radio_display_data[21] = 0xff;
660 radio_display_data[22] = 0xff;
661 radio_display_data[23] = 0xff;
664 if ( navcom1_has_power() && nav1_serviceable->getBoolValue() ) {
665 // Nav1 standby frequency
666 float nav1_stby = nav1_stby_freq->getFloatValue();
667 if ( fabs(nav1_stby) > 999.99 ) {
670 snprintf(digits, 7, "%06.2f", nav1_stby);
671 for ( i = 0; i < 6; ++i ) {
674 radio_display_data[12] = digits[4] << 4 | digits[5];
675 radio_display_data[13] = digits[1] << 4 | digits[2];
676 radio_display_data[14] = 0xf0 | digits[0];
678 // Nav1 in use frequency
679 float nav1 = nav1_freq->getFloatValue();
680 if ( fabs(nav1) > 999.99 ) {
683 snprintf(digits, 7, "%06.2f", nav1);
684 for ( i = 0; i < 6; ++i ) {
687 radio_display_data[15] = digits[4] << 4 | digits[5];
688 radio_display_data[16] = digits[1] << 4 | digits[2];
689 radio_display_data[17] = 0x00 | digits[0];
690 // the 0x00 in the upper nibble of the 6th byte of each display
691 // turns on the decimal point
693 radio_display_data[12] = 0xff;
694 radio_display_data[13] = 0xff;
695 radio_display_data[14] = 0xff;
696 radio_display_data[15] = 0xff;
697 radio_display_data[16] = 0xff;
698 radio_display_data[17] = 0xff;
701 if ( navcom2_has_power() && nav2_serviceable->getBoolValue() ) {
702 // Nav2 standby frequency
703 float nav2_stby = nav2_stby_freq->getFloatValue();
704 if ( fabs(nav2_stby) > 999.99 ) {
707 snprintf(digits, 7, "%06.2f", nav2_stby);
708 for ( i = 0; i < 6; ++i ) {
711 radio_display_data[24] = digits[4] << 4 | digits[5];
712 radio_display_data[25] = digits[1] << 4 | digits[2];
713 radio_display_data[26] = 0xf0 | digits[0];
715 // Nav2 in use frequency
716 float nav2 = nav2_freq->getFloatValue();
717 if ( fabs(nav2) > 999.99 ) {
720 snprintf(digits, 7, "%06.2f", nav2);
721 for ( i = 0; i < 6; ++i ) {
724 radio_display_data[27] = digits[4] << 4 | digits[5];
725 radio_display_data[28] = digits[1] << 4 | digits[2];
726 radio_display_data[29] = 0x00 | digits[0];
727 // the 0x00 in the upper nibble of the 6th byte of each display
728 // turns on the decimal point
730 radio_display_data[24] = 0xff;
731 radio_display_data[25] = 0xff;
732 radio_display_data[26] = 0xff;
733 radio_display_data[27] = 0xff;
734 radio_display_data[28] = 0xff;
735 radio_display_data[29] = 0xff;
738 // ADF standby frequency / timer
739 if ( adf_has_power() && adf_serviceable->getBoolValue() ) {
740 if ( adf_stby_mode->getIntValue() == 0 ) {
742 float adf_stby = adf_stby_freq->getFloatValue();
743 if ( fabs(adf_stby) > 1799 ) {
746 snprintf(digits, 7, "%04.0f", adf_stby);
747 for ( i = 0; i < 6; ++i ) {
750 radio_display_data[30] = digits[3] << 4 | 0x0f;
751 radio_display_data[31] = digits[1] << 4 | digits[2];
752 if ( digits[0] == 0 ) {
753 radio_display_data[32] = 0xff;
755 radio_display_data[32] = 0xf0 | digits[0];
761 if ( adf_timer_mode->getIntValue() == 0 ) {
762 time = adf_flight_timer->getDoubleValue();
764 time = adf_elapsed_timer->getDoubleValue();
766 // cout << time << endl;
767 hours = (int)(time / 3600.0);
768 time -= hours * 3600.00;
769 min = (int)(time / 60.0);
786 // cout << big << ":" << little << endl;
787 snprintf(digits, 7, "%02d%02d", big, little);
788 for ( i = 0; i < 6; ++i ) {
791 radio_display_data[30] = digits[2] << 4 | digits[3];
792 radio_display_data[31] = digits[0] << 4 | digits[1];
793 radio_display_data[32] = 0xff;
796 // ADF in use frequency
797 float adf = adf_freq->getFloatValue();
798 if ( fabs(adf) > 1799 ) {
801 snprintf(digits, 7, "%04.0f", adf);
802 for ( i = 0; i < 6; ++i ) {
805 radio_display_data[33] = digits[2] << 4 | digits[3];
806 if ( digits[0] == 0 ) {
807 radio_display_data[34] = 0xf0 | digits[1];
809 radio_display_data[34] = digits[0] << 4 | digits[1];
811 if ( adf_stby_mode->getIntValue() == 0 ) {
812 radio_display_data[35] = 0xff;
814 radio_display_data[35] = 0x0f;
817 radio_display_data[30] = 0xff;
818 radio_display_data[31] = 0xff;
819 radio_display_data[32] = 0xff;
820 radio_display_data[33] = 0xff;
821 radio_display_data[34] = 0xff;
822 radio_display_data[35] = 0xff;
825 // Transponder code and flight level
826 if ( xpdr_has_power() && xpdr_serviceable->getBoolValue() ) {
827 if ( xpdr_func_knob->getIntValue() == 2 ) {
829 radio_display_data[36] = 8 << 4 | 8;
830 radio_display_data[37] = 8 << 4 | 8;
831 radio_display_data[38] = 0xff;
832 radio_display_data[39] = 8 << 4 | 0x0f;
833 radio_display_data[40] = 8 << 4 | 8;
836 int id_code = xpdr_id_code->getIntValue();
838 for ( i = 0; i < 4; ++i ) {
839 digits[i] = id_code / place;
840 id_code -= digits[i] * place;
843 radio_display_data[36] = digits[2] << 4 | digits[3];
844 radio_display_data[37] = digits[0] << 4 | digits[1];
845 radio_display_data[38] = 0xff;
847 if ( xpdr_func_knob->getIntValue() == 3 ||
848 xpdr_func_knob->getIntValue() == 5 )
850 // do flight level display
851 snprintf(digits, 7, "%03d", xpdr_flight_level->getIntValue() );
852 for ( i = 0; i < 6; ++i ) {
855 radio_display_data[39] = digits[2] << 4 | 0x0f;
856 radio_display_data[40] = digits[0] << 4 | digits[1];
858 // blank flight level display
859 radio_display_data[39] = 0xff;
860 radio_display_data[40] = 0xff;
865 radio_display_data[36] = 0xff;
866 radio_display_data[37] = 0xff;
867 radio_display_data[38] = 0xff;
868 radio_display_data[39] = 0xff;
869 radio_display_data[40] = 0xff;
872 ATC610xSetRadios( radios_fd, radio_display_data );
878 /////////////////////////////////////////////////////////////////////
879 // Drive the stepper motors
880 /////////////////////////////////////////////////////////////////////
882 bool FGATC610x::do_steppers() {
883 float diff = mag_compass->getFloatValue() - compass_position;
884 while ( diff < -180.0 ) { diff += 360.0; }
885 while ( diff > 180.0 ) { diff -= 360.0; }
887 int steps = (int)(diff * 4);
888 // cout << "steps = " << steps << endl;
889 if ( steps > 4 ) { steps = 4; }
890 if ( steps < -4 ) { steps = -4; }
892 if ( abs(steps) > 0 ) {
893 unsigned char cmd = 0x80; // stepper command
895 cmd |= 0x20; // go up
897 cmd |= 0x00; // go down
901 // sync compass_position with hardware position
902 compass_position += (float)steps / 4.0;
904 ATC610xSetStepper( stepper_fd, ATC_COMPASS_CH, cmd );
911 bool FGATC610x::process() {
913 // Lock the hardware, skip if it's not ready yet
914 if ( ATC610xLock( lock_fd ) > 0 ) {
916 // process the ATC inputs
917 if ( input0 != NULL ) {
920 if ( input1 != NULL ) {
924 // run our custom nasal script. This is a layer above the raw
925 // hardware inputs. It handles situations where there isn't a
926 // direct 1-1 linear mapping between ATC functionality and FG
927 // functionality, and handles situations where FG expects more
928 // functionality from the interface than the ATC hardware can
931 FGNasalSys *n = (FGNasalSys*)globals->get_subsystem("nasal");
932 bool result = n->parseAndRun( "atcsim.do_hardware()" );
934 SG_LOG( SG_GENERAL, SG_ALERT,
935 "do_atcflightsim_hardware() failed!" );
942 ATC610xRelease( lock_fd );
951 bool FGATC610x::close() {