-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextionFirmware.cpp
334 lines (271 loc) · 7.55 KB
/
NextionFirmware.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
#include "NextionFirmware.h"
int ARDUINOSERIALBUFFERSIZE = 64;
volatile int tx_completed = 0;
void TX_Complete() {
tx_completed = 1;
}
NextionFirmware::NextionFirmware(NeoICSerial * nextion){
_nextion=nextion;
}
uint32_t NextionFirmware::_baudrate; /*nextion serail baudrate*/
uint32_t NextionFirmware::_fwsize; /*undownload byte of tft file*/
uint32_t NextionFirmware::_download_baudrate; /*download baudrate*/
char NextionFirmware::recv[180];
int NextionFirmware::recv_len = 0;
bool NextionFirmware::bdebug=true;
long nuploaded=0;
void NextionFirmware::SerialDbgRecv() {
Serial.println(recv);
for (int i = 0; i < recv_len; i++) {
Serial.print(recv[i], HEX);
if (i > 0) {
if (i % 8 == 0) Serial.print(" ");
if (i % 32 == 0) Serial.print("\r\n");
}
}
}
void NextionFirmware::DebugOut(const char *msg){
if(!bdebug) return;
String string = "DBG: " + String(msg);
sendCommand(string.c_str());
}
bool NextionFirmware::upload_init(uint32_t fwsize,uint32_t download_baudrate){
_fwsize=fwsize;
nuploaded=0;
_download_baudrate=download_baudrate;
if(_getBaudrate() == 0)
{
return 0;
}
uint32_t newbaud = 9600;
if (_baudrate != newbaud) {
String cmd = "baud=";
cmd += String(newbaud);
sendCommand(cmd.c_str());
recvRetString(500, true);
//Serial.print("Setting baud to ");
//Serial.print(newbaud);
//Serial.print(" from ");
//Serial.println(_baudrate);
delay(500);
if (_getBaudrate() == 0)
{
return 0;
}
}
// we change to 4800 baud!
if(!_setDownloadBaudrate(_download_baudrate))
{
return 0;
}
return 1;
}
uint32_t NextionFirmware::_getBaudrate(void)
{
uint32_t baudrate_array[7] = { 4800,9600,115200,19200,57600,38400,2400};
for(uint8_t i = 0; i < 7; i++)
{
if(_searchBaudrate(baudrate_array[i]))
{
_baudrate = baudrate_array[i];
//Serial.print("\r\nBaudrate=");
//Serial.println(_baudrate);
break;
}
}
return _baudrate;
}
bool NextionFirmware::_searchBaudrate(uint32_t baudrate)
{
_nextion->end();
_nextion->begin(baudrate);
_nextion->setTimeout(500);
sendCommand("DRAKJHSUYDGBNCJHGJKSHBDN");
send0Command();
recvRetString(500, true);
sendCommand("connect");
recvRetString(500, true);
delay(110);
sendCommand("ÿÿ");
sendCommand("connect");
recvRetString(500, true);
char* p = strstr(recv, "comok");
if(p==nullptr){
//Serial.println("comok expected.");
return false;
}
return true;
}
void NextionFirmware::send0Command() {
//while (_nextion->available()) { _nextion->read(); }
tx_completed = 0;
_nextion->attachTxCompleteInterrupt(TX_Complete);
_nextion->write((uint8_t)0x00);
_nextion->write(0xFF); _nextion->write(0xFF); _nextion->write(0xFF);
//Serial.print("->0x00FFFFFF");
_nextion->flush();
while (tx_completed == 0) {
delay(50);
}
_nextion->detachTxCompleteInterrupt();
}
void NextionFirmware::sendCommand(const char* cmd)
{
//while (_nextion->available()) { _nextion->read(); }
tx_completed = 0;
_nextion->attachTxCompleteInterrupt(TX_Complete);
//Serial.print("->");
//Serial.print(cmd);
_nextion->write(cmd);
_nextion->write(0xFF);_nextion->write(0xFF); _nextion->write(0xFF);
//Serial.print("0xFFFFFF");
_nextion->flush();
while (tx_completed == 0) {
delay(50);
}
_nextion->detachTxCompleteInterrupt();
}
uint16_t NextionFirmware::recvRetString( uint32_t timeout,bool ack_flag)
{
uint16_t ret = 0;
uint8_t c = 0;
long start;
bool exit_flag = false;
start = millis();
recv_len = 0;
while (millis() - start <= timeout)
{
while (_nextion->available())
{
c = _nextion->read();
recv[recv_len++]= (char)c;
if(ack_flag)
{
if(c == 0x05)
{
exit_flag = true;
}
}
}
if(exit_flag)
{
break;
}
}
recv[recv_len] = 0;
/*Serial.print(" : ");
for (int j = 0; j < recv_len; j++) {
Serial.print((unsigned char)recv[j], HEX);
}
Serial.print("\r\n");
Serial.println(recv);
*/
return recv_len;
}
bool NextionFirmware::_setDownloadBaudrate(uint32_t baudrate)
{
static String cmd = "";
cmd.reserve(70);
delay(100);
sendCommand("runmode=2");
delay(60);
//recvRetString(500, true);
sendCommand("print \"mystop_yesABC\"");
recvRetString(500, true);
sendCommand("get sleepÿÿÿget dimÿÿÿget baudÿÿÿprints \"ABC\",0");
delay(500);
recvRetString(500, true);
//SerialDbgRecv();
static String filesize_str = String(_fwsize,10);
static String baudrate_str = String(baudrate,10);
cmd = "whmi-wri ";
cmd+=filesize_str;
cmd+= "," + baudrate_str + ",1";
sendCommand("00");
sendCommand(cmd.c_str());
_nextion->flushOutput();
_nextion->end();
delay(100);
_nextion->begin(baudrate);
_nextion->setTimeout(500);
recvRetString(500,true); // acknowledge in new baud rate
if(strchr(recv,0x05)) {
return 1;
}
return 0;
}
bool NextionFirmware::wait_acknowledge(Stream* stream) {
if (bdebug) return true;
recvRetString( 500, true);
if (strchr(recv,0x05)) {
//stream->write(0x05);
return true;
}
return false;
}
long NextionFirmware::upload_chunk(Stream* stream,long bytes)
{
int t = 0;
unsigned long tstart=millis();
unsigned long tactive = 0;
unsigned long timeout=30000L;
long maxavail=0;
long pos=0;
long bytes_sent = 0;
if (bytes == 0) return 0;
static String string = "uploading : ";
for(int i=0;i<bytes;i++){
int avail = 0;
do {
avail = stream->available();
if (avail == 0) delay(20);
} while (avail == 0);
if (avail >= ARDUINOSERIALBUFFERSIZE-1) {
string = " Serial buffer overflow: avail=";
string += String(avail);
DebugOut(string.c_str());
}
if(avail>maxavail) {
maxavail=avail;
pos= bytes_sent;
}
while(avail>0){
char c = stream->read();
if (!bdebug) {
_nextion->write(c);
// _nextion->flush();
}
else {
delay(1);
}
avail--;
bytes_sent++;
// string = " bytes_sent=";
// string += String(bytes_sent);
// DebugOut(string.c_str());
if(bytes_sent==bytes) goto trasmitted;
}
tactive = millis() - tstart;
if (tactive > timeout) { goto timeout_flag; }
}
timeout_flag:
string += "TIMEOUT :";
string += String(tactive);
string += "ms , max=";
string += String(timeout);
DebugOut(string.c_str());
trasmitted:
//string =" maxavail=";
//string+=String(maxavail);
//string+=", pos=";
//string+=String(pos);
//DebugOut(string.c_str());
if (true) /*bytes_sent != bytes)*/ {
string = "bytes_sent=";
string += String(bytes_sent);
string += " bytes=";
string += String(bytes);
}
DebugOut(string.c_str());
return bytes_sent;
}