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 global2net( const FGControls *global, FGNetCtrls *net ) {
63 net->version = FG_NET_CTRLS_VERSION;
64 net->aileron = globals->get_controls()->get_aileron();
65 net->elevator = globals->get_controls()->get_elevator();
66 net->elevator_trim = globals->get_controls()->get_elevator_trim();
67 net->rudder = globals->get_controls()->get_rudder();
68 net->flaps = globals->get_controls()->get_flaps();
69 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
70 net->throttle[i] = globals->get_controls()->get_throttle(i);
71 net->mixture[i] = globals->get_controls()->get_mixture(i);
72 net->prop_advance[i] = globals->get_controls()->get_prop_advance(i);
74 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
75 net->fuel_selector[i] = globals->get_controls()->get_fuel_selector(i);
77 for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
78 net->brake[i] = globals->get_controls()->get_brake(i);
81 net->hground = globals->get_scenery()->get_cur_elev();
85 static void net2global( const FGNetCtrls *net, FGControls *global ) {
88 if ( net->version == FG_NET_CTRLS_VERSION ) {
89 globals->get_controls()->set_aileron( net->aileron );
90 globals->get_controls()->set_elevator( net->elevator );
91 globals->get_controls()->set_elevator_trim( net->elevator_trim );
92 globals->get_controls()->set_rudder( net->rudder );
93 globals->get_controls()->set_flaps( net->flaps );
94 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
95 globals->get_controls()->set_throttle( i, net->throttle[i] );
96 globals->get_controls()->set_mixture( i, net->mixture[i] );
97 globals->get_controls()->set_prop_advance( i, net->prop_advance[i]);
99 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
100 globals->get_controls()->set_fuel_selector( i, net->fuel_selector[i] );
102 for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
103 globals->get_controls()->set_brake( i, net->brake[i] );
105 globals->get_controls()->set_gear_down( net->gear_handle );
106 globals->get_scenery()->set_cur_elev( net->hground );
108 SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in net2global()" );
109 SG_LOG( SG_IO, SG_ALERT,
110 "\tsomeone needs to upgrade net_ctrls.hxx and recompile." );
115 // process work for this port
116 bool FGNativeCtrls::process() {
117 SGIOChannel *io = get_io_channel();
118 int length = sizeof(FGNetCtrls);
120 if ( get_direction() == SG_IO_OUT ) {
121 // cout << "size of cur_fdm_state = " << length << endl;
123 global2net( globals->get_controls(), &net_ctrls );
125 if ( ! io->write( (char *)(& net_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 *)(& net_ctrls), length ) == length ) {
132 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
133 net2global( &net_ctrls, globals->get_controls() );
136 while ( io->read( (char *)(& net_ctrls), length ) == length ) {
137 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
138 net2global( &net_ctrls, globals->get_controls() );
148 bool FGNativeCtrls::close() {
149 SGIOChannel *io = get_io_channel();
151 set_enabled( false );
153 if ( ! io->close() ) {