Panda3D
vrpnAnalog.cxx
1 // Filename: vrpnAnalog.cxx
2 // Created by: drose (26Jan01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "vrpnAnalog.h"
16 #include "vrpnAnalogDevice.h"
17 #include "vrpnClient.h"
18 #include "config_vrpn.h"
19 
20 #include "indent.h"
21 
22 #include <algorithm>
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: VrpnAnalog::Constructor
26 // Access: Public
27 // Description:
28 ////////////////////////////////////////////////////////////////////
29 VrpnAnalog::
30 VrpnAnalog(const string &analog_name, vrpn_Connection *connection) :
31  _analog_name(analog_name)
32 {
33  _analog = new vrpn_Analog_Remote(_analog_name.c_str(), connection);
34 
35  _analog->register_change_handler((void*)this, &vrpn_analog_callback);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: VrpnAnalog::Destructor
40 // Access: Public
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 VrpnAnalog::
44 ~VrpnAnalog() {
45  delete _analog;
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: VrpnAnalog::mark
50 // Access: Public
51 // Description: Adds the indicated VrpnAnalogDevice to the list of
52 // devices that are sharing this VrpnAnalog.
53 ////////////////////////////////////////////////////////////////////
54 void VrpnAnalog::
56  if (vrpn_cat.is_debug()) {
57  vrpn_cat.debug() << *this << " marking " << *device << "\n";
58  }
59  _devices.push_back(device);
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: VrpnAnalog::unmark
64 // Access: Public
65 // Description: Removes the indicated VrpnAnalogDevice from the list
66 // of devices that are sharing this VrpnAnalog.
67 ////////////////////////////////////////////////////////////////////
68 void VrpnAnalog::
70  if (vrpn_cat.is_debug()) {
71  vrpn_cat.debug() << *this << " unmarking " << *device << "\n";
72  }
73 
74  Devices::iterator di =
75  find(_devices.begin(), _devices.end(), device);
76 
77  if (di != _devices.end()) {
78  _devices.erase(di);
79  }
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: VrpnAnalog::output
84 // Access: Public
85 // Description:
86 ////////////////////////////////////////////////////////////////////
87 void VrpnAnalog::
88 output(ostream &out) const {
89  out << _analog_name;
90 }
91 
92 ////////////////////////////////////////////////////////////////////
93 // Function: VrpnAnalog::write
94 // Access: Public
95 // Description:
96 ////////////////////////////////////////////////////////////////////
97 void VrpnAnalog::
98 write(ostream &out, int indent_level) const {
99  indent(out, indent_level)
100  << get_analog_name() << " ("
101  << _devices.size() << " devices)\n";
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: VrpnAnalog::vrpn_analog_callback
106 // Access: Private, Static
107 // Description: Receives the analog event data from the VRPN
108 // code and sends it to any interested
109 // VrpnAnalogDevices.
110 ////////////////////////////////////////////////////////////////////
111 void VRPN_CALLBACK VrpnAnalog::
112 vrpn_analog_callback(void *userdata, const vrpn_ANALOGCB info) {
113  VrpnAnalog *self = (VrpnAnalog *)userdata;
114 
115  Devices::iterator di;
116  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
117  VrpnAnalogDevice *device = (*di);
118  device->acquire();
119  for (int i = 0; i < info.num_channel; i++) {
120  if (vrpn_cat.is_debug()) {
121  if (device->get_control_state(i) != info.channel[i]) {
122  vrpn_cat.debug()
123  << *self << " got analog " << i << " = " << info.channel[i] << "\n";
124  }
125  }
126  device->set_control_state(i, info.channel[i]);
127  }
128  device->unlock();
129  }
130 }
double get_control_state(int index) const
Returns the current position of indicated analog control (identified by its index number)...
void set_control_state(int index, double state)
Sets the state of the indicated analog index.
void unmark(VrpnAnalogDevice *device)
Removes the indicated VrpnAnalogDevice from the list of devices that are sharing this VrpnAnalog...
Definition: vrpnAnalog.cxx:69
void unlock()
Releases the mutex associated with this particular device.
Definition: clientDevice.I:90
The Panda interface to a VRPN analog device.
void acquire()
Grabs the mutex associated with this particular device.
Definition: clientDevice.I:76
const string & get_analog_name() const
Returns the name of the analog device that was used to create this VrpnAnalog.
Definition: vrpnAnalog.I:22
void mark(VrpnAnalogDevice *device)
Adds the indicated VrpnAnalogDevice to the list of devices that are sharing this VrpnAnalog.
Definition: vrpnAnalog.cxx:55
This is the actual interface to a particular VRPN analog device, and all of its numbered controls...
Definition: vrpnAnalog.h:42