OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
com_osvr_example_selfcontained.cpp
Go to the documentation of this file.
1 
12 // Copyright 2014 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
29 
30 // Generated JSON header file
31 #include "com_osvr_example_selfcontained_json.h"
32 
33 // Library/third-party includes
34 // - none
35 
36 // Standard includes
37 #include <iostream>
38 
39 // Anonymous namespace to avoid symbol collision
40 namespace {
41 
42 class AnalogExampleDevice {
43  public:
44  AnalogExampleDevice(OSVR_PluginRegContext ctx) : m_myVal(0) {
47 
49  osvrDeviceAnalogConfigure(opts, &m_analog, 1);
50 
52  m_dev.initAsync(ctx, "MyExampleDevice", opts);
53 
55  m_dev.sendJsonDescriptor(com_osvr_example_selfcontained_json);
56 
58  m_dev.registerUpdateCallback(this);
59  }
60 
61  OSVR_ReturnCode update() {
65  volatile int j; // volatile to keep it from being optimized out.
66  for (int i = 0; i < 10000; ++i) {
67  j = i;
68  }
70 
72  m_myVal = (m_myVal + 0.1);
73  if (m_myVal > 10.0) {
74  m_myVal = 0;
75  }
76 
78  osvrDeviceAnalogSetValue(m_dev, m_analog, m_myVal, 0);
79  return OSVR_RETURN_SUCCESS;
80  }
81 
82  private:
85  double m_myVal;
86 };
87 
88 class HardwareDetection {
89  public:
90  HardwareDetection() : m_found(false) {}
91  OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx) {
92 
93  std::cout << "PLUGIN: Got a hardware detection request" << std::endl;
94  if (!m_found) {
95  std::cout << "PLUGIN: We have detected our fake device! Doing "
96  "setup stuff!"
97  << std::endl;
98  m_found = true;
99 
102  ctx, new AnalogExampleDevice(ctx));
103  }
104  return OSVR_RETURN_SUCCESS;
105  }
106 
107  private:
110  bool m_found;
111 };
112 } // namespace
113 
114 OSVR_PLUGIN(com_osvr_example_selfcontained) {
115  osvr::pluginkit::PluginContext context(ctx);
116 
118  context.registerHardwareDetectCallback(new HardwareDetection());
119 
120  return OSVR_RETURN_SUCCESS;
121 }
OSVR_PLUGIN(com_osvr_example_selfcontained)
Wrapper class for OSVR_DeviceToken.
OSVR_ReturnCode osvrDeviceAnalogSetValue(OSVR_DeviceToken dev, OSVR_AnalogDeviceInterface iface, OSVR_AnalogState val, OSVR_ChannelCount chan)
Report the value of a single channel.
C++ wrapper class for the opaque plugin context.
Definition: PluginKit.h:59
Structure used internally to construct the desired type of device.
void registerHardwareDetectCallback(T functor)
Register a hardware detect callback.
Definition: PluginKit.h:70
T * registerObjectForDeletion(OSVR_PluginRegContext ctx, T *obj)
Registers an object to be destroyed with delete when the plugin is unloaded.
#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_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...
OSVR_ReturnCode osvrDeviceAnalogConfigure(OSVR_DeviceInitOptions opts, OSVR_AnalogDeviceInterface *iface, OSVR_ChannelCount numChan)
Specify that your device will implement the Analog interface.