Panda3D
vrpnAnalog.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file vrpnAnalog.cxx
10  * @author drose
11  * @date 2001-01-26
12  */
13 
14 #include "vrpnAnalog.h"
15 #include "vrpnAnalogDevice.h"
16 #include "vrpnClient.h"
17 #include "config_vrpn.h"
18 
19 #include "indent.h"
20 
21 #include <algorithm>
22 
23 /**
24  *
25  */
26 VrpnAnalog::
27 VrpnAnalog(const std::string &analog_name, vrpn_Connection *connection) :
28  _analog_name(analog_name)
29 {
30  _analog = new vrpn_Analog_Remote(_analog_name.c_str(), connection);
31 
32  _analog->register_change_handler((void*)this, &vrpn_analog_callback);
33 }
34 
35 /**
36  *
37  */
38 VrpnAnalog::
39 ~VrpnAnalog() {
40  delete _analog;
41 }
42 
43 /**
44  * Adds the indicated VrpnAnalogDevice to the list of devices that are sharing
45  * this VrpnAnalog.
46  */
48 mark(VrpnAnalogDevice *device) {
49  if (vrpn_cat.is_debug()) {
50  vrpn_cat.debug() << *this << " marking " << *device << "\n";
51  }
52  _devices.push_back(device);
53 }
54 
55 /**
56  * Removes the indicated VrpnAnalogDevice from the list of devices that are
57  * sharing this VrpnAnalog.
58  */
60 unmark(VrpnAnalogDevice *device) {
61  if (vrpn_cat.is_debug()) {
62  vrpn_cat.debug() << *this << " unmarking " << *device << "\n";
63  }
64 
65  Devices::iterator di =
66  find(_devices.begin(), _devices.end(), device);
67 
68  if (di != _devices.end()) {
69  _devices.erase(di);
70  }
71 }
72 
73 /**
74  * Polls the connected device. Normally you should not call this directly;
75  * this will be called by the VrpnClient.
76  */
78 poll() {
79  _analog->mainloop();
80 }
81 
82 /**
83  *
84  */
85 void VrpnAnalog::
86 output(std::ostream &out) const {
87  out << _analog_name;
88 }
89 
90 /**
91  *
92  */
93 void VrpnAnalog::
94 write(std::ostream &out, int indent_level) const {
95  indent(out, indent_level)
96  << get_analog_name() << " ("
97  << _devices.size() << " devices)\n";
98 }
99 
100 /**
101  * Receives the analog event data from the VRPN code and sends it to any
102  * interested VrpnAnalogDevices.
103  */
104 void VRPN_CALLBACK VrpnAnalog::
105 vrpn_analog_callback(void *userdata, const vrpn_ANALOGCB info) {
106  VrpnAnalog *self = (VrpnAnalog *)userdata;
107 
108  Devices::iterator di;
109  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
110  VrpnAnalogDevice *device = (*di);
111  for (int i = 0; i < info.num_channel; i++) {
112  device->set_axis_value(i, info.channel[i]);
113  }
114  }
115 }
The Panda interface to a VRPN analog device.
This is the actual interface to a particular VRPN analog device, and all of its numbered controls.
Definition: vrpnAnalog.h:38
const std::string & get_analog_name() const
Returns the name of the analog device that was used to create this VrpnAnalog.
Definition: vrpnAnalog.I:19
void unmark(VrpnAnalogDevice *device)
Removes the indicated VrpnAnalogDevice from the list of devices that are sharing this VrpnAnalog.
Definition: vrpnAnalog.cxx:60
void mark(VrpnAnalogDevice *device)
Adds the indicated VrpnAnalogDevice to the list of devices that are sharing this VrpnAnalog.
Definition: vrpnAnalog.cxx:48
void poll()
Polls the connected device.
Definition: vrpnAnalog.cxx:78
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.