TwiceAsNice  2019-02-18
Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Attributes | List of all members
JoyStickDriver Class Reference

The JoyStickDriver class provides basic functionality to read events from supported game pads under Linux. More...

#include <joystickdriver.h>

Collaboration diagram for JoyStickDriver:
Collaboration graph

Public Types

typedef std::function< void(int joystick_n, double mag, double angle)> joystickFunc
 
typedef std::function< void(int axis_n, double value)> axisFunc
 
typedef std::function< void(int button_n, int value)> buttonFunc
 

Public Member Functions

 JoyStickDriver ()
 
 ~JoyStickDriver ()
 
bool Connect ()
 
bool Disconnect ()
 
void setPort (const char *port)
 
void setPoll (int ms)
 
const char * getName ()
 
__u32 getVersion ()
 
__u8 getNumOfJoysticks ()
 
__u8 getNumOfAxes ()
 
__u8 getNumrOfButtons ()
 
joystick_position joystickPosition (int n)
 
bool buttonPressed (int n)
 
void setJoystickCallback (joystickFunc joystickCallback)
 
void setAxisCallback (axisFunc axisCallback)
 
void setButtonCallback (buttonFunc buttonCallback)
 

Protected Member Functions

void readEv ()
 

Static Protected Member Functions

static void joystickEvent (int joystick_n, double mag, double angle)
 
static void axisEvent (int axis_n, int value)
 
static void buttonEvent (int button_n, int value)
 
static void * loop (void *obj)
 

Protected Attributes

joystickFunc joystickCallbackFunc
 
buttonFunc buttonCallbackFunc
 
axisFunc axisCallbackFunc
 

Private Attributes

pthread_t thread
 
bool active
 
int joystick_fd
 
js_event * joystick_ev
 
joystick_statejoystick_st
 
__u32 version
 
__u8 axes
 
__u8 buttons
 
char name [256]
 
char dev_path [256]
 
int pollMS
 

Detailed Description

The JoyStickDriver class provides basic functionality to read events from supported game pads under Linux.

It provides functions to read the button, axis, and joystick status and values. By definition, a joystick is the combination of two axis. A game pad may have one or more joysticks depending on the number of reported axis. You can utilize the class in an event driven fashion by using callbacks. The callbacks have a specific signature and must be set. Alternatively, you may query the status and position of the buttons & axis at any time as well.

Since the class runs a non-blocking thread, the thread goes to sleep when there are no events detected in order to reduce CPU utilization. The sleep period is set by default to 100 milliseconds and can be adjusted by the setPoll function.

Each joystick has a normalized magnitude [0 to 1] and an angle. The magnitude is 0 when the stick is not depressed, and 1 when depressed all the way. The angles are measured counter clock wise [0 to 360] with right/east direction being zero.

The axis value is reported in raw values [-32767.0 to 32767.0].

The buttons are either off (0) or on (1)

Member Typedef Documentation

◆ axisFunc

typedef std::function<void(int axis_n, double value)> JoyStickDriver::axisFunc

◆ buttonFunc

typedef std::function<void(int button_n, int value)> JoyStickDriver::buttonFunc

◆ joystickFunc

typedef std::function<void(int joystick_n, double mag, double angle)> JoyStickDriver::joystickFunc

Constructor & Destructor Documentation

◆ JoyStickDriver()

JoyStickDriver::JoyStickDriver ( )

◆ ~JoyStickDriver()

JoyStickDriver::~JoyStickDriver ( )

Member Function Documentation

◆ axisEvent()

void JoyStickDriver::axisEvent ( int  axis_n,
int  value 
)
staticprotected

◆ buttonEvent()

void JoyStickDriver::buttonEvent ( int  button_n,
int  value 
)
staticprotected

◆ buttonPressed()

bool JoyStickDriver::buttonPressed ( int  n)

◆ Connect()

bool JoyStickDriver::Connect ( )

◆ Disconnect()

bool JoyStickDriver::Disconnect ( )

◆ getName()

const char * JoyStickDriver::getName ( )

◆ getNumOfAxes()

__u8 JoyStickDriver::getNumOfAxes ( )

◆ getNumOfJoysticks()

__u8 JoyStickDriver::getNumOfJoysticks ( )

◆ getNumrOfButtons()

__u8 JoyStickDriver::getNumrOfButtons ( )

◆ getVersion()

__u32 JoyStickDriver::getVersion ( )

◆ joystickEvent()

void JoyStickDriver::joystickEvent ( int  joystick_n,
double  mag,
double  angle 
)
staticprotected

◆ joystickPosition()

joystick_position JoyStickDriver::joystickPosition ( int  n)

◆ loop()

void * JoyStickDriver::loop ( void *  obj)
staticprotected

◆ readEv()

void JoyStickDriver::readEv ( )
protected

◆ setAxisCallback()

void JoyStickDriver::setAxisCallback ( axisFunc  axisCallback)

◆ setButtonCallback()

void JoyStickDriver::setButtonCallback ( buttonFunc  buttonCallback)

◆ setJoystickCallback()

void JoyStickDriver::setJoystickCallback ( joystickFunc  joystickCallback)

◆ setPoll()

void JoyStickDriver::setPoll ( int  ms)

◆ setPort()

void JoyStickDriver::setPort ( const char *  port)

Member Data Documentation

◆ active

bool JoyStickDriver::active
private

◆ axes

__u8 JoyStickDriver::axes
private

◆ axisCallbackFunc

axisFunc JoyStickDriver::axisCallbackFunc
protected

◆ buttonCallbackFunc

buttonFunc JoyStickDriver::buttonCallbackFunc
protected

◆ buttons

__u8 JoyStickDriver::buttons
private

◆ dev_path

char JoyStickDriver::dev_path[256]
private

◆ joystick_ev

js_event* JoyStickDriver::joystick_ev
private

◆ joystick_fd

int JoyStickDriver::joystick_fd
private

◆ joystick_st

joystick_state* JoyStickDriver::joystick_st
private

◆ joystickCallbackFunc

joystickFunc JoyStickDriver::joystickCallbackFunc
protected

◆ name

char JoyStickDriver::name[256]
private

◆ pollMS

int JoyStickDriver::pollMS
private

◆ thread

pthread_t JoyStickDriver::thread
private

◆ version

__u32 JoyStickDriver::version
private

The documentation for this class was generated from the following files: