OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Location2DComponent.cpp
Go to the documentation of this file.
1 
12 // Copyright 2015 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 // Internal Includes
28 #include <osvr/Common/BaseDevice.h>
30 #include <osvr/Common/Buffer.h>
31 #include <osvr/Util/Verbosity.h>
32 
33 // Library/third-party includes
34 
35 namespace osvr {
36 namespace common {
37 
38  namespace messages {
40  public:
42  OSVR_ChannelCount sensor)
43  : m_location(location), m_sensor(sensor) {}
44 
46 
47  template <typename T> void processMessage(T &p) {
48  p(m_location);
49  p(m_sensor);
50  }
51  LocationData getData() const {
52  LocationData ret;
53  ret.sensor = m_sensor;
54  ret.location = m_location;
55  return ret;
56  }
57 
58  private:
59  OSVR_Location2DState m_location;
60  OSVR_ChannelCount m_sensor;
61  };
62  const char *LocationRecord::identifier() {
63  return "com.osvr.location2D.locationrecord";
64  }
65  } // namespace messages
66 
67  shared_ptr<Location2DComponent>
69  shared_ptr<Location2DComponent> ret(new Location2DComponent(numChan));
70  return ret;
71  }
72 
73  Location2DComponent::Location2DComponent(OSVR_ChannelCount numChan)
74  : m_numSensor(numChan) {}
75 
76  void
77  Location2DComponent::sendLocationData(OSVR_Location2DState location,
78  OSVR_ChannelCount sensor,
79  OSVR_TimeValue const &timestamp) {
80 
81  Buffer<> buf;
82  messages::LocationRecord::MessageSerialization msg(location, sensor);
83  serialize(buf, msg);
84 
85  m_getParent().packMessage(buf, locationRecord.getMessageType(),
86  timestamp);
87  }
88 
89  int VRPN_CALLBACK
90  Location2DComponent::m_handleLocationRecord(void *userdata,
91  vrpn_HANDLERPARAM p) {
92  auto self = static_cast<Location2DComponent *>(userdata);
93  auto bufReader = readExternalBuffer(p.buffer, p.payload_len);
94 
95  messages::LocationRecord::MessageSerialization msg;
96  deserialize(bufReader, msg);
97  auto data = msg.getData();
98  auto timestamp = util::time::fromStructTimeval(p.msg_time);
99 
100  for (auto const &cb : self->m_cb) {
101  cb(data, timestamp);
102  }
103  return 0;
104  }
105 
106  void Location2DComponent::registerLocationHandler(LocationHandler handler) {
107  if (m_cb.empty()) {
108  m_registerHandler(&Location2DComponent::m_handleLocationRecord,
109  this, locationRecord.getMessageType());
110  }
111  m_cb.push_back(handler);
112  }
113  void Location2DComponent::m_parentSet() {
115  }
116 
117 } // namespace common
118 } // namespace osvr
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Definition: ChannelCountC.h:51
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
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.
static shared_ptr< Location2DComponent > create(OSVR_ChannelCount numSensor=1)
Factory method.
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::LocationRecord locationRecord
Message from server to client, containing 2D location data.