1 // joyclient.cxx -- Agwagon joystick client class
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/io/iochannel.hxx>
27 #include <Aircraft/aircraft.hxx>
29 #include "joyclient.hxx"
32 FGJoyClient::FGJoyClient() {
35 FGJoyClient::~FGJoyClient() {
39 // open hailing frequencies
40 bool FGJoyClient::open() {
42 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
43 << "is already in use, ignoring" );
47 SGIOChannel *io = get_io_channel();
49 if ( ! io->open( get_direction() ) ) {
50 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
60 // process work for this port
61 bool FGJoyClient::process() {
62 SGIOChannel *io = get_io_channel();
63 int length = sizeof(int[2]);
65 if ( get_direction() == SG_IO_OUT ) {
66 SG_LOG( SG_IO, SG_ALERT, "joyclient protocol is read only" );
68 } else if ( get_direction() == SG_IO_IN ) {
69 SG_LOG( SG_IO, SG_DEBUG, "Searching for data." );
70 if ( io->get_type() == sgFileType ) {
71 if ( io->read( (char *)(& buf), length ) == length ) {
72 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
75 SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
77 double aileron = ((double)msg[0] / 2048.0) - 1.0;
78 double elevator = ((double)msg[1] / 2048.0) - 1.0;
79 if ( fabs(aileron) < 0.05 ) {
82 if ( fabs(elevator) < 0.05 ) {
85 globals->get_controls()->set_aileron( aileron );
86 globals->get_controls()->set_elevator( -elevator );
89 while ( io->read( (char *)(& buf), length ) == length ) {
90 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
93 SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
95 double aileron = ((double)msg[0] / 2048.0) - 1.0;
96 double elevator = ((double)msg[1] / 2048.0) - 1.0;
97 if ( fabs(aileron) < 0.05 ) {
100 if ( fabs(elevator) < 0.05 ) {
103 globals->get_controls()->set_aileron( aileron );
104 globals->get_controls()->set_elevator( -elevator );
114 bool FGJoyClient::close() {
115 SGIOChannel *io = get_io_channel();
117 set_enabled( false );
119 if ( ! io->close() ) {