QtPdCom  1.4.1
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 
91  Q_INVOKABLE bool poll();
92 
93  protected:
94  double scale;
95  double offset;
96 
97  const PdCom::Selector &getSelector() const;
98 
99  private:
100  struct Q_DECL_HIDDEN Impl;
101  std::unique_ptr<Impl> impl;
102 
103  ScalarSubscriber(const ScalarSubscriber &); // not to be used
104 };
105 
106 /****************************************************************************/
107 
113 template <class T>
115 {
116  if (not hasVariable()) {
117  qWarning() << "ScalarSubscriber::writeValue(): Not subscribed!";
118  return;
119  }
120 
121  if (scale == 0.0) {
122  qWarning() << "Avoiding division by zero scale.";
123  return;
124  }
125 
126  getVariable().setValue((value - offset) / scale, getSelector());
127 }
128 
129 /****************************************************************************/
130 
131 } // namespace
132 
133 #endif
Definition: ScalarSubscriber.cpp:35
Transmission mode for subscriptions.
Definition: Transmission.h:82
Definition: BroadcastModel.h:32
const PdCom::Selector & getSelector() const
Definition: ScalarSubscriber.cpp:433
void writeValue(T)
Write a value to the process.
Definition: ScalarSubscriber.h:114
double offset
Definition: ScalarSubscriber.h:95
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:101
double scale
Definition: ScalarSubscriber.h:94
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