From 6b37ca9f9a4bfbcba8e4fe9eae2ce52f3b96afa2 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 28 Jul 2018 14:57:41 -0700 Subject: [PATCH] UsbCamera: Allow silencing of Connecting message (#1231) --- .../main/java/edu/wpi/cscore/UsbCamera.java | 9 +++++++++ cscore/src/main/native/cpp/UsbCameraImpl.cpp | 20 +++++++++++++++---- cscore/src/main/native/cpp/UsbCameraImpl.h | 1 + .../src/main/native/cpp/UsbCameraProperty.cpp | 2 +- .../src/main/native/cpp/UsbCameraProperty.h | 12 +++++++++++ cscore/src/main/native/include/cscore_oo.h | 4 ++++ cscore/src/main/native/include/cscore_oo.inl | 6 ++++++ 7 files changed, 49 insertions(+), 5 deletions(-) diff --git a/cscore/src/main/java/edu/wpi/cscore/UsbCamera.java b/cscore/src/main/java/edu/wpi/cscore/UsbCamera.java index 175256350c..6bfc844282 100644 --- a/cscore/src/main/java/edu/wpi/cscore/UsbCamera.java +++ b/cscore/src/main/java/edu/wpi/cscore/UsbCamera.java @@ -43,4 +43,13 @@ public class UsbCamera extends VideoCamera { public String getPath() { return CameraServerJNI.getUsbCameraPath(m_handle); } + + /** + * Set how verbose the camera connection messages are. + * @param level 0=don't display Connecting message, 1=do display message + */ + void setConnectVerbose(int level) { + CameraServerJNI.setProperty(CameraServerJNI.getSourceProperty(m_handle, "connect_verbose"), + level); + } } diff --git a/cscore/src/main/native/cpp/UsbCameraImpl.cpp b/cscore/src/main/native/cpp/UsbCameraImpl.cpp index 8e5ba5d7cb..95b7893f09 100644 --- a/cscore/src/main/native/cpp/UsbCameraImpl.cpp +++ b/cscore/src/main/native/cpp/UsbCameraImpl.cpp @@ -50,6 +50,8 @@ static constexpr char const* kPropWbValue = "white_balance_temperature"; static constexpr char const* kPropExAuto = "exposure_auto"; static constexpr char const* kPropExValue = "exposure_absolute"; static constexpr char const* kPropBrValue = "brightness"; +static constexpr char const* kPropConnectVerbose = "connect_verbose"; +static constexpr unsigned kPropConnectVerboseId = 0; // Conversions v4l2_fract time per frame from/to frames per second (fps) static inline int FractToFPS(const struct v4l2_fract& timeperframe) { @@ -221,6 +223,11 @@ UsbCameraImpl::UsbCameraImpl(wpi::StringRef name, wpi::StringRef path) m_active{true} { SetDescription(GetDescriptionImpl(m_path.c_str())); SetQuirks(); + CreateProperty(kPropConnectVerbose, [] { + return std::make_unique(kPropConnectVerbose, + kPropConnectVerboseId, + CS_PROP_INTEGER, 0, 1, 1, 1, 1); + }); } UsbCameraImpl::~UsbCameraImpl() { @@ -453,7 +460,7 @@ void UsbCameraImpl::DeviceDisconnect() { void UsbCameraImpl::DeviceConnect() { if (m_fd >= 0) return; - SINFO("Connecting to USB camera on " << m_path); + if (m_connectVerbose) SINFO("Connecting to USB camera on " << m_path); // Try to open the device SDEBUG3("opening device"); @@ -489,7 +496,8 @@ void UsbCameraImpl::DeviceConnect() { for (size_t i = 0; i < m_propertyData.size(); ++i) { const auto prop = static_cast(m_propertyData[i].get()); - if (!prop || !prop->valueSet || prop->percentage) continue; + if (!prop || !prop->valueSet || !prop->device || prop->percentage) + continue; if (!prop->DeviceSet(lock2, m_fd)) SWARNING("failed to set property " << prop->name); } @@ -694,8 +702,12 @@ CS_StatusValue UsbCameraImpl::DeviceCmdSetProperty( } // Actually set the new value on the device (if possible) - if (!prop->DeviceSet(lock, m_fd, value, valueStr)) - return CS_PROPERTY_WRITE_FAILED; + if (!prop->device) { + if (prop->id == kPropConnectVerboseId) m_connectVerbose = value; + } else { + if (!prop->DeviceSet(lock, m_fd, value, valueStr)) + return CS_PROPERTY_WRITE_FAILED; + } // Cache the set values UpdatePropertyValue(property, setString, value, valueStr); diff --git a/cscore/src/main/native/cpp/UsbCameraImpl.h b/cscore/src/main/native/cpp/UsbCameraImpl.h index 20726d6689..d4b83595ed 100644 --- a/cscore/src/main/native/cpp/UsbCameraImpl.h +++ b/cscore/src/main/native/cpp/UsbCameraImpl.h @@ -143,6 +143,7 @@ class UsbCameraImpl : public SourceImpl { bool m_modeSetPixelFormat{false}; bool m_modeSetResolution{false}; bool m_modeSetFPS{false}; + int m_connectVerbose{1}; #ifdef __linux__ unsigned m_capabilities = 0; #endif diff --git a/cscore/src/main/native/cpp/UsbCameraProperty.cpp b/cscore/src/main/native/cpp/UsbCameraProperty.cpp index 99d91af894..dc5bb460e1 100644 --- a/cscore/src/main/native/cpp/UsbCameraProperty.cpp +++ b/cscore/src/main/native/cpp/UsbCameraProperty.cpp @@ -289,7 +289,7 @@ bool UsbCameraProperty::DeviceSet(std::unique_lock& lock, bool UsbCameraProperty::DeviceSet(std::unique_lock& lock, int fd, int newValue, wpi::StringRef newValueStr) const { - if (fd < 0) return true; + if (!device || fd < 0) return true; unsigned idCopy = id; int rv = 0; diff --git a/cscore/src/main/native/cpp/UsbCameraProperty.h b/cscore/src/main/native/cpp/UsbCameraProperty.h index 6a3c8e4702..b977ef2bc6 100644 --- a/cscore/src/main/native/cpp/UsbCameraProperty.h +++ b/cscore/src/main/native/cpp/UsbCameraProperty.h @@ -26,6 +26,15 @@ class UsbCameraProperty : public PropertyImpl { UsbCameraProperty() = default; explicit UsbCameraProperty(wpi::StringRef name_) : PropertyImpl{name_} {} + // Software property constructor + UsbCameraProperty(wpi::StringRef name_, unsigned id_, CS_PropertyKind kind_, + int minimum_, int maximum_, int step_, int defaultValue_, + int value_) + : PropertyImpl(name_, kind_, minimum_, maximum_, step_, defaultValue_, + value_), + device{false}, + id{id_} {} + // Normalized property constructor UsbCameraProperty(wpi::StringRef name_, int rawIndex_, const UsbCameraProperty& rawProp, int defaultValue_, @@ -55,6 +64,9 @@ class UsbCameraProperty : public PropertyImpl { wpi::StringRef newValueStr) const; #endif + // If this is a device (rather than software) property + bool device{true}; + // If this is a percentage (rather than raw) property bool percentage{false}; diff --git a/cscore/src/main/native/include/cscore_oo.h b/cscore/src/main/native/include/cscore_oo.h index a1342307e0..94b3ab95c6 100644 --- a/cscore/src/main/native/include/cscore_oo.h +++ b/cscore/src/main/native/include/cscore_oo.h @@ -268,6 +268,10 @@ class UsbCamera : public VideoCamera { /// Get the path to the device. std::string GetPath() const; + + /// Set how verbose the camera connection messages are. + /// @param level 0=don't display Connecting message, 1=do display message + void SetConnectVerbose(int level); }; /// A source that represents a MJPEG-over-HTTP (IP) camera. diff --git a/cscore/src/main/native/include/cscore_oo.inl b/cscore/src/main/native/include/cscore_oo.inl index 68541a4d78..3c95487b1e 100644 --- a/cscore/src/main/native/include/cscore_oo.inl +++ b/cscore/src/main/native/include/cscore_oo.inl @@ -233,6 +233,12 @@ inline std::string UsbCamera::GetPath() const { return ::cs::GetUsbCameraPath(m_handle, &m_status); } +inline void UsbCamera::SetConnectVerbose(int level) { + m_status = 0; + SetProperty(GetSourceProperty(m_handle, "connect_verbose", &m_status), level, + &m_status); +} + inline HttpCamera::HttpCamera(wpi::StringRef name, wpi::StringRef url, HttpCameraKind kind) { m_handle = CreateHttpCamera(