Joystick.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "Joystick.h"
11 #include "js.h"
12 
13 // Module specification
14 // <rtc-template block="module_spec">
15 static const char* joystick_spec[] =
16  {
17  "implementation_id", "Joystick",
18  "type_name", "Joystick",
19  "description", "Access a joystick control device.",
20  "version", HRPSYS_PACKAGE_VERSION,
21  "vendor", "AIST",
22  "category", "Human input",
23  "activity_type", "PERIODIC",
24  "kind", "DataFlowComponent",
25  "max_instance", "1",
26  "language", "C++",
27  "lang_type", "compile",
28  // Configuration variables
29  "conf.default.device", "/dev/input/js1",
30  "conf.default.debugLevel", "0",
31  ""
32  };
33 // </rtc-template>
34 
40  // <rtc-template block="initializer">
41  : RTC::DataFlowComponentBase(manager),
42  m_axesOut("Axes", m_axes),
43  m_buttonsOut("Buttons", m_buttons),
44  m_debugLevel(0)
45 
46  // </rtc-template>
47 {
48 }
49 
54 {
55 }
56 
57 
58 
59 RTC::ReturnCode_t Joystick::onInitialize()
60 {
61  // <rtc-template block="bind_config">
62  // Bind variables and configuration variable
63  bindParameter("device", m_device, "/dev/input/js1");
64  bindParameter("debugLevel", m_debugLevel, "0");
65 
66  // Registration: InPort/OutPort/Service
67  // <rtc-template block="registration">
68  // Set InPort buffers
69 
70  // Set OutPort buffer
71  addOutPort("Axes", m_axesOut);
72  addOutPort("Buttons", m_buttonsOut);
73 
74  // Set service provider to Ports
75 
76  // Set service consumers to Ports
77 
78  // Set CORBA Service Ports
79 
80  // </rtc-template>
81 
82  return RTC::RTC_OK;
83 }
84 
85 
86 /*
87 RTC::ReturnCode_t Joystick::onFinalize()
88 {
89  return RTC::RTC_OK;
90 }
91 */
92 
93 /*
94 RTC::ReturnCode_t Joystick::onStartup(RTC::UniqueId ec_id)
95 {
96  return RTC::RTC_OK;
97 }
98 */
99 
100 /*
101 RTC::ReturnCode_t Joystick::onShutdown(RTC::UniqueId ec_id)
102 {
103  return RTC::RTC_OK;
104 }
105 */
106 
107 RTC::ReturnCode_t Joystick::onActivated(RTC::UniqueId ec_id)
108 {
109  std::cout << "Joystick::onActivated(" << ec_id << ")" << std::endl;
110  m_js = new joystick(m_device.c_str());
111  if (m_js->is_open()){
112  m_axes.data.length(m_js->nAxes());
113  for (unsigned int i=0; i<m_js->nAxes(); i++){
114  m_axes.data[i] = m_js->getAxisState(i);
115  }
116  m_buttons.data.length(m_js->nButtons());
117  for (unsigned int i=0; i<m_js->nButtons(); i++){
118  m_buttons.data[i] = m_js->getButtonState(i);
119  }
120  return RTC::RTC_OK;
121  }else{
122  std::cerr << "Joystick device(" << m_device << ") is not opened" << std::endl;
123  return RTC::RTC_ERROR;
124  }
125 }
126 
127 
128 RTC::ReturnCode_t Joystick::onDeactivated(RTC::UniqueId ec_id)
129 {
130  std::cout << "Joystick::onDeactivated(" << ec_id << ")" << std::endl;
131  delete m_js;
132  return RTC::RTC_OK;
133 }
134 
135 
136 
137 RTC::ReturnCode_t Joystick::onExecute(RTC::UniqueId ec_id)
138 {
139  while(m_js->readEvent());
140  if (m_debugLevel > 0) printf("axes:");
141  for (unsigned int i=0; i<m_js->nAxes(); i++){
142  m_axes.data[i] = m_js->getAxisState(i);
143  if (m_debugLevel > 0) printf("%4.1f ", m_axes.data[i]);
144  }
145  if (m_debugLevel > 0) printf(", buttons:");
146  for (unsigned int i=0; i<m_js->nButtons(); i++){
147  m_buttons.data[i] = m_js->getButtonState(i);
148  if (m_debugLevel > 0) printf("%d", m_buttons.data[i]);
149  }
150  if (m_debugLevel > 0) printf("\n");
151 
152  m_axesOut.write();
154 
155  return RTC::RTC_OK;
156 }
157 
158 
159 /*
160 RTC::ReturnCode_t Joystick::onAborting(RTC::UniqueId ec_id)
161 {
162  return RTC::RTC_OK;
163 }
164 */
165 
166 /*
167 RTC::ReturnCode_t Joystick::onError(RTC::UniqueId ec_id)
168 {
169  return RTC::RTC_OK;
170 }
171 */
172 
173 /*
174 RTC::ReturnCode_t Joystick::onReset(RTC::UniqueId ec_id)
175 {
176  return RTC::RTC_OK;
177 }
178 */
179 
180 /*
181 RTC::ReturnCode_t Joystick::onStateUpdate(RTC::UniqueId ec_id)
182 {
183  return RTC::RTC_OK;
184 }
185 */
186 
187 /*
188 RTC::ReturnCode_t Joystick::onRateChanged(RTC::UniqueId ec_id)
189 {
190  return RTC::RTC_OK;
191 }
192 */
193 
194 
195 
196 extern "C"
197 {
198 
199  void JoystickInit(RTC::Manager* manager)
200  {
202  manager->registerFactory(profile,
203  RTC::Create<Joystick>,
204  RTC::Delete<Joystick>);
205  }
206 
207 };
208 
209 
png_infop png_charpp int png_charpp profile
void JoystickInit(RTC::Manager *manager)
Definition: Joystick.cpp:199
OutPort< TimedFloatSeq > m_axesOut
Definition: Joystick.h:245
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
Definition: Joystick.cpp:128
TimedBooleanSeq m_buttons
Definition: Joystick.h:246
Access a joystick control device.
bool readEvent()
Definition: js.cpp:40
png_uint_32 i
virtual RTC::ReturnCode_t onInitialize()
Definition: Joystick.cpp:59
bool addOutPort(const char *name, OutPortBase &outport)
bool getButtonState(int i_index) const
Definition: js.h:13
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
Definition: Joystick.cpp:137
std::string m_device
Definition: Joystick.h:282
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
joystick * m_js
Definition: Joystick.h:281
OutPort< TimedBooleanSeq > m_buttonsOut
Definition: Joystick.h:253
unsigned int m_debugLevel
Definition: Joystick.h:283
TimedFloatSeq m_axes
Definition: Joystick.h:237
Joystick(RTC::Manager *manager)
constructor
Definition: Joystick.cpp:39
unsigned int nAxes() const
Definition: js.h:17
Definition: js.h:6
static const char * joystick_spec[]
Definition: Joystick.cpp:15
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
Definition: Joystick.cpp:107
virtual bool write(DataType &value)
bool is_open() const
Definition: js.h:15
virtual ~Joystick()
destructor
Definition: Joystick.cpp:53
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
float getAxisState(int i_index) const
Definition: js.h:14
unsigned int nButtons() const
Definition: js.h:16


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50