CuteHMI - Modbus (CuteHMI.Modbus.2)
Register16Controller.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_REGISTER16CONTROLLER_HPP
2 #define H_EXTENSIONS_CUTEHMI_MODBUS_2_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 
11 namespace cutehmi {
12 namespace modbus {
13 
17 class CUTEHMI_MODBUS_API Register16Controller:
19  protected internal::RegisterControllerMixin<Register16Controller>
20 {
21  Q_OBJECT
22 
25 
26  public:
27  enum Encoding {
29  UINT16
30  };
31  Q_ENUM(Encoding)
32 
33  static constexpr qreal INITIAL_VALUE = 0.0;
34  static constexpr qreal INITIAL_VALUE_SCALE = 1.0;
35  static constexpr Encoding INITIAL_ENCODING = UINT16;
36 
37  Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
38  Q_PROPERTY(qreal valueScale READ valueScale WRITE setValueScale NOTIFY valueScaleChanged)
39  Q_PROPERTY(Encoding encoding READ encoding WRITE setEncoding NOTIFY encodingChanged)
40 
41  Register16Controller(QObject * parent = nullptr);
42 
43  ~Register16Controller() override;
44 
45  qreal value() const;
46 
47  void setValue(qreal value);
48 
49  qreal valueScale() const;
50 
51  void setValueScale(qreal valueScale);
52 
53  Encoding encoding() const;
54 
55  void setEncoding(Encoding encoding);
56 
57  public slots:
58  void writeValue();
59 
60  signals:
61  void valueChanged();
62 
63  void valueUpdated();
64 
65  void valueScaleChanged();
66 
67  void encodingChanged();
68 
69  void valueWritten();
70 
71  void valueFailed();
72 
73  void valueMismatch();
74 
75  protected:
76  virtual AbstractDevice::Function readRegistersFunction() const = 0;
77 
78  virtual AbstractDevice::Function writeRegisterFunction() const = 0;
79 
80  virtual void requestWriteRegister(quint16 address, quint16 value, QUuid * requestId) const = 0;
81 
82  virtual Register16 * registerAt(quint16 address) const = 0;
83 
84  void timerEvent(QTimerEvent * event) override;
85 
86  quint16 bytes() const override;
87 
88  void onDeviceDestroyed() override;
89 
90  protected slots:
91  void onRequestCompleted(QJsonObject request, QJsonObject reply) override;
92 
93  void resetRegister();
94 
95  private:
96  void updateValue();
97 
98  void updateValue(quint16 value);
99 
100  void updateValue(const QJsonValue & value);
101 
102  static qreal Decode(quint16 value, Encoding encoding);
103 
104  static quint16 Encode(qreal value, Encoding encoding);
105 
106  static bool ValidateEncoding(qreal value, Encoding encoding);
107 
108  void requestWrite(qreal value);
109 
110  bool verifyRegisterValue() const;
111 
112  private:
113  struct Members
114  {
115  qreal value;
116  qreal valueScale;
117  Encoding encoding;
118  bool postponedWritePending;
119  bool adjustingValue;
120  qreal requestedValue;
121  Register16 * register16;
122  QUuid requestId;
123  QBasicTimer writeTimer;
124 
125  Members():
126  value(INITIAL_VALUE),
127  valueScale(INITIAL_VALUE_SCALE),
128  encoding(INITIAL_ENCODING),
129  postponedWritePending(false),
130  adjustingValue(false),
131  requestedValue(0.0),
132  register16(nullptr)
133  {
134  }
135  };
136 
137  MPtr<Members> m;
138 };
139 
140 }
141 }
142 
143 #endif
144 
145 //(c)C: Copyright © 2019, Michał Policht <michal@policht.pl>. All rights reserved.
146 //(c)C: This file is a part of CuteHMI.
147 //(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.
148 //(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.
149 //(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/>.
QTimerEvent
cutehmi::modbus::Register16Controller
Register controller for 16 bit registers.
Definition: Register16Controller.hpp:17
cutehmi::modbus::Register16Controller::INT16
@ INT16
Definition: Register16Controller.hpp:28
cutehmi::modbus::internal::RegisterControllerMixin
Definition: RegisterControllerMixin.hpp:17
QUuid
cutehmi::modbus::AbstractDevice
Abstract Modbus device.
Definition: AbstractDevice.hpp:32
QBasicTimer
cutehmi::MPtr< Members >
QJsonValue
QObject
cutehmi
cutehmi::modbus::AbstractRegisterController
Abstract register controller.
Definition: AbstractRegisterController.hpp:16
QJsonObject
cutehmi::modbus::Register16Controller::Encoding
Encoding
Definition: Register16Controller.hpp:27
cutehmi::modbus::Register16
Cached properties of 16 bit register.
Definition: Register16.hpp:14