15 #define MESSAGE_PREFIX "IJoypadController:"
16 #define myInfo() yInfo() << MESSAGE_PREFIX
17 #define myError() yError() << MESSAGE_PREFIX
18 #define myDebug() yDebug() << MESSAGE_PREFIX
19 #define myWarning() yWarning() << MESSAGE_PREFIX
20 #define buttActionGroupName "BUTTON_EXECUTE"
22 #define JoyData yarp::dev::IJoypadEvent::joyData
26 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
31 EventDrivenEnabled(false)
36 bool isEqual(
const float& a,
const float& b,
const float& tolerance)
38 return fabs(a - b) < tolerance;
41 bool isEqual(
const double& a,
const double& b,
const double& tolerance)
43 return fabs(a - b) < tolerance;
48 if (a.
size() != b.
size())
return false;
50 for (
size_t i = 0; i < a.
size(); i++)
52 if (fabs(a[i] - b[i]) > tolerance)
63 if(getRawButtonCount(count) && count)
66 for(
unsigned int i = 0; i < count; i++)
68 getRawButton(i, value);
69 old_buttons.push_back(value);
73 if(getRawAxisCount(count) && count)
76 for(
unsigned int i = 0; i < count; i++)
79 old_axes.push_back(value);
83 if(getRawHatCount(count) && count)
85 unsigned char value = 0;
86 for(
unsigned int i = 0; i < count; i++)
89 old_hats.push_back(value);
93 if(getRawTrackballCount(count) && count)
96 for(
unsigned int i = 0; i < count; i++)
98 getRawTrackball(i, value);
99 old_trackballs.push_back(value);
103 if(getRawStickCount(count) && count)
106 for(
unsigned int i = 0; i < count; i++)
109 old_sticks.push_back(value);
114 if(getRawTouchSurfaceCount(count) && count)
117 for(
unsigned int i = 0; i < count; i++)
119 getRawTouch(i, value);
120 old_touches.push_back(value);
128 bool perform =
false;
135 std::vector<JoyData<float> > buttons;
136 std::vector<JoyData<double> > axes;
137 std::vector<JoyData<unsigned char> > hats;
138 std::vector<JoyData<yarp::sig::Vector> > trackBalls;
139 std::vector<JoyData<yarp::sig::Vector> > sticks;
140 std::vector<JoyData<yarp::sig::Vector> > Touch;
142 if(getRawButtonCount(count) && count)
145 for(
unsigned int i = 0; i < count; i++)
147 getRawButton(i, value);
148 if(!
isEqual(value, old_buttons[i], 0.00001f))
151 buttons.emplace_back(i, value);
152 old_buttons[i] = value;
157 if(getRawAxisCount(count) && count)
160 for(
unsigned int i = 0; i < count; i++)
162 getRawAxis(i, value);
163 if(!
isEqual(value, old_axes[i], 0.00001))
166 axes.emplace_back(i, value);
172 if(getRawHatCount(count) && count)
174 unsigned char value = 0;
175 for(
unsigned int i = 0; i < count; i++)
178 if(value != old_hats[i])
181 hats.emplace_back(i, value);
187 if(getRawTrackballCount(count) && count)
190 for(
unsigned int i = 0; i < count; i++)
192 getRawTrackball(i, value);
193 if(!
isEqual(value, old_trackballs[i], 0.00001))
196 trackBalls.emplace_back(i, value);
197 old_trackballs[i] = value;
202 if(getRawStickCount(count) && count)
205 for(
unsigned int i = 0; i < count; i++)
208 if(!
isEqual(value, old_sticks[i], 0.00001))
211 sticks.emplace_back(i, value);
212 old_sticks[i] = value;
217 if(getRawTouchSurfaceCount(count) && count)
220 for(
unsigned int i = 0; i < count; i++)
222 getRawTouch(i, value);
223 if(!
isEqual(value, old_touches[i], 0.00001))
226 Touch.emplace_back(i, value);
227 old_touches[i] = value;
234 m_event->action(buttons, axes, hats, trackBalls, sticks, Touch);
250 EventDrivenEnabled =
true;
259 yError() <<
"IJoypadController: event thread is already running";
265 EventDrivenEnabled =
true;
271 yError() <<
"IJoypadController: you must provide a valid event to start the event thread";
280 EventDrivenEnabled =
false;
289 return getRawAxisCount(axis_count);
294 return getRawButtonCount(button_count);
299 return getRawTrackballCount(Trackball_count);
304 return getRawHatCount(Hat_count);
309 return getRawTouchSurfaceCount(touch_count);
314 return getRawStickCount(stick_count);
319 if (EventDrivenEnabled)
321 yError() <<
"EventDriven is enable.. you can't poll the joypad state";
324 return getRawStickDoF(stick_id, DoF);
329 if (EventDrivenEnabled)
331 yError() <<
"EventDriven is enable.. you can't poll the joypad state";
334 return getRawButton(button_id, value);
339 if (EventDrivenEnabled)
341 yError() <<
"EventDriven is enable.. you can't poll the joypad state";
345 return getRawTrackball(trackball_id, value);
350 if (EventDrivenEnabled)
352 yError() <<
"EventDriven is enable.. you can't poll the joypad state";
356 return getRawHat(hat_id, value);
361 if (EventDrivenEnabled)
363 yError() <<
"EventDriven is enable.. you can't poll the joypad state";
367 return getRawAxis(axis_id, value);
372 if (EventDrivenEnabled)
374 yError() <<
"EventDriven is enable.. you can't poll the joypad state";
378 return getRawStick(stick_id, value, coordinate_mode);
383 if (EventDrivenEnabled)
385 yError() <<
"EventDriven is enable.. you can't poll the joypad state";
389 return getRawTouch(touch_id, value);
394 if (m_actions.find(action_id) != m_actions.end())
396 myInfo() <<
"executing script" << action_id <<
":" << m_actions[action_id];
397 system(m_actions[action_id].c_str());
401 myWarning() <<
"no scripts associated to button" << action_id;
411 int& actCount = count ? *count : dummy;
420 if(!actionsGroup.
size())
427 for(i = 1; i < actionsGroup.
size(); i++)
431 yDebug() <<
"error parsing cfg";
437 unsigned int buttonCount;
438 if(!this->getButtonCount(buttonCount))
440 myError() <<
"unable to get button count while parsing the actions";
446 (
unsigned int) keyvalue.
get(0).
asInt32() > buttonCount-1 ||
449 myError() <<
"Button's actions parameters must be in the format 'unsigned int string' and the button id must be in range";
458 myInfo() << actCount <<
"action parsed successfully";