ARGoS 3
A parallel, multi-engine simulator for swarm robotics
qtopengl_joystick.cpp
Go to the documentation of this file.
1
10#include "qtopengl_joystick.h"
11#include <argos3/core/utility/configuration/argos_exception.h>
12#include <argos3/core/utility/logging/argos_log.h>
13
14namespace argos {
15
16 /****************************************/
17 /****************************************/
18
19 CQTOpenGLJoystick::CQTOpenGLJoystick(QObject *parent, int joystickEventTimeout, bool doAutoRepeat, int repeatDelay)
20 : QObject(parent)
21 {
22 if ( SDL_Init(SDL_INIT_JOYSTICK) == 0 ) {
23 int i;
24 for (i = 0; i < SDL_NumJoysticks(); i++) {
25 joystickNames.append(SDL_JoystickName(i));
26 LOG << "[INFO] Found joystick #" << i << ": \"" << SDL_JoystickName(i) << "\"" << std::endl;
27 }
28 connect(&joystickTimer, SIGNAL(timeout()), this, SLOT(processEvents()));
29 } else {
30 THROW_ARGOSEXCEPTION("QTOpenGLJoystick: couldn't initialize SDL joystick support");
31 }
32
33 joystick = NULL;
35 autoRepeat = doAutoRepeat;
36 autoRepeatDelay = repeatDelay;
37 eventTimeout = joystickEventTimeout;
38 }
39
40/****************************************/
41/****************************************/
42
44 {
45 if ( isOpen() )
46 close();
47
48 SDL_Quit();
49 }
50
51 /****************************************/
52 /****************************************/
53
55 {
56 if ( isOpen() )
57 close();
58
59 joystick = SDL_JoystickOpen(stick);
60 if ( joystick ) {
61 numAxes = SDL_JoystickNumAxes(joystick);
62 numButtons = SDL_JoystickNumButtons(joystick);
63 numHats = SDL_JoystickNumHats(joystick);
64 numTrackballs = SDL_JoystickNumBalls(joystick);
66 } else {
67 THROW_ARGOSEXCEPTION("QTOpenGLJoystick: couldn't open SDL joystick #%d" << stick);
68 }
69 }
70
71 /****************************************/
72 /****************************************/
73
75 {
76 joystickTimer.stop();
77 if ( joystick )
78 SDL_JoystickClose(joystick);
79 joystick = NULL;
81 }
82
83 /****************************************/
84 /****************************************/
85
87 {
88 if ( !isOpen() )
89 return;
90
91 SDL_JoystickUpdate();
92
93 int i;
94 for (i = 0; i < numAxes; i++) {
95 Sint16 moved = SDL_JoystickGetAxis(joystick, i);
96 if ( abs(moved) >= deadzones[i] ) {
97 if ( (moved != axes[i]) ) {
98 int deltaMoved = abs(axes[i] - moved);
99 if ( deltaMoved >= sensitivities[i] )
100 emit axisValueChanged(i, moved);
101 axes[i] = moved;
102 axisRepeatTimers[i].restart();
103 } else if (autoRepeat && moved != 0) {
104 if ( axisRepeatTimers[i].elapsed() >= autoRepeatDelay ) {
105 emit axisValueChanged(i, moved);
106 axes[i] = moved;
107 }
108 } else
109 axisRepeatTimers[i].restart();
110 } else
111 emit axisValueChanged(i, 0);
112 }
113 for (i = 0; i < numButtons; i++) {
114 Uint8 changed = SDL_JoystickGetButton(joystick, i);
115 if ( (changed != buttons[i]) ) {
116 emit buttonValueChanged(i, (bool) changed);
117 buttons[i] = changed;
118 buttonRepeatTimers[i].restart();
119 } else if (autoRepeat && changed != 0) {
120 if ( buttonRepeatTimers[i].elapsed() >= autoRepeatDelay ) {
121 emit buttonValueChanged(i, (bool) changed);
122 buttons[i] = changed;
123 }
124 } else
125 buttonRepeatTimers[i].restart();
126 }
127 for (i = 0; i < numHats; i++) {
128 Uint8 changed = SDL_JoystickGetHat(joystick, i);
129 if ( (changed != hats[i]) ) {
130 emit hatValueChanged(i, changed);
131 hats[i] = changed;
132 hatRepeatTimers[i].restart();
133 } else if (autoRepeat && changed != 0) {
134 if ( hatRepeatTimers[i].elapsed() >= autoRepeatDelay ) {
135 emit hatValueChanged(i, changed);
136 hats[i] = changed;
137 }
138 } else
139 hatRepeatTimers[i].restart();
140 }
141
142 for (i = 0; i < numTrackballs; i++) {
143 int dx, dy;
144 SDL_JoystickGetBall(joystick, i, &dx, &dy);
145 if ( dx != 0 || dy != 0 )
146 emit trackballValueChanged(i, dx, dy);
147 }
148 }
149
150 /****************************************/
151 /****************************************/
152
154 {
155 if ( isOpen() ) {
156 SDL_JoystickUpdate();
157 return SDL_JoystickGetAxis(joystick, axis);
158 } else
159 return 0;
160 }
161
162 /****************************************/
163 /****************************************/
164
165}
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
CARGoSLog LOG(std::cout, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_GREEN))
Definition argos_log.h:179
void hatValueChanged(int hat, int value)
void buttonValueChanged(int button, bool value)
CQTOpenGLJoystick(QObject *parent=0, int joystickEventTimeout=SDL_JOYSTICK_DEFAULT_EVENT_TIMEOUT, bool doAutoRepeat=TRUE, int autoRepeatDelay=SDL_JOYSTICK_DEFAULT_AUTOREPEAT_DELAY)
void trackballValueChanged(int trackball, int deltaX, int deltaY)
void axisValueChanged(int axis, int value)
QMap< int, int > sensitivities