00001 // Filename: vrpnAnalog.cxx 00002 // Created by: drose (26Jan01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "vrpnAnalog.h" 00020 #include "vrpnAnalogDevice.h" 00021 #include "vrpnClient.h" 00022 #include "config_vrpn.h" 00023 00024 #include <indent.h> 00025 00026 #include <algorithm> 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: VrpnAnalog::Constructor 00030 // Access: Public 00031 // Description: 00032 //////////////////////////////////////////////////////////////////// 00033 VrpnAnalog:: 00034 VrpnAnalog(const string &analog_name, vrpn_Connection *connection) : 00035 _analog_name(analog_name) 00036 { 00037 _analog = new vrpn_Analog_Remote(_analog_name.c_str(), connection); 00038 00039 _analog->register_change_handler((void*)this, &vrpn_analog_callback); 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: VrpnAnalog::Destructor 00044 // Access: Public 00045 // Description: 00046 //////////////////////////////////////////////////////////////////// 00047 VrpnAnalog:: 00048 ~VrpnAnalog() { 00049 delete _analog; 00050 } 00051 00052 //////////////////////////////////////////////////////////////////// 00053 // Function: VrpnAnalog::mark 00054 // Access: Public 00055 // Description: Adds the indicated VrpnAnalogDevice to the list of 00056 // devices that are sharing this VrpnAnalog. 00057 //////////////////////////////////////////////////////////////////// 00058 void VrpnAnalog:: 00059 mark(VrpnAnalogDevice *device) { 00060 if (vrpn_cat.is_debug()) { 00061 vrpn_cat.debug() << *this << " marking " << *device << "\n"; 00062 } 00063 _devices.push_back(device); 00064 } 00065 00066 //////////////////////////////////////////////////////////////////// 00067 // Function: VrpnAnalog::unmark 00068 // Access: Public 00069 // Description: Removes the indicated VrpnAnalogDevice from the list 00070 // of devices that are sharing this VrpnAnalog. 00071 //////////////////////////////////////////////////////////////////// 00072 void VrpnAnalog:: 00073 unmark(VrpnAnalogDevice *device) { 00074 if (vrpn_cat.is_debug()) { 00075 vrpn_cat.debug() << *this << " unmarking " << *device << "\n"; 00076 } 00077 00078 Devices::iterator di = 00079 find(_devices.begin(), _devices.end(), device); 00080 00081 if (di != _devices.end()) { 00082 _devices.erase(di); 00083 } 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function: VrpnAnalog::output 00088 // Access: Public 00089 // Description: 00090 //////////////////////////////////////////////////////////////////// 00091 void VrpnAnalog:: 00092 output(ostream &out) const { 00093 out << _analog_name; 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function: VrpnAnalog::write 00098 // Access: Public 00099 // Description: 00100 //////////////////////////////////////////////////////////////////// 00101 void VrpnAnalog:: 00102 write(ostream &out, int indent_level) const { 00103 indent(out, indent_level) 00104 << get_analog_name() << " (" 00105 << _devices.size() << " devices)\n"; 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: VrpnAnalog::vrpn_analog_callback 00110 // Access: Private, Static 00111 // Description: Receives the analog event data from the VRPN 00112 // code and sends it to any interested 00113 // VrpnAnalogDevices. 00114 //////////////////////////////////////////////////////////////////// 00115 void VrpnAnalog:: 00116 vrpn_analog_callback(void *userdata, const vrpn_ANALOGCB info) { 00117 VrpnAnalog *self = (VrpnAnalog *)userdata; 00118 00119 Devices::iterator di; 00120 for (di = self->_devices.begin(); di != self->_devices.end(); ++di) { 00121 VrpnAnalogDevice *device = (*di); 00122 device->lock(); 00123 for (int i = 0; i < info.num_channel; i++) { 00124 if (vrpn_cat.is_debug()) { 00125 if (device->get_control_state(i) != info.channel[i]) { 00126 vrpn_cat.debug() 00127 << *self << " got analog " << i << " = " << info.channel[i] << "\n"; 00128 } 00129 } 00130 device->set_control_state(i, info.channel[i]); 00131 } 00132 device->unlock(); 00133 } 00134 }