OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
com_osvr_example_Locomotion.cpp
1 
7 // Copyright 2015 Sensics Inc.
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 
21 // Internal Includes
24 
25 // Generated JSON header file
26 #include "com_osvr_example_Locomotion_json.h"
27 
28 // Library/third-party includes
29 
30 // Standard includes
31 #include <iostream>
32 #include <chrono>
33 #include <thread>
34 #include <random>
35 #include <ctime>
36 
37 // Anonymous namespace to avoid symbol collision
38 namespace {
39 
40 class LocomotionDevice {
41  public:
42  LocomotionDevice(OSVR_PluginRegContext ctx) {
45 
46  osvrDeviceLocomotionConfigure(opts, &m_locomotion);
47 
49  m_dev.initAsync(ctx, "Locomotion", opts);
50 
52  m_dev.sendJsonDescriptor(com_osvr_example_Locomotion_json);
53 
55  m_dev.registerUpdateCallback(this);
56 
58  std::srand(std::time(0));
59  }
60 
61  OSVR_ReturnCode update() {
62 
63  std::this_thread::sleep_for(std::chrono::milliseconds(
64  250)); // Simulate waiting a quarter second for data.
65 
66  OSVR_TimeValue times;
67  osvrTimeValueGetNow(&times);
68 
69  int randVel = rand() % (int)(11);
70  int randPos = rand();
71 
74 
75  velo.data[0] = randVel * std::abs(std::sin(randVel));
76  velo.data[1] = randVel * randVel * std::abs(std::sin(randVel));
77  posn.data[0] = std::abs(std::sin(randPos));
78  posn.data[1] = std::abs(std::cos(randPos));
79 
80  osvrDeviceLocomotionReportNaviVelocity(m_locomotion, velo, 0, &times);
81 
82  osvrDeviceLocomotionReportNaviPosition(m_locomotion, posn, 0, &times);
83 
84  return OSVR_RETURN_SUCCESS;
85  }
86 
87  private:
89  OSVR_LocomotionDeviceInterface m_locomotion;
90 };
91 
92 class HardwareDetection {
93  public:
94  HardwareDetection() : m_found(false) {}
95  OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx) {
96 
97  if (m_found) {
98  return OSVR_RETURN_SUCCESS;
99  }
100 
101  std::cout << "PLUGIN: Got a hardware detection request" << std::endl;
102 
104  m_found = true;
105 
106  std::cout << "PLUGIN: We have detected Locomotion device! "
107  << std::endl;
110  new LocomotionDevice(ctx));
111 
112  return OSVR_RETURN_SUCCESS;
113  }
114 
115  private:
116  bool m_found;
117 };
118 } // namespace
119 
120 OSVR_PLUGIN(com_osvr_example_Locomotion) {
121 
122  osvr::pluginkit::PluginContext context(ctx);
123 
125  context.registerHardwareDetectCallback(new HardwareDetection());
126 
127  return OSVR_RETURN_SUCCESS;
128 }
Wrapper class for OSVR_DeviceToken.
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
struct OSVR_LocomotionDeviceInterfaceObject * OSVR_LocomotionDeviceInterface
Opaque type used in conjunction with a device token to send data on Locomotion Interface.
OSVR_ReturnCode osvrDeviceLocomotionReportNaviVelocity(OSVR_LocomotionDeviceInterface iface, OSVR_NaviVelocityState naviVelocity, OSVR_ChannelCount sensor, OSVR_TimeValue const *timestamp)
Report velocity data for a specific sensor.
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.
double data[2]
Internal array data.
Definition: Vec2C.h:50
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.
Header including the full PluginKit C++ interface.
OSVR_ReturnCode osvrDeviceLocomotionConfigure(OSVR_DeviceInitOptions opts, OSVR_LocomotionDeviceInterface *iface)
Specify that your device will implement the Locomotion interface.
#define OSVR_PLUGIN(PLUGIN_NAME)
This macro begins the entry point function of your plugin.
OSVR_ReturnCode osvrDeviceLocomotionReportNaviPosition(OSVR_LocomotionDeviceInterface iface, OSVR_NaviPositionState naviPosition, OSVR_ChannelCount sensor, OSVR_TimeValue const *timestamp)
Report position data for a specific sensor.
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