Nextion User Manual
For Arduino developers
NexHardware.cpp
Go to the documentation of this file.
1 
15 #include "NexHardware.h"
16 
17 #define NEX_RET_CMD_FINISHED (0x01)
18 #define NEX_RET_EVENT_LAUNCHED (0x88)
19 #define NEX_RET_EVENT_UPGRADED (0x89)
20 #define NEX_RET_EVENT_TOUCH_HEAD (0x65)
21 #define NEX_RET_EVENT_POSITION_HEAD (0x67)
22 #define NEX_RET_EVENT_SLEEP_POSITION_HEAD (0x68)
23 #define NEX_RET_CURRENT_PAGE_ID_HEAD (0x66)
24 #define NEX_RET_STRING_HEAD (0x70)
25 #define NEX_RET_NUMBER_HEAD (0x71)
26 #define NEX_RET_INVALID_CMD (0x00)
27 #define NEX_RET_INVALID_COMPONENT_ID (0x02)
28 #define NEX_RET_INVALID_PAGE_ID (0x03)
29 #define NEX_RET_INVALID_PICTURE_ID (0x04)
30 #define NEX_RET_INVALID_FONT_ID (0x05)
31 #define NEX_RET_INVALID_BAUD (0x11)
32 #define NEX_RET_INVALID_VARIABLE (0x1A)
33 #define NEX_RET_INVALID_OPERATION (0x1B)
34 
35 /*
36  * Receive uint32_t data.
37  *
38  * @param number - save uint32_t data.
39  * @param timeout - set timeout time.
40  *
41  * @retval true - success.
42  * @retval false - failed.
43  *
44  */
45 bool recvRetNumber(uint32_t *number, uint32_t timeout)
46 {
47  bool ret = false;
48  uint8_t temp[8] = {0};
49 
50  if (!number)
51  {
52  goto __return;
53  }
54 
55  nexSerial.setTimeout(timeout);
56  if (sizeof(temp) != nexSerial.readBytes((char *)temp, sizeof(temp)))
57  {
58  goto __return;
59  }
60 
61  if (temp[0] == NEX_RET_NUMBER_HEAD
62  && temp[5] == 0xFF
63  && temp[6] == 0xFF
64  && temp[7] == 0xFF
65  )
66  {
67  *number = (temp[4] << 24) | (temp[3] << 16) | (temp[2] << 8) | (temp[1]);
68  ret = true;
69  }
70 
71 __return:
72 
73  if (ret)
74  {
75  dbSerialPrint("recvRetNumber :");
76  dbSerialPrintln(*number);
77  }
78  else
79  {
80  dbSerialPrintln("recvRetNumber err");
81  }
82 
83  return ret;
84 }
85 
86 
87 /*
88  * Receive string data.
89  *
90  * @param buffer - save string data.
91  * @param len - string buffer length.
92  * @param timeout - set timeout time.
93  *
94  * @return the length of string buffer.
95  *
96  */
97 uint16_t recvRetString(char *buffer, uint16_t len, uint32_t timeout)
98 {
99  uint16_t ret = 0;
100  bool str_start_flag = false;
101  uint8_t cnt_0xff = 0;
102  String temp = String("");
103  uint8_t c = 0;
104  long start;
105 
106  if (!buffer || len == 0)
107  {
108  goto __return;
109  }
110 
111  start = millis();
112  while (millis() - start <= timeout)
113  {
114  while (nexSerial.available())
115  {
116  c = nexSerial.read();
117  if (str_start_flag)
118  {
119  if (0xFF == c)
120  {
121  cnt_0xff++;
122  if (cnt_0xff >= 3)
123  {
124  break;
125  }
126  }
127  else
128  {
129  temp += (char)c;
130  }
131  }
132  else if (NEX_RET_STRING_HEAD == c)
133  {
134  str_start_flag = true;
135  }
136  }
137 
138  if (cnt_0xff >= 3)
139  {
140  break;
141  }
142  }
143 
144  ret = temp.length();
145  ret = ret > len ? len : ret;
146  strncpy(buffer, temp.c_str(), ret);
147 
148 __return:
149 
150  dbSerialPrint("recvRetString[");
151  dbSerialPrint(temp.length());
152  dbSerialPrint(",");
153  dbSerialPrint(temp);
154  dbSerialPrintln("]");
155 
156  return ret;
157 }
158 
159 /*
160  * Send command to Nextion.
161  *
162  * @param cmd - the string of command.
163  */
164 void sendCommand(const char* cmd)
165 {
166  while (nexSerial.available())
167  {
168  nexSerial.read();
169  }
170 
171  nexSerial.print(cmd);
172  nexSerial.write(0xFF);
173  nexSerial.write(0xFF);
174  nexSerial.write(0xFF);
175 }
176 
177 
178 /*
179  * Command is executed successfully.
180  *
181  * @param timeout - set timeout time.
182  *
183  * @retval true - success.
184  * @retval false - failed.
185  *
186  */
187 bool recvRetCommandFinished(uint32_t timeout)
188 {
189  bool ret = false;
190  uint8_t temp[4] = {0};
191 
192  nexSerial.setTimeout(timeout);
193  if (sizeof(temp) != nexSerial.readBytes((char *)temp, sizeof(temp)))
194  {
195  ret = false;
196  }
197 
198  if (temp[0] == NEX_RET_CMD_FINISHED
199  && temp[1] == 0xFF
200  && temp[2] == 0xFF
201  && temp[3] == 0xFF
202  )
203  {
204  ret = true;
205  }
206 
207  if (ret)
208  {
209  dbSerialPrintln("recvRetCommandFinished ok");
210  }
211  else
212  {
213  dbSerialPrintln("recvRetCommandFinished err");
214  }
215 
216  return ret;
217 }
218 
219 
220 bool nexInit(void)
221 {
222  bool ret1 = false;
223  bool ret2 = false;
224 
225  dbSerialBegin(9600);
226  nexSerial.begin(9600);
227  sendCommand("");
228  sendCommand("bkcmd=1");
229  ret1 = recvRetCommandFinished();
230  sendCommand("page 0");
231  ret2 = recvRetCommandFinished();
232  return ret1 && ret2;
233 }
234 
235 void nexLoop(NexTouch *nex_listen_list[])
236 {
237  static uint8_t __buffer[10];
238 
239  uint16_t i;
240  uint8_t c;
241 
242  while (nexSerial.available() > 0)
243  {
244  delay(10);
245  c = nexSerial.read();
246 
247  if (NEX_RET_EVENT_TOUCH_HEAD == c)
248  {
249  if (nexSerial.available() >= 6)
250  {
251  __buffer[0] = c;
252  for (i = 1; i < 7; i++)
253  {
254  __buffer[i] = nexSerial.read();
255  }
256  __buffer[i] = 0x00;
257 
258  if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6])
259  {
260  NexTouch::iterate(nex_listen_list, __buffer[1], __buffer[2], (int32_t)__buffer[3]);
261  }
262 
263  }
264  }
265  }
266 }
267 
void nexLoop(NexTouch *nex_listen_list[])
Listen touch event and calling callbacks attached before.
#define nexSerial
Define nexSerial for communicate with Nextion touch panel.
Definition: NexConfig.h:37
bool nexInit(void)
Init Nextion.
The definition of base API for using Nextion.
Father class of the components with touch events.
Definition: NexTouch.h:53