WPILibC++  2020.3.2
SendableCameraWrapper.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <functional>
11 #include <memory>
12 #include <string>
13 
14 #ifndef DYNAMIC_CAMERA_SERVER
15 #include <cscore_oo.h>
16 #else
17 namespace cs {
18 class VideoSource;
19 } // namespace cs
20 typedef int CS_Handle;
21 typedef CS_Handle CS_Source;
22 #endif
23 
24 #include "frc/smartdashboard/Sendable.h"
25 #include "frc/smartdashboard/SendableHelper.h"
26 
27 namespace frc {
28 
29 class SendableCameraWrapper;
30 
31 namespace detail {
32 constexpr const char* kProtocol = "camera_server://";
33 std::shared_ptr<SendableCameraWrapper>& GetSendableCameraWrapper(
34  CS_Source source);
35 void AddToSendableRegistry(Sendable* sendable, std::string name);
36 } // namespace detail
37 
42  public SendableHelper<SendableCameraWrapper> {
43  private:
44  struct private_init {};
45 
46  public:
53  SendableCameraWrapper(CS_Source source, const private_init&);
54 
63  static SendableCameraWrapper& Wrap(const cs::VideoSource& source);
64  static SendableCameraWrapper& Wrap(CS_Source source);
65 
66  void InitSendable(SendableBuilder& builder) override;
67 
68  private:
69  std::string m_uri;
70 };
71 
72 #ifndef DYNAMIC_CAMERA_SERVER
74  const private_init&)
75  : m_uri(detail::kProtocol) {
76  CS_Status status = 0;
77  auto name = cs::GetSourceName(source, &status);
78  detail::AddToSendableRegistry(this, name);
79  m_uri += name;
80 }
81 
83  const cs::VideoSource& source) {
84  return Wrap(source.GetHandle());
85 }
86 
87 inline SendableCameraWrapper& SendableCameraWrapper::Wrap(CS_Source source) {
88  auto& wrapper = detail::GetSendableCameraWrapper(source);
89  if (!wrapper)
90  wrapper = std::make_shared<SendableCameraWrapper>(source, private_init{});
91  return *wrapper;
92 }
93 #endif
94 
95 } // namespace frc
frc::SendableCameraWrapper::SendableCameraWrapper
SendableCameraWrapper(CS_Source source, const private_init &)
Creates a new sendable wrapper.
Definition: SendableCameraWrapper.h:73
frc::SendableCameraWrapper
A wrapper to make video sources sendable and usable from Shuffleboard.
Definition: SendableCameraWrapper.h:41
cs
CameraServer (cscore) namespace.
Definition: cscore_cpp.h:35
frc::SendableCameraWrapper::InitSendable
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
cs::VideoSource
A source for video that provides a sequence of frames.
Definition: cscore_oo.h:97
frc::SendableCameraWrapper::Wrap
static SendableCameraWrapper & Wrap(const cs::VideoSource &source)
Gets a sendable wrapper object for the given video source, creating the wrapper if one does not alrea...
Definition: SendableCameraWrapper.h:82
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableBuilder
Definition: SendableBuilder.h:23