iMSTK
Interactive Medical Simulation Toolkit
imstkVRPNDeviceManager.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkVRPNDeviceManager.h"
8 
9 #include "vrpn_Analog.h"
10 #include "vrpn_Button.h"
11 #include "vrpn_Tracker.h"
12 
13 #include "imstkDeviceClient.h"
14 #include "imstkLogger.h"
15 #include "imstkVRPNDeviceClient.h"
16 
17 namespace imstk
18 {
19 VRPNDeviceManager::VRPNDeviceManager(const std::string& machine /*= "localhost"*/,
20  int port /*= vrpn_DEFAULT_LISTEN_PORT_NO*/) :
21  m_machine(machine),
22  m_port(port),
23  m_deviceConnections(new vrpn_MainloopContainer())
24 {
25  setSleepDelay(1000 / 60);
26 }
27 
28 void
29 VRPNDeviceManager::addDeviceClient(std::shared_ptr<VRPNDeviceClient> client)
30 {
31  std::string name = client->getDeviceName();
32  void* handle = client.get();
33 
34  std::string address = name + "@" + m_machine;
35  const char* _address = address.c_str();
36 
37  int type = client->getType();
38 
39  if ( (type & VRPNAnalog) != 0)
40  {
41  LOG(INFO) << "Adding Analog Device: " << name;
42  vrpn_Analog_Remote* vrpnAnalog = new vrpn_Analog_Remote(_address);
43  m_deviceConnections->add(vrpnAnalog);
44  vrpnAnalog->register_change_handler(handle, VRPNDeviceClient::analogChangeHandler);
45  m_deviceMap[VRPNAnalog].push_back(client);
46  }
47  if ( (type & VRPNTracker) != 0)
48  {
49  LOG(INFO) << "Adding Tracker Device: " << name;
50  vrpn_Tracker_Remote* vrpnTracker = new vrpn_Tracker_Remote(_address);
51  m_deviceConnections->add(vrpnTracker);
52  vrpnTracker->register_change_handler(handle, VRPNDeviceClient::trackerPositionChangeHandler);
53  vrpnTracker->register_change_handler(handle, VRPNDeviceClient::trackerVelocityChangeHandler);
54  m_deviceMap[VRPNTracker].push_back(client);
55  }
56  if ( (type & VRPNButton) != 0)
57  {
58  LOG(INFO) << "Adding Button Device: " << name;
59  vrpn_Button_Remote* vrpnButton = new vrpn_Button_Remote(_address);
60  m_deviceConnections->add(vrpnButton);
61  vrpnButton->register_change_handler(handle, VRPNDeviceClient::buttonChangeHandler);
62  m_deviceMap[VRPNButton].push_back(client);
63  }
64 }
65 
66 std::shared_ptr<imstk::DeviceClient>
67 VRPNDeviceManager::makeDeviceClient(const std::string& deviceName, VRPNDeviceType deviceType)
68 {
69  auto client = std::make_shared<VRPNDeviceClient>(deviceName, deviceType, m_machine);
70  addDeviceClient(client);
71  return client;
72 }
73 
74 std::shared_ptr<imstk::DeviceClient>
75 VRPNDeviceManager::makeDeviceClient(const std::string deviceName)
76 {
77  return makeDeviceClient(deviceName, VRPNAnalog);
78 }
79 
80 bool
82 {
83  return true;
84 }
85 
86 void
88 {
89  m_deviceConnections->mainloop();
90 }
91 
92 void
94 {
95  m_deviceConnections->clear();
96 }
97 } // imstk
static void VRPN_CALLBACK trackerPositionChangeHandler(void *userData, const _vrpn_TRACKERCB t)
VRPN call back for position and orientation data.
static void VRPN_CALLBACK trackerVelocityChangeHandler(void *userData, const _vrpn_TRACKERVELCB v)
VRPN call back for velocity data.
void updateModule() override
Run the server module.
Compound Geometry.
std::shared_ptr< DeviceClient > makeDeviceClient(const std::string &deviceName, VRPNDeviceType deviceType)
Creates a client from the given parameters.
static void VRPN_CALLBACK buttonChangeHandler(void *userData, const _vrpn_BUTTONCB b)
VRPN call back for button changed (pressed or released)
VRPNDeviceManager(const std::string &machine="localhost", int port=vrpn_DEFAULT_LISTEN_PORT_NO)
Constructor.
static void VRPN_CALLBACK analogChangeHandler(void *userData, const _vrpn_ANALOGCB a)
VRPN call back analog data.
void uninitModule() override
Clean the server module.
bool initModule() override
Initialize the server module.