]> git.mxchange.org Git - flightgear.git/blob - src/Network/joyclient.cxx
Changes contributed by Tony Peden to put wind data "on the bus".
[flightgear.git] / src / Network / joyclient.cxx
1 // joyclient.cxx -- Agwagon joystick client class
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000  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
26 #include <Aircraft/aircraft.hxx>
27
28 #include "iochannel.hxx"
29 #include "joyclient.hxx"
30
31
32 FGJoyClient::FGJoyClient() {
33 }
34
35 FGJoyClient::~FGJoyClient() {
36 }
37
38
39 // open hailing frequencies
40 bool FGJoyClient::open() {
41     if ( is_enabled() ) {
42         FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel " 
43                 << "is already in use, ignoring" );
44         return false;
45     }
46
47     FGIOChannel *io = get_io_channel();
48
49     if ( ! io->open( get_direction() ) ) {
50         FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
51         return false;
52     }
53
54     set_enabled( true );
55
56     return true;
57 }
58
59
60 // process work for this port
61 bool FGJoyClient::process() {
62     FGIOChannel *io = get_io_channel();
63     int length = sizeof(int[2]);
64
65     if ( get_direction() == out ) {
66         FG_LOG( FG_IO, FG_ALERT, "joyclient protocol is read only" );
67         return false;
68     } else if ( get_direction() == in ) {
69         FG_LOG( FG_IO, FG_DEBUG, "Searching for data." );
70         while ( io->read( (char *)(& buf), length ) == length ) {
71             FG_LOG( FG_IO, FG_DEBUG, "Success reading data." );
72             int *msg;
73             msg = (int *)buf;
74             FG_LOG( FG_IO, FG_DEBUG, "X = " << msg[0] << " Y = " << msg[1] );
75             double aileron = ((double)msg[0] / 2048.0) - 1.0;
76             double elevator = ((double)msg[1] / 2048.0) - 1.0;
77             if ( fabs(aileron) < 0.05 ) {
78                 aileron = 0.0;
79             }
80             if ( fabs(elevator) < 0.05 ) {
81                 elevator = 0.0;
82             }
83             controls.set_aileron( aileron );
84             controls.set_elevator( -elevator );
85         }
86     }
87
88     return true;
89 }
90
91
92 // close the channel
93 bool FGJoyClient::close() {
94     FGIOChannel *io = get_io_channel();
95
96     set_enabled( false );
97
98     if ( ! io->close() ) {
99         return false;
100     }
101
102     return true;
103 }