29 #ifdef OSVR_HAVE_USBSERIALENUM
33 #include "com_osvr_Multiserver_YEI_3Space_Sensor_json.h"
36 #include <json/reader.h>
37 #include <json/value.h>
39 #include <vrpn_YEI_3Space.h>
51 class CStringArray : boost::noncopyable {
53 typedef std::unique_ptr<char[]> UniqueCharArray;
54 void push_back(std::string
const &str) {
56 if (m_arrayHasNullTerminator()) {
60 const size_t stringLength = str.size() + 1;
61 UniqueCharArray copy(
new char[stringLength]);
62 memcpy(copy.get(), str.c_str(), stringLength);
63 m_dataOwnership.push_back(std::move(copy));
65 m_data.push_back(m_dataOwnership.back().get());
67 const char **get_array() {
69 if (!m_arrayHasNullTerminator()) {
70 m_data.push_back(
nullptr);
76 bool m_arrayHasNullTerminator()
const {
77 return !m_data.empty() &&
nullptr == m_data.back();
79 std::vector<const char *> m_data;
80 std::vector<UniqueCharArray> m_dataOwnership;
85 Json::Value
const &root, std::string port) {
87 throw std::runtime_error(
88 "Could not create a YEI device: no port specified!.");
92 bool calibrate_gyros_on_setup =
93 root.get(
"calibrateGyrosOnSetup",
false).asBool();
94 bool tare_on_setup = root.get(
"tareOnSetup",
false).asBool();
95 double frames_per_second = root.get(
"framesPerSecond", 250).asFloat();
97 Json::Value commands = root.get(
"resetCommands", Json::arrayValue);
98 CStringArray reset_commands;
100 if (commands.empty()) {
102 reset_commands.push_back(
"123,2");
104 for (Json::ArrayIndex i = 0, e = commands.size(); i < e; ++i) {
105 reset_commands.push_back(commands[i].asString());
111 reg.registerDevice(
new vrpn_YEI_3Space_Sensor(
112 reg.useDecoratedName(data.getName(
"YEI_3Space_Sensor")).c_str(),
113 reg.getVRPNConnection(), port.c_str(), 115200, calibrate_gyros_on_setup,
114 tare_on_setup, frames_per_second, 0, 0, 1, 0,
115 reset_commands.get_array()));
116 reg.setDeviceDescriptor(
121 const char *params) {
124 if (!reader.parse(params, root)) {
125 throw std::runtime_error(
"Could not parse configuration: " +
126 reader.getFormattedErrorMessages());
128 auto port = root.get(
"port",
"").asString();
129 #ifdef OSVR_HAVE_USBSERIALENUM
133 createYEIImpl(data, ctx, root, port);
135 static const uint16_t vID = 0x2476;
136 static const uint16_t pID = 0x1010;
137 for (
auto &&dev : osvr::usbserial::enumerate(vID, pID)) {
138 createYEIImpl(data, ctx, root, dev.getPort());
141 #else // !OSVR_HAVE_USBSERIALENUM
143 createYEIImpl(data, ctx, root, port);
144 #endif // OSVR_HAVE_USBSERIALENUM
std::string makeString(const char(&arrayLiteral)[N])
Safely and easily convert a literal array of characters (like from osvr_json_to_c) into a std::string...
Header with a convenience function to make a std::string out of a non-null-terminated char array (str...
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...
std::string normalizeAndVerifySerialPort(std::string const &port)
Normalizes and verifies the accessibility of a serial port and throws a std::exception unless it is a...