OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LocomotionComponent.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
27 #include <osvr/Common/BaseDevice.h>
29 #include <osvr/Common/Buffer.h>
30 #include <osvr/Util/Verbosity.h>
31 
32 // Library/third-party includes
33 // - none
34 
35 // Standard includes
36 // - none
37 
38 namespace osvr {
39 namespace common {
40 
41  namespace messages {
43  public:
45  OSVR_ChannelCount sensor)
46  : m_naviVelState(state), m_sensor(sensor) {}
47 
49 
50  template <typename T> void processMessage(T &p) {
51  p(m_naviVelState);
52  p(m_sensor);
53  }
54  NaviVelocityData getData() const {
55  NaviVelocityData ret;
56  ret.sensor = m_sensor;
57  ret.naviVelState = m_naviVelState;
58  return ret;
59  }
60 
61  private:
62  OSVR_NaviVelocityState m_naviVelState;
63  OSVR_ChannelCount m_sensor;
64  };
65  const char *NaviVelocityRecord::identifier() {
66  return "com.osvr.locomotion.navivelocityrecord";
67  }
68 
70  public:
72  OSVR_ChannelCount sensor)
73  : m_naviPosnState(state), m_sensor(sensor) {}
74 
76 
77  template <typename T> void processMessage(T &p) {
78  p(m_naviPosnState);
79  p(m_sensor);
80  }
81  NaviPositionData getData() const {
82  NaviPositionData ret;
83  ret.sensor = m_sensor;
84  ret.naviPosnState = m_naviPosnState;
85  return ret;
86  }
87 
88  private:
89  OSVR_NaviPositionState m_naviPosnState;
90  OSVR_ChannelCount m_sensor;
91  };
92  const char *NaviPositionRecord::identifier() {
93  return "com.osvr.locomotion.navipositionrecord";
94  }
95 
96  } // namespace messages
97 
98  shared_ptr<LocomotionComponent> LocomotionComponent::create() {
99  shared_ptr<LocomotionComponent> ret(new LocomotionComponent());
100  return ret;
101  }
102 
103  LocomotionComponent::LocomotionComponent() {}
104 
105  void LocomotionComponent::sendNaviVelocityData(
106  OSVR_NaviVelocityState naviVelocityState, OSVR_ChannelCount sensor,
107  OSVR_TimeValue const &timestamp) {
108 
109  Buffer<> buf;
110 
111  messages::NaviVelocityRecord::MessageSerialization msg(
112  naviVelocityState, sensor);
113 
114  serialize(buf, msg);
115  m_getParent().packMessage(buf, naviVelRecord.getMessageType(),
116  timestamp);
117  }
118 
119  void LocomotionComponent::sendNaviPositionData(
120  OSVR_NaviPositionState naviPositionState, OSVR_ChannelCount sensor,
121  OSVR_TimeValue const &timestamp) {
122 
123  Buffer<> buf;
124 
125  messages::NaviPositionRecord::MessageSerialization msg(
126  naviPositionState, sensor);
127  serialize(buf, msg);
128 
129  m_getParent().packMessage(buf, naviPosnRecord.getMessageType(),
130  timestamp);
131  }
132 
133  int VRPN_CALLBACK
134  LocomotionComponent::m_handleNaviVelocityRecord(void *userdata,
135  vrpn_HANDLERPARAM p) {
136  auto self = static_cast<LocomotionComponent *>(userdata);
137  auto bufReader = readExternalBuffer(p.buffer, p.payload_len);
138 
139  messages::NaviVelocityRecord::MessageSerialization msg;
140  deserialize(bufReader, msg);
141  auto data = msg.getData();
142  auto timestamp = util::time::fromStructTimeval(p.msg_time);
143 
144  for (auto const &cb : self->m_cb_vel) {
145  cb(data, timestamp);
146  }
147  return 0;
148  }
149 
150  int VRPN_CALLBACK
151  LocomotionComponent::m_handleNaviPositionRecord(void *userdata,
152  vrpn_HANDLERPARAM p) {
153  auto self = static_cast<LocomotionComponent *>(userdata);
154  auto bufReader = readExternalBuffer(p.buffer, p.payload_len);
155 
156  messages::NaviPositionRecord::MessageSerialization msg;
157  deserialize(bufReader, msg);
158  auto data = msg.getData();
159  auto timestamp = util::time::fromStructTimeval(p.msg_time);
160 
161  for (auto const &cb : self->m_cb_posn) {
162  cb(data, timestamp);
163  }
164  return 0;
165  }
166 
167  void LocomotionComponent::registerNaviVelocityHandler(
168  NaviVelocityHandler handler) {
169  if (m_cb_vel.empty()) {
170  m_registerHandler(&LocomotionComponent::m_handleNaviVelocityRecord,
171  this, naviVelRecord.getMessageType());
172  }
173  m_cb_vel.push_back(handler);
174  }
175  void LocomotionComponent::registerNaviPositionHandler(
176  NaviPositionHandler handler) {
177  if (m_cb_posn.empty()) {
178  m_registerHandler(&LocomotionComponent::m_handleNaviPositionRecord,
179  this, naviPosnRecord.getMessageType());
180  }
181  m_cb_posn.push_back(handler);
182  }
183 
184  void LocomotionComponent::m_parentSet() {
185 
188  }
189 
190 } // namespace common
191 } // namespace osvr
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Definition: ChannelCountC.h:51
messages::NaviVelocityRecord naviVelRecord
Message from server to client, containing navigation velocity.
void deserialize(BufferReaderType &reader, MessageClass &msg)
Deserializes a message from a buffer, using a MessageClass
void serialize(BufferType &buf, MessageClass &msg)
Serializes a message into a buffer, using a MessageClass
void m_registerHandler(vrpn_MESSAGEHANDLER handler, void *userdata, RawMessageType const &msgType)
Registers a handler whose lifetime is tied to the lifetime of the component.
void fromStructTimeval(TimeValue &dest, struct timeval const &src)
Convert a struct timeval to a TimeValue.
Definition: TimeValue.h:104
static shared_ptr< LocomotionComponent > create()
Factory method.
A structure defining a 2D vector, which represents position.
Definition: Vec2C.h:48
Parent & m_getParent()
Gets the parent - only call if m_hasParent() is true.
Internal, configured header file for verbosity macros.
Header.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
Header defining buffer types, with optional alignment dependent on Boost version. ...
BufferReader< ExternalBufferReadingWrapper< CharType > > readExternalBuffer(const CharType *buf, size_t len)
Constructs and returns a buffer reader for an externally-allocated buffer: it's on you to supply a va...
Definition: Buffer.h:198
void registerMessageType(MessageRegistration< T > &messageReg)
Call with a MessageRegistration object, and the message type will be registered and stored in the typ...
Definition: BaseDevice.h:72
messages::NaviPositionRecord naviPosnRecord
Optional message from server to client, containing navigation position.