CuteHMI - Modbus (CuteHMI.Modbus.2)
AbstractRegisterController.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_ABSTRACTREGISTERCONTROLLER_HPP
2 #define H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_ABSTRACTREGISTERCONTROLLER_HPP
3 
4 #include "internal/common.hpp"
5 #include "AbstractDevice.hpp"
6 
7 #include <QObject>
8 #include <QQmlParserStatus>
9 
10 namespace cutehmi {
11 namespace modbus {
12 
16 class CUTEHMI_MODBUS_API AbstractRegisterController:
17  public QObject,
18  public QQmlParserStatus
19 {
20  Q_OBJECT
21  Q_INTERFACES(QQmlParserStatus)
22 
23  public:
27  enum WriteMode {
32  };
33  Q_ENUM(WriteMode)
34 
35  static constexpr unsigned int INITIAL_ADDRESS = 0;
36  static constexpr WriteMode INITIAL_WRITE_MODE = WRITE_DELAYED;
37  static constexpr int INITIAL_WRITE_DELAY = 500;
38  static constexpr bool INITIAL_BUSY = true;
39  static constexpr bool INITIAL_READ_ON_WRITE = true;
40  static constexpr bool INITIAL_ENABLED = true;
41 
45  Q_PROPERTY(AbstractDevice * device READ device WRITE setDevice NOTIFY deviceChanged)
46 
50  // Note: unsigned int is guaranteed to be at least 16 bits wide by the standard. Using unsigned int instead of quint16 (aka
51  // ushort), because when using quint16 QML throws an error (unsupported type "ushort") for an alias to quint16 property.
52  Q_PROPERTY(unsigned int address READ address WRITE setAddress NOTIFY addressChanged)
53 
57  Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
58 
62  Q_PROPERTY(bool readOnWrite READ readOnWrite WRITE setReadOnWrite NOTIFY readOnWriteChanged)
63 
67  Q_PROPERTY(WriteMode writeMode READ writeMode WRITE setWriteMode NOTIFY writeModeChanged)
68 
72  Q_PROPERTY(int writeDelay READ writeDelay WRITE setWriteDelay NOTIFY writeDelayChanged)
73 
77  Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
78 
79  AbstractRegisterController(QObject * parent = nullptr);
80 
81  AbstractDevice * device() const;
82 
83  void setDevice(AbstractDevice * device);
84 
85  unsigned int address() const;
86 
87  void setAddress(unsigned int address);
88 
89  bool busy() const;
90 
91  bool readOnWrite() const;
92 
93  void setReadOnWrite(bool readOnWrite);
94 
95  WriteMode writeMode() const;
96 
97  void setWriteMode(WriteMode writeMode);
98 
99  int writeDelay() const;
100 
101  void setWriteDelay(int writeDelay);
102 
103  bool enabled() const;
104 
105  void setEnabled(bool enabled);
106 
107  signals:
108  void deviceChanged();
109 
110  void addressChanged();
111 
112  void busyChanged();
113 
114  void readOnWriteChanged();
115 
116  void writeModeChanged();
117 
118  void writeDelayChanged();
119 
120  void enabledChanged();
121 
122  protected:
123  virtual void requestReadRegisters(quint16 address, quint16 amount, QUuid * requestId) const = 0;
124 
125  virtual quint16 bytes() const = 0;
126 
127  virtual void onDeviceDestroyed() = 0;
128 
129  void classBegin() override;
130 
131  void componentComplete() override;
132 
133  void setBusy(bool busy);
134 
135  protected slots:
136  virtual void onRequestCompleted(QJsonObject request, QJsonObject reply) = 0;
137 
138  private:
139  bool deviceReady() const;
140 
141  struct Members
142  {
143  AbstractDevice * device;
144  unsigned int address;
145  bool busy;
146  bool readOnWrite;
147  WriteMode writeMode;
148  int writeDelay;
149  bool enabled;
150  bool deferRequestRead;
151 
152  Members():
153  device(nullptr),
154  address(INITIAL_ADDRESS),
155  busy(INITIAL_BUSY),
156  readOnWrite(INITIAL_READ_ON_WRITE),
157  writeMode(INITIAL_WRITE_MODE),
158  writeDelay(INITIAL_WRITE_DELAY),
159  enabled(INITIAL_ENABLED),
160  deferRequestRead(false)
161  {
162  }
163  };
164 
165  MPtr<Members> m;
166 };
167 
168 }
169 }
170 
171 #endif
172 
173 //(c)C: Copyright © 2019-2020, Michał Policht <michal@policht.pl>, Yuri Chornoivan <yurchor@ukr.net>. All rights reserved.
174 //(c)C: This file is a part of CuteHMI.
175 //(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.
176 //(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.
177 //(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/>.
cutehmi::modbus::AbstractRegisterController::WRITE_POSTPONED
@ WRITE_POSTPONED
Writes are postponed, which means that write will not be requested until previous write has finished.
Definition: AbstractRegisterController.hpp:29
cutehmi::modbus::AbstractRegisterController::WRITE_DELAYED
@ WRITE_DELAYED
Writes are delayed. Write will be requested after amount of time specified by writeDelay property has...
Definition: AbstractRegisterController.hpp:28
QUuid
cutehmi::modbus::AbstractDevice
Abstract Modbus device.
Definition: AbstractDevice.hpp:33
cutehmi::MPtr< Members >
QObject
cutehmi
cutehmi::modbus::AbstractRegisterController
Abstract register controller.
Definition: AbstractRegisterController.hpp:16
QJsonObject
cutehmi::modbus::AbstractRegisterController::WRITE_IMMEDIATE
@ WRITE_IMMEDIATE
Writes are requested immediately, which may creates a queue of pending writes if they are not proceed...
Definition: AbstractRegisterController.hpp:30
cutehmi::modbus::AbstractRegisterController::WRITE_EXPLICIT
@ WRITE_EXPLICIT
Writes are explicit, i.e. write requires an explicit call to write value function.
Definition: AbstractRegisterController.hpp:31
cutehmi::modbus::AbstractRegisterController::WriteMode
WriteMode
Write mode enum.
Definition: AbstractRegisterController.hpp:27
QQmlParserStatus