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/aircraft.hxx>
32 #include "joyclient.hxx"
35 FGJoyClient::FGJoyClient() {
38 FGJoyClient::~FGJoyClient() {
42 // open hailing frequencies
43 bool FGJoyClient::open() {
45 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
46 << "is already in use, ignoring" );
50 SGIOChannel *io = get_io_channel();
52 if ( ! io->open( get_direction() ) ) {
53 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
63 // process work for this port
64 bool FGJoyClient::process() {
65 SGIOChannel *io = get_io_channel();
66 int length = sizeof(int[2]);
68 if ( get_direction() == SG_IO_OUT ) {
69 SG_LOG( SG_IO, SG_ALERT, "joyclient protocol is read only" );
71 } else if ( get_direction() == SG_IO_IN ) {
72 SG_LOG( SG_IO, SG_DEBUG, "Searching for data." );
73 if ( io->get_type() == sgFileType ) {
74 if ( io->read( (char *)(& buf), length ) == length ) {
75 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
78 SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
80 double aileron = ((double)msg[0] / 2048.0) - 1.0;
81 double elevator = ((double)msg[1] / 2048.0) - 1.0;
82 if ( fabs(aileron) < 0.05 ) {
85 if ( fabs(elevator) < 0.05 ) {
88 globals->get_controls()->set_aileron( aileron );
89 globals->get_controls()->set_elevator( -elevator );
92 while ( io->read( (char *)(& buf), length ) == length ) {
93 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
96 SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
98 double aileron = ((double)msg[0] / 2048.0) - 1.0;
99 double elevator = ((double)msg[1] / 2048.0) - 1.0;
100 if ( fabs(aileron) < 0.05 ) {
103 if ( fabs(elevator) < 0.05 ) {
106 globals->get_controls()->set_aileron( aileron );
107 globals->get_controls()->set_elevator( -elevator );
117 bool FGJoyClient::close() {
118 SGIOChannel *io = get_io_channel();
120 set_enabled( false );
122 if ( ! io->close() ) {