QtPdCom  1.5.2
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
33namespace 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;
75
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
113template <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
#define QTPDCOM_PUBLIC
Definition Export.h:30
Definition ScalarSubscriber.cpp:36
Q_INVOKABLE bool poll()
Poll an active subscription.
Definition ScalarSubscriber.cpp:422
double getFilterConstant() const
Definition ScalarSubscriber.cpp:391
ScalarSubscriber(const ScalarSubscriber &)
void clearVariable()
Unsubscribe from a Variable.
Definition ScalarSubscriber.cpp:362
double offset
Definition ScalarSubscriber.h:95
PdCom::Variable getVariable() const
Definition ScalarSubscriber.cpp:398
std::unique_ptr< Impl > impl
Definition ScalarSubscriber.h:101
const void * getData() const
Definition ScalarSubscriber.cpp:410
ScalarSubscriber()
Constructor.
Definition ScalarSubscriber.cpp:238
virtual void newValues(std::chrono::nanoseconds)=0
const PdCom::Selector & getSelector() const
Definition ScalarSubscriber.cpp:435
void writeValue(T)
Write a value to the process.
Definition ScalarSubscriber.h:114
bool hasVariable() const
Subscription active.
Definition ScalarSubscriber.cpp:378
double scale
Definition ScalarSubscriber.h:94
void setVariable(PdCom::Variable pv, const PdCom::Selector &selector={}, const Transmission &=event_mode, double scale=1.0, double offset=0.0, double tau=0.0)
Subscribe to a process variable via variable.
Definition ScalarSubscriber.cpp:257
virtual void stateChange(PdCom::Subscription::State)
Definition ScalarSubscriber.cpp:386
Transmission mode for subscriptions.
Definition Transmission.h:86
Definition BroadcastModel.h:32
constexpr struct QtPdCom::event_mode_tag event_mode