1 // native_ctrls.cxx -- FGFS "Native" controls I/O class
3 // Written by Curtis Olson, started July 2001.
5 // Copyright (C) 2001 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.
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/io/iochannel.hxx>
27 #include <Scenery/scenery.hxx> // ground elevation
29 #include "native_ctrls.hxx"
32 FGNativeCtrls::FGNativeCtrls() {
35 FGNativeCtrls::~FGNativeCtrls() {
39 // open hailing frequencies
40 bool FGNativeCtrls::open() {
42 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
43 << "is already in use, ignoring" );
47 SGIOChannel *io = get_io_channel();
49 if ( ! io->open( get_direction() ) ) {
50 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
60 static void global2raw( const FGControls *global, FGRawCtrls *raw ) {
63 raw->version = FG_RAW_CTRLS_VERSION;
64 raw->aileron = globals->get_controls()->get_aileron();
65 raw->elevator = globals->get_controls()->get_elevator();
66 raw->elevator_trim = globals->get_controls()->get_elevator_trim();
67 raw->rudder = globals->get_controls()->get_rudder();
68 raw->flaps = globals->get_controls()->get_flaps();
69 for ( i = 0; i < FGRawCtrls::FG_MAX_ENGINES; ++i ) {
70 raw->throttle[i] = globals->get_controls()->get_throttle(i);
71 raw->mixture[i] = globals->get_controls()->get_mixture(i);
72 raw->prop_advance[i] = globals->get_controls()->get_prop_advance(i);
74 for ( i = 0; i < FGRawCtrls::FG_MAX_TANKS; ++i ) {
75 raw->fuel_selector[i] = globals->get_controls()->get_fuel_selector(i);
77 for ( i = 0; i < FGRawCtrls::FG_MAX_WHEELS; ++i ) {
78 raw->brake[i] = globals->get_controls()->get_brake(i);
81 raw->hground = globals->get_scenery()->get_cur_elev();
85 static void raw2global( const FGRawCtrls *raw, FGControls *global ) {
88 if ( raw->version == FG_RAW_CTRLS_VERSION ) {
89 globals->get_controls()->set_aileron( raw->aileron );
90 globals->get_controls()->set_elevator( raw->elevator );
91 globals->get_controls()->set_elevator_trim( raw->elevator_trim );
92 globals->get_controls()->set_rudder( raw->rudder );
93 globals->get_controls()->set_flaps( raw->flaps );
94 for ( i = 0; i < FGRawCtrls::FG_MAX_ENGINES; ++i ) {
95 globals->get_controls()->set_throttle( i, raw->throttle[i] );
96 globals->get_controls()->set_mixture( i, raw->mixture[i] );
97 globals->get_controls()->set_prop_advance( i, raw->prop_advance[i]);
99 for ( i = 0; i < FGRawCtrls::FG_MAX_TANKS; ++i ) {
100 globals->get_controls()->set_fuel_selector( i, raw->fuel_selector[i] );
102 for ( i = 0; i < FGRawCtrls::FG_MAX_WHEELS; ++i ) {
103 globals->get_controls()->set_brake( i, raw->brake[i] );
105 globals->get_controls()->set_gear_down( raw->gear_handle );
106 globals->get_scenery()->set_cur_elev( raw->hground );
108 SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in raw2global()" );
109 SG_LOG( SG_IO, SG_ALERT,
110 "\tsomeone needs to upgrade raw_ctrls.hxx and recompile." );
115 // process work for this port
116 bool FGNativeCtrls::process() {
117 SGIOChannel *io = get_io_channel();
118 int length = sizeof(FGRawCtrls);
120 if ( get_direction() == SG_IO_OUT ) {
121 // cout << "size of cur_fdm_state = " << length << endl;
123 global2raw( globals->get_controls(), &raw_ctrls );
125 if ( ! io->write( (char *)(& raw_ctrls), length ) ) {
126 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
129 } else if ( get_direction() == SG_IO_IN ) {
130 if ( io->get_type() == sgFileType ) {
131 if ( io->read( (char *)(& raw_ctrls), length ) == length ) {
132 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
133 raw2global( &raw_ctrls, globals->get_controls() );
136 while ( io->read( (char *)(& raw_ctrls), length ) == length ) {
137 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
138 raw2global( &raw_ctrls, globals->get_controls() );
148 bool FGNativeCtrls::close() {
149 SGIOChannel *io = get_io_channel();
151 set_enabled( false );
153 if ( ! io->close() ) {