]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
Work on nav2_obs tuner.
[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 global2net( const FGControls *global, FGNetCtrls *net ) {
61     int i;
62
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);
73     }
74     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
75         net->fuel_selector[i] = globals->get_controls()->get_fuel_selector(i);
76     }
77     for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
78         net->brake[i] =  globals->get_controls()->get_brake(i);
79     }
80
81     net->hground = globals->get_scenery()->get_cur_elev();
82 }
83
84
85 static void net2global( const FGNetCtrls *net, FGControls *global ) {
86     int i;
87
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]);
98         }
99         for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
100             globals->get_controls()->set_fuel_selector( i, net->fuel_selector[i] );
101         }
102         for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
103             globals->get_controls()->set_brake( i, net->brake[i] );
104         }
105         globals->get_controls()->set_gear_down( net->gear_handle );
106         globals->get_scenery()->set_cur_elev( net->hground );
107     } else {
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." );
111     }
112 }
113
114
115 // process work for this port
116 bool FGNativeCtrls::process() {
117     SGIOChannel *io = get_io_channel();
118     int length = sizeof(FGNetCtrls);
119
120     if ( get_direction() == SG_IO_OUT ) {
121         // cout << "size of cur_fdm_state = " << length << endl;
122
123         global2net( globals->get_controls(), &net_ctrls );
124
125         if ( ! io->write( (char *)(& net_ctrls), length ) ) {
126             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
127             return false;
128         }
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() );
134             }
135         } else {
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() );
139             }
140         }
141     }
142
143     return true;
144 }
145
146
147 // close the channel
148 bool FGNativeCtrls::close() {
149     SGIOChannel *io = get_io_channel();
150
151     set_enabled( false );
152
153     if ( ! io->close() ) {
154         return false;
155     }
156
157     return true;
158 }