OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
com_osvr_example_EyeTracker.cpp
1 
8 // Copyright 2015 Sensics Inc.
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 
22 // Internal Includes
25 
26 // Generated JSON header file
27 #include "com_osvr_example_EyeTracker_json.h"
28 
29 // Library/third-party includes
30 
31 // Standard includes
32 #include <iostream>
33 #include <memory>
34 #include <ctime>
35 #include <cmath>
36 #include <chrono>
37 #include <thread>
38 
39 // Anonymous namespace to avoid symbol collision
40 namespace {
41 
42 OSVR_MessageType eyeTrackerMessage;
43 
44 class EyeTrackerDevice {
45  public:
46  EyeTrackerDevice(OSVR_PluginRegContext ctx) : m_Blink(false) {
49 
50  osvrDeviceEyeTrackerConfigure(opts, &m_eyetracker, 2);
51 
53  m_dev.initAsync(ctx, "EyeTracker", opts);
54 
56  m_dev.sendJsonDescriptor(com_osvr_example_EyeTracker_json);
57 
59  m_dev.registerUpdateCallback(this);
60 
62  std::srand(std::time(0));
63  }
64 
65  OSVR_ReturnCode update() {
66 
67  std::this_thread::sleep_for(std::chrono::milliseconds(
68  250)); // Simulate waiting a quarter second for data.
69 
70  OSVR_TimeValue times;
71 
72  osvrTimeValueGetNow(&times);
73  int randVal = std::rand();
74 
75  // Left eye Data
76  OSVR_Vec2 gaze2D;
77  gaze2D.data[0] = std::abs(std::sin(randVal));
78  gaze2D.data[1] = std::abs(std::cos(randVal));
79 
80  OSVR_Vec3 gaze3D;
81  gaze3D.data[0] = std::abs(std::sin(randVal) / (std::cos(randVal)));
82  gaze3D.data[1] = std::abs(std::cos(randVal) / (std::sin(randVal)));
83  gaze3D.data[2] = std::abs(std::sin(randVal) * std::sin(randVal));
84 
85  OSVR_EyeTracker2DState location;
86  location = gaze2D;
87 
89  dir.direction = gaze3D;
90  dir.basePoint = gaze3D;
91 
92  osvrDeviceEyeTrackerReportGaze(m_eyetracker, location, dir.direction,
93  dir.basePoint, 0, &times);
94  m_Blink = !m_Blink;
95  osvrDeviceEyeTrackerReportBlink(m_eyetracker, m_Blink, 0, &times);
96 
97  return OSVR_RETURN_SUCCESS;
98  }
99 
100  private:
101  bool m_Blink;
103  OSVR_EyeTrackerDeviceInterface m_eyetracker;
104 };
105 
106 class HardwareDetection {
107  public:
108  HardwareDetection() : m_found(false) {}
109  OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx) {
110 
111  if (m_found) {
112  return OSVR_RETURN_SUCCESS;
113  }
114 
115  std::cout << "PLUGIN: Got a hardware detection request" << std::endl;
116 
117  // we always detect an eye tracker in sample plugin
118  m_found = true;
119 
120  std::cout << "PLUGIN: We have detected Eye Tracker device! "
121  << std::endl;
124  new EyeTrackerDevice(ctx));
125 
126  return OSVR_RETURN_SUCCESS;
127  }
128 
129  private:
130  bool m_found;
131 };
132 } // namespace
133 
134 OSVR_PLUGIN(com_osvr_example_EyeTracker) {
135 
136  osvr::pluginkit::PluginContext context(ctx);
137 
139  context.registerHardwareDetectCallback(new HardwareDetection());
140 
141  return OSVR_RETURN_SUCCESS;
142 }
Wrapper class for OSVR_DeviceToken.
A structure defining a 3D vector, often a position/translation.
Definition: Vec3C.h:48
void osvrTimeValueGetNow(OSVR_TimeValue *dest)
Gets the current time in the TimeValue. Parallel to gettimeofday.
C++ wrapper class for the opaque plugin context.
Definition: PluginKit.h:59
OSVR_ReturnCode osvrDeviceEyeTrackerReportBlink(OSVR_EyeTrackerDeviceInterface iface, OSVR_EyeTrackerBlinkState blink, OSVR_ChannelCount sensor, OSVR_TimeValue const *timestamp)
Report the blink state.
double data[3]
Internal array data.
Definition: Vec3C.h:50
struct OSVR_EyeTrackerDeviceInterfaceObject * OSVR_EyeTrackerDeviceInterface
Opaque type used to send data on eye tracker interface (which internally is carried over several inte...
A structure defining a 2D vector, which represents position.
Definition: Vec2C.h:48
T * registerObjectForDeletion(OSVR_PluginRegContext ctx, T *obj)
Registers an object to be destroyed with delete when the plugin is unloaded.
OSVR_ReturnCode osvrDeviceEyeTrackerConfigure(OSVR_DeviceInitOptions opts, OSVR_EyeTrackerDeviceInterface *iface, OSVR_ChannelCount numSensors)
Specify that your device will implement the Eye Tracker interface.
double data[2]
Internal array data.
Definition: Vec2C.h:50
struct OSVR_MessageTypeObject * OSVR_MessageType
Opaque type of a registered message type within the core library.
struct OSVR_DeviceInitObject * OSVR_DeviceInitOptions
Opaque type of a device initialization object.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
OSVR_DeviceInitOptions osvrDeviceCreateInitOptions(OSVR_PluginRegContext ctx)
Create a OSVR_DeviceInitOptions object.
OSVR_ReturnCode osvrDeviceEyeTrackerReportGaze(OSVR_EyeTrackerDeviceInterface iface, OSVR_EyeGazePosition2DState gazePosition, OSVR_EyeGazeDirectionState gazeDirection, OSVR_EyeGazeBasePoint3DState gazeBasePoint, OSVR_ChannelCount sensor, OSVR_TimeValue const *timestamp)
Report both 2D and 3D gaze for an eye.
State for 3D gaze report.
Header including the full PluginKit C++ interface.
#define OSVR_PLUGIN(PLUGIN_NAME)
This macro begins the entry point function of your plugin.
OSVR_EXTERN_C_BEGIN typedef void * OSVR_PluginRegContext
A context pointer passed in to your plugin's entry point and other locations of control flow transfer...
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81