]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
Substantial rewrite of FGNewMat, the material class. Most of the
[flightgear.git] / src / Network / native_ctrls.cxx
1 // native_ctrls.cxx -- FGFS "Native" controls I/O class
2 //
3 // Written by Curtis Olson, started July 2001.
4 //
5 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/io/iochannel.hxx>
26
27 #include <Scenery/scenery.hxx>  // ground elevation
28
29 #include "native_ctrls.hxx"
30
31
32 FGNativeCtrls::FGNativeCtrls() {
33 }
34
35 FGNativeCtrls::~FGNativeCtrls() {
36 }
37
38
39 // open hailing frequencies
40 bool FGNativeCtrls::open() {
41     if ( is_enabled() ) {
42         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
43                 << "is already in use, ignoring" );
44         return false;
45     }
46
47     SGIOChannel *io = get_io_channel();
48
49     if ( ! io->open( get_direction() ) ) {
50         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
51         return false;
52     }
53
54     set_enabled( true );
55
56     return true;
57 }
58
59
60 static void global2raw( const FGControls *global, FGRawCtrls *raw ) {
61     int i;
62
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 < 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);
73     }
74     for ( i = 0; i < FG_MAX_WHEELS; ++i ) {
75         raw->brake[i] =  globals->get_controls()->get_brake(i);
76     }
77
78     raw->hground = scenery.get_cur_elev();
79 }
80
81
82 static void raw2global( const FGRawCtrls *raw, FGControls *global ) {
83     int i;
84
85     if ( raw->version == FG_RAW_CTRLS_VERSION ) {
86         globals->get_controls()->set_aileron( raw->aileron );
87         globals->get_controls()->set_elevator( raw->elevator );
88         globals->get_controls()->set_elevator_trim( raw->elevator_trim );
89         globals->get_controls()->set_rudder( raw->rudder );
90         globals->get_controls()->set_flaps( raw->flaps );
91         for ( i = 0; i < FG_MAX_ENGINES; ++i ) {    
92             globals->get_controls()->set_throttle( i, raw->throttle[i] );
93             globals->get_controls()->set_mixture( i, raw->mixture[i] );
94             globals->get_controls()->set_prop_advance( i, raw->prop_advance[i]);
95         }
96         for ( i = 0; i < FG_MAX_WHEELS; ++i ) {
97             globals->get_controls()->set_brake( i, raw->brake[i] );
98         }
99         scenery.set_cur_elev( raw->hground );
100     } else {
101         SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in raw2global()" );
102         SG_LOG( SG_IO, SG_ALERT,
103                 "\tsomeone needs to upgrade raw_ctrls.hxx and recompile." );
104     }
105 }
106
107
108 // process work for this port
109 bool FGNativeCtrls::process() {
110     SGIOChannel *io = get_io_channel();
111     int length = sizeof(FGRawCtrls);
112
113     if ( get_direction() == SG_IO_OUT ) {
114         // cout << "size of cur_fdm_state = " << length << endl;
115
116         global2raw( globals->get_controls(), &raw_ctrls );
117
118         if ( ! io->write( (char *)(& raw_ctrls), length ) ) {
119             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
120             return false;
121         }
122     } else if ( get_direction() == SG_IO_IN ) {
123         if ( io->get_type() == sgFileType ) {
124             if ( io->read( (char *)(& raw_ctrls), length ) == length ) {
125                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
126                 raw2global( &raw_ctrls, globals->get_controls() );
127             }
128         } else {
129             while ( io->read( (char *)(& raw_ctrls), length ) == length ) {
130                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
131                 raw2global( &raw_ctrls, globals->get_controls() );
132             }
133         }
134     }
135
136     return true;
137 }
138
139
140 // close the channel
141 bool FGNativeCtrls::close() {
142     SGIOChannel *io = get_io_channel();
143
144     set_enabled( false );
145
146     if ( ! io->close() ) {
147         return false;
148     }
149
150     return true;
151 }