CuteHMI - Modbus (CuteHMI.Modbus.4)
AbstractRegisterController.hpp
1#ifndef H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_ABSTRACTREGISTERCONTROLLER_HPP
2#define H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_ABSTRACTREGISTERCONTROLLER_HPP
3
4#include "internal/common.hpp"
5#include "AbstractDevice.hpp"
6
7#include <QObject>
8#include <QQmlParserStatus>
9#include <QQmlEngine>
10
11namespace cutehmi {
12namespace modbus {
13
17class CUTEHMI_MODBUS_API AbstractRegisterController:
18 public QObject,
19 public QQmlParserStatus
20{
21 Q_OBJECT
22 Q_INTERFACES(QQmlParserStatus)
23 QML_NAMED_ELEMENT(AbstractRegisterController)
24 QML_UNCREATABLE("AbstractRegisterController is an abstract class")
25
26 public:
30 enum WriteMode {
35 };
36 Q_ENUM(WriteMode)
37
38 static constexpr unsigned int INITIAL_ADDRESS = 0;
39 static constexpr WriteMode INITIAL_WRITE_MODE = WRITE_DELAYED;
40 static constexpr int INITIAL_WRITE_DELAY = 500;
41 static constexpr bool INITIAL_BUSY = true;
42 static constexpr bool INITIAL_READ_ON_WRITE = true;
43 static constexpr bool INITIAL_ENABLED = true;
44
48 Q_PROPERTY(cutehmi::modbus::AbstractDevice * device READ device WRITE setDevice NOTIFY deviceChanged)
49
53 // Note: unsigned int is guaranteed to be at least 16 bits wide by the standard. Using unsigned int instead of quint16 (aka
54 // ushort), because when using quint16 QML throws an error (unsupported type "ushort") for an alias to quint16 property.
55 Q_PROPERTY(unsigned int address READ address WRITE setAddress NOTIFY addressChanged)
56
60 Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
61
65 Q_PROPERTY(bool readOnWrite READ readOnWrite WRITE setReadOnWrite NOTIFY readOnWriteChanged)
66
70 Q_PROPERTY(WriteMode writeMode READ writeMode WRITE setWriteMode NOTIFY writeModeChanged)
71
75 Q_PROPERTY(int writeDelay READ writeDelay WRITE setWriteDelay NOTIFY writeDelayChanged)
76
80 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
81
82 AbstractRegisterController(QObject * parent = nullptr);
83
84 AbstractDevice * device() const;
85
86 void setDevice(AbstractDevice * device);
87
88 unsigned int address() const;
89
90 void setAddress(unsigned int address);
91
92 bool busy() const;
93
94 bool readOnWrite() const;
95
96 void setReadOnWrite(bool readOnWrite);
97
98 WriteMode writeMode() const;
99
100 void setWriteMode(WriteMode writeMode);
101
102 int writeDelay() const;
103
104 void setWriteDelay(int writeDelay);
105
106 bool enabled() const;
107
108 void setEnabled(bool enabled);
109
110 signals:
111 void deviceChanged();
112
113 void addressChanged();
114
115 void busyChanged();
116
117 void readOnWriteChanged();
118
119 void writeModeChanged();
120
121 void writeDelayChanged();
122
123 void enabledChanged();
124
125 protected:
126 virtual void requestReadRegisters(quint16 address, quint16 amount, QUuid * requestId) const = 0;
127
128 virtual quint16 bytes() const = 0;
129
130 virtual void onDeviceDestroyed() = 0;
131
132 void classBegin() override;
133
134 void componentComplete() override;
135
136 void setBusy(bool busy);
137
138 protected slots:
139 virtual void onRequestCompleted(QJsonObject request, QJsonObject reply) = 0;
140
141 private:
142 bool deviceReady() const;
143
144 struct Members
145 {
146 AbstractDevice * device;
147 unsigned int address;
148 bool busy;
149 bool readOnWrite;
150 WriteMode writeMode;
151 int writeDelay;
152 bool enabled;
153 bool deferRequestRead;
154
155 Members():
156 device(nullptr),
157 address(INITIAL_ADDRESS),
158 busy(INITIAL_BUSY),
159 readOnWrite(INITIAL_READ_ON_WRITE),
160 writeMode(INITIAL_WRITE_MODE),
161 writeDelay(INITIAL_WRITE_DELAY),
162 enabled(INITIAL_ENABLED),
163 deferRequestRead(false)
164 {
165 }
166 };
167
169};
170
171}
172}
173
174#endif
175
176//(c)C: Copyright © 2022, Michał Policht <michal@policht.pl>. All rights reserved.
177//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
178//(c)C: This file is a part of CuteHMI.
179//(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.
180//(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.
181//(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/>.
182//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
183//(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:
184//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
185//(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
WriteMode
Write mode enum.
Definition: AbstractRegisterController.hpp:30
@ WRITE_DELAYED
Writes are delayed. Write will be requested after amount of time specified by writeDelay property has...
Definition: AbstractRegisterController.hpp:31
@ WRITE_POSTPONED
Writes are postponed, which means that write will not be requested until previous write has finished.
Definition: AbstractRegisterController.hpp:32
@ WRITE_IMMEDIATE
Writes are requested immediately, which may creates a queue of pending writes if they are not proceed...
Definition: AbstractRegisterController.hpp:33
@ WRITE_EXPLICIT
Writes are explicit, i.e. write requires an explicit call to write value function.
Definition: AbstractRegisterController.hpp:34