Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Protected Attributes

CxUtils::Joystick Class Reference

Simple Joystick class for interfacing to a joystick. More...

#include <joystick.h>

List of all members.

Classes

class  Callback
 Simple Joystick Callback class which can be used to receive automatic joystick updates. More...

Public Types

enum  Axes {
  X = 0, Y, Z, R,
  U, V, POV, PovX,
  PovY
}
 

Enumerations for the different axes of a joystick/gamepad.

More...
enum  Event { None = 0, ButtonDown, ButtonUp }
 

Enumerations for the different types of Joystick events.

More...

Public Member Functions

 Joystick ()
 Constructor.
 ~Joystick ()
 Destructor.
int Initialize (const unsigned int num=AnyJoystick, const unsigned int freq=25, const std::string &calibrationFile=std::string())
 Initializes the joystick.
int Shutdown ()
 Shutsdown the joystick interface.
int GetAxis (const short int axis) const
 Gets the value of the joystick axis.
int GetPOV () const
 Gets the Point of View (POV) HAT values.
int GetButtonNumber () const
 Gets the number of the current button being pressed.
int GetID () const
int RegisterCallback (Joystick::Callback *cb)
 Register a callback to receive joystick updates on at a fixed frequency. This will clear any other callbacks, since only one may be registered at a time.
int RegisterCallback (void(*func)(const Joystick &jstick, void *args), void *args)
 Register a callback to receive joystick updates on at a fixed frequency. This will clear any other callbacks, since only one may be registered at a time.
int RegisterCallback (void(*func)(const unsigned int buttonNumber, const Joystick::Event type, void *args), void *args)
 Register a callback to receive joystick updates on at a fixed frequency. This will clear any other callbacks, since only one may be registered at a time.
void ClearCallbacks ()
 Clears callback for automatic updates from joystick.
void PrintJoystickInfo () const
 Prints joystick values to console.
bool IsButtonPressed (const unsigned int num=0) const
 Find out if a button is pressed or a specific button by number.
bool IsConnected () const
double GetAxisPercentage (const short int axis, const short int deadzone=0) const
 Gets the value of the joystick axis.
unsigned int GetUpdateFrequencey () const

Static Public Member Functions

static short int GetAxisFromLetter (const char ch)
 Converts the character letter represening the axis ('x', 'X') to a number identifier such as JOYSTICK_X_AXIS.
static char GetLetterFromAxis (const short int axis)
 Converts the axis number to a charcter.

Static Public Attributes

static const short int MinAxisValue = -32767
 Minimum joystick axis value.
static const short int MaxAxisValue = 32767
 Maximum joystick axis value.
static const short int MaxAxis = 9
 Total number of supported joystick axes.
static const unsigned int MaxButtons = 32
 Maximum number of joystick buttons.
static const unsigned short int PovOff = 65535
 POV value when POV centered.
static const unsigned int AnyJoystick = UINT_MAX
 Constant for selection of any joystick.

Static Protected Member Functions

static void JoystickThreadFunction (void *args)
 Function performed within a thread which continuously reads data from the joystick based on update frequency.
static void CallbackThreadFunction (void *args)
 Method that generates Joystick callbacks in Linux. This is done differently than the windows version of this code because reading from the joystick is a blocking function call.

Protected Attributes

Thread mCallbackThread
Mutex mJoystickMutex
 I/O protection.
Mutex mJoyCallbackMutex
 Mutex for callbacks.
Thread mJoystickThread
 Thread where data is read from joystick.
Callback::Data::List mCallbacks
 Callbacks to run when joystick updates.
int mID
 Joystick ID number.
int mButtons
 Button values.
int * mpAxis
 Axis data.
unsigned int mUpdateFrequency
 Update frequency.
bool mJoystickConnected
 Connection flag.
void * mpJoyInterface
 Interface to Joystick.

Detailed Description

Simple Joystick class for interfacing to a joystick.

