Documentation
For Arduino users
NexUpload.cpp
Go to the documentation of this file.
1 
16 #include "NexUpload.h"
17 #include <SoftwareSerial.h>
18 
19 //#define USE_SOFTWARE_SERIAL
20 #ifdef USE_SOFTWARE_SERIAL
21 SoftwareSerial dbSerial(3, 2); /* RX:D3, TX:D2 */
22 #define DEBUG_SERIAL_ENABLE
23 #endif
24 
25 #ifdef DEBUG_SERIAL_ENABLE
26 #define dbSerialPrint(a) dbSerial.print(a)
27 #define dbSerialPrintln(a) dbSerial.println(a)
28 #define dbSerialBegin(a) dbSerial.begin(a)
29 #else
30 #define dbSerialPrint(a) do{}while(0)
31 #define dbSerialPrintln(a) do{}while(0)
32 #define dbSerialBegin(a) do{}while(0)
33 #endif
34 
35 NexUpload::NexUpload(const char *file_name,const uint8_t SD_chip_select,uint32_t download_baudrate)
36 {
37  _file_name = file_name;
38  _SD_chip_select = SD_chip_select;
39  _download_baudrate = download_baudrate;
40 }
41 
42 NexUpload::NexUpload(const String file_Name,const uint8_t SD_chip_select,uint32_t download_baudrate)
43 {
44  NexUpload(file_Name.c_str(),SD_chip_select,download_baudrate);
45 }
46 
47 void NexUpload::upload(void)
48 {
49  dbSerialBegin(9600);
50  if(!_checkFile())
51  {
52  dbSerialPrintln("the file is error");
53  return;
54  }
55  if(_getBaudrate() == 0)
56  {
57  dbSerialPrintln("get baudrate error");
58  return;
59  }
60  if(!_setDownloadBaudrate(_download_baudrate))
61  {
62  dbSerialPrintln("modify baudrate error");
63  return;
64  }
65  if(!_downloadTftFile())
66  {
67  dbSerialPrintln("download file error");
68  return;
69  }
70  dbSerialPrintln("download ok\r\n");
71 }
72 
73 uint16_t NexUpload::_getBaudrate(void)
74 {
75  uint32_t baudrate_array[7] = {115200,19200,9600,57600,38400,4800,2400};
76  for(uint8_t i = 0; i < 7; i++)
77  {
78  if(_searchBaudrate(baudrate_array[i]))
79  {
80  _baudrate = baudrate_array[i];
81  dbSerialPrintln("get baudrate");
82  break;
83  }
84  }
85  return _baudrate;
86 }
87 
88 bool NexUpload::_checkFile(void)
89 {
90  dbSerialPrintln("start _checkFile");
91  if(!SD.begin(_SD_chip_select))
92  {
93  dbSerialPrintln("init sd failed");
94  return 0;
95  }
96  if(!SD.exists(_file_name))
97  {
98  dbSerialPrintln("file is not exit");
99  }
100  _myFile = SD.open(_file_name);
101  _undownloadByte = _myFile.size();
102  dbSerialPrintln("tft file size is:");
103  dbSerialPrintln(_undownloadByte);
104  dbSerialPrintln("check file ok");
105  return 1;
106 }
107 
108 bool NexUpload::_searchBaudrate(uint32_t baudrate)
109 {
110  String string = String("");
111  nexSerial.begin(baudrate);
112  this->sendCommand("");
113  this->sendCommand("connect");
114  this->recvRetString(string);
115  if(string.indexOf("comok") != -1)
116  {
117  return 1;
118  }
119  return 0;
120 }
121 
122 void NexUpload::sendCommand(const char* cmd)
123 {
124 
125  while (nexSerial.available())
126  {
127  nexSerial.read();
128  }
129 
130  nexSerial.print(cmd);
131  nexSerial.write(0xFF);
132  nexSerial.write(0xFF);
133  nexSerial.write(0xFF);
134 }
135 
136 uint16_t NexUpload::recvRetString(String &string, uint32_t timeout,bool recv_flag)
137 {
138  uint16_t ret = 0;
139  uint8_t c = 0;
140  long start;
141  bool exit_flag = false;
142  start = millis();
143  while (millis() - start <= timeout)
144  {
145  while (nexSerial.available())
146  {
147  c = nexSerial.read();
148  if(c == 0)
149  {
150  continue;
151  }
152  string += (char)c;
153  if(recv_flag)
154  {
155  if(string.indexOf(0x05) != -1)
156  {
157  exit_flag = true;
158  }
159  }
160  }
161  if(exit_flag)
162  {
163  break;
164  }
165  }
166  ret = string.length();
167  return ret;
168 }
169 
170 bool NexUpload::_setDownloadBaudrate(uint32_t baudrate)
171 {
172  String string = String("");
173  String cmd = String("");
174 
175  String filesize_str = String(_undownloadByte,10);
176  String baudrate_str = String(baudrate,10);
177  cmd = "whmi-wri " + filesize_str + "," + baudrate_str + ",0";
178 
179  dbSerialPrintln(cmd);
180  this->sendCommand("");
181  this->sendCommand(cmd.c_str());
182  delay(50);
183  nexSerial.begin(baudrate);
184  this->recvRetString(string,500);
185  if(string.indexOf(0x05) != -1)
186  {
187  return 1;
188  }
189  return 0;
190 }
191 
192 bool NexUpload::_downloadTftFile(void)
193 {
194  uint8_t c;
195  uint16_t send_timer = 0;
196  uint16_t last_send_num = 0;
197  String string = String("");
198  send_timer = _undownloadByte / 4096 + 1;
199  last_send_num = _undownloadByte % 4096;
200 
201  while(send_timer)
202  {
203 
204  if(send_timer == 1)
205  {
206  for(uint16_t j = 1; j <= 4096; j++)
207  {
208  if(j <= last_send_num)
209  {
210  c = _myFile.read();
211  nexSerial.write(c);
212  }
213  else
214  {
215  break;
216  }
217  }
218  }
219 
220  else
221  {
222  for(uint16_t i = 1; i <= 4096; i++)
223  {
224  c = _myFile.read();
225  nexSerial.write(c);
226  }
227  }
228  this->recvRetString(string,500,true);
229  if(string.indexOf(0x05) != -1)
230  {
231  string = "";
232  }
233  else
234  {
235  return 0;
236  }
237  --send_timer;
238  }
239 }
240 
#define nexSerial
Define nexSerial for communicate with Nextion touch panel.
Definition: NexConfig.h:37
NexUpload(const char *file_name, const uint8_t SD_chip_select, uint32_t download_baudrate)
Constructor.
Definition: NexUpload.cpp:35
#define dbSerial
Define dbSerial for the output of debug messages.
Definition: NexConfig.h:32
The definition of class NexUpload.