2001-07-22 20:01:25 +00:00
|
|
|
// native_ctrls.cxx -- FGFS "Native" controls I/O class
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started July 2001.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/io/iochannel.hxx>
|
|
|
|
|
2001-10-26 05:42:08 +00:00
|
|
|
#include <Scenery/scenery.hxx> // ground elevation
|
|
|
|
|
2001-07-22 20:01:25 +00:00
|
|
|
#include "native_ctrls.hxx"
|
|
|
|
|
|
|
|
|
|
|
|
FGNativeCtrls::FGNativeCtrls() {
|
|
|
|
}
|
|
|
|
|
|
|
|
FGNativeCtrls::~FGNativeCtrls() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// open hailing frequencies
|
|
|
|
bool FGNativeCtrls::open() {
|
|
|
|
if ( is_enabled() ) {
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
|
|
|
|
<< "is already in use, ignoring" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SGIOChannel *io = get_io_channel();
|
|
|
|
|
|
|
|
if ( ! io->open( get_direction() ) ) {
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_enabled( true );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-28 22:10:49 +00:00
|
|
|
static void global2net( const FGControls *global, FGNetCtrls *net ) {
|
2001-07-22 20:01:25 +00:00
|
|
|
int i;
|
|
|
|
|
2002-09-28 22:10:49 +00:00
|
|
|
net->version = FG_NET_CTRLS_VERSION;
|
|
|
|
net->aileron = globals->get_controls()->get_aileron();
|
|
|
|
net->elevator = globals->get_controls()->get_elevator();
|
|
|
|
net->elevator_trim = globals->get_controls()->get_elevator_trim();
|
|
|
|
net->rudder = globals->get_controls()->get_rudder();
|
|
|
|
net->flaps = globals->get_controls()->get_flaps();
|
|
|
|
for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
|
|
|
|
net->throttle[i] = globals->get_controls()->get_throttle(i);
|
|
|
|
net->mixture[i] = globals->get_controls()->get_mixture(i);
|
|
|
|
net->prop_advance[i] = globals->get_controls()->get_prop_advance(i);
|
2001-07-22 20:01:25 +00:00
|
|
|
}
|
2002-09-28 22:10:49 +00:00
|
|
|
for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
|
|
|
|
net->fuel_selector[i] = globals->get_controls()->get_fuel_selector(i);
|
2002-07-05 19:04:04 +00:00
|
|
|
}
|
2002-09-28 22:10:49 +00:00
|
|
|
for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
|
|
|
|
net->brake[i] = globals->get_controls()->get_brake(i);
|
2001-07-22 20:01:25 +00:00
|
|
|
}
|
2001-10-26 05:42:08 +00:00
|
|
|
|
2002-09-28 22:10:49 +00:00
|
|
|
net->hground = globals->get_scenery()->get_cur_elev();
|
2001-07-22 20:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-28 22:10:49 +00:00
|
|
|
static void net2global( const FGNetCtrls *net, FGControls *global ) {
|
2001-07-22 20:01:25 +00:00
|
|
|
int i;
|
|
|
|
|
2002-09-28 22:10:49 +00:00
|
|
|
if ( net->version == FG_NET_CTRLS_VERSION ) {
|
|
|
|
globals->get_controls()->set_aileron( net->aileron );
|
|
|
|
globals->get_controls()->set_elevator( net->elevator );
|
|
|
|
globals->get_controls()->set_elevator_trim( net->elevator_trim );
|
|
|
|
globals->get_controls()->set_rudder( net->rudder );
|
|
|
|
globals->get_controls()->set_flaps( net->flaps );
|
|
|
|
for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
|
|
|
|
globals->get_controls()->set_throttle( i, net->throttle[i] );
|
|
|
|
globals->get_controls()->set_mixture( i, net->mixture[i] );
|
|
|
|
globals->get_controls()->set_prop_advance( i, net->prop_advance[i]);
|
2001-09-04 14:38:15 +00:00
|
|
|
}
|
2002-09-28 22:10:49 +00:00
|
|
|
for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
|
|
|
|
globals->get_controls()->set_fuel_selector( i, net->fuel_selector[i] );
|
2002-07-05 19:04:04 +00:00
|
|
|
}
|
2002-09-28 22:10:49 +00:00
|
|
|
for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
|
|
|
|
globals->get_controls()->set_brake( i, net->brake[i] );
|
2001-09-04 14:38:15 +00:00
|
|
|
}
|
2002-09-28 22:10:49 +00:00
|
|
|
globals->get_controls()->set_gear_down( net->gear_handle );
|
|
|
|
globals->get_scenery()->set_cur_elev( net->hground );
|
2001-09-04 14:38:15 +00:00
|
|
|
} else {
|
2002-09-28 22:10:49 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in net2global()" );
|
2001-09-04 14:38:15 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT,
|
2002-09-28 22:10:49 +00:00
|
|
|
"\tsomeone needs to upgrade net_ctrls.hxx and recompile." );
|
2001-07-22 20:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// process work for this port
|
|
|
|
bool FGNativeCtrls::process() {
|
|
|
|
SGIOChannel *io = get_io_channel();
|
2002-09-28 22:10:49 +00:00
|
|
|
int length = sizeof(FGNetCtrls);
|
2001-07-22 20:01:25 +00:00
|
|
|
|
|
|
|
if ( get_direction() == SG_IO_OUT ) {
|
|
|
|
// cout << "size of cur_fdm_state = " << length << endl;
|
|
|
|
|
2002-09-28 22:10:49 +00:00
|
|
|
global2net( globals->get_controls(), &net_ctrls );
|
2001-07-22 20:01:25 +00:00
|
|
|
|
2002-09-28 22:10:49 +00:00
|
|
|
if ( ! io->write( (char *)(& net_ctrls), length ) ) {
|
2001-07-22 20:01:25 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if ( get_direction() == SG_IO_IN ) {
|
|
|
|
if ( io->get_type() == sgFileType ) {
|
2002-09-28 22:10:49 +00:00
|
|
|
if ( io->read( (char *)(& net_ctrls), length ) == length ) {
|
2001-07-22 20:01:25 +00:00
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
|
2002-09-28 22:10:49 +00:00
|
|
|
net2global( &net_ctrls, globals->get_controls() );
|
2001-07-22 20:01:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2002-09-28 22:10:49 +00:00
|
|
|
while ( io->read( (char *)(& net_ctrls), length ) == length ) {
|
2001-07-22 20:01:25 +00:00
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
|
2002-09-28 22:10:49 +00:00
|
|
|
net2global( &net_ctrls, globals->get_controls() );
|
2001-07-22 20:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// close the channel
|
|
|
|
bool FGNativeCtrls::close() {
|
|
|
|
SGIOChannel *io = get_io_channel();
|
|
|
|
|
|
|
|
set_enabled( false );
|
|
|
|
|
|
|
|
if ( ! io->close() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|