TwiceAsNice
2019-02-18
|
The JoyStickDriver class provides basic functionality to read events from supported game pads under Linux. More...
#include <joystickdriver.h>
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_state * | joystick_st |
__u32 | version |
__u8 | axes |
__u8 | buttons |
char | name [256] |
char | dev_path [256] |
int | pollMS |
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)
typedef std::function<void(int axis_n, double value)> JoyStickDriver::axisFunc |
typedef std::function<void(int button_n, int value)> JoyStickDriver::buttonFunc |
typedef std::function<void(int joystick_n, double mag, double angle)> JoyStickDriver::joystickFunc |
JoyStickDriver::JoyStickDriver | ( | ) |
JoyStickDriver::~JoyStickDriver | ( | ) |
|
staticprotected |
|
staticprotected |
bool JoyStickDriver::buttonPressed | ( | int | n | ) |
bool JoyStickDriver::Connect | ( | ) |
bool JoyStickDriver::Disconnect | ( | ) |
const char * JoyStickDriver::getName | ( | ) |
__u8 JoyStickDriver::getNumOfAxes | ( | ) |
__u8 JoyStickDriver::getNumOfJoysticks | ( | ) |
__u8 JoyStickDriver::getNumrOfButtons | ( | ) |
__u32 JoyStickDriver::getVersion | ( | ) |
|
staticprotected |
joystick_position JoyStickDriver::joystickPosition | ( | int | n | ) |
|
staticprotected |
|
protected |
void JoyStickDriver::setAxisCallback | ( | axisFunc | axisCallback | ) |
void JoyStickDriver::setButtonCallback | ( | buttonFunc | buttonCallback | ) |
void JoyStickDriver::setJoystickCallback | ( | joystickFunc | joystickCallback | ) |
void JoyStickDriver::setPoll | ( | int | ms | ) |
void JoyStickDriver::setPort | ( | const char * | port | ) |
|
private |
|
private |
|
protected |
|
protected |
|
private |
|
private |
|
private |
|
private |
|
private |
|
protected |
|
private |
|
private |
|
private |
|
private |