~ubuntu-branches/ubuntu/quantal/flightgear/quantal

« back to all changes in this revision

Viewing changes to src/Network/joyclient.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Ove Kaaven
  • Date: 2002-03-27 21:50:15 UTC
  • Revision ID: james.westby@ubuntu.com-20020327215015-0rvi3o8iml0a8s93
Tags: upstream-0.7.9
ImportĀ upstreamĀ versionĀ 0.7.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// joyclient.cxx -- Agwagon joystick client class
 
2
//
 
3
// Written by Curtis Olson, started April 2000.
 
4
//
 
5
// Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
//
 
21
// $Id: joyclient.cxx,v 1.6 2001/07/22 17:51:18 curt Exp $
 
22
 
 
23
 
 
24
#include <simgear/debug/logstream.hxx>
 
25
#include <simgear/io/iochannel.hxx>
 
26
 
 
27
#include <Aircraft/aircraft.hxx>
 
28
 
 
29
#include "joyclient.hxx"
 
30
 
 
31
 
 
32
FGJoyClient::FGJoyClient() {
 
33
}
 
34
 
 
35
FGJoyClient::~FGJoyClient() {
 
36
}
 
37
 
 
38
 
 
39
// open hailing frequencies
 
40
bool FGJoyClient::open() {
 
41
    if ( is_enabled() ) {
 
42
        SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
 
43
                << "is already in use, ignoring" );
 
44
        return false;
 
45
    }
 
46
 
 
47
    SGIOChannel *io = get_io_channel();
 
48
 
 
49
    if ( ! io->open( get_direction() ) ) {
 
50
        SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
 
51
        return false;
 
52
    }
 
53
 
 
54
    set_enabled( true );
 
55
 
 
56
    return true;
 
57
}
 
58
 
 
59
 
 
60
// process work for this port
 
61
bool FGJoyClient::process() {
 
62
    SGIOChannel *io = get_io_channel();
 
63
    int length = sizeof(int[2]);
 
64
 
 
65
    if ( get_direction() == SG_IO_OUT ) {
 
66
        SG_LOG( SG_IO, SG_ALERT, "joyclient protocol is read only" );
 
67
        return false;
 
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." );
 
73
                int *msg;
 
74
                msg = (int *)buf;
 
75
                SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
 
76
                        << msg[1] );
 
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 ) {
 
80
                    aileron = 0.0;
 
81
                }
 
82
                if ( fabs(elevator) < 0.05 ) {
 
83
                    elevator = 0.0;
 
84
                }
 
85
                globals->get_controls()->set_aileron( aileron );
 
86
                globals->get_controls()->set_elevator( -elevator );
 
87
            }
 
88
        } else {
 
89
            while ( io->read( (char *)(& buf), length ) == length ) {
 
90
                SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
 
91
                int *msg;
 
92
                msg = (int *)buf;
 
93
                SG_LOG( SG_IO, SG_DEBUG, "X = " << msg[0] << " Y = "
 
94
                        << msg[1] );
 
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 ) {
 
98
                    aileron = 0.0;
 
99
                }
 
100
                if ( fabs(elevator) < 0.05 ) {
 
101
                    elevator = 0.0;
 
102
                }
 
103
                globals->get_controls()->set_aileron( aileron );
 
104
                globals->get_controls()->set_elevator( -elevator );
 
105
            }
 
106
        }
 
107
    }
 
108
 
 
109
    return true;
 
110
}
 
111
 
 
112
 
 
113
// close the channel
 
114
bool FGJoyClient::close() {
 
115
    SGIOChannel *io = get_io_channel();
 
116
 
 
117
    set_enabled( false );
 
118
 
 
119
    if ( ! io->close() ) {
 
120
        return false;
 
121
    }
 
122
 
 
123
    return true;
 
124
}