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 "native_ctrls.hxx"
30 FGNativeCtrls::FGNativeCtrls() {
33 FGNativeCtrls::~FGNativeCtrls() {
37 // open hailing frequencies
38 bool FGNativeCtrls::open() {
40 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
41 << "is already in use, ignoring" );
45 SGIOChannel *io = get_io_channel();
47 if ( ! io->open( get_direction() ) ) {
48 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
58 static void global2raw( FGControls *global, FGRawCtrls *raw ) {
61 raw->aileron = globals->get_controls()->get_aileron();
62 raw->elevator = globals->get_controls()->get_elevator();
63 raw->elevator_trim = globals->get_controls()->get_elevator_trim();
64 raw->rudder = globals->get_controls()->get_rudder();
65 raw->flaps = globals->get_controls()->get_flaps();
66 for ( i = 0; i < FG_MAX_ENGINES; ++i ) {
67 raw->throttle[i] = globals->get_controls()->get_throttle(i);
68 raw->mixture[i] = globals->get_controls()->get_mixture(i);
69 raw->prop_advance[i] = globals->get_controls()->get_prop_advance(i);
71 for ( i = 0; i < FG_MAX_WHEELS; ++i ) {
72 raw->brake[i] = globals->get_controls()->get_brake(i);
77 static void raw2global( FGRawCtrls *raw, FGControls *global ) {
80 globals->get_controls()->set_aileron( raw->aileron );
81 globals->get_controls()->set_elevator( raw->elevator );
82 globals->get_controls()->set_elevator_trim( raw->elevator_trim );
83 globals->get_controls()->set_rudder( raw->rudder );
84 globals->get_controls()->set_flaps( raw->flaps );
85 for ( i = 0; i < FG_MAX_ENGINES; ++i ) {
86 globals->get_controls()->set_throttle( i, raw->throttle[i] );
87 globals->get_controls()->set_mixture( i, raw->mixture[i] );
88 globals->get_controls()->set_prop_advance( i, raw->prop_advance[i] );
90 for ( i = 0; i < FG_MAX_WHEELS; ++i ) {
91 globals->get_controls()->set_brake( i, raw->brake[i] );
96 // process work for this port
97 bool FGNativeCtrls::process() {
98 SGIOChannel *io = get_io_channel();
99 int length = sizeof(FGRawCtrls);
101 if ( get_direction() == SG_IO_OUT ) {
102 // cout << "size of cur_fdm_state = " << length << endl;
104 global2raw( globals->get_controls(), &raw_ctrls );
106 if ( ! io->write( (char *)(& raw_ctrls), length ) ) {
107 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
110 } else if ( get_direction() == SG_IO_IN ) {
111 if ( io->get_type() == sgFileType ) {
112 if ( io->read( (char *)(& raw_ctrls), length ) == length ) {
113 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
114 raw2global( &raw_ctrls, globals->get_controls() );
117 while ( io->read( (char *)(& raw_ctrls), length ) == length ) {
118 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
119 raw2global( &raw_ctrls, globals->get_controls() );
129 bool FGNativeCtrls::close() {
130 SGIOChannel *io = get_io_channel();
132 set_enabled( false );
134 if ( ! io->close() ) {