| 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 |
|
|
/** Poll an active subscription. |
| 87 |
|
|
* |
| 88 |
|
|
* \return false if variable could not be polled. |
| 89 |
|
|
* |
| 90 |
|
|
*/ |
| 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 |
|
|
|
| 108 |
|
|
/** Write a value to the process. |
| 109 |
|
|
* |
| 110 |
|
|
* This is a convenience function, that checks for the subscription, before |
| 111 |
|
|
* writing the value. |
| 112 |
|
|
*/ |
| 113 |
|
|
template <class T> |
| 114 |
|
✗ |
void ScalarSubscriber::writeValue(T value) |
| 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 |
| 134 |
|
|
|