OSVR-Core
VrpnAnalogServer.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_VrpnAnalogServer_h_GUID_16F2A08F_2ECF_4EBB_D7B0_A2B87ACC9272
26 #define INCLUDED_VrpnAnalogServer_h_GUID_16F2A08F_2ECF_4EBB_D7B0_A2B87ACC9272
27 
28 // Internal Includes
29 #include "DeviceConstructionData.h"
31 
32 // Library/third-party includes
33 #include <vrpn_Analog.h>
34 
35 // Standard includes
36 #include <cmath>
37 
38 namespace osvr {
39 namespace connection {
40  class VrpnAnalogServer : public vrpn_Analog, public AnalogServerInterface {
41  public:
42  typedef vrpn_Analog Base;
44  : Base(init.getQualifiedName().c_str(), init.conn) {
45  m_setNumChannels(std::min(*init.obj.getAnalogs(),
46  OSVR_ChannelCount(vrpn_CHANNEL_MAX)));
47  // Initialize data
48  memset(Base::channel, 0, sizeof(Base::channel));
49  memset(Base::last, 0, sizeof(Base::last));
50 
51  // Report interface out.
52  init.obj.returnAnalogInterface(*this);
53  }
54 
55  static const vrpn_uint32 CLASS_OF_SERVICE = vrpn_CONNECTION_LOW_LATENCY;
56 
57  virtual bool setValue(value_type val, OSVR_ChannelCount chan,
58  util::time::TimeValue const &tv) {
59  if (chan >= m_getNumChannels()) {
60  return false;
61  }
62  Base::channel[chan] = val;
63  m_reportChanges(tv);
64  return true;
65  }
66  virtual void setValues(value_type val[], OSVR_ChannelCount chans,
67  util::time::TimeValue const &tv) {
68  if (chans > m_getNumChannels()) {
69  chans = m_getNumChannels();
70  }
71  for (OSVR_ChannelCount i = 0; i < chans; ++i) {
72  Base::channel[i] = val[i];
73  }
74  m_reportChanges(tv);
75  }
76 
77  private:
78  OSVR_ChannelCount m_getNumChannels() {
79  return static_cast<OSVR_ChannelCount>(Base::num_channel);
80  }
81  void m_setNumChannels(OSVR_ChannelCount chans) {
82  Base::num_channel = chans;
83  }
84  void m_reportChanges(util::time::TimeValue const &tv) {
85  struct timeval t;
86  util::time::toStructTimeval(t, tv);
87  Base::report_changes(CLASS_OF_SERVICE, t);
88  }
89  };
90 
91 } // namespace connection
92 } // namespace osvr
93 #endif // INCLUDED_VrpnAnalogServer_h_GUID_16F2A08F_2ECF_4EBB_D7B0_A2B87ACC9272
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Definition: ChannelCountC.h:51
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
virtual void setValues(value_type val[], OSVR_ChannelCount chans, util::time::TimeValue const &tv)
Sets the values from the val array at channels [0, chans), and reports changes with the given timesta...
Definition: VrpnAnalogServer.h:66
Definition: VrpnAnalogServer.h:40
Interface for external access to generating analog reports.
Definition: AnalogServerInterface.h:45
void returnAnalogInterface(osvr::connection::AnalogServerInterface &iface)
Returns an analog interface through the pointer-pointer.
Definition: DeviceInitObject.cpp:76
Definition: DeviceConstructionData.h:41
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
virtual bool setValue(value_type val, OSVR_ChannelCount chan, util::time::TimeValue const &tv)
Sets the value to val at channel chan, and reports changes with the given timestamp.
Definition: VrpnAnalogServer.h:57