CuteHMI - Modbus (CuteHMI.Modbus.4)
Register16Controller.hpp
1#ifndef H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_REGISTER16CONTROLLER_HPP
2#define H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_REGISTER16CONTROLLER_HPP
3
4#include "internal/common.hpp"
5#include "internal/RegisterControllerMixin.hpp"
6#include "Register16.hpp"
7#include "AbstractRegisterController.hpp"
8
9#include <QBasicTimer>
10#include <QQmlEngine>
11
12namespace cutehmi {
13namespace modbus {
14
18class CUTEHMI_MODBUS_API Register16Controller:
20 protected internal::RegisterControllerMixin<Register16Controller>
21{
22 Q_OBJECT
23 QML_NAMED_ELEMENT(Register16Controller)
24 QML_UNCREATABLE("Register16Controller is an abstract class")
25
28
29 public:
30 enum Encoding {
32 UINT16
33 };
34 Q_ENUM(Encoding)
35
36 static constexpr qreal INITIAL_VALUE = 0.0;
37 static constexpr qreal INITIAL_VALUE_SCALE = 1.0;
38 static constexpr Encoding INITIAL_ENCODING = UINT16;
39
40 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
41 Q_PROPERTY(qreal valueScale READ valueScale WRITE setValueScale NOTIFY valueScaleChanged)
42 Q_PROPERTY(Encoding encoding READ encoding WRITE setEncoding NOTIFY encodingChanged)
43
44 Register16Controller(QObject * parent = nullptr);
45
46 ~Register16Controller() override;
47
48 qreal value() const;
49
50 void setValue(qreal value);
51
52 qreal valueScale() const;
53
54 void setValueScale(qreal valueScale);
55
56 Encoding encoding() const;
57
58 void setEncoding(Encoding encoding);
59
60 public slots:
61 void writeValue();
62
63 signals:
64 void valueChanged();
65
66 void valueUpdated();
67
68 void valueScaleChanged();
69
70 void encodingChanged();
71
72 void valueWritten();
73
74 void valueFailed();
75
76 void valueMismatch();
77
78 protected:
79 virtual AbstractDevice::Function readRegistersFunction() const = 0;
80
81 virtual AbstractDevice::Function writeRegisterFunction() const = 0;
82
83 virtual void requestWriteRegister(quint16 address, quint16 value, QUuid * requestId) const = 0;
84
85 virtual Register16 * registerAt(quint16 address) const = 0;
86
87 void timerEvent(QTimerEvent * event) override;
88
89 quint16 bytes() const override;
90
91 void onDeviceDestroyed() override;
92
93 protected slots:
94 void onRequestCompleted(QJsonObject request, QJsonObject reply) override;
95
96 void resetRegister();
97
98 private:
99 void updateValue();
100
101 void updateValue(quint16 value);
102
103 void updateValue(const QJsonValue & value);
104
105 static qreal Decode(quint16 value, Encoding encoding);
106
107 static quint16 Encode(qreal value, Encoding encoding);
108
109 static bool ValidateEncoding(qreal value, Encoding encoding);
110
111 void requestWrite(qreal value);
112
113 bool verifyRegisterValue() const;
114
115 private:
116 struct Members
117 {
118 qreal value;
119 qreal valueScale;
120 Encoding encoding;
121 bool postponedWritePending;
122 bool adjustingValue;
123 qreal requestedValue;
124 Register16 * register16;
125 QUuid requestId;
126 QBasicTimer writeTimer;
127
128 Members():
129 value(INITIAL_VALUE),
130 valueScale(INITIAL_VALUE_SCALE),
131 encoding(INITIAL_ENCODING),
132 postponedWritePending(false),
133 adjustingValue(false),
134 requestedValue(0.0),
135 register16(nullptr)
136 {
137 }
138 };
139
141};
142
143}
144}
145
146#endif
147
148//(c)C: Copyright © 2022, Michał Policht <michal@policht.pl>. All rights reserved.
149//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
150//(c)C: This file is a part of CuteHMI.
151//(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
152//(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
153//(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
154//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
155//(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
156//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
157//(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Abstract Modbus device.
Definition: AbstractDevice.hpp:38
Abstract register controller.
Definition: AbstractRegisterController.hpp:20
Register controller for 16 bit registers.
Definition: Register16Controller.hpp:21
Encoding
Definition: Register16Controller.hpp:30
@ INT16
Definition: Register16Controller.hpp:31
Cached properties of 16 bit register.
Definition: Register16.hpp:15
Definition: RegisterControllerMixin.hpp:18