27 #ifdef VRPN_USE_MOTIONNODE
48 #if !defined(MNCAPI_PREVIEW_SIZE)
51 # define MNCAPI_IMPORT_API __declspec(dllimport)
52 # define MNCAPI_CALL_API __cdecl
54 # define MNCAPI_IMPORT_API
55 # define MNCAPI_CALL_API
56 #endif // defined(_WIN32)
60 #define MNCAPI_PREVIEW_SIZE (14)
75 #endif // !defined(MNCAPI_PREVIEW_SIZE)
82 # define MOTION_C_API_LIBRARY "MotionCAPI.dll"
86 # if defined(__APPLE__)
87 # define MOTION_C_API_LIBRARY "libMotionCAPI.dylib"
89 # define MOTION_C_API_LIBRARY "libMotionCAPI.so"
91 # define GetProcAddress dlsym
110 typedef std::vector<float> data_type;
112 Sampler(
const std::string &address,
unsigned port);
115 bool get_data_block(data_type &data,
const unsigned &num_sensor);
120 HMODULE m_library_handle;
122 void *m_library_handle;
135 :
vrpn_Tracker(name, c), m_num_sensor(num_sensor), m_handle(NULL)
137 #if defined(TRACKER_MOTIONNODE_TEST)
138 printf(
"vrpn_Tracker_MotionNode {\n");
139 #endif // TRACKER_MOTIONNODE_TEST
144 std::string remote_address =
"127.0.0.1";
145 unsigned remote_port = 32079;
146 if (NULL != address) {
147 remote_address = address;
157 for (
int i=0; i<4; i++) {
158 sampler_type::data_type data;
159 if (sampler->get_data_block(data, m_num_sensor) && !data.empty()) {
166 if (NULL == m_handle) {
167 fprintf(stderr,
"MotionNode driver failed to start sampling, device not currently reading\n");
169 #if defined(TRACKER_MOTIONNODE_TEST)
172 "Connected to Motion data service at \"%s:%d\"\n",
173 remote_address.c_str(), remote_port);
175 #endif // TRACKER_MOTIONNODE_TEST
179 if ((NULL == m_handle) && (NULL != sampler)) {
184 if (NULL != m_handle) {
190 #if defined(TRACKER_MOTIONNODE_TEST)
192 #endif // TRACKER_MOTIONNODE_TEST
197 #if defined(TRACKER_MOTIONNODE_TEST)
198 printf(
"~vrpn_Tracker_MotionNode() {\n");
199 #endif // TRACKER_MOTIONNODE_TEST
201 if (NULL != m_handle) {
202 sampler_type *sampler = reinterpret_cast<sampler_type *>(m_handle);
208 #if defined(TRACKER_MOTIONNODE_TEST)
209 printf(
"} ~vrpn_Tracker_MotionNode()\n");
210 #endif // TRACKER_MOTIONNODE_TEST
221 void vrpn_Tracker_MotionNode::get_report()
223 if (NULL != m_handle) {
224 sampler_type *sampler = reinterpret_cast<sampler_type *>(m_handle);
226 sampler_type::data_type data;
227 if (sampler->get_data_block(data, m_num_sensor) && !data.empty()) {
229 for (std::size_t i=0; i<m_num_sensor; i++) {
231 if (index + 4 < data.size()) {
248 void vrpn_Tracker_MotionNode::send_report()
255 fprintf(stderr,
"MotionNode: cannot write message: tossing\n");
261 Sampler::Sampler(
const std::string &address,
unsigned port)
262 : m_handle(0), m_library_handle(NULL), m_mncapi_sample(NULL)
269 if (NULL == library_handle) {
270 library_handle = LoadLibrary(
"C:/Program Files/Motion/tools/plugin/capi/" MOTION_C_API_LIBRARY);
276 const char *SearchPaths[3] = {
279 #if defined(__APPLE__)
283 #endif // defined(__APPLE__)
286 void *library_handle = NULL;
287 for (
int i=0; i<3; i++) {
288 library_handle = dlopen(SearchPaths[i], RTLD_LAZY);
289 if (NULL != library_handle) {
290 #if defined(TRACKER_MOTIONNODE_TEST)
291 printf(
"Loaded Motion C API library, \"%s\"\n", SearchPaths[i]);
292 #endif // TRACKER_MOTIONNODE_TEST
297 #endif // defined(_WIN32)
299 if (NULL != library_handle) {
300 m_library_handle = library_handle;
304 reinterpret_cast<MNCAPI_OPEN_HOST_FN>(
GetProcAddress(m_library_handle,
"mncapi_open_host"));
305 if (NULL != mncapi_open_host) {
307 int handle = mncapi_open_host(
MNCAPI_PREVIEW, address.c_str(), static_cast<int>(port));
313 reinterpret_cast<MNCAPI_SAMPLE_FN>(
GetProcAddress(m_library_handle,
"mncapi_sample"));
314 if (NULL != mncapi_sample) {
315 m_mncapi_sample = mncapi_sample;
319 "failed to bind function \"mncapi_sample\" from Motion C API library\n");
325 "failed to connect to Motion data service at \"%s:%d\"\n",
326 address.c_str(), port);
332 "failed to bind function \"mncapi_open_host\" from Motion C API library\n");
344 if (NULL != m_library_handle) {
347 reinterpret_cast<MNCAPI_CLOSE_FN>(
GetProcAddress(m_library_handle,
"mncapi_close"));
348 if (NULL != mncapi_close) {
349 mncapi_close(m_handle);
354 "failed to bind function \"mncapi_close\" from Motion C API library\n");
358 m_mncapi_sample = NULL;
361 if (FreeLibrary(m_library_handle)) {
363 if (0 == dlclose(m_library_handle)) {
364 #endif // defined(_WIN32)
366 fprintf(stderr,
"failed to unload Motion C API library\n");
369 m_library_handle = NULL;
373 bool Sampler::get_data_block(data_type &data,
const unsigned &num_sensor)
377 if ((m_handle > 0) && (NULL != m_mncapi_sample) && (num_sensor > 0)) {
381 int sample_result = m_mncapi_sample(m_handle, &data[0], data.size());
390 #endif // VRPN_USE_MOTIONNODE