-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathNextion.cpp
338 lines (299 loc) · 8.72 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
/*
HMI Nextion Library
Bentley Born
Ricardo Mena C
http://crcibernetica.com
License
**********************************************************************************
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/>.
Licence can be viewed at
http://www.gnu.org/licenses/gpl-3.0.txt
Please maintain this license information along with authorship
and copyright notices in any redistribution of this code
**********************************************************************************
*/
#include "Nextion.h"
#if defined(USE_SOFTWARE_SERIAL)
Nextion::Nextion(SoftwareSerial &next, uint32_t baud): nextion(&next){
nextion->begin(baud);
flushSerial();
}
#else
Nextion::Nextion(HardwareSerial &next, uint32_t baud): nextion(&next){
nextion->begin(baud);
flushSerial();
}
#endif
void Nextion::buttonToggle(boolean &buttonState, String objName, uint8_t picDefualtId, uint8_t picSelected){
String tempStr = "";
if (buttonState) {
tempStr = objName + ".picc="+String(picDefualtId);//Select this picture
sendCommand(tempStr.c_str());
tempStr = "ref "+objName;//Refresh component
sendCommand(tempStr.c_str());
buttonState = false;
} else {
tempStr = objName + ".picc="+String(picSelected);//Select this picture
sendCommand(tempStr.c_str());
tempStr = "ref "+objName;//Refresh this component
sendCommand(tempStr.c_str());
buttonState = true;
}
}//end buttonPressed
uint8_t Nextion::buttonOnOff(String find_component, String unknown_component, uint8_t pin, int btn_prev_state){
uint8_t btn_state = btn_prev_state;
if((unknown_component == find_component) && (!btn_state)){
btn_state = 1;//Led is ON
digitalWrite(pin, HIGH);
}else if((unknown_component == find_component) && (btn_state)){
btn_state = 0;
digitalWrite(pin, LOW);
}else{
//return -1;
}//end if
return btn_state;
}//end buttonOnOff
boolean Nextion::setComponentValue(String component, int value){
String compValue = component +".val=" + value;//Set component value
sendCommand(compValue.c_str());
boolean acki = ack();
return acki;
}//set_component_value
boolean Nextion::ack(void){
/* CODE+END*/
uint8_t bytes[4] = {0};
nextion->setTimeout(20);
if (sizeof(bytes) != nextion->readBytes((char *)bytes, sizeof(bytes))){
return 0;
}//end if
if((bytes[1]==0xFF)&&(bytes[2]==0xFF)&&(bytes[3]==0xFF)){
switch (bytes[0]) {
case 0x00:
return false; break;
//return "0"; break;
case 0x01:
return true; break;
//return "1"; break;
/*case 0x03:
return "3"; break;
case 0x04:
return "4"; break;
case 0x05:
return "5"; break;
case 0x1A:
return "1A"; break;
case 0x1B:
return "1B"; break;//*/
default:
return false;
}//end switch
}//end if
}//end
unsigned int Nextion::getComponentValue(String component){
String getValue = "get "+ component +".val";//Get componetn value
unsigned int value = 0;
sendCommand(getValue.c_str());
uint8_t temp[8] = {0};
nextion->setTimeout(20);
if (sizeof(temp) != nextion->readBytes((char *)temp, sizeof(temp))){
return -1;
}//end if
if((temp[0]==(0x71))&&(temp[5]==0xFF)&&(temp[6]==0xFF)&&(temp[7]==0xFF)){
value = (temp[4] << 24) | (temp[3] << 16) | (temp[2] << 8) | (temp[1]);//Little-endian convertion
}//end if
return value;
}//get_component_value */
boolean Nextion::setComponentText(String component, String txt){
String componentText = component + ".txt=\"" + txt + "\"";//Set Component text
sendCommand(componentText.c_str());
return ack();
}//end set_component_txt
boolean Nextion::updateProgressBar(int x, int y, int maxWidth, int maxHeight, int value, int emptyPictureID, int fullPictureID, int orientation){
int w1 = 0;
int h1 = 0;
int w2 = 0;
int h2 = 0;
int offset1 = 0;
int offset2 = 0;
if(orientation == 0){ // horizontal
value = map(value, 0, 100, 0, maxWidth);
w1 = value;
h1 = maxHeight;
w2 = maxWidth - value;
h2 = maxHeight;
offset1 = x + value;
offset2 = y;
}else{ // vertical
value = map(value, 0, 100, 0, maxHeight);
offset2 = y;
y = y + maxHeight - value;
w1 = maxWidth;
h1 = value;
w2 = maxWidth;
h2 = maxHeight - value;
offset1 = x;
}//end if
String wipe = "picq " + String(x) + "," + String(y) + "," + String(w1) + "," + String(h1) + "," + String(fullPictureID);
sendCommand(wipe.c_str());
wipe = "picq " + String(offset1) + "," + String(offset2) + "," + String(w2) + "," + String(h2) + "," + String(emptyPictureID);
sendCommand(wipe.c_str());
return ack();
}//end updateProgressBar
String Nextion::getComponentText(String component, uint32_t timeout){
String tempStr = "get " + component + ".txt";
sendCommand(tempStr.c_str());
tempStr = "";
tempStr = listen(timeout);
/*unsigned long start = millis();
uint8_t ff = 0;//end message
while((millis()-start < timeout)){
if(nextion->available()){
char b = nextion->read();
if(String(b, HEX) == "ffff"){ff++;}
tempStr += String(b);
if(ff == 3){//End line
ff = 0;
break;
}//end if
}//end if
}//end while
if(tempStr.startsWith("p")){//0x70
tempStr = tempStr.substring(1, tempStr.length()-3);
}else{
return "1a";
}//end if*/
return tempStr;
}//getComponentText
String Nextion::listen(unsigned long timeout){//returns generic
char _bite;
char _end = 0xff;//end of file x3
String cmd;
int countEnd = 0;
while(nextion->available()>0){
delay(10);
if(nextion->available()>0){
_bite = nextion->read();
cmd += _bite;
if(_bite == _end){
countEnd++;
}//end if
if(countEnd == 3){
break;
}//end if
}//end if
}//end while
/* if(cmd != ""){
for(int o = 0 ; o < cmd.length(); o++){
Serial.print(cmd[o], HEX);
}
Serial.println();
}//*/
String temp = "";
switch (cmd[0]) {
case 'e'://0x65 Same than default -.-
countEnd = 0;//Revision for not include last space " "
for(uint8_t i = 0; i<cmd.length(); i++){
if(cmd[i] == _end){countEnd++;}//end if
temp += String(cmd[i], HEX);//add hexadecimal value
if(countEnd == 3){
return temp;
}//end if
temp += " ";//For easy visualization
}//end for
break;
case 'f'://0x66
//Serial.print(String(cmd[1], HEX));
return String(cmd[1], DEC);
break;
case 'g'://0x67
cmd = String(cmd[2], DEC) + "," + String(cmd[4], DEC) +","+ String(cmd[5], DEC);
return cmd;
break;
case 'h'://0x68
cmd = String(cmd[2], DEC) + "," + String(cmd[4], DEC) +","+ String(cmd[5], DEC);
cmd = "68 " + cmd;
return cmd;
break;
case 'p'://0x70
cmd = cmd.substring(1, cmd.length()-3);
cmd = "70 " + cmd;
return cmd;
break;
default:
// cmd += String(b, HEX);
//if(ff == 3){break;}//end if
//cmd += " ";//
return cmd;//
break;
}//end switch
return "";
}//end listen
/*String Nextion::listen(unsigned long timeout){
//TODO separar todos los eventos 0x65 0x66 0x67 0x68
char _bite;
char _end = 0xff;//end of file x3
String cmd;
int countEnd = 0;
unsigned long start = millis();
while(nextion->available()>0){
delay(10);
if(nextion->available()>0){
_bite = nextion->read();
cmd += String(_bite, HEX);
if(_bite == _end){
countEnd++;
}//end if
if(countEnd == 3){
break;
}//end if
}//end if
}//end while
return cmd;
}//end listen_nextion*/
uint8_t Nextion::pageId(void){
sendCommand("sendme");
int a = -1;
String pagId = listen();
// Serial.print("ID = ");
//Serial.print(pagId);
//Serial.println("<-");
if(pagId != ""){
return pagId.toInt();
}
return -1;
}//pageId
void Nextion::sendCommand(const char* cmd){
while (nextion->available()){
nextion->read();
}//end while
nextion->print(cmd);
nextion->write(0xFF);
nextion->write(0xFF);
nextion->write(0xFF);
}//end sendCommand
boolean Nextion::init(const char* pageId){
String page = "page " + String(pageId);//Page
sendCommand("");
ack();
sendCommand(page.c_str());
delay(100);
return ack();
}//end nextion_init
void Nextion::flushSerial(){
Serial.flush();
nextion->flush();
}//end flush