QtPdCom  1.3.3
ScalarSubscriber.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2009-2022 Florian Pose <fp@igh.de>
4  *
5  * This file is part of the QtPdCom library.
6  *
7  * The QtPdCom library is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * The QtPdCom library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  * License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>.
19  *
20  ****************************************************************************/
21 
22 #ifndef QTPDCOM_SCALARSUBSCRIBER_H
23 #define QTPDCOM_SCALARSUBSCRIBER_H
24 
25 #include "Transmission.h"
26 #include "Export.h"
27 
28 #include <pdcom5/Variable.h>
29 #include <pdcom5/Subscription.h>
30 
31 #include <QDebug> // qWarning()
32 
33 namespace QtPdCom {
34 
35 /****************************************************************************/
36 
40 {
41  public:
43  virtual ~ScalarSubscriber();
44 
47  void setVariable(
48  PdCom::Variable pv,
49  const PdCom::Selector &selector = {},
50  const Transmission & = event_mode,
52  double scale = 1.0,
53  double offset = 0.0,
54  double tau = 0.0
57  );
58 
61  void setVariable(
62  PdCom::Process *process,
63  const QString &path,
64  const PdCom::Selector &selector = {},
65  const Transmission & = event_mode,
67  double scale = 1.0,
68  double offset = 0.0,
69  double tau = 0.0
72  );
73  void clearVariable();
74  bool hasVariable() const;
76  virtual void newValues(std::chrono::nanoseconds) = 0;
77  virtual void stateChange(PdCom::Subscription::State);
78 
79  template <class T> void writeValue(T);
80 
81  double getFilterConstant() const;
82 
83  PdCom::Variable getVariable() const;
84  const void *getData() const;
85 
86  protected:
87  double scale;
88  double offset;
89 
90  const PdCom::Selector &getSelector() const;
91 
92  private:
93  struct Q_DECL_HIDDEN Impl;
94  std::unique_ptr<Impl> impl;
95 
96  ScalarSubscriber(const ScalarSubscriber &); // not to be used
97 };
98 
99 /****************************************************************************/
100 
106 template <class T>
108 {
109  if (not hasVariable()) {
110  qWarning() << "ScalarSubscriber::writeValue(): Not subscribed!";
111  return;
112  }
113 
114  if (scale == 0.0) {
115  qWarning() << "Avoiding division by zero scale.";
116  return;
117  }
118 
119  getVariable().setValue((value - offset) / scale, getSelector());
120 }
121 
122 /****************************************************************************/
123 
124 } // namespace
125 
126 #endif
Definition: ScalarSubscriber.cpp:35
Transmission mode for subscriptions.
Definition: Transmission.h:77
Definition: BroadcastModel.h:32
const PdCom::Selector & getSelector() const
Definition: ScalarSubscriber.cpp:420
void writeValue(T)
Write a value to the process.
Definition: ScalarSubscriber.h:107
double offset
Definition: ScalarSubscriber.h:88
Subscriber of a single scalar value.
Definition: ScalarSubscriber.h:39
#define QTPDCOM_PUBLIC
Definition: Export.h:30
std::unique_ptr< Impl > impl
Definition: ScalarSubscriber.h:94
double scale
Definition: ScalarSubscriber.h:87
PdCom::Variable getVariable() const
Definition: ScalarSubscriber.cpp:396
bool hasVariable() const
Subscription active.
Definition: ScalarSubscriber.cpp:376
constexpr struct QtPdCom::event_mode_tag event_mode