]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
Initial revision.
[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 "native_ctrls.hxx"
28
29
30 FGNativeCtrls::FGNativeCtrls() {
31 }
32
33 FGNativeCtrls::~FGNativeCtrls() {
34 }
35
36
37 // open hailing frequencies
38 bool FGNativeCtrls::open() {
39     if ( is_enabled() ) {
40         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
41                 << "is already in use, ignoring" );
42         return false;
43     }
44
45     SGIOChannel *io = get_io_channel();
46
47     if ( ! io->open( get_direction() ) ) {
48         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
49         return false;
50     }
51
52     set_enabled( true );
53
54     return true;
55 }
56
57
58 static void global2raw( FGControls *global, FGRawCtrls *raw ) {
59     int i;
60
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);
70     }
71     for ( i = 0; i < FG_MAX_WHEELS; ++i ) {
72         raw->brake[i] =  globals->get_controls()->get_brake(i);
73     }
74 }
75
76
77 static void raw2global( FGRawCtrls *raw, FGControls *global ) {
78     int i;
79
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] );
89     }
90     for ( i = 0; i < FG_MAX_WHEELS; ++i ) {
91         globals->get_controls()->set_brake( i, raw->brake[i] );
92     }
93 }
94
95
96 // process work for this port
97 bool FGNativeCtrls::process() {
98     SGIOChannel *io = get_io_channel();
99     int length = sizeof(FGRawCtrls);
100
101     if ( get_direction() == SG_IO_OUT ) {
102         // cout << "size of cur_fdm_state = " << length << endl;
103
104         global2raw( globals->get_controls(), &raw_ctrls );
105
106         if ( ! io->write( (char *)(& raw_ctrls), length ) ) {
107             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
108             return false;
109         }
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() );
115             }
116         } else {
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() );
120             }
121         }
122     }
123
124     return true;
125 }
126
127
128 // close the channel
129 bool FGNativeCtrls::close() {
130     SGIOChannel *io = get_io_channel();
131
132     set_enabled( false );
133
134     if ( ! io->close() ) {
135         return false;
136     }
137
138     return true;
139 }