YARP
Yet Another Robot Platform
IJoypadController.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_DEV_IJOYPADCONTROLLER_H
10 #define YARP_DEV_IJOYPADCONTROLLER_H
11 
12 #include <yarp/sig/Vector.h>
13 #include <yarp/dev/api.h>
14 #include <yarp/os/Vocab.h>
15 #include <yarp/os/PeriodicThread.h>
16 #include <yarp/os/Searchable.h>
17 #include <yarp/dev/GenericVocabs.h>
18 #include <map>
19 #include <vector>
20 #include <string>
21 
22 #define HAT_ACTIONS_ID_SHIFT 100
23 
24 namespace yarp
25 {
26  namespace dev
27  {
28  class IJoypadController;
29  class IJoypadEvent;
30  class IJoypadEventDriven;
31  }
32 }
33 
35 {
36 public:
37  enum JoypadCtrl_coordinateMode {JypCtrlcoord_POLAR = 0, JypCtrlcoord_CARTESIAN = 1};
38 
39 protected:
41 
42  virtual bool parseActions(const yarp::os::Searchable& cfg, int *count = nullptr);
43  virtual bool executeAction(int action_id);
44 
45 public:
46 
52  virtual ~IJoypadController(){}
53 
61  virtual bool eventDriven(bool enable, yarp::dev::IJoypadEvent* event = nullptr){return false;}
62  virtual bool isEventDriven(){return false;}
63 
70  virtual bool getAxisCount(unsigned int& axis_count) = 0;
71 
78  virtual bool getButtonCount(unsigned int& button_count) = 0;
79 
86  virtual bool getTrackballCount(unsigned int& Trackball_count) = 0;
87 
94  virtual bool getHatCount(unsigned int& Hat_count) = 0;
95 
103  virtual bool getTouchSurfaceCount(unsigned int& touch_count) = 0;
104 
111  virtual bool getStickCount(unsigned int& stick_count) = 0;
112 
120  virtual bool getStickDoF(unsigned int stick_id, unsigned int& DoF) = 0;
121 
129  virtual bool getButton(unsigned int button_id, float& value) = 0;
130 
138  virtual bool getTrackball(unsigned int trackball_id, yarp::sig::Vector& value) = 0;
139 
147  virtual bool getHat(unsigned int hat_id, unsigned char& value) = 0;
148 
158  virtual bool getAxis(unsigned int axis_id, double& value) = 0;
159 
169  virtual bool getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) = 0;
170 
177  virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector& value) = 0;
178 };
179 
181 {
182 public:
183  virtual ~IJoypadEvent();
184 
185  template <typename T> struct joyData
186  {
187  unsigned int m_id;
189 
190  joyData(unsigned int id, const T& datum)
191  {
192  m_id = id;
193  m_datum = datum;
194  }
195  };
196 
197  virtual void action(std::vector<joyData<float> > buttons,
198  std::vector<joyData<double> > axes,
199  std::vector<joyData<unsigned char> > hats,
200  std::vector<joyData<yarp::sig::Vector> > trackBalls,
201  std::vector<joyData<yarp::sig::Vector> > sticks,
202  std::vector<joyData<yarp::sig::Vector> > Touch) = 0;
203 };
204 
205 
206 
209 {
210 private:
211  yarp::dev::IJoypadEvent* m_event;
212  bool EventDrivenEnabled;
213  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<float>) old_buttons;
214  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<double>) old_axes;
215  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<unsigned char>) old_hats;
216  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::sig::Vector>) old_trackballs;
217  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::sig::Vector>) old_sticks;
218  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::sig::Vector>) old_touches;
219 protected:
220  virtual bool getRawAxisCount(unsigned int& axis_count) = 0;
221  virtual bool getRawButtonCount(unsigned int& button_count) = 0;
222  virtual bool getRawTrackballCount(unsigned int& Trackball_count) = 0;
223  virtual bool getRawHatCount(unsigned int& Hat_count) = 0;
224  virtual bool getRawTouchSurfaceCount(unsigned int& touch_count) = 0;
225  virtual bool getRawStickCount(unsigned int& stick_count) = 0;
226  virtual bool getRawStickDoF(unsigned int stick_id, unsigned int& DoF) = 0;
227  virtual bool getRawButton(unsigned int button_id, float& value) = 0;
228  virtual bool getRawTrackball(unsigned int trackball_id, yarp::sig::Vector& value) = 0;
229  virtual bool getRawHat(unsigned int hat_id, unsigned char& value) = 0;
230  virtual bool getRawAxis(unsigned int axis_id, double& value) = 0;
231  virtual bool getRawStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) = 0;
232  virtual bool getRawTouch(unsigned int touch_id, yarp::sig::Vector& value) = 0;
236 
237 public:
238 
239  bool getAxisCount(unsigned int& axis_count) override final;
240  bool getButtonCount(unsigned int& button_count) override final;
241  bool getTrackballCount(unsigned int& Trackball_count) override final;
242  bool getHatCount(unsigned int& Hat_count) override final;
243  bool getTouchSurfaceCount(unsigned int& touch_count) override final;
244  bool getStickCount(unsigned int& stick_count) override final;
245  bool getStickDoF(unsigned int stick_id, unsigned int& DoF) override final;
246  bool getButton(unsigned int button_id, float& value) override final;
247  bool getTrackball(unsigned int trackball_id, yarp::sig::Vector& value) override final;
248  bool getHat(unsigned int hat_id, unsigned char& value) override final;
249  bool getAxis(unsigned int axis_id, double& value) override final;
250  bool getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) override final;
251  bool getTouch(unsigned int touch_id, yarp::sig::Vector& value) override final;
255 
256 
257 
259 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
260 #if defined(_MSC_VER) && _MSC_VER <= 1900 // VS 2015
261  explicit IJoypadEventDriven(YARP_DEPRECATED_MSG("Use IJoypadEventDriven(double)") int rate);
262 #else
263  YARP_DEPRECATED_MSG("Use IJoypadEventDriven(double)")
264  explicit IJoypadEventDriven(int rate);
265 #endif
266 #endif
267  explicit IJoypadEventDriven(double period);
268 
269  bool threadInit() override final;
270  void run() override final;
271 
272  bool eventDriven(bool enable, yarp::dev::IJoypadEvent* event = nullptr) override;
273  bool isEventDriven() override { return EventDrivenEnabled;}
274 };
275 
276 
277 #define YRPJOY_HAT_CENTERED 0x00
278 #define YRPJOY_HAT_UP 0x01
279 #define YRPJOY_HAT_RIGHT 0x02
280 #define YRPJOY_HAT_DOWN 0x04
281 #define YRPJOY_HAT_LEFT 0x08
282 #define YRPJOY_HAT_RIGHTUP (YRPJOY_HAT_RIGHT|YRPJOY_HAT_UP)
283 #define YRPJOY_HAT_RIGHTDOWN (YRPJOY_HAT_RIGHT|YRPJOY_HAT_DOWN)
284 #define YRPJOY_HAT_LEFTUP (YRPJOY_HAT_LEFT |YRPJOY_HAT_UP)
285 #define YRPJOY_HAT_LEFTDOWN (YRPJOY_HAT_LEFT |YRPJOY_HAT_DOWN)
286 
288 
298 
299 #endif //#define YARP_DEV_IJOYPADCONTROLLER_H
300 
301 //todo list 23/03/2017..
302 //1. complete the single port functionality between server and client and test
303 //2. complete the rpc_only functionality in the client (add the parameter in the open() and test it)
304 //3. sdl hat #define to vocab traduction (just in case they change something)
305 //4. SDLJoypad implement stick functionality
306 //5. SDLJoypad implement deadband
307 //6. SDLJoypad implement button/axis remapping
yarp::dev::IJoypadController::JoypadCtrl_coordinateMode
JoypadCtrl_coordinateMode
Definition: IJoypadController.h:37
yarp::dev::IJoypadEventDriven::getRawButtonCount
virtual bool getRawButtonCount(unsigned int &button_count)=0
yarp::dev::IJoypadEventDriven::getStickCount
bool getStickCount(unsigned int &stick_count) override final
get the number of the sticks
Definition: IJoypadController.cpp:312
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
yarp::dev::IJoypadController::getTrackballCount
virtual bool getTrackballCount(unsigned int &Trackball_count)=0
Get number of trackballs.
yarp::dev::IJoypadController::eventDriven
virtual bool eventDriven(bool enable, yarp::dev::IJoypadEvent *event=nullptr)
Activate event Driven mode.
Definition: IJoypadController.h:61
YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARGS
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARGS(...)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:340
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::dev::IJoypadEventDriven::getRawStickDoF
virtual bool getRawStickDoF(unsigned int stick_id, unsigned int &DoF)=0
VOCAB_STICKDOF
constexpr yarp::conf::vocab32_t VOCAB_STICKDOF
Definition: IJoypadController.h:294
Vector.h
contains the definition of a Vector type
yarp::dev::IJoypadController::parseActions
virtual bool parseActions(const yarp::os::Searchable &cfg, int *count=nullptr)
Definition: IJoypadController.cpp:407
yarp::dev::IJoypadEventDriven::getTouchSurfaceCount
bool getTouchSurfaceCount(unsigned int &touch_count) override final
get the number of touch surface.
Definition: IJoypadController.cpp:307
yarp::dev::IJoypadEventDriven::getRawTouch
virtual bool getRawTouch(unsigned int touch_id, yarp::sig::Vector &value)=0
yarp::dev::IJoypadEventDriven::getStick
bool getStick(unsigned int stick_id, yarp::sig::Vector &value, JoypadCtrl_coordinateMode coordinate_mode) override final
Get the value of a stick if present, return false otherwise.
Definition: IJoypadController.cpp:370
yarp::dev::IJoypadController::getAxis
virtual bool getAxis(unsigned int axis_id, double &value)=0
Get the value of an axis if present, return false otherwise.
yarp::dev::IJoypadController::getAxisCount
virtual bool getAxisCount(unsigned int &axis_count)=0
Get number of Axes.
YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:339
yarp::dev::IJoypadController::getButtonCount
virtual bool getButtonCount(unsigned int &button_count)=0
Get number of Buttons.
yarp::dev::IJoypadEventDriven::IJoypadEventDriven
IJoypadEventDriven()
Definition: IJoypadController.cpp:24
yarp::dev::IJoypadController::JypCtrlcoord_POLAR
@ JypCtrlcoord_POLAR
Definition: IJoypadController.h:37
VOCAB_TRACKBALL
constexpr yarp::conf::vocab32_t VOCAB_TRACKBALL
Definition: IJoypadController.h:290
yarp::dev::IJoypadEventDriven
Definition: IJoypadController.h:209
yarp::dev::IJoypadController::getTouchSurfaceCount
virtual bool getTouchSurfaceCount(unsigned int &touch_count)=0
get the number of touch surface.
yarp::dev::IJoypadController::getStickDoF
virtual bool getStickDoF(unsigned int stick_id, unsigned int &DoF)=0
Get the Degree Of Freedom count for desired stick.
yarp::dev::IJoypadEventDriven::threadInit
bool threadInit() override final
Initialization method.
Definition: IJoypadController.cpp:60
Searchable.h
yarp::dev::IJoypadEventDriven::getRawAxisCount
virtual bool getRawAxisCount(unsigned int &axis_count)=0
yarp::dev::IJoypadEventDriven::getHat
bool getHat(unsigned int hat_id, unsigned char &value) override final
Get the value of an Hat.
Definition: IJoypadController.cpp:348
yarp::dev::IJoypadController::m_actions
std::map< int, std::string > m_actions
Definition: IJoypadController.h:40
yarp::dev::IJoypadEventDriven::getTrackballCount
bool getTrackballCount(unsigned int &Trackball_count) override final
Get number of trackballs.
Definition: IJoypadController.cpp:297
yarp::sig::VectorOf< double >
yarp::dev::IJoypadController::getStickCount
virtual bool getStickCount(unsigned int &stick_count)=0
get the number of the sticks
yarp::dev::IJoypadController::~IJoypadController
virtual ~IJoypadController()
Destructor.
Definition: IJoypadController.h:52
yarp::dev::IJoypadEventDriven::getRawHatCount
virtual bool getRawHatCount(unsigned int &Hat_count)=0
VOCAB_POLAR
constexpr yarp::conf::vocab32_t VOCAB_POLAR
Definition: IJoypadController.h:296
yarp::dev::IJoypadEvent::joyData::joyData
joyData(unsigned int id, const T &datum)
Definition: IJoypadController.h:190
VOCAB_BUTTON
constexpr yarp::conf::vocab32_t VOCAB_BUTTON
Definition: IJoypadController.h:289
yarp::dev::IJoypadEventDriven::getStickDoF
bool getStickDoF(unsigned int stick_id, unsigned int &DoF) override final
Get the Degree Of Freedom count for desired stick.
Definition: IJoypadController.cpp:317
yarp::dev::IJoypadController::getButton
virtual bool getButton(unsigned int button_id, float &value)=0
Get the value of a button.
yarp::dev::IJoypadController::getTouch
virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector &value)=0
Get the value of a touch if present, return false otherwise.
yarp::dev::IJoypadEvent::joyData::m_datum
T m_datum
Definition: IJoypadController.h:188
yarp::dev::IJoypadEvent::action
virtual void action(std::vector< joyData< float > > buttons, std::vector< joyData< double > > axes, std::vector< joyData< unsigned char > > hats, std::vector< joyData< yarp::sig::Vector > > trackBalls, std::vector< joyData< yarp::sig::Vector > > sticks, std::vector< joyData< yarp::sig::Vector > > Touch)=0
yarp::dev::IJoypadEventDriven::getRawAxis
virtual bool getRawAxis(unsigned int axis_id, double &value)=0
VOCAB_AXIS
constexpr yarp::conf::vocab32_t VOCAB_AXIS
Definition: IJoypadController.h:292
VOCAB_TOUCH
constexpr yarp::conf::vocab32_t VOCAB_TOUCH
Definition: IJoypadController.h:295
yarp::dev::IJoypadEvent::~IJoypadEvent
virtual ~IJoypadEvent()
yarp::dev::IJoypadEventDriven::getRawStickCount
virtual bool getRawStickCount(unsigned int &stick_count)=0
yarp::dev::IJoypadEventDriven::getRawTrackballCount
virtual bool getRawTrackballCount(unsigned int &Trackball_count)=0
yarp::dev::IJoypadController::executeAction
virtual bool executeAction(int action_id)
Definition: IJoypadController.cpp:392
yarp::dev::IJoypadEventDriven::getRawHat
virtual bool getRawHat(unsigned int hat_id, unsigned char &value)=0
YARP_dev_API
#define YARP_dev_API
Definition: api.h:19
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
yarp::dev::IJoypadEvent::joyData::m_id
unsigned int m_id
Definition: IJoypadController.h:187
yarp::dev::IJoypadEventDriven::getHatCount
bool getHatCount(unsigned int &Hat_count) override final
Get number of Hats.
Definition: IJoypadController.cpp:302
PeriodicThread.h
VOCAB_CARTESIAN
constexpr yarp::conf::vocab32_t VOCAB_CARTESIAN
Definition: IJoypadController.h:297
VOCAB_HAT
constexpr yarp::conf::vocab32_t VOCAB_HAT
Definition: IJoypadController.h:291
yarp::dev::IJoypadEventDriven::getRawStick
virtual bool getRawStick(unsigned int stick_id, yarp::sig::Vector &value, JoypadCtrl_coordinateMode coordinate_mode)=0
yarp::dev::IJoypadEventDriven::getRawButton
virtual bool getRawButton(unsigned int button_id, float &value)=0
yarp::dev::IJoypadController::JypCtrlcoord_CARTESIAN
@ JypCtrlcoord_CARTESIAN
Definition: IJoypadController.h:37
yarp::dev::IJoypadEventDriven::getButtonCount
bool getButtonCount(unsigned int &button_count) override final
Get number of Buttons.
Definition: IJoypadController.cpp:292
YARP_DEPRECATED_MSG
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2883
yarp::dev::IJoypadEventDriven::getAxis
bool getAxis(unsigned int axis_id, double &value) override final
Get the value of an axis if present, return false otherwise.
Definition: IJoypadController.cpp:359
yarp::dev::IJoypadController::getHat
virtual bool getHat(unsigned int hat_id, unsigned char &value)=0
Get the value of an Hat.
yarp::dev::IJoypadController::getStick
virtual bool getStick(unsigned int stick_id, yarp::sig::Vector &value, JoypadCtrl_coordinateMode coordinate_mode)=0
Get the value of a stick if present, return false otherwise.
yarp::dev::IJoypadEvent::joyData
Definition: IJoypadController.h:186
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::conf::vocab32_t
std::int32_t vocab32_t
Definition: numeric.h:52
Vocab.h
yarp::dev::IJoypadEventDriven::getTrackball
bool getTrackball(unsigned int trackball_id, yarp::sig::Vector &value) override final
Get the axes change of a Trackball.
Definition: IJoypadController.cpp:337
yarp::dev::IJoypadController::isEventDriven
virtual bool isEventDriven()
Definition: IJoypadController.h:62
VOCAB_IJOYPADCTRL
constexpr yarp::conf::vocab32_t VOCAB_IJOYPADCTRL
Definition: IJoypadController.h:287
yarp::dev::IJoypadController::getHatCount
virtual bool getHatCount(unsigned int &Hat_count)=0
Get number of Hats.
yarp::dev::IJoypadEventDriven::getAxisCount
bool getAxisCount(unsigned int &axis_count) override final
Get number of Axes.
Definition: IJoypadController.cpp:287
yarp::dev::IJoypadController::getTrackball
virtual bool getTrackball(unsigned int trackball_id, yarp::sig::Vector &value)=0
Get the axes change of a Trackball.
yarp::dev::IJoypadEventDriven::getButton
bool getButton(unsigned int button_id, float &value) override final
Get the value of a button.
Definition: IJoypadController.cpp:327
yarp::dev::IJoypadEventDriven::getRawTrackball
virtual bool getRawTrackball(unsigned int trackball_id, yarp::sig::Vector &value)=0
VOCAB_STICK
constexpr yarp::conf::vocab32_t VOCAB_STICK
Definition: IJoypadController.h:293
GenericVocabs.h
yarp::dev::IJoypadEventDriven::getTouch
bool getTouch(unsigned int touch_id, yarp::sig::Vector &value) override final
Get the value of a touch if present, return false otherwise.
Definition: IJoypadController.cpp:381
api.h
yarp::dev::IJoypadEventDriven::getRawTouchSurfaceCount
virtual bool getRawTouchSurfaceCount(unsigned int &touch_count)=0
yarp::dev::IJoypadController
Definition: IJoypadController.h:35
yarp::dev::IJoypadEvent
Definition: IJoypadController.h:181