forked from DanNixon/NeoNextion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextion.cpp
446 lines (398 loc) · 10.5 KB
/
Nextion.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*! \file */
#include "Nextion.h"
#include "INextionTouchable.h"
/*!
* \brief Creates a new device driver.
* \param stream Stream (serial port) the device is connected to
* \param flushSerialBeforeTx If the serial port should be flushed before
* transmission
*/
Nextion::Nextion(Stream &stream, bool flushSerialBeforeTx)
: m_serialPort(stream)
, m_timeout(500)
, m_flushSerialBeforeTx(flushSerialBeforeTx)
, m_touchableList(NULL)
{
}
/*!
* \brief Initialises the device.
* \return True if initialisation was successful.
*/
bool Nextion::init()
{
sendCommand("");
sendCommand("bkcmd=1");
bool result1 = checkCommandComplete();
sendCommand("page 0");
bool result2 = checkCommandComplete();
return (result1 && result2);
}
/*!
* \brief Polls for new messages and touch events.
*/
void Nextion::poll()
{
while (m_serialPort.available() > 0)
{
char c = m_serialPort.read();
if (c == NEX_RET_EVENT_TOUCH_HEAD)
{
delay(10);
if (m_serialPort.available() >= 6)
{
static uint8_t buffer[8];
buffer[0] = c;
uint8_t i;
for (i = 1; i < 7; i++)
buffer[i] = m_serialPort.read();
buffer[i] = 0x00;
if (buffer[4] == 0xFF && buffer[5] == 0xFF && buffer[6] == 0xFF)
{
ITouchableListItem *item = m_touchableList;
while (item != NULL)
{
item->item->processEvent(buffer[1], buffer[2], buffer[3]);
item = item->next;
}
}
}
}
}
}
/*!
* \brief Refreshes the entire page.
* \return True if successful
*/
bool Nextion::refresh()
{
sendCommand("ref 0");
return checkCommandComplete();
}
/*!
* \brief Refreshes a specific object.
* \param objectName Name of the object to refresh
* \return True if successful
*/
bool Nextion::refresh(const char *objectName)
{
size_t commandLen = 5 + strlen(objectName);
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "ref %s", objectName);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Puts the device into sleep mode.
* \return True if successful
*/
bool Nextion::sleep()
{
sendCommand("sleep=1");
return checkCommandComplete();
}
/*!
* \brief Wakes the device from sleep mode.
* \return True if successful
*/
bool Nextion::wake()
{
sendCommand("sleep=0");
return checkCommandComplete();
}
/*!
* \brief Gets the current backlight brightness.
* \return Brightness
*/
uint16_t Nextion::getBrightness()
{
sendCommand("get dim");
uint32_t val;
if (receiveNumber(&val))
return val;
else
return 0;
}
/*!
* \brief Sets the backlight brightness.
* \param val Brightness value (0-100)
* \param persist If set to true value will be set as new power on default
* \return True if successful
*/
bool Nextion::setBrightness(uint16_t val, bool persist)
{
size_t commandLen = 10;
char commandBuffer[commandLen];
if (persist)
snprintf(commandBuffer, commandLen, "dims=%d", val);
else
snprintf(commandBuffer, commandLen, "dim=%d", val);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Gets the ID of the current displayed page.
* \return Page ID
*/
uint8_t Nextion::getCurrentPage()
{
sendCommand("sendme");
uint8_t temp[5] = {0};
if (sizeof(temp) != m_serialPort.readBytes((char *)temp, sizeof(temp)))
return 0;
if (temp[0] == NEX_RET_CURRENT_PAGE_ID_HEAD && temp[2] == 0xFF &&
temp[3] == 0xFF && temp[4] == 0xFF)
return temp[1];
return 0;
}
/*!
* \brief Clears the current display.
* \param colour Colour to set display to
* \return True if successful
*/
bool Nextion::clear(uint32_t colour)
{
size_t commandLen = 9;
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "cls %ld", colour);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Draws a pre uploaded picture on the display.
* \param x X position
* \param y Y position
* \param id ID of the picture to display
* \return True if successful
*/
bool Nextion::drawPicture(uint16_t x, uint16_t y, uint8_t id)
{
size_t commandLen = 21;
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "pic %d,%d,%d", x, y, id);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Draws a cropped pre uplaoded picture on the display.
* \param x X position
* \param y Y position
* \param w Width
* \param h Height
* \param id ID of the picture to display
* \return True if successful
*/
bool Nextion::drawPicture(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
uint8_t id)
{
size_t commandLen = 35;
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "picq %d,%d,%d,%d,%d", x, y, w, h, id);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Draws a string on the display.
* \param x X position
* \param y Y position
* \param w Width
* \param h Height
* \param fontID ID of the font to use
* \param str String to draw
* \param bgColour Colour of the background of the bounding box
* \param fgColour Colour of the text
* \param bgType Background type
* \param xCentre X alignment
* \param yCentre Y alignment
* \return True if successful
*/
bool Nextion::drawStr(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
uint8_t fontID, char *str, uint32_t bgColour,
uint32_t fgColour, uint8_t bgType,
NextionFontAlignment xCentre,
NextionFontAlignment yCentre)
{
size_t commandLen = 65 + strlen(str);
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "xstr %d,%d,%d,%d,%d,%ld,%ld,%d,%d,%d,%s",
x, y, w, h, fontID, fgColour, bgColour, xCentre, yCentre, bgType,
str);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Draws a line on the display.
* \param x1 X position of first vertex
* \param y1 Y position of first vertex
* \param x2 X position of second vertex
* \param y2 Y position of second vertex
* \param colour Colour
* \return True if successful
*/
bool Nextion::drawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,
uint32_t colour)
{
size_t commandLen = 35;
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "line %d,%d,%d,%d,%ld", x1, y1, x2, y2,
colour);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Draws a rectangle on the display.
* \param x X position
* \param y Y position
* \param w Width
* \param h Height
* \param filled If the rectangle should be filled with a solid colour
* \param colour Colour
* \return True if successful
*/
bool Nextion::drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
bool filled, uint32_t colour)
{
size_t commandLen = 35;
char commandBuffer[commandLen];
if (filled)
snprintf(commandBuffer, commandLen, "draw %d,%d,%d,%d,%ld", x, y, x + w,
y + h, colour);
else
snprintf(commandBuffer, commandLen, "fill %d,%d,%d,%d,%ld", x, y, w, h,
colour);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Draws a circle on the display.
* \param x X position
* \param y Y position
* \param r Radius
* \param colour Colour
* \return True if successful
*/
bool Nextion::drawCircle(uint16_t x, uint16_t y, uint16_t r, uint32_t colour)
{
size_t commandLen = 27;
char commandBuffer[commandLen];
snprintf(commandBuffer, commandLen, "cir %d,%d,%d,%ld", x, y, r, colour);
sendCommand(commandBuffer);
return checkCommandComplete();
}
/*!
* \brief Adds a INextionTouchable to the list of registered touchable
* elements.
* \param touchable Pointer to the INextionTouchable
*
* Required for touch events from an INextionTouchable to be polled.
*
* Should be called automatically by INextionTouchable::INextionTouchable.
*/
void Nextion::registerTouchable(INextionTouchable *touchable)
{
ITouchableListItem *newListItem = new ITouchableListItem;
newListItem->item = touchable;
newListItem->next = NULL;
if (m_touchableList == NULL)
m_touchableList = newListItem;
else
{
ITouchableListItem *item = m_touchableList;
while (item->next != NULL)
item = item->next;
item->next = newListItem;
}
}
/*!
* \brief Sends a command to the device.
* \param command Command to send
*/
void Nextion::sendCommand(char *command)
{
if (m_flushSerialBeforeTx)
m_serialPort.flush();
m_serialPort.print(command);
m_serialPort.write(0xFF);
m_serialPort.write(0xFF);
m_serialPort.write(0xFF);
}
/*!
* \brief Checks if the last command was successful.
* \return True if command was successful
*/
bool Nextion::checkCommandComplete()
{
bool ret = false;
uint8_t temp[4] = {0};
if (sizeof(temp) != m_serialPort.readBytes((char *)temp, sizeof(temp)))
ret = false;
if (temp[0] == NEX_RET_CMD_FINISHED && temp[1] == 0xFF && temp[2] == 0xFF &&
temp[3] == 0xFF)
ret = true;
return ret;
}
/*!
* \brief Receive a number from the device.
* \param number Pointer to the number to store received number in
* \return True if receive was successful
*/
bool Nextion::receiveNumber(uint32_t *number)
{
uint8_t temp[8] = {0};
if (!number)
return false;
if (sizeof(temp) != m_serialPort.readBytes((char *)temp, sizeof(temp)))
return false;
if (temp[0] == NEX_RET_NUMBER_HEAD && temp[5] == 0xFF && temp[6] == 0xFF &&
temp[7] == 0xFF)
{
*number = (temp[4] << 24) | (temp[3] << 16) | (temp[2] << 8) | (temp[1]);
return true;
}
return false;
}
/*!
* \brief Receive a string from the device.
* \param buffer Pointer to buffer to store string in
* \param len Maximum length of data to receive
* \return Actual length of string received
*/
size_t Nextion::receiveString(char *buffer, size_t len)
{
memset(buffer, 0, len);
bool have_header_flag = false;
uint8_t flag_count = 0;
size_t pos = 0;
if (!buffer || len == 0)
return false;
uint32_t start = millis();
while (millis() - start <= m_timeout)
{
while (m_serialPort.available())
{
char c = m_serialPort.read();
if (have_header_flag)
{
if (c == 0xFF || c == 0xFFFFFFFF)
{
flag_count++;
if (flag_count >= 3)
break;
}
else
{
buffer[pos] = c;
pos++;
if (pos == len - 1)
break;
}
}
else if (c == NEX_RET_STRING_HEAD)
have_header_flag = true;
}
if (flag_count >= 3)
break;
}
pos++;
buffer[pos] = '\0';
return pos;
}