1 // joyclient.cxx -- Agwagon joystick client class
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/iochannel.hxx>
30 #include <Aircraft/controls.hxx>
31 #include <Main/globals.hxx>
33 #include "joyclient.hxx"
36 FGJoyClient::FGJoyClient() {
39 FGJoyClient::~FGJoyClient() {
43 // open hailing frequencies
44 bool FGJoyClient::open() {
46 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
47 << "is already in use, ignoring" );
51 SGIOChannel *io = get_io_channel();
53 if ( ! io->open( get_direction() ) ) {
54 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
64 // process work for this port
65 bool FGJoyClient::process() {
66 SGIOChannel *io = get_io_channel();
67 int length = sizeof(int[2]);
69 if ( get_direction() == SG_IO_OUT ) {
70 SG_LOG( SG_IO, SG_ALERT, "joyclient protocol is read only" );
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." );
79 SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
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 ) {
86 if ( fabs(elevator) < 0.05 ) {
89 globals->get_controls()->set_aileron( aileron );
90 globals->get_controls()->set_elevator( -elevator );
93 while ( io->read( (char *)(& buf), length ) == length ) {
94 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
97 SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
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 ) {
104 if ( fabs(elevator) < 0.05 ) {
107 globals->get_controls()->set_aileron( aileron );
108 globals->get_controls()->set_elevator( -elevator );
118 bool FGJoyClient::close() {
119 SGIOChannel *io = get_io_channel();
121 set_enabled( false );
123 if ( ! io->close() ) {