31extern int VRPN_ANALOG_CB;
32extern int VRPN_BTN_CB;
33extern int VRPN_BTN_STATE_CB;
34extern int VRPN_TRACKER_CB;
35extern int VRPN_TRACKER_VEL_CB;
36extern int VRPN_TRACKER_ACC_CB;
37extern int VRPN_DIAL_CB;
38extern int VRPN_TEXT_CB;
51 mEnableAnalogData =
false;
52 mEnableButtonData =
false;
53 mEnableButtonStateData =
false;
54 mEnableTrackerData =
false;
55 mEnableTrackerVelData =
false;
56 mEnableTrackerAccelData =
false;
57 mEnableDialData =
false;
58 mEnableTextData =
false;
63 SAFE_DELETE(m_analog);
64 SAFE_DELETE(m_button);
65 SAFE_DELETE(m_traker);
67 SAFE_DELETE(m_textMessage);
70void SVrpn::EnableAnalogData(
bool state)
73 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
75 boost::mutex::scoped_lock l(m_mutex);
78 mEnableAnalogData = state;
81void SVrpn::EnableButtonData(
bool state)
84 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
86 boost::mutex::scoped_lock l(m_mutex);
88 mEnableButtonData = state;
91void SVrpn::EnableButtonStateData(
bool state)
94 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
96 boost::mutex::scoped_lock l(m_mutex);
98 mEnableButtonStateData = state;
101void SVrpn::EnableTrackerData(
bool state)
104 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
106 boost::mutex::scoped_lock l(m_mutex);
108 mEnableTrackerData = state;
111void SVrpn::EnableTrackerVelData(
bool state)
114 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
116 boost::mutex::scoped_lock l(m_mutex);
118 mEnableTrackerVelData = state;
121void SVrpn::EnableTrackerAccelData(
bool state)
124 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
126 boost::mutex::scoped_lock l(m_mutex);
128 mEnableTrackerAccelData = state;
131void SVrpn::EnableDialData(
bool state)
134 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
136 boost::mutex::scoped_lock l(m_mutex);
138 mEnableDialData = state;
141void SVrpn::EnableTextData(
bool state)
144 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
146 boost::mutex::scoped_lock l(m_mutex);
148 mEnableTextData = state;
151bool SVrpn::GetEnableAnalogData()
153 return mEnableAnalogData;
156bool SVrpn::GetEnableButtonData()
158 return mEnableButtonData;
161bool SVrpn::GetEnableButtonStateData()
163 return mEnableButtonStateData;
166bool SVrpn::GetEnableTrackerData()
168 return mEnableTrackerData;
171bool SVrpn::GetEnableTrackerVelData()
173 return mEnableTrackerVelData;
176bool SVrpn::GetEnableTrackerAccelData()
178 return mEnableTrackerAccelData;
181bool SVrpn::GetEnableDialData()
183 return mEnableDialData;
186bool SVrpn::GetEnableTextData()
188 return mEnableTextData;
191void VRPN_CALLBACK SVrpn::handle_analog(
void* userData,
const vrpn_ANALOGCB analog)
194 if (svrpn->GetEnableAnalogData())
197 for (
int i = 0; i < analog.num_channel; i++)
199 data->analogData.push_back((
float)analog.channel[i]);
202 OBJpostEvent(VRPN_ANALOG_CB, SCOL_PTR svrpn, SCOL_PTR data);
206void VRPN_CALLBACK SVrpn::handle_button_state(
void* userData,
const vrpn_BUTTONCB button)
209 if (svrpn->GetEnableButtonStateData())
212 data->valueId = button.button;
213 data->btnState = button.state;
214 OBJpostEvent(VRPN_BTN_STATE_CB, SCOL_PTR svrpn, SCOL_PTR data);
218void VRPN_CALLBACK SVrpn::handle_button_states(
void* userData,
const vrpn_BUTTONSTATESCB button)
221 if (svrpn->GetEnableButtonData())
224 for (
int i = 0; i < button.num_buttons; i++)
226 data->btnData.push_back(button.states[i]);
229 OBJpostEvent(VRPN_BTN_CB, SCOL_PTR svrpn, SCOL_PTR data);
233void VRPN_CALLBACK SVrpn::handle_tracker(
void* userData,
const vrpn_TRACKERCB tracker)
236 if (svrpn->GetEnableTrackerData())
239 data->valueId = tracker.sensor;
240 data->vectorData.push_back((
float)tracker.pos[0]);
241 data->vectorData.push_back((
float)tracker.pos[1]);
242 data->vectorData.push_back((
float)tracker.pos[2]);
244 data->quatData.push_back((
float)tracker.quat[0]);
245 data->quatData.push_back((
float)tracker.quat[1]);
246 data->quatData.push_back((
float)tracker.quat[2]);
247 data->quatData.push_back((
float)tracker.quat[3]);
249 OBJpostEvent(VRPN_TRACKER_CB, SCOL_PTR svrpn, SCOL_PTR data);
253void VRPN_CALLBACK SVrpn::handle_tracker_vel(
void* userData,
const vrpn_TRACKERVELCB tracker)
256 if (svrpn->GetEnableTrackerVelData())
259 data->valueId = tracker.sensor;
260 data->vectorData.push_back((
float)tracker.vel[0]);
261 data->vectorData.push_back((
float)tracker.vel[1]);
262 data->vectorData.push_back((
float)tracker.vel[2]);
264 data->quatData.push_back((
float)tracker.vel_quat[0]);
265 data->quatData.push_back((
float)tracker.vel_quat[1]);
266 data->quatData.push_back((
float)tracker.vel_quat[2]);
267 data->quatData.push_back((
float)tracker.vel_quat[3]);
269 OBJpostEvent(VRPN_TRACKER_VEL_CB, SCOL_PTR svrpn, SCOL_PTR data);
273void VRPN_CALLBACK SVrpn::handle_tracker_acc(
void* userData,
const vrpn_TRACKERACCCB tracker)
276 if (svrpn->GetEnableTrackerAccelData())
279 data->valueId = tracker.sensor;
280 data->vectorData.push_back((
float)tracker.acc[0]);
281 data->vectorData.push_back((
float)tracker.acc[1]);
282 data->vectorData.push_back((
float)tracker.acc[2]);
284 data->quatData.push_back((
float)tracker.acc_quat[0]);
285 data->quatData.push_back((
float)tracker.acc_quat[1]);
286 data->quatData.push_back((
float)tracker.acc_quat[2]);
287 data->quatData.push_back((
float)tracker.acc_quat[3]);
289 OBJpostEvent(VRPN_TRACKER_ACC_CB, SCOL_PTR svrpn, SCOL_PTR data);
293void VRPN_CALLBACK SVrpn::handle_dial(
void* userData,
const vrpn_DIALCB dial)
296 if (svrpn->GetEnableDialData())
301void VRPN_CALLBACK SVrpn::handle_text(
void* userData,
const vrpn_TEXTCB text)
304 if (svrpn->GetEnableTextData() && (text.type == vrpn_TEXT_NORMAL))
307 data->textData = text.message;
309 OBJpostEvent(VRPN_TEXT_CB, SCOL_PTR svrpn, SCOL_PTR data);
316 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
318 boost::mutex::scoped_lock l(m_mutex);
322 if (m_analog == 0 && m_button == 0 && m_traker == 0 && m_dial == 0 && m_textMessage == 0)
324 std::string deviceName = m_name +
"@" + m_host;
325 m_analog =
new vrpn_Analog_Remote(deviceName.c_str());
326 m_analog->register_change_handler(
this, SVrpn::handle_analog);
328 m_button =
new vrpn_Button_Remote(deviceName.c_str());
329 m_button->register_change_handler(
this, SVrpn::handle_button_state);
330 m_button->register_states_handler(
this, SVrpn::handle_button_states);
332 m_traker =
new vrpn_Tracker_Remote(deviceName.c_str());
333 m_traker->register_change_handler(
this, SVrpn::handle_tracker);
334 m_traker->register_change_handler(
this, SVrpn::handle_tracker_vel);
335 m_traker->register_change_handler(
this, SVrpn::handle_tracker_acc);
337 m_dial =
new vrpn_Dial_Remote(deviceName.c_str());
338 m_dial->register_change_handler(
this, SVrpn::handle_dial);
340 m_textMessage =
new vrpn_Text_Receiver(deviceName.c_str());
341 m_textMessage->register_message_handler(
this, SVrpn::handle_text);
345 if (m_analog && mEnableAnalogData)
346 m_analog->mainloop();
348 if (m_button && (mEnableButtonData || mEnableButtonStateData))
349 m_button->mainloop();
351 if (m_traker && (mEnableTrackerData || mEnableTrackerAccelData || mEnableTrackerVelData))
352 m_traker->mainloop();
354 if (m_dial && mEnableDialData)
357 if (m_textMessage && mEnableTextData)
358 m_textMessage->mainloop();
366sVrpnThread::sVrpnThread()
369 m_thread = boost::thread(boost::bind(&sVrpnThread::UpdateThread,
this));
372sVrpnThread::~sVrpnThread()
388void sVrpnThread::Kill()
397SVrpn* sVrpnThread::AddDevice(
const std::string& host,
const std::string& name)
400 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
402 boost::mutex::scoped_lock l(m_mutex);
405 deviceList.insert(device);
409void sVrpnThread::RemoveDevice(
SVrpn* device)
413 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
415 boost::mutex::scoped_lock l(m_mutex);
417 DeviceList::iterator idevice = deviceList.find(device);
418 if (idevice != deviceList.end())
420 deviceList.erase(idevice);
425 if(deviceList.size() == 0)
431void sVrpnThread::UpdateThread()
437 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
439 boost::mutex::scoped_lock l(m_mutex);
442 DeviceList::iterator idevice;
443 for (idevice = deviceList.begin(); idevice != deviceList.end(); idevice++)
445 SVrpn* device = (*idevice);
SVrpn(const std::string &host, const std::string &name)