Documentation
For Arduino users
CompText.ino
1 
19 #include "Nextion.h"
20 
21 void t0PopCallback(void *ptr);
22 void b0PopCallback(void *ptr);
23 void b1PopCallback(void *ptr);
24 
25 /*
26  * Declare a text object [page id:0,component id:1, component name: "t0"].
27  */
28 NexText t0 = NexText(0, 1, "t0");
29 
30 /*
31  * Declare a button object [page id:0,component id:2, component name: "b0"].
32  */
33 NexButton b0 = NexButton(0, 2, "b0");
34 
35 /*
36  * Declare a button object [page id:0,component id:3, component name: "b1"].
37  */
38 NexButton b1 = NexButton(0, 3, "b1");
39 
40 char buffer[100] = {0};
41 
42 /*
43  * Register object t0, b0, b1, to the touch event list.
44  */
45 NexTouch *nex_listen_list[] =
46 {
47  &t0,
48  &b0,
49  &b1,
50  NULL
51 };
52 
53 /*
54  * Text component pop callback function.
55  */
56 void t0PopCallback(void *ptr)
57 {
58  dbSerialPrintln("t0PopCallback");
59  t0.setText("50");
60 }
61 
62 /*
63  * Button0 component pop callback function.
64  * In this example,the value of the text component will plus one every time when button0 is released.
65  */
66 void b0PopCallback(void *ptr)
67 {
68  uint16_t len;
69  uint16_t number;
70 
71  dbSerialPrintln("b0PopCallback");
72 
73  memset(buffer, 0, sizeof(buffer));
74  t0.getText(buffer, sizeof(buffer));
75 
76  number = atoi(buffer);
77  number += 1;
78 
79  memset(buffer, 0, sizeof(buffer));
80  itoa(number, buffer, 10);
81 
82  t0.setText(buffer);
83 }
84 
85 /*
86  * Button1 component pop callback function.
87  * In this example,the value of the text component will minus one every time when button1 is released.
88  */
89 void b1PopCallback(void *ptr)
90 {
91  uint16_t len;
92  uint16_t number;
93 
94  dbSerialPrintln("b1PopCallback");
95 
96  memset(buffer, 0, sizeof(buffer));
97  t0.getText(buffer, sizeof(buffer));
98 
99  number = atoi(buffer);
100  number -= 1;
101 
102  memset(buffer, 0, sizeof(buffer));
103  itoa(number, buffer, 10);
104 
105  t0.setText(buffer);
106 }
107 
108 void setup(void)
109 {
110  /* Set the baudrate which is for debug and communicate with Nextion screen. */
111  nexInit();
112 
113  /* Register the pop event callback function of the current text component. */
114  t0.attachPop(t0PopCallback);
115 
116  /* Register the pop event callback function of the current button0 component. */
117  b0.attachPop(b0PopCallback);
118 
119  /* Register the pop event callback function of the current button1 component. */
120  b1.attachPop(b1PopCallback);
121 
122  dbSerialPrintln("setup done");
123 }
124 
125 void loop(void)
126 {
127  /*
128  * When a pop or push event occured every time,
129  * the corresponding component[right page id and component id] in touch event list will be asked.
130  */
131  nexLoop(nex_listen_list);
132 }
133 
void nexLoop(NexTouch *nex_listen_list[])
Listen touch event and calling callbacks attached before.
bool setText(const char *buffer)
Set text attribute of component.
Definition: NexText.cpp:32
void attachPop(NexTouchEventCb pop, void *ptr=NULL)
Attach an callback function of pop touch event.
Definition: NexTouch.cpp:39
bool nexInit(void)
Init Nextion.
NexButton component.
Definition: NexButton.h:35
uint16_t getText(char *buffer, uint16_t len)
Get text attribute of component.
Definition: NexText.cpp:22
The header file including all other header files provided by this library.
Father class of the components with touch events.
Definition: NexTouch.h:53
NexText component.
Definition: NexText.h:30