]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
Erik Hofman:
[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 < 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);
73     }
74     for ( i = 0; i < FGRawCtrls::FG_MAX_WHEELS; ++i ) {
75         raw->brake[i] =  globals->get_controls()->get_brake(i);
76     }
77
78     raw->hground = globals->get_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 < FGRawCtrls::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 < FGRawCtrls::FG_MAX_WHEELS; ++i ) {
97             globals->get_controls()->set_brake( i, raw->brake[i] );
98         }
99         globals->get_controls()->set_gear_down( raw->gear_handle );
100         globals->get_scenery()->set_cur_elev( raw->hground );
101     } else {
102         SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in raw2global()" );
103         SG_LOG( SG_IO, SG_ALERT,
104                 "\tsomeone needs to upgrade raw_ctrls.hxx and recompile." );
105     }
106 }
107
108
109 // process work for this port
110 bool FGNativeCtrls::process() {
111     SGIOChannel *io = get_io_channel();
112     int length = sizeof(FGRawCtrls);
113
114     if ( get_direction() == SG_IO_OUT ) {
115         // cout << "size of cur_fdm_state = " << length << endl;
116
117         global2raw( globals->get_controls(), &raw_ctrls );
118
119         if ( ! io->write( (char *)(& raw_ctrls), length ) ) {
120             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
121             return false;
122         }
123     } else if ( get_direction() == SG_IO_IN ) {
124         if ( io->get_type() == sgFileType ) {
125             if ( io->read( (char *)(& raw_ctrls), length ) == length ) {
126                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
127                 raw2global( &raw_ctrls, globals->get_controls() );
128             }
129         } else {
130             while ( io->read( (char *)(& raw_ctrls), length ) == length ) {
131                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
132                 raw2global( &raw_ctrls, globals->get_controls() );
133             }
134         }
135     }
136
137     return true;
138 }
139
140
141 // close the channel
142 bool FGNativeCtrls::close() {
143     SGIOChannel *io = get_io_channel();
144
145     set_enabled( false );
146
147     if ( ! io->close() ) {
148         return false;
149     }
150
151     return true;
152 }