1 // jsclient.cxx -- simple UDP networked joystick client
3 // Copyright (C) 2003 by Manuel Bessler and Stephen Lowry
5 // based on joyclient.cxx by Curtis Olson
6 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/io/iochannel.hxx>
28 #include <Aircraft/aircraft.hxx>
30 #include "jsclient.hxx"
33 FGJsClient::FGJsClient() {
36 FGJsClient::~FGJsClient() {
40 // open hailing frequencies
41 bool FGJsClient::open() {
43 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
44 << "is already in use, ignoring" );
48 SGIOChannel *io = get_io_channel();
50 if ( ! io->open( get_direction() ) ) {
51 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
61 // process work for this port
62 bool FGJsClient::process() {
63 SGIOChannel *io = get_io_channel();
64 int length = 4+4+4+4+4+4;
66 if ( get_direction() == SG_IO_OUT ) {
67 SG_LOG( SG_IO, SG_ALERT, "JsClient protocol is read only" );
69 } else if ( get_direction() == SG_IO_IN ) {
70 SG_LOG( SG_IO, SG_DEBUG, "Searching for data." );
71 if ( io->get_type() == sgFileType ) {
72 if ( io->read( (char *)(& buf), length ) == length ) {
73 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
75 msg = (long int *)buf;
76 SG_LOG( SG_IO, SG_DEBUG, "ax0 = " << msg[0] << " ax1 = "
77 << msg[1] << "ax2 = " << msg[2] << "ax3 = " << msg[3]);
78 double axis1 = ((double)msg[0] / 2147483647.0);
79 double axis2 = ((double)msg[1] / 2147483647.0);
80 if ( fabs(axis1) < 0.05 ) {
83 if ( fabs(axis2) < 0.05 ) {
86 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES, axis1);
87 // globals->get_controls()->set_aileron( axis1 );
88 // globals->get_controls()->set_elevator( -axis2 );
91 while ( io->read( (char *)(& buf), length ) == length ) {
92 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
94 msg = (long int *)buf;
95 SG_LOG( SG_IO, SG_DEBUG, "ax0 = " << msg[0] << " ax1 = "
96 << msg[1] << "ax2 = " << msg[2] << "ax3 = " << msg[3]);
97 double axis1 = ((double)msg[0] / 2147483647.0);
98 double axis2 = ((double)msg[1] / 2147483647.0);
99 if ( fabs(axis1) < 0.05 ) {
102 if ( fabs(axis2) < 0.05 ) {
105 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES, axis1);
106 // globals->get_controls()->set_aileron( axis1 );
107 // globals->get_controls()->set_elevator( -axis2 );
117 bool FGJsClient::close() {
118 SGIOChannel *io = get_io_channel();
120 set_enabled( false );
122 if ( ! io->close() ) {