OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
AnalogRemoteFactory.cpp
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 // Internal Includes
26 #include "AnalogRemoteFactory.h"
27 #include "RemoteHandlerInternals.h"
31 #include <osvr/Util/EigenInterop.h>
34 #include <osvr/Util/UniquePtr.h>
35 #include <osvr/Common/Transform.h>
38 #include "PureClientContext.h"
40 #include <osvr/Util/ValueOrRange.h>
41 #include <osvr/Util/Verbosity.h>
42 
43 // Library/third-party includes
44 #include <vrpn_Analog.h>
45 #include <boost/lexical_cast.hpp>
46 #include <boost/any.hpp>
47 #include <boost/variant/get.hpp>
48 #include <json/value.h>
49 #include <json/reader.h>
50 
51 // Standard includes
52 // - none
53 
54 namespace osvr {
55 namespace client {
56 
58  public:
60  VRPNAnalogHandler(vrpn_ConnectionPtr const &conn, const char *src,
61  boost::optional<int> sensor,
62  common::InterfaceList &ifaces)
63  : m_remote(new vrpn_Analog_Remote(src, conn.get())),
64  m_internals(ifaces), m_all(!sensor.is_initialized()) {
65  m_remote->register_change_handler(this, &VRPNAnalogHandler::handle);
66  OSVR_DEV_VERBOSE("Constructed an AnalogHandler for " << src);
67 
68  if (sensor.is_initialized()) {
69  m_sensors.setValue(*sensor);
70  }
71  }
72  virtual ~VRPNAnalogHandler() {
73  m_remote->unregister_change_handler(this,
74  &VRPNAnalogHandler::handle);
75  }
76 
77  static void VRPN_CALLBACK handle(void *userdata, vrpn_ANALOGCB info) {
78  auto self = static_cast<VRPNAnalogHandler *>(userdata);
79  self->m_handle(info);
80  }
81  virtual void update() { m_remote->mainloop(); }
82 
83  private:
84  void m_handle(vrpn_ANALOGCB const &info) {
85  auto maxChannel =
86  m_all ? info.num_channel - 1 : m_sensors.getValue();
87  if (m_sensors.isValue() && (maxChannel < m_sensors.getValue())) {
88  // early out if we're looking for just a single channel number
89  // that isn't in this report.
90  return;
91  }
92  OSVR_TimeValue timestamp;
93  osvrStructTimevalToTimeValue(&timestamp, &(info.msg_time));
94 
95  if (m_all) {
96  if (m_sensors.empty()) {
97  m_sensors.setRangeMaxMin(maxChannel);
98  } else {
99  m_sensors.extendRangeToMax(maxChannel);
100  }
101  }
102  for (auto sensor : m_sensors.getIntersection(
103  RangeType::RangeZeroTo(maxChannel))) {
104  OSVR_AnalogReport report;
105  report.sensor = sensor;
107  report.state = info.channel[report.sensor];
108  m_internals.setStateAndTriggerCallbacks(timestamp, report);
109  }
110  }
111  unique_ptr<vrpn_Analog_Remote> m_remote;
112  RemoteHandlerInternals m_internals;
113  bool m_all;
114  RangeType m_sensors;
115  };
116 
117  AnalogRemoteFactory::AnalogRemoteFactory(
118  VRPNConnectionCollection const &conns)
119  : m_conns(conns) {}
120 
121  shared_ptr<RemoteHandler> AnalogRemoteFactory::
123  common::InterfaceList &ifaces, common::ClientContext &) {
124 
125  shared_ptr<RemoteHandler> ret;
126 
127  if (source.hasTransform()) {
128  OSVR_DEV_VERBOSE(
129  "Ignoring transform found on route for Analog data!");
130  }
131 
132  auto const &devElt = source.getDeviceElement();
133 
135  ret.reset(new VRPNAnalogHandler(m_conns.getConnection(devElt),
136  devElt.getFullDeviceName().c_str(),
137  source.getSensorNumber(), ifaces));
138  return ret;
139  }
140 
141 } // namespace client
142 } // namespace osvr
The result of resolving a tree node to a device: either an original source to connect to...
int32_t sensor
Identifies the sensor/channel that the report comes from.
Header including PathTree.h and all additional headers needed to define related types.
Header to bring unique_ptr into the osvr namespace.
Header.
OSVR_AnalogState state
The analog state.
void setStateAndTriggerCallbacks(const OSVR_TimeValue &timestamp, ReportType const &report)
Set state and call callbacks for a report type.
void osvrStructTimevalToTimeValue(OSVR_TimeValue *dest, const struct timeval *src)
Converts from a TimeValue struct to your system's struct timeval.
Internal, configured header file for verbosity macros.
Header for interoperation between the Eigen math library, the internal mini math library, and VRPN's quatlib.
shared_ptr< RemoteHandler > operator()(common::OriginalSource const &source, common::InterfaceList &ifaces, common::ClientContext &ctx)
Report type for a callback on an analog interface.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
Base class for remote device handler classes.
Definition: RemoteHandler.h:40
Class holding shared implementation between the various handlers. Primarily used to avoid the need fo...