Definition at line 64 of file joystick.h.


Member Enumeration Documentation

Enumerations for the different axes of a joystick/gamepad.

Enumerator:
X 
Y 
Z 
R 
U 
V 
POV 
PovX 
PovY 

Definition at line 79 of file joystick.h.

Enumerations for the different types of Joystick events.

Enumerator:
None 
ButtonDown 
ButtonUp 

Definition at line 97 of file joystick.h.


Constructor & Destructor Documentation

Joystick::Joystick (  )

Constructor.

Definition at line 123 of file joystick.cpp.

Joystick::~Joystick (  )

Destructor.

Definition at line 141 of file joystick.cpp.


Member Function Documentation

void Joystick::CallbackThreadFunction ( void *  args ) [static, protected]

Method that generates Joystick callbacks in Linux. This is done differently than the windows version of this code because reading from the joystick is a blocking function call.

Definition at line 993 of file joystick.cpp.

void Joystick::ClearCallbacks (  )

Clears callback for automatic updates from joystick.

Definition at line 541 of file joystick.cpp.

int Joystick::GetAxis ( const short int  axis ) const

Gets the value of the joystick axis.

Parameters:
axisThe axis number, see Axes enumeration.
Returns:
Joystick axis value.

Definition at line 360 of file joystick.cpp.

short int Joystick::GetAxisFromLetter ( const char  ch ) [static]

Converts the character letter represening the axis ('x', 'X') to a number identifier such as JOYSTICK_X_AXIS.

Parameters:
chThe character represening the axis. Values can be upper or lower case. ('x', 'y', 'z', etc.)
Returns:
The axis number. For example 'x' -> JOYSTICK_X_AXIS.

Definition at line 912 of file joystick.cpp.

double Joystick::GetAxisPercentage ( const short int  axis,
const short int  deadzone = 0 
) const

Gets the value of the joystick axis.

Parameters:
axisThe axis number, see Axes enumeration.
deadzoneJoystick deadzone percentage. [0,100].
Returns:
Joystick axis value as a percentage [-100,100].

Definition at line 622 of file joystick.cpp.

int Joystick::GetButtonNumber (  ) const

Gets the number of the current button being pressed.

Returns:
0 If not button pressed otherwise [1,32].

Definition at line 410 of file joystick.cpp.

int CxUtils::Joystick::GetID (  ) const [inline]

Definition at line 157 of file joystick.h.

char Joystick::GetLetterFromAxis ( const short int  axis ) [static]

Converts the axis number to a charcter.

Parameters:
axisThe axis number. X, Y, Z, R, U, V, etc.
Returns:
The letter representing the axis, 0 if unknown.

Definition at line 952 of file joystick.cpp.

int Joystick::GetPOV (  ) const

Gets the Point of View (POV) HAT values.

Returns:
Angles of the POV north is 0, east is 90, south is 180, and west is 270. A value of 65535 is when nothing pressed on POV.

Definition at line 386 of file joystick.cpp.

unsigned int CxUtils::Joystick::GetUpdateFrequencey (  ) const [inline]

Definition at line 169 of file joystick.h.

int Joystick::Initialize ( const unsigned int  num = AnyJoystick,
const unsigned int  freq = 25,
const std::string &  calibrationFile = std::string() 
)

Initializes the joystick.

Parameters:
numThe joystick ID number. If set to AnyJoystick (default) then function connects to first available joystick. [0,10].
freqFrequency for updates in Hz. [1,50].
calibrationFileUsed only in linux with jscalibrator. Specify the calibration file data to correct joystick values. (Optional Field).
Returns:
1 if connection made, otherwise 0.

Check for calibratrion data

there is a whole format, I'm just grabbing what I need

Definition at line 170 of file joystick.cpp.

bool Joystick::IsButtonPressed ( const unsigned int  num = 0 ) const

Find out if a button is pressed or a specific button by number.

