Choreonoid  1.8
SharedJoystick.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_SHARED_JOYSTICK_H
7 #define CNOID_UTIL_SHARED_JOYSTICK_H
8 
9 #include <cnoid/Referenced>
10 #include <cnoid/Joystick>
11 
12 namespace cnoid {
13 
14 class SharedJoystick : public Referenced
15 {
16 public:
17  //Switch mode with either button
20 
22  joystick = &defaultJoystick;
23  mode_ = 0;
24  numModes_ = 0;
25  prevButtonState = false;
26  }
27 
28  void setJoystick(JoystickInterface* joystick){
29  this->joystick = joystick;
30  }
31 
32  int addMode(){
33  int id = numModes_++;
34  return id;
35  }
36 
38  joystick->readCurrentState();
39  return (joystick->getButtonState(MODE_BUTTON1) || joystick->getButtonState(MODE_BUTTON2));
40  }
41 
42  void updateState(int targetMode){
43  if(targetMode == 0){
44  bool state = getModeButtonState();
45  if(!prevButtonState && state){
46  ++mode_;
47  if(mode_ >= numModes_){
48  mode_ = 0;
49  }
50  }
51  prevButtonState = state;
52  }
53  }
54 
55  int mode() const {
56  return mode_;
57  }
58 
59  bool isMode(int id) const {
60  return (mode_ == id);
61  }
62 
63  void setMode(int id){
64  mode_ = id;
65  }
66 
67  double getPosition(int axis, double threshold = 0.0) const {
68  double pos = joystick->getPosition(axis);
69  return (fabs(pos) >= threshold) ? pos : 0.0;
70  }
71 
72  double getPosition(int targetMode, int axis, double threshold = 0.0) const {
73  return isMode(targetMode) ? getPosition(axis, threshold) : 0.0;
74  }
75 
76  bool getButtonState(int button) const {
77  return joystick->getButtonState(button);
78  }
79 
80  bool getButtonState(int targetMode, int button) const {
81  return isMode(targetMode) ? joystick->getButtonState(button) : false;
82  }
83 
84 private:
85  JoystickInterface* joystick;
86  Joystick defaultJoystick;
87  int mode_;
88  int numModes_;
89  bool prevButtonState;
90 };
91 
93 
94 }
95 
96 #endif
cnoid::SharedJoystick::getButtonState
bool getButtonState(int targetMode, int button) const
Definition: SharedJoystick.h:80
cnoid::Joystick::LOGO_BUTTON
@ LOGO_BUTTON
Definition: Joystick.h:55
cnoid::SharedJoystick::getPosition
double getPosition(int targetMode, int axis, double threshold=0.0) const
Definition: SharedJoystick.h:72
cnoid::SharedJoystick::getPosition
double getPosition(int axis, double threshold=0.0) const
Definition: SharedJoystick.h:67
cnoid::Joystick
Definition: Joystick.h:18
cnoid::SharedJoystick::updateState
void updateState(int targetMode)
Definition: SharedJoystick.h:42
cnoid::SharedJoystick::MODE_BUTTON1
static const Joystick::ButtonID MODE_BUTTON1
Definition: SharedJoystick.h:18
cnoid::SharedJoystick::SharedJoystick
SharedJoystick()
Definition: SharedJoystick.h:21
cnoid::JoystickInterface
Definition: JoystickInterface.h:13
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::SharedJoystick::setJoystick
void setJoystick(JoystickInterface *joystick)
Definition: SharedJoystick.h:28
cnoid::SharedJoystick::MODE_BUTTON2
static const Joystick::ButtonID MODE_BUTTON2
Definition: SharedJoystick.h:19
cnoid::JoystickInterface::readCurrentState
virtual bool readCurrentState()=0
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Joystick::ButtonID
ButtonID
Definition: Joystick.h:44
cnoid::Joystick::SELECT_BUTTON
@ SELECT_BUTTON
Definition: Joystick.h:51
cnoid::SharedJoystick::getModeButtonState
bool getModeButtonState()
Definition: SharedJoystick.h:37
cnoid::Referenced
Definition: Referenced.h:54
cnoid::SharedJoystick::setMode
void setMode(int id)
Definition: SharedJoystick.h:63
cnoid::SharedJoystick::mode
int mode() const
Definition: SharedJoystick.h:55
cnoid::SharedJoystick::addMode
int addMode()
Definition: SharedJoystick.h:32
cnoid::SharedJoystick::getButtonState
bool getButtonState(int button) const
Definition: SharedJoystick.h:76
cnoid::SharedJoystick
Definition: SharedJoystick.h:14
cnoid::SharedJoystick::isMode
bool isMode(int id) const
Definition: SharedJoystick.h:59
cnoid::SharedJoystickPtr
ref_ptr< SharedJoystick > SharedJoystickPtr
Definition: SharedJoystick.h:92
cnoid::JoystickInterface::getPosition
virtual double getPosition(int axis) const =0
cnoid::JoystickInterface::getButtonState
virtual bool getButtonState(int button) const =0