Skip to content

Commit

Permalink
Merge ee771b4 into 6cc5602
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Ruzzenenti committed Jul 27, 2017
2 parents 6cc5602 + ee771b4 commit 41ec5f6
Show file tree
Hide file tree
Showing 8 changed files with 595 additions and 113 deletions.
45 changes: 25 additions & 20 deletions src/devices/SDLJoypad/SDLJoypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,54 +242,59 @@ bool SDLJoypad::parseStickInfo(const yarp::os::Searchable& cfg)
return true;
}


bool SDLJoypad::close()
{
return false;
}

bool SDLJoypad::getAxisCount(unsigned int& axes_count)
bool SDLJoypad::getRawAxisCount(unsigned int& axes_count)
{
axes_count = m_axisCount;
return true;
}

bool SDLJoypad::getButtonCount(unsigned int& button_count)
bool SDLJoypad::getRawButtonCount(unsigned int& button_count)
{
button_count = m_buttonCount;
return true;
}

bool SDLJoypad::getTrackballCount(unsigned int& trackball_count)
bool SDLJoypad::getRawTrackballCount(unsigned int& trackball_count)
{
trackball_count = m_ballCount;
return true;
}

bool SDLJoypad::getHatCount(unsigned int& hat_count)
bool SDLJoypad::getRawHatCount(unsigned int& hat_count)
{
hat_count = m_hatCount;
return true;
}

bool SDLJoypad::getTouchSurfaceCount(unsigned int& touch_count)
bool SDLJoypad::getRawTouchSurfaceCount(unsigned int& touch_count)
{
touch_count = m_touchCount;
return true;
}

bool SDLJoypad::getStickCount(unsigned int& stick_count)
bool SDLJoypad::getRawStickCount(unsigned int& stick_count)
{
stick_count = m_stickCount;
return true;
}

bool SDLJoypad::getStickDoF(unsigned int stick_id, unsigned int& DoF)
bool SDLJoypad::getRawStickDoF(unsigned int stick_id, unsigned int& DoF)
{
return false;
if(stick_id > m_sticks.size()-1)
{
yError() << "SDL_Joypad: stick_id out of bounds when calling 'getStickDoF'' method";
return false;
}
DoF = 2;
return true;
}

bool SDLJoypad::getButton(unsigned int button_id, float& value)
bool SDLJoypad::getRawButton(unsigned int button_id, float& value)
{
if(button_id > m_buttonCount - 1){yError() << "SDLJoypad: button id out of bound!"; return false;}
updateJoypad();
Expand All @@ -315,7 +320,7 @@ bool SDLJoypad::getButton(unsigned int button_id, float& value)
return true;
}

bool SDLJoypad::getRawAxis(unsigned int axis_id, double& value)
bool SDLJoypad::getPureAxis(unsigned int axis_id, double& value)
{
if(axis_id > m_axisCount - 1){yError() << "SDLJoypad: axis id out of bound!"; return false;}
size_t i;
Expand All @@ -338,12 +343,12 @@ bool SDLJoypad::getRawAxis(unsigned int axis_id, double& value)
return true;
}

bool SDLJoypad::getAxis(unsigned int axis_id, double& value)
bool SDLJoypad::getRawAxis(unsigned int axis_id, double& value)
{
if(axis_id > m_axisCount - 1){yError() << "SDLJoypad: axis id out of bound!"; return false;}
//if(!m_axes.at(axis_id)) {yWarning() << "SDLJoypad: requested axis is part of a stick!";}
updateJoypad();
return getRawAxis(axis_id, value);
return getPureAxis(axis_id, value);
}

yarp::sig::Vector Vector3(const double& x, const double& y)
Expand All @@ -354,9 +359,9 @@ yarp::sig::Vector Vector3(const double& x, const double& y)
return ret;
}

bool SDLJoypad::getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode)
bool SDLJoypad::getRawStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode)
{
if(stick_id > m_stickCount - 1){yError() << "SDLJoypad: stick id out of bound!"; return false;}
if (stick_id > m_stickCount - 1){yError() << "SDLJoypad: stick id out of bound!"; return false;}
value.clear();
updateJoypad();
stick& stk = m_sticks[stick_id];
Expand All @@ -368,9 +373,9 @@ bool SDLJoypad::getStick(unsigned int stick_id, yarp::sig::Vector& value, Joypad
value.push_back(val * stk.direction[i] * (fabs(val) > stk.deadZone));
}

if(coordinate_mode == JypCtrlcoord_POLAR)
if (coordinate_mode == JypCtrlcoord_POLAR)
{
if(stk.axes_ids.size() > 2)
if (stk.axes_ids.size() > 2)
{
yError() << "polar coordinate system is supported only for bidimensional stick at the moment";
return false;
Expand All @@ -380,12 +385,12 @@ bool SDLJoypad::getStick(unsigned int stick_id, yarp::sig::Vector& value, Joypad
return true;
}

bool SDLJoypad::getTouch(unsigned int touch_id, yarp::sig::Vector& value)
bool SDLJoypad::getRawTouch(unsigned int touch_id, yarp::sig::Vector& value)
{
return false;
}

bool SDLJoypad::getHat(unsigned int hat_id, unsigned char& value)
bool SDLJoypad::getRawHat(unsigned int hat_id, unsigned char& value)
{
if(hat_id > m_hatCount - 1){yError() << "SDLJoypad: axis id out of bound!"; return false;}
updateJoypad();
Expand All @@ -407,7 +412,7 @@ bool SDLJoypad::getHat(unsigned int hat_id, unsigned char& value)
return true;
}

bool SDLJoypad::getTrackball(unsigned int trackball_id, yarp::sig::Vector& value)
bool SDLJoypad::getRawTrackball(unsigned int trackball_id, yarp::sig::Vector& value)
{
if(trackball_id > m_ballCount - 1){yError() << "SDLJoypad: trackball id out of bound!"; return false;}
updateJoypad();
Expand Down
34 changes: 17 additions & 17 deletions src/devices/SDLJoypad/SDLJoypad.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct yarp::dev::SDLJoypadImpl::stick
std::vector<int> direction;
};

class yarp::dev::SDLJoypad : public yarp::dev::IJoypadController,
class yarp::dev::SDLJoypad : public yarp::dev::IJoypadEventDriven,//public yarp::dev::IJoypadController,
public yarp::dev::DeviceDriver
{
typedef std::vector<yarp::dev::SDLJoypadImpl::stick> stickVector;
Expand All @@ -52,41 +52,41 @@ class yarp::dev::SDLJoypad : public yarp::dev::IJoypadController,
void updateJoypad();
void pollActions();
bool parseStickInfo(const yarp::os::Searchable& cfg);
bool getRawAxis(unsigned int axis_id, double& value);
bool getPureAxis(unsigned int axis_id, double& value);
public:

SDLJoypad();
virtual ~SDLJoypad();
//DeviceDriver
virtual bool open(yarp::os::Searchable& config);
virtual bool close();
virtual bool open(yarp::os::Searchable& config) YARP_OVERRIDE;
virtual bool close() YARP_OVERRIDE;

//IJoypadController
virtual bool getAxisCount(unsigned int& axis_count) override;
virtual bool getRawAxisCount(unsigned int& axis_count) YARP_OVERRIDE;

virtual bool getButtonCount(unsigned int& button_count) override;
virtual bool getRawButtonCount(unsigned int& button_count) YARP_OVERRIDE;

virtual bool getHatCount(unsigned int& hat_count) override;
virtual bool getRawHatCount(unsigned int& hat_count) YARP_OVERRIDE;

virtual bool getTrackballCount(unsigned int& trackball_count) override;
virtual bool getRawTrackballCount(unsigned int& trackball_count) YARP_OVERRIDE;

virtual bool getTouchSurfaceCount(unsigned int& touch_count) override;
virtual bool getRawTouchSurfaceCount(unsigned int& touch_count) YARP_OVERRIDE;

virtual bool getStickCount(unsigned int& stick_count) override;
virtual bool getRawStickCount(unsigned int& stick_count) YARP_OVERRIDE;

virtual bool getStickDoF(unsigned int stick_id, unsigned int& DoF) override;
virtual bool getRawStickDoF(unsigned int stick_id, unsigned int& DoF) YARP_OVERRIDE;

virtual bool getButton(unsigned int button_id, float& value) override;
virtual bool getRawButton(unsigned int button_id, float& value) YARP_OVERRIDE;

virtual bool getTrackball(unsigned int trackball_id, yarp::sig::Vector& value) override;
virtual bool getRawTrackball(unsigned int trackball_id, yarp::sig::Vector& value) YARP_OVERRIDE;

virtual bool getHat(unsigned int hat_id, unsigned char& value) override;
virtual bool getRawHat(unsigned int hat_id, unsigned char& value) YARP_OVERRIDE;

virtual bool getAxis(unsigned int axis_id, double& value) override;
virtual bool getRawAxis(unsigned int axis_id, double& value) YARP_OVERRIDE;

virtual bool getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) override;
virtual bool getRawStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) YARP_OVERRIDE;

virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector& value) override;
virtual bool getRawTouch(unsigned int touch_id, yarp::sig::Vector& value) YARP_OVERRIDE;
};


Expand Down
113 changes: 110 additions & 3 deletions src/libYARP_dev/include/yarp/dev/IJoypadController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <yarp/sig/Vector.h>
#include <yarp/dev/api.h>
#include <yarp/os/Vocab.h>
#include <yarp/os/RateThread.h>
#include <map>
#include <vector>

#define HAT_ACTIONS_ID_SHIFT 100

Expand All @@ -19,19 +21,23 @@ namespace yarp
namespace dev
{
class IJoypadController;
class IJoypadEvent;
class IJoypadEventDriven;
}
}

class YARP_dev_API yarp::dev::IJoypadController
class YARP_dev_API yarp::dev::IJoypadController
{
public:
enum JoypadCtrl_coordinateMode {JypCtrlcoord_POLAR = 0, JypCtrlcoord_CARTESIAN = 1};

protected:
std::map<int, std::string> m_actions;

virtual bool parseActions(const yarp::os::Searchable& cfg, int *count = YARP_NULLPTR);
virtual bool executeAction(int action_id);
public:

enum JoypadCtrl_coordinateMode {JypCtrlcoord_POLAR = 0, JypCtrlcoord_CARTESIAN = 1};
public:

/**
Destructor
Expand All @@ -40,6 +46,16 @@ class YARP_dev_API yarp::dev::IJoypadController
*/
virtual ~IJoypadController(){}

/**
Activate event Driven mode
* @brief eventDriven
* @param enable a bool to turn on or off the eventDriven mode
* * @param event a pointer to a valid yarp::dev::IJoypadEvent object whom action() method will be called on event detection
* @return true if succeded. false otherwise
*/
virtual bool eventDriven(bool enable, yarp::dev::IJoypadEvent* event = YARP_NULLPTR){return false;}
virtual bool isEventDriven(){return false;}

/**
Get number of Axes
* @brief getAxisCount
Expand Down Expand Up @@ -155,6 +171,97 @@ class YARP_dev_API yarp::dev::IJoypadController
*/
virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector& value) = 0;
};

class YARP_dev_API yarp::dev::IJoypadEvent
{
public:
virtual ~IJoypadEvent();

template <typename T> struct joyData
{
unsigned int m_id;
T m_datum;

joyData(unsigned int id, const T& datum)
{
m_id = id;
m_datum = datum;
}
};

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;
};



