This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathadvi3pp_dgus.h
308 lines (255 loc) · 10.2 KB
/
advi3pp_dgus.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* Marlin 3D Printer Firmware For Wanhao Duplicator i3 Plus (ADVi3++)
* DWIN DGUS utility classes
*
* Copyright (C) 2017-2019 Sebastien Andrivet [https://github.com/andrivet/]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ADV_I3_PLUS_PLUS_DGUS_H
#define ADV_I3_PLUS_PLUS_DGUS_H
#ifndef ADVi3PP_UNIT_TEST
#include "Marlin.h"
#endif
#include "duration_t.h"
#include "ADVstring.h"
namespace advi3pp {
//! List of commands and their values (DGUS Mini)
enum class Command: uint8_t
{
WriteRegisterData = 0x80, // 128
ReadRegisterData = 0x81, // 129
WriteRamData = 0x82, // 130
ReadRamData = 0x83, // 131
WriteCurveData = 0x84 // 132
};
//! List of registers and their values (DGUS Mini)
enum class Register: uint8_t
{
Version = 0x00,
Brightness = 0x01,
BuzzerBeepingTime = 0x02,
PictureID = 0x03,
TouchPanelFlag = 0x05,
TouchPanelStatus = 0x06,
TouchPanelPosition = 0x07,
TouchPanelEnable = 0x0B,
RunTime = 0x0C,
R0 = 0x10,
R1 = 0x11,
R2 = 0x12,
R3 = 0x13,
R4 = 0x14,
R5 = 0x15,
R6 = 0x16,
R7 = 0x17,
R8 = 0x18,
R9 = 0x19,
RA = 0x1A,
RtcComAdj = 0x1F,
RtcNow = 0x20,
EnLibOP = 0x40,
LibOPMode = 0x41,
LibID = 0x42,
LibAddress = 0x43,
VP = 0x46,
OPLength = 0x48,
Timer0 = 0x4A,
Timer1 = 0x4C,
Timer2 = 0x4D,
Timer3 = 0x4E,
KeyCode = 0x4F,
TrendlineClear = 0xEB,
ResetTrigger = 0xEE
};
enum class Variable: uint16_t;
enum class Action: uint16_t;
enum class KeyValue: uint16_t;
enum class Page: uint8_t;
// --------------------------------------------------------------------
// Uint8
// --------------------------------------------------------------------
//! An unsigned 8 bits value.
struct Uint8
{
uint8_t byte{}; //!< The actual value
constexpr explicit Uint8(uint8_t value = 0) : byte{value} {}
constexpr explicit Uint8(Register reg) : byte{static_cast<uint8_t>(reg)} {}
constexpr explicit Uint8(Page page) : byte{static_cast<uint8_t>(page)} {}
};
//! An unsigned 8 bits literal such as: 0_u8.
constexpr Uint8 operator "" _u8(unsigned long long int byte) { return Uint8(static_cast<uint8_t>(byte)); }
// --------------------------------------------------------------------
// Uint16
// --------------------------------------------------------------------
//! An unsigned 16 bits value.
struct Uint16
{
uint16_t word; //!< The actual value
constexpr explicit Uint16(uint16_t value = 0) : word{value} {}
constexpr explicit Uint16(int16_t value) : word{static_cast<uint16_t>(value)} {}
constexpr explicit Uint16(long value) : word{static_cast<uint16_t>(value)} {}
constexpr explicit Uint16(double value) : word{static_cast<uint16_t>(value)} {}
constexpr explicit Uint16(bool value) : word{static_cast<uint16_t>(value)} {}
constexpr explicit Uint16(Variable var) : word{static_cast<uint16_t>(var)} {}
};
//! An unsigned 16 bits literal such as: 0_u16.
constexpr Uint16 operator "" _u16(unsigned long long int word) { return Uint16(static_cast<uint16_t>(word)); }
// --------------------------------------------------------------------
// Uint32
// --------------------------------------------------------------------
//! An unsigned 32 bits value.
struct Uint32
{
uint32_t dword; //!< The actual value
constexpr explicit Uint32(uint32_t value = 0) : dword{value} {}
constexpr explicit Uint32(int32_t value) : dword{static_cast<uint32_t>(value)} {}
constexpr explicit Uint32(double value) : dword{static_cast<uint32_t>(value)} {}
};
//! An unsigned 32 bits literal such as: 0_u32.
constexpr Uint32 operator "" _u32(unsigned long long int dword) { return Uint32(static_cast<uint32_t>(dword)); }
// --------------------------------------------------------------------
// Frame
// --------------------------------------------------------------------
//! A frame to be send to the LCD or received from the LCD
struct Frame
{
bool send(bool logging = true); // Logging is only used in DEBUG builds
bool available(uint8_t bytes = 3);
bool receive(bool logging = true); // Logging is only used in DEBUG builds
Command get_command() const;
size_t get_length() const;
void reset();
friend Frame& operator<<(Frame& frame, const Uint8& data);
friend Frame& operator<<(Frame& frame, const Uint16& data);
friend Frame& operator<<(Frame& frame, const Uint32& data);
friend Frame& operator<<(Frame& frame, Page page);
friend Frame& operator<<(Frame& frame, const char* s);
template<size_t L> friend Frame& operator<<(Frame& frame, const ADVString<L>& data) { frame << data.get(); return frame; }
friend Frame& operator>>(Frame& frame, Uint8& data);
friend Frame& operator>>(Frame& frame, Uint16& data);
friend Frame& operator>>(Frame& frame, Uint32& data);
friend Frame& operator>>(Frame& frame, Action& action);
friend Frame& operator>>(Frame& frame, Command& command);
friend Frame& operator>>(Frame& frame, Register& reg);
friend Frame& operator>>(Frame& frame, Variable& var);
#ifdef ADVi3PP_UNIT_TEST
const uint8_t* get_data() const;
#endif
protected:
Frame();
explicit Frame(Command command);
void reset(Command command);
friend Frame& operator<<(Frame& frame, Register reg);
friend Frame& operator<<(Frame& frame, Variable var);
private:
void wait_for_data(uint8_t length);
protected:
static const size_t FRAME_BUFFER_SIZE = 255;
static const uint8_t HEADER_BYTE_0 = 0x5A;
static const uint8_t HEADER_BYTE_1 = 0xA5;
struct Position { enum { Header0 = 0, Header1 = 1, Length = 2, Command = 3, Data = 4, Register = 4, Variable = 4,
NbBytes = 5, NbWords = 6 }; };
uint8_t buffer_[FRAME_BUFFER_SIZE] = {};
uint8_t position_ = 0;
};
// --------------------------------------------------------------------
// IncomingFrame
// --------------------------------------------------------------------
struct IncomingFrame: Frame
{
IncomingFrame(): Frame{} {}
};
// --------------------------------------------------------------------
// WriteRegisterDataRequest
// --------------------------------------------------------------------
struct WriteRegisterDataRequest: Frame
{
explicit WriteRegisterDataRequest(Register reg);
};
// --------------------------------------------------------------------
// ReadRegisterDataRequest
// --------------------------------------------------------------------
struct ReadRegisterDataRequest: Frame
{
ReadRegisterDataRequest(Register reg, uint8_t nb_bytes);
Register get_register() const;
uint8_t get_nb_bytes() const;
};
// --------------------------------------------------------------------
// ReadRegisterDataResponse
// --------------------------------------------------------------------
struct ReadRegisterDataResponse: Frame
{
ReadRegisterDataResponse() = default;
bool receive(Register reg, uint8_t nb_bytes, bool log = true); // Logging is only used in DEBUG builds
bool receive(const ReadRegisterDataRequest& request, bool log = true); // Logging is only used in DEBUG builds
};
// --------------------------------------------------------------------
// ReadRegister (Request and Response)
// --------------------------------------------------------------------
struct ReadRegister: ReadRegisterDataResponse
{
ReadRegister(Register reg, uint8_t nb_bytes);
bool send_and_receive(bool log = true); // Logging is only used in DEBUG builds
private:
ReadRegisterDataRequest request;
};
// --------------------------------------------------------------------
// WriteRamDataRequest
// --------------------------------------------------------------------
struct WriteRamDataRequest: Frame
{
explicit WriteRamDataRequest(Variable var);
void reset(Variable var);
};
// --------------------------------------------------------------------
// ReadRamDataRequest
// --------------------------------------------------------------------
struct ReadRamDataRequest: Frame
{
ReadRamDataRequest(Variable var, uint8_t nb_words);
Variable get_variable() const;
uint8_t get_nb_words() const;
};
// --------------------------------------------------------------------
// ReadRamDataResponse
// --------------------------------------------------------------------
struct ReadRamDataResponse: Frame
{
ReadRamDataResponse() = default;
bool receive(Variable var, uint8_t nb_words);
bool receive(const ReadRamDataRequest& request);
};
// --------------------------------------------------------------------
// ReadRamData (Request and Response)
// --------------------------------------------------------------------
struct ReadRamData: ReadRamDataResponse
{
ReadRamData(Variable var, uint8_t nb_words);
bool send_and_receive();
private:
ReadRamDataRequest request;
};
// --------------------------------------------------------------------
// WriteCurveDataRequest
// --------------------------------------------------------------------
struct WriteCurveDataRequest: Frame
{
explicit WriteCurveDataRequest(uint8_t channels);
};
// --------------------------------------------------------------------
}
#endif //ADV_I3_PLUS_PLUS_DGUS_H