Parameters:
numButton numberr [1,32], 0 for any button.
Returns:
True if the specific button or any button is pressed, otherwise false.

Definition at line 576 of file joystick.cpp.

bool Joystick::IsConnected (  ) const
Returns:
True if connected to joystick, otherwise false.

Definition at line 606 of file joystick.cpp.

void Joystick::JoystickThreadFunction ( void *  args ) [static, protected]

Function performed within a thread which continuously reads data from the joystick based on update frequency.

Definition at line 655 of file joystick.cpp.

void Joystick::PrintJoystickInfo (  ) const

Prints joystick values to console.

Definition at line 553 of file joystick.cpp.

int Joystick::RegisterCallback ( Joystick::Callback cb )

Register a callback to receive joystick updates on at a fixed frequency. This will clear any other callbacks, since only one may be registered at a time.

Parameters:
cbCallback pointer.
Returns:
1 on success, 0 on failure.

Definition at line 446 of file joystick.cpp.

int Joystick::RegisterCallback ( void(*)(const unsigned int buttonNumber, const Joystick::Event type, void *args)  func,
void *  args 
)

Register a callback to receive joystick updates on at a fixed frequency. This will clear any other callbacks, since only one may be registered at a time.

Parameters:
funcFunction pointer callback to use when buttons are pressed and released.
argsAdditional arugments for the function callback.
Returns:
1 on success, 0 on failure.

Definition at line 514 of file joystick.cpp.

int Joystick::RegisterCallback ( void(*)(const Joystick &jstick, void *args)  func,
void *  args 
)

Register a callback to receive joystick updates on at a fixed frequency. This will clear any other callbacks, since only one may be registered at a time.

Parameters:
funcFunction pointer callback to use when new joystick data is read.
argsAdditional arugments for the function callback.
Returns:
1 on success, 0 on failure.

Definition at line 480 of file joystick.cpp.

int Joystick::Shutdown (  )

Shutsdown the joystick interface.

Returns:
1.

Definition at line 324 of file joystick.cpp.


Member Data Documentation

const unsigned int CxUtils::Joystick::AnyJoystick = UINT_MAX [static]

Constant for selection of any joystick.

Definition at line 72 of file joystick.h.

const short int CxUtils::Joystick::MaxAxis = 9 [static]

Total number of supported joystick axes.

Definition at line 69 of file joystick.h.

const short int CxUtils::Joystick::MaxAxisValue = 32767 [static]

Maximum joystick axis value.

Definition at line 68 of file joystick.h.

const unsigned int CxUtils::Joystick::MaxButtons = 32 [static]

Maximum number of joystick buttons.

Definition at line 70 of file joystick.h.

int CxUtils::Joystick::mButtons [protected]

Button values.

Definition at line 181 of file joystick.h.

Callbacks to run when joystick updates.

Definition at line 179 of file joystick.h.

Definition at line 174 of file joystick.h.

int CxUtils::Joystick::mID [protected]

Joystick ID number.

Definition at line 180 of file joystick.h.

const short int CxUtils::Joystick::MinAxisValue = -32767 [static]

Minimum joystick axis value.

Definition at line 67 of file joystick.h.

Mutex for callbacks.

Definition at line 177 of file joystick.h.

Connection flag.

Definition at line 184 of file joystick.h.

I/O protection.

Definition at line 176 of file joystick.h.

Thread where data is read from joystick.

Definition at line 178 of file joystick.h.

int* CxUtils::Joystick::mpAxis [protected]

Axis data.

Definition at line 182 of file joystick.h.

Interface to Joystick.

Definition at line 185 of file joystick.h.

unsigned int CxUtils::Joystick::mUpdateFrequency [protected]

Update frequency.

Definition at line 183 of file joystick.h.

const unsigned short int CxUtils::Joystick::PovOff = 65535 [static]

POV value when POV centered.

Definition at line 71 of file joystick.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines