23 #include "acquisition_thread.h" 24 #include "force_feedback.h" 26 #include <core/threading/mutex.h> 27 #include <core/exceptions/system.h> 30 #include <linux/joystick.h> 32 #include <sys/types.h> 50 :
Thread(
"JoystickAcquisitionThread",
Thread::OPMODE_CONTINUOUS)
77 __bbhandler = handler;
90 e.
append(
"Could not read all required config values for %s",
name());
94 init(__cfg_device_file);
99 JoystickAcquisitionThread::open_joystick()
101 __fd = open(__cfg_device_file.c_str(), O_RDONLY);
104 "Opening the joystick device file failed");
107 if ( ioctl(__fd, JSIOCGNAME(
sizeof(__joystick_name)), __joystick_name) < 0) {
108 throw Exception(errno,
"Failed to get name of joystick");
110 if ( ioctl(__fd, JSIOCGAXES, &__num_axes) < 0 ) {
111 throw Exception(errno,
"Failed to get number of axes for joystick");
113 if ( ioctl(__fd, JSIOCGBUTTONS, &__num_buttons) < 0 ) {
114 throw Exception(errno,
"Failed to get number of buttons for joystick");
117 if (__axis_values == NULL) {
120 __axis_array_size = std::max((
int)__num_axes, 8);
121 __axis_values = (
float *)malloc(
sizeof(
float) * __axis_array_size);
122 }
else if ( __num_axes > std::max((
int)__axis_array_size, 8) ) {
124 __num_axes = __axis_array_size;
133 memset(__axis_values, 0,
sizeof(
float) * __axis_array_size);
134 __pressed_buttons = 0;
143 JoystickAcquisitionThread::open_forcefeedback()
169 __cfg_device_file = device_file;
172 open_forcefeedback();
177 __data_mutex =
new Mutex();
184 if ( __fd >= 0 ) close(__fd);
196 if ( read(__fd, &e,
sizeof(
struct js_event)) < (
int)
sizeof(
struct js_event) ) {
207 __data_mutex->
lock();
210 if ((e.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) {
212 if (e.number <= 32) {
214 __pressed_buttons |= (1 << e.number);
216 __pressed_buttons &= ~(1 << e.number);
221 }
else if ((e.type & ~JS_EVENT_INIT) == JS_EVENT_AXIS) {
222 if ( e.number >= __axis_array_size ) {
224 "Got value for axis %u, but only %u axes registered. " 225 "Plugged in a different joystick? Ignoring.",
226 e.number + 1 , __axis_array_size);
231 __axis_values[e.number] = (e.value == 0) ? 0. : (e.value / -32767.f);
249 open_forcefeedback();
269 __data_mutex->
lock();
304 return __num_buttons;
314 return __joystick_name;
324 return __pressed_buttons;
334 return __axis_values;
File could not be opened.
JoystickAcquisitionThread()
Constructor.
virtual void loop()
Code to execute in the thread.
bool can_ramp()
Check if ramp effect is supported.
float * axis_values()
Get values for the axes.
Fawkes library namespace.
void unlock()
Unlock the mutex.
bool can_friction()
Check if friction effect is supported.
bool can_triangle()
Check if triangle effect is supported.
char num_buttons() const
Get number of buttons.
Handler class for joystick data.
bool can_square()
Check if square effect is supported.
Thread class encapsulation of pthreads.
bool lock_if_new_data()
Lock data if fresh.
void set_prepfin_conc_loop(bool concurrent=true)
Set concurrent execution of prepare_finalize() and loop().
Logger * logger
This is the Logger member used to access the logger.
bool can_constant()
Check if constant effect is supported.
bool can_periodic()
Check if periodic effect is supported.
bool can_custom()
Check if custom effect is supported.
virtual void joystick_changed(unsigned int pressed_buttons, float *axis_values)=0
Joystick data changed.
virtual void joystick_plugged(char num_axes, char num_buttons)=0
A (new) joystick has been plugged in.
Cause force feedback on a joystick.
unsigned int pressed_buttons() const
Pressed buttons.
Base class for exceptions in Fawkes.
virtual void finalize()
Finalize the thread.
bool can_sine()
Check if sine effect is supported.
operate in continuous mode (default)
bool can_damper()
Check if damper effect is supported.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
char num_axes() const
Get number of axes.
const char * name() const
Get name of thread.
bool can_saw_down()
Check if downward saw effect is supported.
bool can_spring()
Check if spring effect is supported.
const char * joystick_name() const
Get joystick name.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
void unlock()
Unlock data.
bool can_rumble()
Check if rumbling effect is supported.
virtual void joystick_unplugged()=0
The joystick has been unplugged and is no longer available.
void lock()
Lock this mutex.
Mutex mutual exclusion lock.
virtual void init()
Initialize the thread.
Configuration * config
This is the Configuration member used to access the configuration.
bool can_inertia()
Check if inertia effect is supported.
void append(const char *format,...)
Append messages to the message list.
bool can_saw_up()
Check if upward saw effect is supported.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.