VRPN Scol plugin
Loading...
Searching...
No Matches
sVrpn.cpp
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2013 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25
26
27#include <iostream>
28#include "sVrpn.h"
29
30
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;
39
40SVrpn::SVrpn(const std::string& host, const std::string& name)
41{
42 m_host = host;
43 m_name = name;
44
45 m_analog = 0;
46 m_button = 0;
47 m_traker = 0;
48 m_dial = 0;
49 m_textMessage = 0;
50
51 mEnableAnalogData = false;
52 mEnableButtonData = false;
53 mEnableButtonStateData = false;
54 mEnableTrackerData = false;
55 mEnableTrackerVelData = false;
56 mEnableTrackerAccelData = false;
57 mEnableDialData = false;
58 mEnableTextData = false;
59}
60
62{
63 SAFE_DELETE(m_analog);
64 SAFE_DELETE(m_button);
65 SAFE_DELETE(m_traker);
66 SAFE_DELETE(m_dial);
67 SAFE_DELETE(m_textMessage);
68}
69
70void SVrpn::EnableAnalogData(bool state)
71{
72#ifdef SCOL_STATIC
73 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
74#else
75 boost::mutex::scoped_lock l(m_mutex);
76#endif
77
78 mEnableAnalogData = state;
79}
80
81void SVrpn::EnableButtonData(bool state)
82{
83#ifdef SCOL_STATIC
84 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
85#else
86 boost::mutex::scoped_lock l(m_mutex);
87#endif
88 mEnableButtonData = state;
89}
90
91void SVrpn::EnableButtonStateData(bool state)
92{
93#ifdef SCOL_STATIC
94 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
95#else
96 boost::mutex::scoped_lock l(m_mutex);
97#endif
98 mEnableButtonStateData = state;
99}
100
101void SVrpn::EnableTrackerData(bool state)
102{
103#ifdef SCOL_STATIC
104 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
105#else
106 boost::mutex::scoped_lock l(m_mutex);
107#endif
108 mEnableTrackerData = state;
109}
110
111void SVrpn::EnableTrackerVelData(bool state)
112{
113#ifdef SCOL_STATIC
114 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
115#else
116 boost::mutex::scoped_lock l(m_mutex);
117#endif
118 mEnableTrackerVelData = state;
119}
120
121void SVrpn::EnableTrackerAccelData(bool state)
122{
123#ifdef SCOL_STATIC
124 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
125#else
126 boost::mutex::scoped_lock l(m_mutex);
127#endif
128 mEnableTrackerAccelData = state;
129}
130
131void SVrpn::EnableDialData(bool state)
132{
133#ifdef SCOL_STATIC
134 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
135#else
136 boost::mutex::scoped_lock l(m_mutex);
137#endif
138 mEnableDialData = state;
139}
140
141void SVrpn::EnableTextData(bool state)
142{
143#ifdef SCOL_STATIC
144 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
145#else
146 boost::mutex::scoped_lock l(m_mutex);
147#endif
148 mEnableTextData = state;
149}
150
151bool SVrpn::GetEnableAnalogData()
152{
153 return mEnableAnalogData;
154}
155
156bool SVrpn::GetEnableButtonData()
157{
158 return mEnableButtonData;
159}
160
161bool SVrpn::GetEnableButtonStateData()
162{
163 return mEnableButtonStateData;
164}
165
166bool SVrpn::GetEnableTrackerData()
167{
168 return mEnableTrackerData;
169}
170
171bool SVrpn::GetEnableTrackerVelData()
172{
173 return mEnableTrackerVelData;
174}
175
176bool SVrpn::GetEnableTrackerAccelData()
177{
178 return mEnableTrackerAccelData;
179}
180
181bool SVrpn::GetEnableDialData()
182{
183 return mEnableDialData;
184}
185
186bool SVrpn::GetEnableTextData()
187{
188 return mEnableTextData;
189}
190
191void VRPN_CALLBACK SVrpn::handle_analog(void* userData, const vrpn_ANALOGCB analog)
192{
193 SVrpn* svrpn = (SVrpn*)userData;
194 if (svrpn->GetEnableAnalogData())
195 {
196 SCbData* data = new SCbData();
197 for (int i = 0; i < analog.num_channel; i++)
198 {
199 data->analogData.push_back((float)analog.channel[i]);
200 }
201
202 OBJpostEvent(VRPN_ANALOG_CB, SCOL_PTR svrpn, SCOL_PTR data);
203 }
204}
205
206void VRPN_CALLBACK SVrpn::handle_button_state(void* userData, const vrpn_BUTTONCB button)
207{
208 SVrpn* svrpn = (SVrpn*)userData;
209 if (svrpn->GetEnableButtonStateData())
210 {
211 SCbData* data = new SCbData();
212 data->valueId = button.button;
213 data->btnState = button.state;
214 OBJpostEvent(VRPN_BTN_STATE_CB, SCOL_PTR svrpn, SCOL_PTR data);
215 }
216}
217
218void VRPN_CALLBACK SVrpn::handle_button_states(void* userData, const vrpn_BUTTONSTATESCB button)
219{
220 SVrpn* svrpn = (SVrpn*)userData;
221 if (svrpn->GetEnableButtonData())
222 {
223 SCbData* data = new SCbData();
224 for (int i = 0; i < button.num_buttons; i++)
225 {
226 data->btnData.push_back(button.states[i]);
227 }
228
229 OBJpostEvent(VRPN_BTN_CB, SCOL_PTR svrpn, SCOL_PTR data);
230 }
231}
232
233void VRPN_CALLBACK SVrpn::handle_tracker(void* userData, const vrpn_TRACKERCB tracker)
234{
235 SVrpn* svrpn = (SVrpn*)userData;
236 if (svrpn->GetEnableTrackerData())
237 {
238 SCbData* data = new SCbData();
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]);
243
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]);
248
249 OBJpostEvent(VRPN_TRACKER_CB, SCOL_PTR svrpn, SCOL_PTR data);
250 }
251}
252
253void VRPN_CALLBACK SVrpn::handle_tracker_vel(void* userData, const vrpn_TRACKERVELCB tracker)
254{
255 SVrpn* svrpn = (SVrpn*)userData;
256 if (svrpn->GetEnableTrackerVelData())
257 {
258 SCbData* data = new SCbData();
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]);
263
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]);
268
269 OBJpostEvent(VRPN_TRACKER_VEL_CB, SCOL_PTR svrpn, SCOL_PTR data);
270 }
271}
272
273void VRPN_CALLBACK SVrpn::handle_tracker_acc(void* userData, const vrpn_TRACKERACCCB tracker)
274{
275 SVrpn* svrpn = (SVrpn*)userData;
276 if (svrpn->GetEnableTrackerAccelData())
277 {
278 SCbData* data = new SCbData();
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]);
283
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]);
288
289 OBJpostEvent(VRPN_TRACKER_ACC_CB, SCOL_PTR svrpn, SCOL_PTR data);
290 }
291}
292
293void VRPN_CALLBACK SVrpn::handle_dial(void* userData, const vrpn_DIALCB dial)
294{
295 SVrpn* svrpn = (SVrpn*)userData;
296 if (svrpn->GetEnableDialData())
297 {
298 }
299}
300
301void VRPN_CALLBACK SVrpn::handle_text(void* userData, const vrpn_TEXTCB text)
302{
303 SVrpn* svrpn = (SVrpn*)userData;
304 if (svrpn->GetEnableTextData() && (text.type == vrpn_TEXT_NORMAL))
305 {
306 SCbData* data = new SCbData();
307 data->textData = text.message;
308
309 OBJpostEvent(VRPN_TEXT_CB, SCOL_PTR svrpn, SCOL_PTR data);
310 }
311}
312
313void SVrpn::Update()
314{
315#ifdef SCOL_STATIC
316 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
317#else
318 boost::mutex::scoped_lock l(m_mutex);
319#endif
320
321 // try to connect
322 if (m_analog == 0 && m_button == 0 && m_traker == 0 && m_dial == 0 && m_textMessage == 0)
323 {
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);
327
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);
331
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);
336
337 m_dial = new vrpn_Dial_Remote(deviceName.c_str());
338 m_dial->register_change_handler(this, SVrpn::handle_dial);
339
340 m_textMessage = new vrpn_Text_Receiver(deviceName.c_str());
341 m_textMessage->register_message_handler(this, SVrpn::handle_text);
342 }
343 else // update
344 {
345 if (m_analog && mEnableAnalogData)
346 m_analog->mainloop();
347
348 if (m_button && (mEnableButtonData || mEnableButtonStateData))
349 m_button->mainloop();
350
351 if (m_traker && (mEnableTrackerData || mEnableTrackerAccelData || mEnableTrackerVelData))
352 m_traker->mainloop();
353
354 if (m_dial && mEnableDialData)
355 m_dial->mainloop();
356
357 if (m_textMessage && mEnableTextData)
358 m_textMessage->mainloop();
359 }
360}
361
362
363// Initialize singleton
364sVrpnThread* sVrpnThread::_singleton = 0;
365
366sVrpnThread::sVrpnThread()
367{
368 m_terminate = false;
369 m_thread = boost::thread(boost::bind(&sVrpnThread::UpdateThread, this));
370}
371
372sVrpnThread::~sVrpnThread()
373{
374 m_terminate = true;
375 m_thread.join();
376}
377
378sVrpnThread* sVrpnThread::GetInstance()
379{
380 if (0 == _singleton)
381 {
382 _singleton = new sVrpnThread();
383 }
384
385 return _singleton;
386}
387
388void sVrpnThread::Kill()
389{
390 if (0 != _singleton)
391 {
392 delete _singleton;
393 _singleton = 0;
394 }
395}
396
397SVrpn* sVrpnThread::AddDevice(const std::string& host, const std::string& name)
398{
399#ifdef SCOL_STATIC
400 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
401#else
402 boost::mutex::scoped_lock l(m_mutex);
403#endif
404 SVrpn* device = new SVrpn(host, name);
405 deviceList.insert(device);
406 return device;
407}
408
409void sVrpnThread::RemoveDevice(SVrpn* device)
410{
411 {
412#ifdef SCOL_STATIC
413 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
414#else
415 boost::mutex::scoped_lock l(m_mutex);
416#endif
417 DeviceList::iterator idevice = deviceList.find(device);
418 if (idevice != deviceList.end())
419 {
420 deviceList.erase(idevice);
421 SAFE_DELETE(device);
422 }
423 }
424
425 if(deviceList.size() == 0)
426 {
427 Kill();
428 }
429}
430
431void sVrpnThread::UpdateThread()
432{
433 while (!m_terminate)
434 {
435 {
436#ifdef SCOL_STATIC
437 boost::unique_lock<boost::recursive_mutex> l(m_mutex);
438#else
439 boost::mutex::scoped_lock l(m_mutex);
440#endif
441
442 DeviceList::iterator idevice;
443 for (idevice = deviceList.begin(); idevice != deviceList.end(); idevice++)
444 {
445 SVrpn* device = (*idevice);
446 device->Update();
447 }
448 }
449
450 // Sleep for 1ms so we don't eat the CPU
451 vrpn_SleepMsecs(30);
452 }
453}
454
Definition sVrpn.h:88
~SVrpn()
Definition sVrpn.cpp:61
SVrpn(const std::string &host, const std::string &name)
Definition sVrpn.cpp:40