OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
com_osvr_example_Configured.cpp
Go to the documentation of this file.
1 
13 // Copyright 2015 Sensics, Inc.
14 //
15 // Licensed under the Apache License, Version 2.0 (the "License");
16 // you may not use this file except in compliance with the License.
17 // You may obtain a copy of the License at
18 //
19 // http://www.apache.org/licenses/LICENSE-2.0
20 //
21 // Unless required by applicable law or agreed to in writing, software
22 // distributed under the License is distributed on an "AS IS" BASIS,
23 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 // See the License for the specific language governing permissions and
25 // limitations under the License.
26 
27 // Internal Includes
32 
33 // Generated JSON header file
34 #include "com_osvr_example_Configured_json.h"
35 
36 // Library/third-party includes
37 #include <json/value.h>
38 #include <json/reader.h>
39 
40 // Standard includes
41 #include <iostream>
42 #include <chrono>
43 #include <thread>
44 
45 // Anonymous namespace to avoid symbol collision
46 namespace {
47 
48 class ConfiguredDevice {
49  public:
50  ConfiguredDevice(OSVR_PluginRegContext ctx, double myVal, int sleepTime)
51  : m_myVal(myVal), m_sleepTime(sleepTime) {
54 
56  osvrDeviceAnalogConfigure(opts, &m_analog, 1);
57 
59  m_dev.initAsync(ctx, "MyConfiguredDevice", opts);
60 
62  m_dev.sendJsonDescriptor(com_osvr_example_Configured_json);
63 
65  m_dev.registerUpdateCallback(this);
66  }
67 
68  OSVR_ReturnCode update() {
71  std::this_thread::sleep_for(std::chrono::seconds(m_sleepTime));
72 
74  osvrDeviceAnalogSetValue(m_dev, m_analog, m_myVal, 0);
75  return OSVR_RETURN_SUCCESS;
76  }
77 
78  private:
81  double m_myVal;
82  int m_sleepTime;
83 };
84 
85 class ConfiguredDeviceConstructor {
86  public:
89  OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx, const char *params) {
90  // Read the JSON data from parameters.
91  Json::Value root;
92  if (params) {
93  Json::Reader r;
94  if (!r.parse(params, root)) {
95  std::cerr << "Could not parse parameters!" << std::endl;
96  }
97  }
98 
99  if (!root.isMember("value")) {
100  // If they configured us but didn't provide a "value" item, warn
101  // - pretending that "value" is some important config value.
102  // If very important, could just return OSVR_RETURN_FAILURE here.
103  std::cerr << "Warning: got configuration, but nothing specified "
104  "for \"value\" - will use default!"
105  << std::endl;
106  }
107 
108  // Using `get` here instead of `[]` lets us provide a default value.
109  double val = root.get("value", 5.0).asDouble();
110 
111  // We didn't warn about this one, because we're pretending this is a
112  // less important optional parameter.
113  int sleepTime = root.get("sleepTime", 1).asInt();
114 
115  // OK, now that we have our parameters, create the device.
117  ctx, new ConfiguredDevice(ctx, val, sleepTime));
118 
119  return OSVR_RETURN_SUCCESS;
120  }
121 };
122 } // namespace
123 
124 OSVR_PLUGIN(com_osvr_example_Configured) {
125 
128  ctx, "ConfiguredDevice", new ConfiguredDeviceConstructor);
129 
130  return OSVR_RETURN_SUCCESS;
131 }
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.
OSVR_PLUGIN(com_osvr_example_Configured)
T * registerObjectForDeletion(OSVR_PluginRegContext ctx, T *obj)
Registers an object to be destroyed with delete when the plugin is unloaded.
void registerDriverInstantiationCallback(OSVR_PluginRegContext ctx, const char driverName[], T functor)
Registers a function object to be called when the server is told to instantiate a driver by name with...
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.
struct OSVR_AnalogDeviceInterfaceObject * OSVR_AnalogDeviceInterface
Opaque type used in conjunction with a device token to send data on an analog 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.