Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef __CX_JOYSTICK_H
00041 #define __CX_JOYSTICK_H
00042
00043 #ifdef WIN32
00044 #pragma warning(disable : 4251) // Disables an annoying template DLL warning.
00045 #endif
00046
00047 #include <limits.h>
00048 #include <vector>
00049 #include <string>
00050
00051 #include "thread.h"
00052 #include "mutex.h"
00053 #include "timer.h"
00054 #include "cxutils/math/cxmath.h"
00055
00056 namespace CxUtils
00057 {
00064 class CX_UTILS_DLL Joystick
00065 {
00066 public:
00067 static const short int MinAxisValue = -32767;
00068 static const short int MaxAxisValue = 32767;
00069 static const short int MaxAxis = 9;
00070 static const unsigned int MaxButtons = 32;
00071 static const unsigned short int PovOff = 65535;
00072 static const unsigned int AnyJoystick = UINT_MAX;
00073
00074
00075
00076
00077
00078
00079 enum Axes
00080 {
00081 X = 0,
00082 Y,
00083 Z,
00084 R,
00085 U,
00086 V,
00087 POV,
00088 PovX,
00089 PovY
00090 };
00097 enum Event
00098 {
00099 None = 0,
00100 ButtonDown,
00101 ButtonUp
00102 };
00110 class CX_UTILS_DLL Callback
00111 {
00112 public:
00120 class CX_UTILS_DLL Data
00121 {
00122 public:
00123 typedef std::vector<Data> List;
00124 Data() : mpJoyCallback(0), mpJoyFunc(0), mpJoyButtonFunc(0), mpJoyFuncArgs(0)
00125 {
00126 }
00127 Data(const Data& data) : mpJoyCallback(0), mpJoyFunc(0), mpJoyFuncArgs(0)
00128 {
00129 *this = data;
00130 }
00131 ~Data() {}
00132 Data& operator=(const Data& data)
00133 {
00134 mpJoyCallback = data.mpJoyCallback;
00135 mpJoyFunc = data.mpJoyFunc;
00136 mpJoyButtonFunc = data.mpJoyButtonFunc;
00137 mpJoyFuncArgs = data.mpJoyFuncArgs;
00138 return *this;
00139 }
00140 Joystick::Callback* mpJoyCallback;
00141 void (*mpJoyFunc)(const Joystick& jstick, void* args);
00142 void (*mpJoyButtonFunc)(const unsigned int buttonNumber, const Joystick::Event type, void* args);
00143 void *mpJoyFuncArgs;
00144 };
00145 virtual void ProcessJoystickEvent(const Joystick& jstick) = 0;
00146 virtual void ProcessButtonEvent(const unsigned int buttonNumber, const Joystick::Event type) {}
00147 };
00148 Joystick();
00149 ~Joystick();
00150 int Initialize(const unsigned int num = AnyJoystick,
00151 const unsigned int freq = 25,
00152 const std::string& calibrationFile = std::string());
00153 int Shutdown();
00154 int GetAxis(const short int axis) const;
00155 int GetPOV() const;
00156 int GetButtonNumber() const;
00157 int GetID() const { return mID; }
00158 int RegisterCallback(Joystick::Callback* cb);
00159 int RegisterCallback(void (*func)(const Joystick& jstick, void* args), void* args);
00160 int RegisterCallback(void (*func)(const unsigned int buttonNumber, const Joystick::Event type, void* args),
00161 void* args);
00162 void ClearCallbacks();
00163 void PrintJoystickInfo() const;
00164 bool IsButtonPressed(const unsigned int num = 0) const;
00165 bool IsConnected() const;
00166 static short int GetAxisFromLetter(const char ch);
00167 static char GetLetterFromAxis(const short int axis);
00168 double GetAxisPercentage(const short int axis, const short int deadzone = 0) const;
00169 unsigned int GetUpdateFrequencey() const { return mUpdateFrequency; }
00170 protected:
00171 static void JoystickThreadFunction(void* args);
00172 #ifndef WIN32
00173 static void CallbackThreadFunction(void* args);
00174 Thread mCallbackThread;
00175 #endif
00176 Mutex mJoystickMutex;
00177 Mutex mJoyCallbackMutex;
00178 Thread mJoystickThread;
00179 Callback::Data::List mCallbacks;
00180 int mID;
00181 int mButtons;
00182 int *mpAxis;
00183 unsigned int mUpdateFrequency;
00184 bool mJoystickConnected;
00185 void* mpJoyInterface;
00186 };
00187
00188 typedef Joystick::Callback JoystickCallback;
00189 }
00190
00191 #endif
00192
00193