FlightGear next
joyclient.cxx
Go to the documentation of this file.
1// joyclient.cxx -- Agwagon joystick client class
2//
3// Written by Curtis Olson, started April 2000.
4//
5// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20//
21// $Id$
22
23#ifdef HAVE_CONFIG_H
24# include "config.h"
25#endif
26
27#include <simgear/debug/logstream.hxx>
28#include <simgear/io/iochannel.hxx>
29
30#include <Aircraft/controls.hxx>
31#include <Main/globals.hxx>
32
33#include "joyclient.hxx"
34
35
38
41
42
43// open hailing frequencies
45 if ( is_enabled() ) {
46 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
47 << "is already in use, ignoring" );
48 return false;
49 }
50
51 SGIOChannel *io = get_io_channel();
52
53 if ( ! io->open( get_direction() ) ) {
54 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
55 return false;
56 }
57
58 set_enabled( true );
59
60 return true;
61}
62
63
64// process work for this port
66 SGIOChannel *io = get_io_channel();
67 int length = sizeof(int[2]);
68
69 if ( get_direction() == SG_IO_OUT ) {
70 SG_LOG( SG_IO, SG_ALERT, "joyclient protocol is read only" );
71 return false;
72 } else if ( get_direction() == SG_IO_IN ) {
73 SG_LOG( SG_IO, SG_DEBUG, "Searching for data." );
74 if ( io->get_type() == sgFileType ) {
75 if ( io->read( (char *)(& buf), length ) == length ) {
76 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
77 int *msg;
78 msg = (int *)buf;
79 SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
80 << msg[1] );
81 double aileron = ((double)msg[0] / 2048.0) - 1.0;
82 double elevator = ((double)msg[1] / 2048.0) - 1.0;
83 if ( fabs(aileron) < 0.05 ) {
84 aileron = 0.0;
85 }
86 if ( fabs(elevator) < 0.05 ) {
87 elevator = 0.0;
88 }
89 globals->get_controls()->set_aileron( aileron );
90 globals->get_controls()->set_elevator( -elevator );
91 }
92 } else {
93 while ( io->read( (char *)(& buf), length ) == length ) {
94 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
95 int *msg;
96 msg = (int *)buf;
97 SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
98 << msg[1] );
99 double aileron = ((double)msg[0] / 2048.0) - 1.0;
100 double elevator = ((double)msg[1] / 2048.0) - 1.0;
101 if ( fabs(aileron) < 0.05 ) {
102 aileron = 0.0;
103 }
104 if ( fabs(elevator) < 0.05 ) {
105 elevator = 0.0;
106 }
107 globals->get_controls()->set_aileron( aileron );
108 globals->get_controls()->set_elevator( -elevator );
109 }
110 }
111 }
112
113 return true;
114}
115
116
117// close the channel
119 SGIOChannel *io = get_io_channel();
120
121 set_enabled( false );
122
123 if ( ! io->close() ) {
124 return false;
125 }
126
127 return true;
128}
bool open()
Definition joyclient.cxx:44
bool process()
Definition joyclient.cxx:65
SGProtocolDir get_direction() const
Definition protocol.hxx:65
SGIOChannel * get_io_channel() const
Definition protocol.hxx:90
void set_enabled(const bool b)
Definition protocol.hxx:88
bool is_enabled() const
Definition protocol.hxx:87
FGGlobals * globals
Definition globals.cxx:142