OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
org_osvr_example_SampleCPlugin.c
Go to the documentation of this file.
1 
14 /*
15 // Copyright 2015 Sensics, Inc.
16 //
17 // Licensed under the Apache License, Version 2.0 (the "License");
18 // you may not use this file except in compliance with the License.
19 // You may obtain a copy of the License at
20 //
21 // http://www.apache.org/licenses/LICENSE-2.0
22 //
23 // Unless required by applicable law or agreed to in writing, software
24 // distributed under the License is distributed on an "AS IS" BASIS,
25 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 // See the License for the specific language governing permissions and
27 // limitations under the License.
28 */
29 
30 /* Internal Includes */
35 #include <osvr/Util/BoolC.h>
36 
37 #include "org_osvr_example_SampleCPlugin_json.h"
38 
39 /* Library/third-party includes */
40 /* none */
41 
42 /* Standard includes */
43 #include <stdio.h>
44 #include <stdlib.h>
45 
46 typedef struct MyDevice {
47  OSVR_DeviceToken devToken;
51  double myVal;
52  OSVR_CBool buttonPressed;
53 } MyDevice;
54 
55 static OSVR_ReturnCode myDeviceUpdate(void *userdata) {
56  MyDevice *mydev = (MyDevice *)userdata;
57  /* dummy time-wasting loop - simulating "blocking waiting for device data",
58  * which is possible because this is an async device. */
59  int i;
60  for (i = 0; i < 1000; ++i) {
61  }
62 
63  /* Make up some dummy data that changes to report. */
64  mydev->myVal = (mydev->myVal + 0.1);
65  if (mydev->myVal > 10.) {
66  mydev->myVal = 0.;
67  }
68 
69  /* Report the value of channel 0 */
70  osvrDeviceAnalogSetValue(mydev->devToken, mydev->analog, mydev->myVal, 0);
71 
72  /* Toggle the button 0 */
73  if (OSVR_TRUE == mydev->buttonPressed) {
74  mydev->buttonPressed = OSVR_FALSE;
75  } else {
76  mydev->buttonPressed = OSVR_TRUE;
77  }
78  osvrDeviceButtonSetValue(mydev->devToken, mydev->button,
79  (mydev->buttonPressed == OSVR_TRUE)
82  0);
83 
84  /* Report the identity pose for sensor 0 */
85  OSVR_PoseState pose;
86  osvrPose3SetIdentity(&pose);
87  osvrDeviceTrackerSendPose(mydev->devToken, mydev->tracker, &pose, 0);
88 
89  return OSVR_RETURN_SUCCESS;
90 }
91 
92 /* Startup routine for a device instance - similar function as the constructor
93  * in the C++ examples. Note that error checking (return values) has been
94  * omitted for clarity - see documentation. */
95 static void myDeviceInit(OSVR_PluginRegContext ctx, MyDevice *mydev) {
96  /* Set initial values in the struct. */
97  mydev->myVal = 0.;
98  mydev->buttonPressed = OSVR_FALSE;
99 
100  /* Create the initialization options */
102 
103  /* Indicate that we'll want 1 analog channel. */
104  osvrDeviceAnalogConfigure(opts, &mydev->analog, 1);
105 
106  /* Indicate that we'll want 1 button. */
107  osvrDeviceButtonConfigure(opts, &mydev->button, 1);
108 
109  /* Indicate that we'll report tracking too. */
110  osvrDeviceTrackerConfigure(opts, &mydev->tracker);
111 
112  /* Create the async device token with the options */
113  osvrDeviceSyncInitWithOptions(ctx, "MyDevice", opts, &mydev->devToken);
114 
115  /* Send the JSON device descriptor. */
116  osvrDeviceSendJsonDescriptor(mydev->devToken,
117  org_osvr_example_SampleCPlugin_json,
118  sizeof(org_osvr_example_SampleCPlugin_json));
119 
120  /* Register update callback */
121  osvrDeviceRegisterUpdateCallback(mydev->devToken, &myDeviceUpdate,
122  (void *)mydev);
123 }
124 /* Shutdown and free routine for a device instance - parallels the combination
125  * of the destructor and `delete`/`osvr::util::generic_deleter<>` in the C++
126  * examples */
127 static void myDeviceShutdown(void *mydev) {
128  printf("Destroying sample device\n");
129  free(mydev);
130 }
131 
132 OSVR_PLUGIN(org_osvr_example_SampleCPlugin) {
133  /* Allocate a struct for our device data. */
134  MyDevice *mydev = (MyDevice *)malloc(sizeof(MyDevice));
135 
136  /* Ask the server to tell us when to shutdown the device and free this
137  * struct */
138  osvrPluginRegisterDataWithDeleteCallback(ctx, &myDeviceShutdown,
139  (void *)mydev);
140 
141  /* Call a function to set up the device callbacks, etc. */
142  myDeviceInit(ctx, mydev);
143 
144  return OSVR_RETURN_SUCCESS;
145 }
uint8_t OSVR_CBool
A pre-C99-safe bool type. Canonical values for true and false are provided. Interpretation of other v...
Definition: BoolC.h:50
#define OSVR_FALSE
Canonical "false" value for OSVR_CBool.
Definition: BoolC.h:54
Header providing a C-safe "bool" type, because we can't depend on Visual Studio providing proper C99 ...
struct OSVR_TrackerDeviceInterfaceObject * OSVR_TrackerDeviceInterface
Opaque type used in conjunction with a device token to send data on a tracker interface.
struct OSVR_ButtonDeviceInterfaceObject * OSVR_ButtonDeviceInterface
Opaque type used in conjunction with a device token to send data on a button interface.
OSVR_ReturnCode osvrDeviceAnalogSetValue(OSVR_DeviceToken dev, OSVR_AnalogDeviceInterface iface, OSVR_AnalogState val, OSVR_ChannelCount chan)
Report the value of a single channel.
OSVR_ReturnCode osvrDeviceButtonSetValue(OSVR_DeviceToken dev, OSVR_ButtonDeviceInterface iface, OSVR_ButtonState val, OSVR_ChannelCount chan)
Report the value of a single channel.
OSVR_ReturnCode osvrDeviceTrackerSendPose(OSVR_DeviceToken dev, OSVR_TrackerDeviceInterface iface, OSVR_PoseState const *val, OSVR_ChannelCount sensor)
Report the full rigid body pose of a sensor, automatically generating a timestamp.
#define OSVR_TRUE
Canonical "true" value for OSVR_CBool.
Definition: BoolC.h:52
OSVR_ReturnCode osvrDeviceRegisterUpdateCallback(OSVR_DeviceToken dev, OSVR_DeviceUpdateCallback updateCallback, void *userData=NULL)
Register the update callback of a device.
Header including the full PluginKit C API.
struct OSVR_DeviceTokenObject * OSVR_DeviceToken
Opaque type of a registered device token 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 osvrDeviceSendJsonDescriptor(OSVR_DeviceToken dev, const char *json, size_t len)
Submit a JSON self-descriptor string for the device.
OSVR_ReturnCode osvrDeviceSyncInitWithOptions(OSVR_PluginRegContext ctx, const char *name, OSVR_DeviceInitOptions options, OSVR_DeviceToken *device)
Initialize a synchronous device token.
#define OSVR_BUTTON_NOT_PRESSED
OSVR_ButtonState value indicating "button up".
#define OSVR_PLUGIN(PLUGIN_NAME)
This macro begins the entry point function of your plugin.
OSVR_ReturnCode osvrDeviceButtonConfigure(OSVR_DeviceInitOptions opts, OSVR_ButtonDeviceInterface *iface, OSVR_ChannelCount numChan)
Specify that your device will implement the Button interface.
#define OSVR_BUTTON_PRESSED
OSVR_ButtonState value indicating "button down".
OSVR_ReturnCode osvrPluginRegisterDataWithDeleteCallback(OSVR_PluginRegContext ctx, OSVR_PluginDataDeleteCallback deleteCallback, void *pluginData)
Register plugin data along with an appropriate deleter callback.
A structure defining a 3D (6DOF) rigid body pose: translation and rotation.
Definition: Pose3C.h:54
struct OSVR_AnalogDeviceInterfaceObject * OSVR_AnalogDeviceInterface
Opaque type used in conjunction with a device token to send data on an analog interface.
OSVR_ReturnCode osvrDeviceTrackerConfigure(OSVR_DeviceInitOptions opts, OSVR_TrackerDeviceInterface *iface)
Specify that your device will implement the Tracker 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...
void osvrPose3SetIdentity(OSVR_Pose3 *pose)
Set a pose to identity.
Definition: Pose3C.h:62
OSVR_ReturnCode osvrDeviceAnalogConfigure(OSVR_DeviceInitOptions opts, OSVR_AnalogDeviceInterface *iface, OSVR_ChannelCount numChan)
Specify that your device will implement the Analog interface.