From 905bafd26d6b6e1338c26bfe2a7c9d3b733a7511 Mon Sep 17 00:00:00 2001 From: John Zhao Date: Wed, 4 Apr 2018 10:50:27 +0800 Subject: [PATCH] Add device layer --- CMakeLists.txt | 2 ++ samples/CMakeLists.txt | 8 ++++++++ samples/device/CMakeLists.txt | 22 +++++++++++++++++++++ samples/device/camera.cc | 13 +++++++++++++ samples/glog_init.h | 30 +++++++++++++++++++++++++++++ samples/uvc/CMakeLists.txt | 8 ++++---- samples/uvc/camera.cc | 25 +----------------------- src/device/context.cc | 30 +++++++++++++++++++++++++++++ src/device/context.h | 36 +++++++++++++++++++++++++++++++++++ src/device/device.cc | 22 +++++++++++++++++++++ src/device/device.h | 31 ++++++++++++++++++++++++++++++ src/uvc/uvc-libuvc.cc | 2 +- src/uvc/uvc-v4l2.cc | 11 ++++++++++- src/uvc/uvc.h | 1 + 14 files changed, 211 insertions(+), 30 deletions(-) create mode 100644 samples/device/CMakeLists.txt create mode 100644 samples/device/camera.cc create mode 100644 samples/glog_init.h create mode 100644 src/device/context.cc create mode 100644 src/device/context.h create mode 100644 src/device/device.cc create mode 100644 src/device/device.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 547a9c9..da950ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,8 @@ set(MYNTEYE_SRCS ${UVC_SRC} src/internal/types.cc src/public/types.cc + src/device/context.cc + src/device/device.cc ) set(MYNTEYE_LINKLIBS diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 0ef96c8..2c6de86 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -40,6 +40,14 @@ include(${PRO_DIR}/cmake/Common.cmake) set(OUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/_output") +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} +) + +# samples above device layer + +add_subdirectory(device) + # samples above uvc layer add_subdirectory(uvc) diff --git a/samples/device/CMakeLists.txt b/samples/device/CMakeLists.txt new file mode 100644 index 0000000..8db16fe --- /dev/null +++ b/samples/device/CMakeLists.txt @@ -0,0 +1,22 @@ +get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) + +set_outdir( + "${OUT_DIR}/lib/${DIR_NAME}" + "${OUT_DIR}/lib/${DIR_NAME}" + "${OUT_DIR}/bin/${DIR_NAME}" +) + +include_directories( + ${PRO_DIR}/src +) + +## camera_d + +add_executable(camera_d camera.cc) +target_link_libraries(camera_d mynteye ${OpenCV_LIBS}) + +if(OS_WIN) + target_compile_definitions(camera_d + PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES + ) +endif() diff --git a/samples/device/camera.cc b/samples/device/camera.cc new file mode 100644 index 0000000..55dec9e --- /dev/null +++ b/samples/device/camera.cc @@ -0,0 +1,13 @@ +#include "glog_init.h" // NOLINT + +#include "device/context.h" + +MYNTEYE_USE_NAMESPACE + +int main(int argc, char *argv[]) { + glog_init _(argc, argv); + + Context context; + + return 0; +} diff --git a/samples/glog_init.h b/samples/glog_init.h new file mode 100644 index 0000000..5bf062f --- /dev/null +++ b/samples/glog_init.h @@ -0,0 +1,30 @@ +#ifndef MYNTEYE_GLOG_INIT_H_ // NOLINT +#define MYNTEYE_GLOG_INIT_H_ +#pragma once + +#include + +struct glog_init { + glog_init(int /*argc*/, char *argv[]) { + // FLAGS_logtostderr = true; + FLAGS_alsologtostderr = true; + FLAGS_colorlogtostderr = true; + + FLAGS_log_dir = "."; + FLAGS_max_log_size = 1024; + FLAGS_stop_logging_if_full_disk = true; + + FLAGS_v = 2; + + google::InitGoogleLogging(argv[0]); + + VLOG(2) << __func__; + } + + ~glog_init() { + VLOG(2) << __func__; + google::ShutdownGoogleLogging(); + } +}; + +#endif // MYNTEYE_GLOG_INIT_H_ NOLINT diff --git a/samples/uvc/CMakeLists.txt b/samples/uvc/CMakeLists.txt index 2fbb2f6..a7cde78 100644 --- a/samples/uvc/CMakeLists.txt +++ b/samples/uvc/CMakeLists.txt @@ -10,13 +10,13 @@ include_directories( ${PRO_DIR}/src ) -## camera +## camera_u -add_executable(camera camera.cc) -target_link_libraries(camera mynteye ${OpenCV_LIBS}) +add_executable(camera_u camera.cc) +target_link_libraries(camera_u mynteye ${OpenCV_LIBS}) if(OS_WIN) - target_compile_definitions(camera + target_compile_definitions(camera_u PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES ) endif() diff --git a/samples/uvc/camera.cc b/samples/uvc/camera.cc index f5be80f..b123648 100644 --- a/samples/uvc/camera.cc +++ b/samples/uvc/camera.cc @@ -1,5 +1,3 @@ -#include - #include #include @@ -14,28 +12,7 @@ #include "internal/types.h" #include "uvc/uvc.h" -struct glog_init { - glog_init(int /*argc*/, char *argv[]) { - // FLAGS_logtostderr = true; - FLAGS_alsologtostderr = true; - FLAGS_colorlogtostderr = true; - - FLAGS_log_dir = "."; - FLAGS_max_log_size = 1024; - FLAGS_stop_logging_if_full_disk = true; - - // FLAGS_v = 2; - - google::InitGoogleLogging(argv[0]); - - VLOG(2) << __func__; - } - - ~glog_init() { - VLOG(2) << __func__; - google::ShutdownGoogleLogging(); - } -}; +#include "glog_init.h" // NOLINT struct frame { const void *data = nullptr; diff --git a/src/device/context.cc b/src/device/context.cc new file mode 100644 index 0000000..1b2baeb --- /dev/null +++ b/src/device/context.cc @@ -0,0 +1,30 @@ +#include "device/context.h" + +#include + +#include "device/device.h" +#include "uvc/uvc.h" + +MYNTEYE_BEGIN_NAMESPACE + +Context::Context() : context_(uvc::create_context()) { + VLOG(2) << __func__; + + for (auto &&device : uvc::query_devices(context_)) { + auto name = uvc::get_name(*device); + 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; + if (vid == MYNTEYE_VID) { + devices_.push_back(Device::Create(name, device)); + } + } +} + +Context::~Context() { + VLOG(2) << __func__; +} + +MYNTEYE_END_NAMESPACE diff --git a/src/device/context.h b/src/device/context.h new file mode 100644 index 0000000..7e12fce --- /dev/null +++ b/src/device/context.h @@ -0,0 +1,36 @@ +#ifndef MYNTEYE_CONTEXT_H_ // NOLINT +#define MYNTEYE_CONTEXT_H_ +#pragma once + +#include +#include + +#include "mynteye/mynteye.h" + +MYNTEYE_BEGIN_NAMESPACE + +namespace uvc { + +struct context; + +} // namespace uvc + +class Device; + +class Context { + public: + Context(); + ~Context(); + + std::vector> devices() const { + return devices_; + } + + private: + std::shared_ptr context_; + std::vector> devices_; +}; + +MYNTEYE_END_NAMESPACE + +#endif // MYNTEYE_CONTEXT_H_ NOLINT diff --git a/src/device/device.cc b/src/device/device.cc new file mode 100644 index 0000000..7d27fd1 --- /dev/null +++ b/src/device/device.cc @@ -0,0 +1,22 @@ +#include "device/device.h" + +#include + +#include "uvc/uvc.h" + +MYNTEYE_BEGIN_NAMESPACE + +Device::Device() { + VLOG(2) << __func__; +} + +Device::~Device() { + VLOG(2) << __func__; +} + +std::shared_ptr Device::Create( + const std::string &name, std::shared_ptr device) { + return nullptr; +} + +MYNTEYE_END_NAMESPACE diff --git a/src/device/device.h b/src/device/device.h new file mode 100644 index 0000000..139b026 --- /dev/null +++ b/src/device/device.h @@ -0,0 +1,31 @@ +#ifndef MYNTEYE_DEVICE_H_ // NOLINT +#define MYNTEYE_DEVICE_H_ +#pragma once + +#include +#include + +#include "mynteye/mynteye.h" + +MYNTEYE_BEGIN_NAMESPACE + +namespace uvc { + +struct device; + +} // namespace uvc + +class Device { + public: + Device(); + virtual ~Device(); + + static std::shared_ptr Create( + const std::string &name, std::shared_ptr device); + + private: +}; + +MYNTEYE_END_NAMESPACE + +#endif // MYNTEYE_DEVICE_H_ NOLINT diff --git a/src/uvc/uvc-libuvc.cc b/src/uvc/uvc-libuvc.cc index 18da68a..f6356f5 100644 --- a/src/uvc/uvc-libuvc.cc +++ b/src/uvc/uvc-libuvc.cc @@ -1,4 +1,4 @@ -#include "uvc.h" // NOLINT +#include "uvc/uvc.h" // NOLINT #include #include diff --git a/src/uvc/uvc-v4l2.cc b/src/uvc/uvc-v4l2.cc index d8d96a2..c306cb0 100644 --- a/src/uvc/uvc-v4l2.cc +++ b/src/uvc/uvc-v4l2.cc @@ -1,4 +1,4 @@ -#include "uvc.h" // NOLINT +#include "uvc/uvc.h" // NOLINT #include #include @@ -79,6 +79,7 @@ struct device { std::string dev_name; // Device name (typically of the form /dev/video*) int busnum, devnum, parent_devnum; // USB device bus number and device number + std::string name; // Device description name int vid, pid, mi; // Vendor ID, product ID, and multiple interface index int fd = -1; // File descriptor for this device @@ -125,6 +126,10 @@ struct device { if (!good) LOG(FATAL) << "Failed to read busnum/devnum"; + if (!(std::ifstream("/sys/class/video4linux/" + name + "/name") >> + this->name)) + LOG(FATAL) << "Failed to read name"; + std::string modalias; if (!(std::ifstream( "/sys/class/video4linux/" + name + "/device/modalias") >> @@ -423,6 +428,10 @@ std::vector> query_devices( return devices; } +std::string get_name(const device &device) { + return device.name; +} + int get_vendor_id(const device &device) { return device.vid; } diff --git a/src/uvc/uvc.h b/src/uvc/uvc.h index 992e77d..6235db6 100644 --- a/src/uvc/uvc.h +++ b/src/uvc/uvc.h @@ -30,6 +30,7 @@ std::vector> query_devices( std::shared_ptr context); // Static device properties +std::string get_name(const device &device); int get_vendor_id(const device &device); int get_product_id(const device &device);