forked from itead/ITEADLIB_Arduino_Nextion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NexUpload.cpp
240 lines (219 loc) · 5.49 KB
/
NexUpload.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
/**
* @file NexUpload.cpp
*
* The implementation of download tft file for nextion.
*
* @author Chen Zengpeng (email:<[email protected]>)
* @date 2016/3/29
* @copyright
* Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n
* 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 2 of
* the License, or (at your option) any later version.
*/
#include "NexUpload.h"
#include <SoftwareSerial.h>
//#define USE_SOFTWARE_SERIAL
#ifdef USE_SOFTWARE_SERIAL
SoftwareSerial dbSerial(3, 2); /* RX:D3, TX:D2 */
#define DEBUG_SERIAL_ENABLE
#endif
#ifdef DEBUG_SERIAL_ENABLE
#define dbSerialPrint(a) dbSerial.print(a)
#define dbSerialPrintln(a) dbSerial.println(a)
#define dbSerialBegin(a) dbSerial.begin(a)
#else
#define dbSerialPrint(a) do{}while(0)
#define dbSerialPrintln(a) do{}while(0)
#define dbSerialBegin(a) do{}while(0)
#endif
NexUpload::NexUpload(const char *file_name,const uint8_t SD_chip_select,uint32_t download_baudrate)
{
_file_name = file_name;
_SD_chip_select = SD_chip_select;
_download_baudrate = download_baudrate;
}
NexUpload::NexUpload(const String file_Name,const uint8_t SD_chip_select,uint32_t download_baudrate)
{
NexUpload(file_Name.c_str(),SD_chip_select,download_baudrate);
}
void NexUpload::upload(void)
{
dbSerialBegin(9600);
if(!_checkFile())
{
dbSerialPrintln("the file is error");
return;
}
if(_getBaudrate() == 0)
{
dbSerialPrintln("get baudrate error");
return;
}
if(!_setDownloadBaudrate(_download_baudrate))
{
dbSerialPrintln("modify baudrate error");
return;
}
if(!_downloadTftFile())
{
dbSerialPrintln("download file error");
return;
}
dbSerialPrintln("download ok\r\n");
}
uint16_t NexUpload::_getBaudrate(void)
{
uint32_t baudrate_array[7] = {115200,19200,9600,57600,38400,4800,2400};
for(uint8_t i = 0; i < 7; i++)
{
if(_searchBaudrate(baudrate_array[i]))
{
_baudrate = baudrate_array[i];
dbSerialPrintln("get baudrate");
break;
}
}
return _baudrate;
}
bool NexUpload::_checkFile(void)
{
dbSerialPrintln("start _checkFile");
if(!SD.begin(_SD_chip_select))
{
dbSerialPrintln("init sd failed");
return 0;
}
if(!SD.exists(_file_name))
{
dbSerialPrintln("file is not exit");
}
_myFile = SD.open(_file_name);
_undownloadByte = _myFile.size();
dbSerialPrintln("tft file size is:");
dbSerialPrintln(_undownloadByte);
dbSerialPrintln("check file ok");
return 1;
}
bool NexUpload::_searchBaudrate(uint32_t baudrate)
{
String string = String("");
nexSerial.begin(baudrate);
this->sendCommand("");
this->sendCommand("connect");
this->recvRetString(string);
if(string.indexOf("comok") != -1)
{
return 1;
}
return 0;
}
void NexUpload::sendCommand(const char* cmd)
{
while (nexSerial.available())
{
nexSerial.read();
}
nexSerial.print(cmd);
nexSerial.write(0xFF);
nexSerial.write(0xFF);
nexSerial.write(0xFF);
}
uint16_t NexUpload::recvRetString(String &string, uint32_t timeout,bool recv_flag)
{
uint16_t ret = 0;
uint8_t c = 0;
long start;
bool exit_flag = false;
start = millis();
while (millis() - start <= timeout)
{
while (nexSerial.available())
{
c = nexSerial.read();
if(c == 0)
{
continue;
}
string += (char)c;
if(recv_flag)
{
if(string.indexOf(0x05) != -1)
{
exit_flag = true;
}
}
}
if(exit_flag)
{
break;
}
}
ret = string.length();
return ret;
}
bool NexUpload::_setDownloadBaudrate(uint32_t baudrate)
{
String string = String("");
String cmd = String("");
String filesize_str = String(_undownloadByte,10);
String baudrate_str = String(baudrate,10);
cmd = "whmi-wri " + filesize_str + "," + baudrate_str + ",0";
dbSerialPrintln(cmd);
this->sendCommand("");
this->sendCommand(cmd.c_str());
delay(50);
nexSerial.begin(baudrate);
this->recvRetString(string,500);
if(string.indexOf(0x05) != -1)
{
return 1;
}
return 0;
}
bool NexUpload::_downloadTftFile(void)
{
uint8_t c;
uint16_t send_timer = 0;
uint16_t last_send_num = 0;
String string = String("");
send_timer = _undownloadByte / 4096 + 1;
last_send_num = _undownloadByte % 4096;
while(send_timer)
{
if(send_timer == 1)
{
for(uint16_t j = 1; j <= 4096; j++)
{
if(j <= last_send_num)
{
c = _myFile.read();
nexSerial.write(c);
}
else
{
break;
}
}
}
else
{
for(uint16_t i = 1; i <= 4096; i++)
{
c = _myFile.read();
nexSerial.write(c);
}
}
this->recvRetString(string,500,true);
if(string.indexOf(0x05) != -1)
{
string = "";
}
else
{
return 0;
}
--send_timer;
}
}