-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvtkVRPNAnalog.cxx
151 lines (117 loc) · 3.91 KB
/
vtkVRPNAnalog.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*=========================================================================
Name: vtkVRPNAnalog.cxx
Author: David Borland, The Renaissance Computing Institute (RENCI)
Copyright: The Renaissance Computing Institute (RENCI)
License: Licensed under the RENCI Open Source Software License v. 1.0.
See included License.txt or
http://www.renci.org/resources/open-source-software-license
for details.
=========================================================================*/
#include "vtkVRPNAnalog.h"
#include "vtkObjectFactory.h"
#include "vtkstd/vector"
#include <vrpn_Analog.h>
class vtkVRPNAnalogInternals
{
public:
vtkstd::vector<double> Channel;
};
vtkCxxRevisionMacro(vtkVRPNAnalog, "$Revision: 1.0 $");
vtkStandardNewMacro(vtkVRPNAnalog);
// Callbacks
static void VRPN_CALLBACK HandleAnalog(void* userData, const vrpn_ANALOGCB a);
//----------------------------------------------------------------------------
vtkVRPNAnalog::vtkVRPNAnalog()
{
this->Internals = new vtkVRPNAnalogInternals();
this->Analog = NULL;
this->SetNumberOfChannels(1);
}
//----------------------------------------------------------------------------
vtkVRPNAnalog::~vtkVRPNAnalog()
{
if (this->Analog) delete this->Analog;
delete this->Internals;
}
//----------------------------------------------------------------------------
int vtkVRPNAnalog::Initialize()
{
// Check that the device name is set
if (this->DeviceName == NULL)
{
vtkErrorMacro(<<"DeviceName not set.");
return 0;
}
// Create the VRPN analog remote
this->Analog = new vrpn_Analog_Remote(this->DeviceName);
// Set up the analog callback
if (this->Analog->register_change_handler(this, HandleAnalog) == -1)
{
vtkErrorMacro(<<"Can't register callback.");
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
void vtkVRPNAnalog::Update()
{
if (this->Analog)
{
this->Analog->mainloop();
}
}
//----------------------------------------------------------------------------
void vtkVRPNAnalog::InvokeInteractionEvent()
{
if (this->Analog)
{
// XXX: Should there be a flag to check for new data?
this->InvokeEvent(vtkVRPNDevice::AnalogEvent);
}
}
//----------------------------------------------------------------------------
void vtkVRPNAnalog::SetNumberOfChannels(int num)
{
int currentNum = this->Internals->Channel.size();
this->Internals->Channel.resize(num);
for (int i = currentNum; i < num; i++)
{
this->SetChannel(i, 0.0);
}
}
//----------------------------------------------------------------------------
int vtkVRPNAnalog::GetNumberOfChannels()
{
return this->Internals->Channel.size();
}
//----------------------------------------------------------------------------
void vtkVRPNAnalog::SetChannel(int channel, double value)
{
this->Internals->Channel[channel] = value;
}
//----------------------------------------------------------------------------
double vtkVRPNAnalog::GetChannel(int channel)
{
return this->Internals->Channel[channel];
}
//----------------------------------------------------------------------------
void VRPN_CALLBACK HandleAnalog(void* userData, const vrpn_ANALOGCB a) {
vtkVRPNAnalog* analog = static_cast<vtkVRPNAnalog*>(userData);
int num = analog->GetNumberOfChannels() < a.num_channel ? analog->GetNumberOfChannels() : a.num_channel;
for (int i = 0; i < num; i++)
{
analog->SetChannel(i, a.channel[i]);
}
}
//----------------------------------------------------------------------------
void vtkVRPNAnalog::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Analog: "; this->Analog->print();
os << indent << "Channel: ";
for (unsigned int i = 0; i < this->Internals->Channel.size(); i++)
{
os << this->Internals->Channel[i] << " ";
}
os << "\n";
}