OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
EyeTrackerComponent.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  : m_notification(notification) {}
43 
45 
46  template <typename T> void processMessage(T &p) {
47  p(m_notification.sensor);
48  }
49  OSVR_EyeNotification getNotification() const {
51  ret.sensor = m_notification.sensor;
52  return ret;
53  }
54 
55  private:
56  OSVR_EyeNotification m_notification;
57  };
58  const char *EyeRegion::identifier() {
59  return "com.osvr.eyetracker.eyeregion";
60  }
61  } // namespace messages
62 
63  shared_ptr<EyeTrackerComponent>
65  shared_ptr<EyeTrackerComponent> ret(new EyeTrackerComponent(numChan));
66  return ret;
67  }
68 
69  EyeTrackerComponent::EyeTrackerComponent(OSVR_ChannelCount numChan)
70  : m_numSensor(numChan) {}
71 
72  void
73  EyeTrackerComponent::sendNotification(OSVR_ChannelCount sensor,
74  OSVR_TimeValue const &timestamp) {
75 
76  Buffer<> buf;
77  OSVR_EyeNotification notification;
78  notification.sensor = sensor;
79  messages::EyeRegion::MessageSerialization msg(notification);
80 
81  serialize(buf, msg);
82 
83  m_getParent().packMessage(buf, eyeRegion.getMessageType(), timestamp);
84  }
85 
86  int VRPN_CALLBACK
87  EyeTrackerComponent::m_handleEyeRegion(void *userdata,
88  vrpn_HANDLERPARAM p) {
89  auto self = static_cast<EyeTrackerComponent *>(userdata);
90  auto bufReader = readExternalBuffer(p.buffer, p.payload_len);
91 
92  messages::EyeRegion::MessageSerialization msg;
93  deserialize(bufReader, msg);
94  auto data = msg.getNotification();
95  auto timestamp = util::time::fromStructTimeval(p.msg_time);
96 
97  for (auto const &cb : self->m_cb) {
98  cb(data, timestamp);
99  }
100  return 0;
101  }
102 
103  void EyeTrackerComponent::registerEyeHandler(EyeHandler handler) {
104  if (m_cb.empty()) {
105  m_registerHandler(&EyeTrackerComponent::m_handleEyeRegion, this,
106  eyeRegion.getMessageType());
107  }
108  m_cb.push_back(handler);
109  }
110  void EyeTrackerComponent::m_parentSet() {
112  }
113 
114 } // namespace common
115 } // 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
static shared_ptr< EyeTrackerComponent > create(OSVR_ChannelCount numSensor=2)
Factory method.
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
messages::EyeRegion eyeRegion
Message from server to client, containing eye data.
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