]> git.mxchange.org Git - flightgear.git/blob - src/Network/jsclient.cxx
7a59e78ea86ff9ceeadec97248fb7ee0c3f6dcca
[flightgear.git] / src / Network / jsclient.cxx
1 // jsclient.cxx -- simple UDP networked joystick client
2 //
3 // Copyright (C) 2003 by Manuel Bessler and Stephen Lowry
4 //
5 // based on joyclient.cxx by Curtis Olson
6 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/io/iochannel.hxx>
27
28 #include <Aircraft/aircraft.hxx>
29
30 #include "jsclient.hxx"
31
32
33 FGJsClient::FGJsClient() {
34 }
35
36 FGJsClient::~FGJsClient() {
37 }
38
39
40 // open hailing frequencies
41 bool FGJsClient::open() {
42     if ( is_enabled() ) {
43         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
44                 << "is already in use, ignoring" );
45         return false;
46     }
47
48     SGIOChannel *io = get_io_channel();
49
50     if ( ! io->open( get_direction() ) ) {
51         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
52         return false;
53     }
54
55     set_enabled( true );
56
57     return true;
58 }
59
60
61 // process work for this port
62 bool FGJsClient::process() {
63     SGIOChannel *io = get_io_channel();
64     int length = 4+4+4+4+4+4;
65
66     if ( get_direction() == SG_IO_OUT ) {
67         SG_LOG( SG_IO, SG_ALERT, "JsClient protocol is read only" );
68         return false;
69     } else if ( get_direction() == SG_IO_IN ) {
70         SG_LOG( SG_IO, SG_DEBUG, "Searching for data." );
71         if ( io->get_type() == sgFileType ) {
72             if ( io->read( (char *)(& buf), length ) == length ) {
73                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
74                 long int *msg;
75                 msg = (long int *)buf;
76                 SG_LOG( SG_IO, SG_DEBUG, "ax0 = " << msg[0] << " ax1 = "
77                         << msg[1] << "ax2 = " << msg[2] << "ax3 = " << msg[3]);
78                 double axis1 = ((double)msg[0] / 2147483647.0);
79                 double axis2 = ((double)msg[1] / 2147483647.0);
80                 if ( fabs(axis1) < 0.05 ) {
81                     axis1 = 0.0;
82                 }
83                 if ( fabs(axis2) < 0.05 ) {
84                     axis2 = 0.0;
85                 }
86                 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES, axis1);
87 //              globals->get_controls()->set_aileron( axis1 );
88 //              globals->get_controls()->set_elevator( -axis2 );
89             }
90         } else {
91             while ( io->read( (char *)(& buf), length ) == length ) {
92                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
93                 long int *msg;
94                 msg = (long int *)buf;
95                 SG_LOG( SG_IO, SG_DEBUG, "ax0 = " << msg[0] << " ax1 = "
96                         << msg[1] << "ax2 = " << msg[2] << "ax3 = " << msg[3]);
97                 double axis1 = ((double)msg[0] / 2147483647.0);
98                 double axis2 = ((double)msg[1] / 2147483647.0);
99                 if ( fabs(axis1) < 0.05 ) {
100                     axis1 = 0.0;
101                 }
102                 if ( fabs(axis2) < 0.05 ) {
103                     axis2 = 0.0;
104                 }
105                 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES, axis1);
106 //              globals->get_controls()->set_aileron( axis1 );
107 //              globals->get_controls()->set_elevator( -axis2 );
108             }
109         }
110     }
111
112     return true;
113 }
114
115
116 // close the channel
117 bool FGJsClient::close() {
118     SGIOChannel *io = get_io_channel();
119
120     set_enabled( false );
121
122     if ( ! io->close() ) {
123         return false;
124     }
125
126     return true;
127 }