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 <tuple>
10
11using namespace yarp::dev;
12using namespace yarp::sig;
13using namespace yarp::os;
14
15namespace {
16YARP_LOG_COMPONENT(JOYPADCONTROLCLIENT, "yarp.device.JoypadControl_nwc_yarp")
17}
18
22
23bool JoypadControl_nwc_yarp::getJoypadInfo()
24{
25 bool v= true;
32
33 return v;
34}
35
37{
38 if (!parseParams(config)) { return false; }
39
40 //open the rpc port
41 if(!m_rpcPort.open(m_local + "/rpc:o"))
42 {
43 yCError(JOYPADCONTROLCLIENT) << "Unable to open rpc port.";
44 return false;
45 }
46 yCInfo(JOYPADCONTROLCLIENT) << "rpc port opened. starting the handshake";
47
48 //connection for the rpc port
49 if(!yarp::os::NetworkBase::connect(m_local + "/rpc:o", m_remote + "/rpc:i"))
50 {
51 yCError(JOYPADCONTROLCLIENT) << "Handshake failed. unable to connect to remote port" << m_remote + "/rpc:i";
52 return false;
53 }
54
55 if (!m_rpcMsgs.yarp().attachAsClient(m_rpcPort))
56 {
57 yCError(JOYPADCONTROLCLIENT, "Error! Cannot attach the port as a client");
58 return false;
59 }
60
61 // Check the protocol version
62 if (!m_rpcMsgs.checkProtocolVersion()) {
63 return false;
64 }
65
66 //Get basic infos from the joypad
67 if(!getJoypadInfo())
68 {
69 yCError(JOYPADCONTROLCLIENT) << "Unable to get joypad info.";
70 return false;
71 }
72
73 if (m_use_streaming==false) {return true;}
74
75 //The code below is valid only for streaming mode
77 {
78 bool b = true;
79 b &= m_allJoyDataPort.open(m_local + "/allJoyData:i");
80 if (!b) {
81 yCError(JOYPADCONTROLCLIENT) << "Unable to open local streaming ports";
82 close();
83 return false;
84 }
85
86 b&= yarp::os::NetworkBase::connect(m_remote+"/allJoyData:o", m_local+"/allJoyData:i");
87 if (!b) {
88 yCError(JOYPADCONTROLCLIENT) << "Unable to connect to remote streaming port";
89 close();
90 return false;
91 }
92
93 m_allJoyDataPort.useCallback();
94 }
95 else
96 {
97 bool b = true;
98 b&= m_buttonsPort.open(m_local+"/buttons:i");
99 b&= m_axisPort.open(m_local+"/axis:i");
100 b&= m_trackballPort.open(m_local+"/trackball:i");
101 b&= m_touchPort.open(m_local+"/touch:i");
102 b&= m_hatsPort.open(m_local+"/hat:i");
103 b&= m_stickPort.open(m_local+"/stick:i");
104 if (!b) {
105 yCError(JOYPADCONTROLCLIENT) << "Unable to open local streaming ports";
106 close();
107 return false;
108 }
109
110 b&= yarp::os::NetworkBase::connect(m_remote+"/buttons:o",m_local+"/buttons:i");
111 b&= yarp::os::NetworkBase::connect(m_remote+"/axis:o",m_local+"/axis:i");
112 b&= yarp::os::NetworkBase::connect(m_remote+"/trackball:o",m_local+"/trackball:i");
113 b&= yarp::os::NetworkBase::connect(m_remote+"/touch:o",m_local+"/touch:i");
114 b&= yarp::os::NetworkBase::connect(m_remote+"/hat:o",m_local+"/hat:i");
115 b&= yarp::os::NetworkBase::connect(m_remote+"/stick:o",m_local+"/stick:i");
116 if (!b) {
117 yCError(JOYPADCONTROLCLIENT) << "Unable to connect to remote streaming port";
118 close();
119 return false;
120 }
121
122 m_buttonsPort.useCallback();
123 m_axisPort.useCallback();
124 m_trackballPort.useCallback();
125 m_touchPort.useCallback();
126 m_hatsPort.useCallback();
127 m_stickPort.useCallback();
128 }
129
130 return true;
131}
132
133
135{
136 auto ret = m_rpcMsgs.getButtonCount();
137 button_count = ret.button_count;
138 return ret.ret;
139}
140
142{
143 auto ret = m_rpcMsgs.getAxisCount();
144 axis_count = ret.axis_count;
145 return ret.ret;
146}
147
149{
150 auto ret = m_rpcMsgs.getTrackballCount();
151 Trackball_count = ret.Trackball_count;
152 return ret.ret;
153}
154
156{
157 auto ret = m_rpcMsgs.getHatCount();
158 Hat_count = ret.Hat_count;
159 return ret.ret;
160}
161
163{
164 auto ret = m_rpcMsgs.getTouchSurfaceCount();
165 touch_count = ret.touch_count;
166 return ret.ret;
167}
168
170{
171 auto ret = m_rpcMsgs.getStickCount();
172 stick_count = ret.stick_count;
173 return ret.ret;
174}
175
176ReturnValue JoypadControl_nwc_yarp::getButton(size_t button_id, double& value)
177{
178 if(!m_use_streaming)
179 {
180 auto ret = m_rpcMsgs.getButton(button_id);
181 value = ret.value;
182 return ret.ret;
183 }
184 else if (m_use_all_port)
185 {
186 auto tmp = m_allJoyDataPort.getData();
187 if(button_id < tmp.ButtonDataVal.size())
188 {
189 value = tmp.ButtonDataVal[button_id];
190 return ReturnValue_ok;
191 }
192 else
193 {
194 yCError(JOYPADCONTROLCLIENT) << "GetButton() error. button_id out of bound";
196 }
197 }
198 else
199 {
200 auto tmp = m_buttonsPort.getData();
201 if(button_id < tmp.value.size())
202 {
203 value = tmp.value[button_id];
204 return ReturnValue_ok;
205 }
206 else
207 {
208 yCError(JOYPADCONTROLCLIENT) << "GetButton() error. button_id out of bound";
210 }
211 }
212}
213
215{
216 if(!m_use_streaming)
217 {
218 auto ret = m_rpcMsgs.getTrackball(trackball_id);
219 value = ret.value;
220 return ret.ret;
221 }
222 else if (m_use_all_port)
223 {
224 auto tmp = m_allJoyDataPort.getData();
225 if(trackball_id < tmp.TrackballDataVal.size())
226 {
227 value = tmp.TrackballDataVal[trackball_id];
228 return ReturnValue_ok;
229 }
230 else
231 {
232 yCError(JOYPADCONTROLCLIENT) << "GetTrackball() error. trackball_id out of bound";
234 }
235 }
236 else
237 {
238 auto tmp = m_trackballPort.getData();
239 if(trackball_id < tmp.value.size())
240 {
241 value = tmp.value[trackball_id];
242 return ReturnValue_ok;
243 }
244 else
245 {
247 }
248 }
249}
250
251ReturnValue JoypadControl_nwc_yarp::getHat(size_t hat_id, unsigned char& value)
252{
253 if(!m_use_streaming)
254 {
255 auto ret = m_rpcMsgs.getHat(hat_id);
256 value = ret.value;
257 return ret.ret;
258 }
259 else if (m_use_all_port)
260 {
261 auto tmp = m_allJoyDataPort.getData();
262 if(hat_id < tmp.HatsDataVal.size())
263 {
264 value = tmp.HatsDataVal[hat_id];
265 return ReturnValue_ok;
266 }
267 else
268 {
269 yCError(JOYPADCONTROLCLIENT) << "GetHat() error. hat_id out of bound";
271 }
272 }
273 else
274 {
275 auto tmp = m_hatsPort.getData();
276 if (hat_id < tmp.value.size())
277 {
278 value = tmp.value[hat_id];
279 return ReturnValue_ok;
280 }
281 else
282 {
284 }
285 }
286}
287
289{
290 if(!m_use_streaming)
291 {
292 auto ret = m_rpcMsgs.getAllAxes();
293 value = ret.value;
294 return ret.ret;
295 }
296 else
297 {
298 if (m_use_all_port)
299 {
300 auto tmp = m_allJoyDataPort.getData();
301 value = tmp.AxisDataVal;
302 return ReturnValue_ok;
303 }
304 else
305 {
306 auto tmp = m_axisPort.getData();
307 value = tmp.value;
308 return ReturnValue_ok;
309 }
310 }
311}
312
313ReturnValue JoypadControl_nwc_yarp::getAxis(size_t axis_id, double& value)
314{
315 if(!m_use_streaming)
316 {
317 auto ret = m_rpcMsgs.getAxis(axis_id);
318 value = ret.value;
319 return ret.ret;
320 }
321 else
322 {
323 auto tmp = m_axisPort.getData();
324 if(axis_id < tmp.value.size())
325 {
326 value = tmp.value[axis_id];
327 return ReturnValue_ok;
328 }
329 else if (m_use_all_port)
330 {
331 auto tmp = m_allJoyDataPort.getData();
332 if(axis_id < tmp.AxisDataVal.size())
333 {
334 value = tmp.AxisDataVal[axis_id];
335 return ReturnValue_ok;
336 }
337 else
338 {
339 yCError(JOYPADCONTROLCLIENT) << "GetAxis() error. axis_id out of bound";
341 }
342 }
343 else
344 {
345 yCError(JOYPADCONTROLCLIENT) << "GetAxis() error. Axis_id out of bound";
347 }
348 }
349}
350
352{
353 if(!m_use_streaming)
354 {
355 auto ret = m_rpcMsgs.getStick(stick_id,mode);
356 value = ret.value;
357 return ret.ret;
358 }
359 else if (m_use_all_port)
360 {
361 //Only the polar mode is available in streaming.
362 //If cartesian mode is requested, switch back to rpc.
364 {
365 auto ret = m_rpcMsgs.getStick(stick_id,mode);
366 value = ret.value;
367 return ret.ret;
368 }
369 auto tmp = m_allJoyDataPort.getData();
370 if(stick_id < tmp.StickDataVal.size())
371 {
372 value = tmp.StickDataVal[stick_id];
373 return ReturnValue_ok;
374 }
375 else
376 {
377 yCError(JOYPADCONTROLCLIENT) << "getStick() error. stick_id out of bound";
379 }
380 }
381 else
382 {
383 //Only the polar mode is available in streaming.
384 //If cartesian mode is requested, switch back to rpc.
386 {
387 auto ret = m_rpcMsgs.getStick(stick_id,mode);
388 value = ret.value;
389 return ret.ret;
390 }
391 auto tmp = m_stickPort.getData();
392 if(stick_id < tmp.value.size())
393 {
394 value = tmp.value[stick_id];
395 return ReturnValue_ok;
396 }
397 else
398 {
399 yCError(JOYPADCONTROLCLIENT) << "GetAxis() error. stick_id out of bound";
401 }
402
403 return ReturnValue_ok;
404 }
405}
406
407ReturnValue JoypadControl_nwc_yarp::getTouch(size_t touch_id, std::vector<yarp::dev::TouchData>& value)
408{
409 value.clear();
410 if(!m_use_streaming)
411 {
412 auto ret = m_rpcMsgs.getTouch(touch_id);
413 value = ret.value;
414 return ret.ret;
415 }
416 else if (m_use_all_port)
417 {
418 auto tmp = m_allJoyDataPort.getData();
419 if(touch_id < tmp.TouchDataVal.size())
420 {
421 value = tmp.TouchDataVal[touch_id].touches;
422 return ReturnValue_ok;
423 }
424 else
425 {
426 yCError(JOYPADCONTROLCLIENT) << "GetTouch() error. touch_id out of bound";
428 }
429 }
430 else
431 {
432 auto temp = m_touchPort.getData();
433 if(touch_id < temp.value.size())
434 {
435 value.resize(temp.value[touch_id].touches.size());
436 for (size_t i = 0; i < value.size(); i++)
437 {
438 value[i] = temp.value[touch_id].touches[i];
439 }
440 return ReturnValue_ok;
441 }
442 else
443 {
445 }
446 }
447}
448
450{
451 m_buttonsPort.close();
452 m_axisPort.close();
453 m_trackballPort.close();
454 m_touchPort.close();
455 m_hatsPort.close();
456 m_stickPort.close();
457
458 m_rpcPort.close();
459
460 m_allJoyDataPort.close();
461 return true;
462}
#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:751
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.
Definition OSTestMsg.cpp:13