class YARP_dev_API yarp::dev::IJoypadEventDriven : yarp::os::RateThread,
public yarp::dev::IJoypadController
{
private:
yarp::dev::IJoypadEvent* m_event;
bool EventDrivenEnabled;
std::vector<float> old_buttons;
std::vector<double> old_axes;
std::vector<unsigned char> old_hats;
std::vector<yarp::sig::Vector> old_trackballs;
std::vector<yarp::sig::Vector> old_sticks;
std::vector<yarp::sig::Vector> old_touches;
protected:
virtual bool getRawAxisCount(unsigned int& axis_count) = 0;
virtual bool getRawButtonCount(unsigned int& button_count) = 0;
virtual bool getRawTrackballCount(unsigned int& Trackball_count) = 0;
virtual bool getRawHatCount(unsigned int& Hat_count) = 0;
virtual bool getRawTouchSurfaceCount(unsigned int& touch_count) = 0;
virtual bool getRawStickCount(unsigned int& stick_count) = 0;
virtual bool getRawStickDoF(unsigned int stick_id, unsigned int& DoF) = 0;
virtual bool getRawButton(unsigned int button_id, float& value) = 0;
virtual bool getRawTrackball(unsigned int trackball_id, yarp::sig::Vector& value) = 0;
virtual bool getRawHat(unsigned int hat_id, unsigned char& value) = 0;
virtual bool getRawAxis(unsigned int axis_id, double& value) = 0;
virtual bool getRawStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) = 0;
virtual bool getRawTouch(unsigned int touch_id, yarp::sig::Vector& value) = 0;
using IJoypadController::m_actions;
using IJoypadController::executeAction;
using IJoypadController::parseActions;

public:

virtual bool getAxisCount(unsigned int& axis_count) YARP_OVERRIDE YARP_FINAL;
virtual bool getButtonCount(unsigned int& button_count) YARP_OVERRIDE YARP_FINAL;
virtual bool getTrackballCount(unsigned int& Trackball_count) YARP_OVERRIDE YARP_FINAL;
virtual bool getHatCount(unsigned int& Hat_count) YARP_OVERRIDE YARP_FINAL;
virtual bool getTouchSurfaceCount(unsigned int& touch_count) YARP_OVERRIDE YARP_FINAL;
virtual bool getStickCount(unsigned int& stick_count) YARP_OVERRIDE YARP_FINAL;
virtual bool getStickDoF(unsigned int stick_id, unsigned int& DoF) YARP_OVERRIDE YARP_FINAL;
virtual bool getButton(unsigned int button_id, float& value) YARP_OVERRIDE YARP_FINAL;
virtual bool getTrackball(unsigned int trackball_id, yarp::sig::Vector& value) YARP_OVERRIDE YARP_FINAL;
virtual bool getHat(unsigned int hat_id, unsigned char& value) YARP_OVERRIDE YARP_FINAL;
virtual bool getAxis(unsigned int axis_id, double& value) YARP_OVERRIDE YARP_FINAL;
virtual bool getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) YARP_OVERRIDE YARP_FINAL;
virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector& value) YARP_OVERRIDE YARP_FINAL;
using IJoypadController::JoypadCtrl_coordinateMode;
using IJoypadController::JypCtrlcoord_CARTESIAN;
using IJoypadController::JypCtrlcoord_POLAR;



IJoypadEventDriven();

IJoypadEventDriven(int rate);

virtual bool threadInit() YARP_OVERRIDE;
virtual void run() YARP_OVERRIDE;

virtual bool eventDriven(bool enable, yarp::dev::IJoypadEvent* event = YARP_NULLPTR) YARP_OVERRIDE;
virtual bool isEventDriven() YARP_OVERRIDE { return EventDrivenEnabled;}
};


#define YRPJOY_HAT_CENTERED 0x00
#define YRPJOY_HAT_UP 0x01
#define YRPJOY_HAT_RIGHT 0x02
Expand Down

0 comments on commit 41ec5f6

Please sign in to comment.