GCC Code Coverage Report


Directory: ./
File: qtpdcom/QtPdCom1/ScalarSubscriber.h
Date: 2024-11-17 04:08:36
Exec Total Coverage
Lines: 0 8 0.0%
Branches: 0 16 0.0%

Line Branch Exec Source
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
37 /** Subscriber of a single scalar value.
38 */
39 class QTPDCOM_PUBLIC ScalarSubscriber
40 {
41 public:
42 ScalarSubscriber();
43 virtual ~ScalarSubscriber();
44
45 /** Subscribe to a process variable via variable.
46 */
47 void setVariable(
48 PdCom::Variable pv, /**< Process variable. */
49 const PdCom::Selector &selector = {}, /**< Selector. */
50 const Transmission & = event_mode, /**< Transmission
51 details. */
52 double scale = 1.0, /**< Scale factor. */
53 double offset = 0.0, /**< Offset (applied after scaling). */
54 double tau = 0.0 /**< PT1 filter time constant. A value less
55 or equal to 0.0 means, that no filter is
56 applied. */
57 );
58
59 /** Subscribe to a process variable via process and path.
60 */
61 void setVariable(
62 PdCom::Process *process, /**< Process. */
63 const QString &path, /**< Variable path. */
64 const PdCom::Selector &selector = {}, /**< Selector. */
65 const Transmission & = event_mode, /**< Transmission
66 details. */
67 double scale = 1.0, /**< Scale factor. */
68 double offset = 0.0, /**< Offset (applied after scaling). */
69 double tau = 0.0 /**< PT1 filter time constant. A value less
70 or equal to 0.0 means, that no filter is
71 applied. */
72 );
73 void clearVariable();
74 bool hasVariable() const; /**< Subscription active. */
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
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
101 /** Write a value to the process.
102 *
103 * This is a convenience function, that checks for the subscription, before
104 * writing the value.
105 */
106 template <class T>
107 void ScalarSubscriber::writeValue(T value)
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
127