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