diff --git a/CMakeLists.txt b/CMakeLists.txt index da950ff..0e01fec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,6 +91,7 @@ endif() set(MYNTEYE_SRCS ${UVC_SRC} + src/internal/strings.cc src/internal/types.cc src/public/types.cc src/device/context.cc diff --git a/src/device/context.cc b/src/device/context.cc index 1b2baeb..dab5d7d 100644 --- a/src/device/context.cc +++ b/src/device/context.cc @@ -15,10 +15,15 @@ Context::Context() : context_(uvc::create_context()) { auto vid = uvc::get_vendor_id(*device); auto pid = uvc::get_product_id(*device); // auto video_name = uvc::get_video_name(*device); - LOG(INFO) << "name: " << name << ", vid: 0x" << std::hex << vid - << ", pid: 0x" << std::hex << pid; + LOG(INFO) << "UVC device detected, name: " << name << ", vid: 0x" + << std::hex << vid << ", pid: 0x" << std::hex << pid; if (vid == MYNTEYE_VID) { - devices_.push_back(Device::Create(name, device)); + auto d = Device::Create(name, device); + if (d) { + devices_.push_back(d); + } else { + LOG(ERROR) << "Device is not supported by MYNT EYE."; + } } } } diff --git a/src/device/device.cc b/src/device/device.cc index 7d27fd1..4d22527 100644 --- a/src/device/device.cc +++ b/src/device/device.cc @@ -2,6 +2,7 @@ #include +#include "internal/strings.h" #include "uvc/uvc.h" MYNTEYE_BEGIN_NAMESPACE @@ -16,6 +17,10 @@ Device::~Device() { std::shared_ptr Device::Create( const std::string &name, std::shared_ptr device) { + if (name == "MYNTEYE") { + } else if (strings::starts_with(name, "MYNT-EYE-")) { + // TODO(JohnZhao): Create different device by name, such as MYNT-EYE-S1000 + } return nullptr; } diff --git a/src/internal/strings.cc b/src/internal/strings.cc new file mode 100644 index 0000000..30e7bb1 --- /dev/null +++ b/src/internal/strings.cc @@ -0,0 +1,92 @@ +#include "internal/strings.h" + +#include +#include +#include +#include + +MYNTEYE_BEGIN_NAMESPACE + +namespace strings { + +namespace { + +// The most elegant way to iterate the words of a string +// https://stackoverflow.com/questions/236129/the-most-elegant-way-to-iterate-the-words-of-a-string + +template +void tokenize( + const std::string &str, ContainerT &tokens, // NOLINT + const std::string &delimiters = " ", bool trimEmpty = false) { + std::string::size_type pos, lastPos = 0, length = str.length(); + + using value_type = typename ContainerT::value_type; + using size_type = typename ContainerT::size_type; + + while (lastPos < length + 1) { + pos = str.find_first_of(delimiters, lastPos); + if (pos == std::string::npos) { + pos = length; + } + + if (pos != lastPos || !trimEmpty) { + tokens.push_back( + value_type(str.data() + lastPos, (size_type)pos - lastPos)); + } + + lastPos = pos + 1; + } +} + +} // namespace + +int hex2int(const std::string &text) { + try { + return std::stoi(text, nullptr, 16); + } catch (const std::exception &e) { + throw new strings_error("strings conversion error"); + } +} + +bool starts_with(const std::string &text, const std::string &prefix) { + return text.compare(0, prefix.length(), prefix) == 0; +} + +std::vector split( + const std::string &text, const std::string &delimiters) { + std::vector tokens; + tokenize(text, tokens, delimiters); + return tokens; +} + +// What's the best way to trim std::string? +// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring + +void ltrim(std::string &s) { // NOLINT + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { + return !std::isspace(ch); + })); +} + +void rtrim(std::string &s) { // NOLINT + s.erase( + std::find_if( + s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }) + .base(), + s.end()); +} + +void trim(std::string &s) { // NOLINT + ltrim(s); + rtrim(s); +} + +std::string trim_copy(const std::string &text) { + std::string s = text; + trim(s); + return s; +} + +} // namespace strings + +MYNTEYE_END_NAMESPACE diff --git a/src/internal/strings.h b/src/internal/strings.h new file mode 100644 index 0000000..d6c7d03 --- /dev/null +++ b/src/internal/strings.h @@ -0,0 +1,41 @@ +#ifndef MYNTEYE_INTERNAL_STRINGS_H_ // NOLINT +#define MYNTEYE_INTERNAL_STRINGS_H_ +#pragma once + +#include +#include +#include +#include + +#include "mynteye/mynteye.h" + +MYNTEYE_BEGIN_NAMESPACE + +class strings_error : public std::runtime_error { + public: + explicit strings_error(const std::string &what_arg) noexcept + : std::runtime_error(std::move(what_arg)) {} + explicit strings_error(const char *what_arg) noexcept + : std::runtime_error(std::move(what_arg)) {} +}; + +namespace strings { + +int hex2int(const std::string &text); + +bool starts_with(const std::string &text, const std::string &prefix); + +std::vector split( + const std::string &text, const std::string &delimiters); + +void ltrim(std::string &s); // NOLINT +void rtrim(std::string &s); // NOLINT +void trim(std::string &s); // NOLINT + +std::string trim_copy(const std::string &text); + +} // namespace strings + +MYNTEYE_END_NAMESPACE + +#endif // MYNTEYE_INTERNAL_STRINGS_H_ NOLINT