YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
JoypadControl_nwc_yarp.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
8#include <yarp/os/LogStream.h>
9#include <yarp/os/Network.h>
10#include <tuple>
11
12using namespace yarp::dev;
13using namespace yarp::sig;
14using namespace yarp::os;
15
16namespace {
17YARP_LOG_COMPONENT(JOYPADCONTROLCLIENT, "yarp.device.JoypadControl_nwc_yarp")
18}
19
23
24bool JoypadControl_nwc_yarp::getJoypadInfo()
25{
26 bool v= true;
33
34 return v;
35}
36
38{
39 if (!parseParams(config)) { return false; }
40
41 //open the rpc port
42 if(!m_rpcPort.open(m_local + "/rpc:o"))
43 {
44 yCError(JOYPADCONTROLCLIENT) << "Unable to open rpc port.";
45 return false;
46 }
47 yCInfo(JOYPADCONTROLCLIENT) << "rpc port opened. starting the handshake";
48
49 //connection for the rpc port
50 if(!yarp::os::NetworkBase::connect(m_local + "/rpc:o", m_remote + "/rpc:i"))
51 {
52 yCError(JOYPADCONTROLCLIENT) << "Handshake failed. unable to connect to remote port" << m_remote + "/rpc:i";
53 return false;
54 }
55
56 if (!m_rpcMsgs.yarp().attachAsClient(m_rpcPort))
57 {
58 yCError(JOYPADCONTROLCLIENT, "Error! Cannot attach the port as a client");
59 return false;
60 }
61
62 // Check the protocol version
63 if (!m_rpcMsgs.checkProtocolVersion()) {
64 return false;
65 }
66
67 //Get basic infos from the joypad
68 if(!getJoypadInfo())
69 {
70 yCError(JOYPADCONTROLCLIENT) << "Unable to get joypad info.";
71 return false;
72 }
73
74 if (m_use_streaming==false) {return true;}
75
76 //The code below is valid only for streaming mode
78 {
79 bool b = true;
80 b &= m_allJoyDataPort.open(m_local + "/allJoyData:i");
81 if (!b) {
82 yCError(JOYPADCONTROLCLIENT) << "Unable to open local streaming ports";
83 close();
84 return false;
85 }
86
87 b&= yarp::os::NetworkBase::connect(m_remote+"/allJoyData:o", m_local+"/allJoyData:i");
88 if (!b) {
89 yCError(JOYPADCONTROLCLIENT) << "Unable to connect to remote streaming port";
90 close();
91 return false;
92 }
93
94 m_allJoyDataPort.useCallback();
95 }
96 else
97 {
98 bool b = true;
99 b&= m_buttonsPort.open(m_local+"/buttons:i");
100 b&= m_axisPort.open(m_local+"/axis:i");
101 b&= m_trackballPort.open(m_local+"/trackball:i");
102 b&= m_touchPort.open(m_local+"/touch:i");
103 b&= m_hatsPort.open(m_local+"/hat:i");
104 b&= m_stickPort.open(m_local+"/stick:i");
105 if (!b) {
106 yCError(JOYPADCONTROLCLIENT) << "Unable to open local streaming ports";
107 close();
108 return false;
109 }
110
111 b&= yarp::os::NetworkBase::connect(m_remote+"/buttons:o",m_local+"/buttons:i");
112 b&= yarp::os::NetworkBase::connect(m_remote+"/axis:o",m_local+"/axis:i");
113 b&= yarp::os::NetworkBase::connect(m_remote+"/trackball:o",m_local+"/trackball:i");
114 b&= yarp::os::NetworkBase::connect(m_remote+"/touch:o",m_local+"/touch:i");
115 b&= yarp::os::NetworkBase::connect(m_remote+"/hat:o",m_local+"/hat:i");
116 b&= yarp::os::NetworkBase::connect(m_remote+"/stick:o",m_local+"/stick:i");
117 if (!b) {
118 yCError(JOYPADCONTROLCLIENT) << "Unable to connect to remote streaming port";
119 close();
120 return false;
121 }
122
123 m_buttonsPort.useCallback();
124 m_axisPort.useCallback();
125 m_trackballPort.useCallback();
126 m_touchPort.useCallback();
127 m_hatsPort.useCallback();
128 m_stickPort.useCallback();
129 }
130
131 return true;
132}
133
134
136{
137 auto ret = m_rpcMsgs.getButtonCount();
138 button_count = ret.button_count;
139 return ret.ret;
140}
141
143{
144 auto ret = m_rpcMsgs.getAxisCount();
145 axis_count = ret.axis_count;
146 return ret.ret;
147}
148
150{
151 auto ret = m_rpcMsgs.getTrackballCount();
152 Trackball_count = ret.Trackball_count;
153 return ret.ret;
154}
155
157{
158 auto ret = m_rpcMsgs.getHatCount();
159 Hat_count = ret.Hat_count;
160 return ret.ret;
161}
162
164{
165 auto ret = m_rpcMsgs.getTouchSurfaceCount();
166 touch_count = ret.touch_count;
167 return ret.ret;
168}
169
171{
172 auto ret = m_rpcMsgs.getStickCount();
173 stick_count = ret.stick_count;
174 return ret.ret;
175}
176
177ReturnValue JoypadControl_nwc_yarp::getButton(size_t button_id, double& value)
178{
179 if(!m_use_streaming)
180 {
181 auto ret = m_rpcMsgs.getButton(button_id);
182 value = ret.value;
183 return ret.ret;
184 }
185 else if (m_use_all_port)
186 {
187 auto tmp = m_allJoyDataPort.getData();
188 if(button_id < tmp.ButtonDataVal.size())
189 {
190 value = tmp.ButtonDataVal[button_id];
191 return ReturnValue_ok;
192 }
193 else
194 {
195 yCError(JOYPADCONTROLCLIENT) << "GetButton() error. button_id out of bound";
197 }
198 }
199 else
200 {
201 auto tmp = m_buttonsPort.getData();
202 if(button_id < tmp.value.size())
203 {
204 value = tmp.value[button_id];
205 return ReturnValue_ok;
206 }
207 else
208 {
209 yCError(JOYPADCONTROLCLIENT) << "GetButton() error. button_id out of bound";
211 }
212 }
213}
214
216{
217 if(!m_use_streaming)
218 {
219 auto ret = m_rpcMsgs.getTrackball(trackball_id);
220 value = ret.value;
221 return ret.ret;
222 }
223 else if (m_use_all_port)
224 {
225 auto tmp = m_allJoyDataPort.getData();
226 if(trackball_id < tmp.TrackballDataVal.size())
227 {
228 value = tmp.TrackballDataVal[trackball_id];
229 return ReturnValue_ok;
230 }
231 else
232 {
233 yCError(JOYPADCONTROLCLIENT) << "GetTrackball() error. trackball_id out of bound";
235 }
236 }
237 else
238 {
239 auto tmp = m_trackballPort.getData();
240 if(trackball_id < tmp.value.size())
241 {
242 value = tmp.value[trackball_id];
243 return ReturnValue_ok;
244 }
245 else
246 {
248 }
249 }
250}
251
252ReturnValue JoypadControl_nwc_yarp::getHat(size_t hat_id, unsigned char& value)
253{
254 if(!m_use_streaming)
255 {
256 auto ret = m_rpcMsgs.getHat(hat_id);
257 value = ret.value;
258 return ret.ret;
259 }
260 else if (m_use_all_port)
261 {
262 auto tmp = m_allJoyDataPort.getData();
263 if(hat_id < tmp.HatsDataVal.size())
264 {
265 value = tmp.HatsDataVal[hat_id];
266 return ReturnValue_ok;
267 }
268 else
269 {
270 yCError(JOYPADCONTROLCLIENT) << "GetHat() error. hat_id out of bound";
272 }
273 }
274 else
275 {
276 auto tmp = m_hatsPort.getData();
277 if (hat_id < tmp.value.size())
278 {
279 value = tmp.value[hat_id];
280 return ReturnValue_ok;
281 }
282 else
283 {
285 }
286 }
287}
288
290{
291 if(!m_use_streaming)
292 {
293 auto ret = m_rpcMsgs.getAllAxes();
294 value = ret.value;
295 return ret.ret;
296 }
297 else
298 {
299 if (m_use_all_port)
300 {
301 auto tmp = m_allJoyDataPort.getData();
302 value = tmp.AxisDataVal;
303 return ReturnValue_ok;
304 }
305 else
306 {
307 auto tmp = m_axisPort.getData();
308 value = tmp.value;
309 return ReturnValue_ok;
310 }
311 }
312}
313
314ReturnValue JoypadControl_nwc_yarp::getAxis(size_t axis_id, double& value)
315{
316 if(!m_use_streaming)
317 {
318 auto ret = m_rpcMsgs.getAxis(axis_id);
319 value = ret.value;
320 return ret.ret;
321 }
322 else
323 {
324 auto tmp = m_axisPort.getData();
325 if(axis_id < tmp.value.size())
326 {
327 value = tmp.value[axis_id];
328 return ReturnValue_ok;
329 }
330 else if (m_use_all_port)
331 {
332 auto tmp = m_allJoyDataPort.getData();
333 if(axis_id < tmp.AxisDataVal.size())
334 {
335 value = tmp.AxisDataVal[axis_id];
336 return ReturnValue_ok;
337 }
338 else
339 {
340 yCError(JOYPADCONTROLCLIENT) << "GetAxis() error. axis_id out of bound";
342 }
343 }
344 else
345 {
346 yCError(JOYPADCONTROLCLIENT) << "GetAxis() error. Axis_id out of bound";
348 }
349 }
350}
351
353{
354 if(!m_use_streaming)
355 {
356 auto ret = m_rpcMsgs.getStick(stick_id,mode);
357 value = ret.value;
358 return ret.ret;
359 }
360 else if (m_use_all_port)
361 {
362 //Only the polar mode is available in streaming.
363 //If cartesian mode is requested, switch back to rpc.
365 {
366 auto ret = m_rpcMsgs.getStick(stick_id,mode);
367 value = ret.value;
368 return ret.ret;
369 }
370 auto tmp = m_allJoyDataPort.getData();
371 if(stick_id < tmp.StickDataVal.size())
372 {
373 value = tmp.StickDataVal[stick_id];
374 return ReturnValue_ok;
375 }
376 else
377 {
378 yCError(JOYPADCONTROLCLIENT) << "getStick() error. stick_id out of bound";
380 }
381 }
382 else
383 {
384 //Only the polar mode is available in streaming.
385 //If cartesian mode is requested, switch back to rpc.
387 {
388 auto ret = m_rpcMsgs.getStick(stick_id,mode);
389 value = ret.value;
390 return ret.ret;
391 }
392 auto tmp = m_stickPort.getData();
393 if(stick_id < tmp.value.size())
394 {
395 value = tmp.value[stick_id];
396 return ReturnValue_ok;
397 }
398 else
399 {
400 yCError(JOYPADCONTROLCLIENT) << "GetAxis() error. stick_id out of bound";
402 }
403
404 return ReturnValue_ok;
405 }
406}
407
408ReturnValue JoypadControl_nwc_yarp::getTouch(size_t touch_id, std::vector<yarp::dev::TouchData>& value)
409{
410 value.clear();
411 if(!m_use_streaming)
412 {
413 auto ret = m_rpcMsgs.getTouch(touch_id);
414 value = ret.value;
415 return ret.ret;
416 }
417 else if (m_use_all_port)
418 {
419 auto tmp = m_allJoyDataPort.getData();
420 if(touch_id < tmp.TouchDataVal.size())
421 {
422 value = tmp.TouchDataVal[touch_id].touches;
423 return ReturnValue_ok;
424 }
425 else
426 {
427 yCError(JOYPADCONTROLCLIENT) << "GetTouch() error. touch_id out of bound";
429 }
430 }
431 else
432 {
433 auto temp = m_touchPort.getData();
434 if(touch_id < temp.value.size())
435 {
436 value.resize(temp.value[touch_id].touches.size());
437 for (size_t i = 0; i < value.size(); i++)
438 {
439 value[i] = temp.value[touch_id].touches[i];
440 }
441 return ReturnValue_ok;
442 }
443 else
444 {
446 }
447 }
448}
449
451{
452 m_buttonsPort.close();
453 m_axisPort.close();
454 m_trackballPort.close();
455 m_touchPort.close();
456 m_hatsPort.close();
457 m_stickPort.close();
458
459 m_rpcPort.close();
460
461 m_allJoyDataPort.close();
462 return true;
463}
FeatureMode mode
#define ReturnValue_ok
Definition ReturnValue.h:82
#define ReturnValue_error_method_failed
Definition ReturnValue.h:87
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
yarp::dev::ReturnValue getTrackballCount(size_t &Trackball_count) override
Get number of trackballs.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue getAxisCount(size_t &axis_count) override
Get number of axes.
yarp::dev::ReturnValue getTouch(size_t touch_id, std::vector< yarp::dev::TouchData > &value) override
Get the value of a touch if present, return false otherwise.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue getTouchSurfaceCount(size_t &touch_count) override
Get the number of touch surface.
yarp::dev::ReturnValue getButtonCount(size_t &button_count) override
Get number of buttons.
yarp::dev::ReturnValue getStick(size_t stick_id, yarp::dev::StickData &value, JoypadCtrl_coordinateMode coordinate_mode) override
Get the value of a stick if present, return false otherwise.
yarp::dev::ReturnValue getButton(size_t button_id, double &value) override
Get the value of a button.
yarp::dev::ReturnValue getAllAxes(std::vector< double > &values) override
Get the values of all available joystick axes.
yarp::dev::ReturnValue getHatCount(size_t &Hat_count) override
Get number of hats.
yarp::dev::ReturnValue getTrackball(size_t trackball_id, yarp::dev::TrackballData &value) override
Get the axes change of a Trackball.
yarp::dev::ReturnValue getAxis(size_t axis_id, double &value) override
Get the value of an axis if present, return false otherwise.
yarp::dev::ReturnValue getHat(size_t hat_id, unsigned char &value) override
Get the value of an Hat.
yarp::dev::ReturnValue getStickCount(size_t &stick_count) override
Get the number of the sticks.
virtual return_getAxis getAxis(const std::int32_t axis_id)
virtual return_getHat getHat(const std::int32_t hat_id)
virtual return_getHatCount getHatCount()
virtual return_getAllAxes getAllAxes()
virtual return_getAxisCount getAxisCount()
virtual return_getTrackballCount getTrackballCount()
virtual return_getStick getStick(const std::int32_t stick_id, const yarp::dev::IJoypadController::JoypadCtrl_coordinateMode mode)
virtual return_getButton getButton(const std::int32_t button_id)
virtual return_getTouchSurfaceCount getTouchSurfaceCount()
virtual return_getTouch getTouch(const std::int32_t touch_id)
virtual return_getButtonCount getButtonCount()
virtual return_getTrackball getTrackball(const std::int32_t trackball_id)
virtual return_getStickCount getStickCount()
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition Network.cpp:784
void close() override
Stop port activity.
Definition Port.cpp:330
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A base class for nested structures that can be searched.
Definition Searchable.h:31
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.