Documentation
For Arduino users
CompButton.ino
1 
18 #include "Nextion.h"
19 
20 /*
21  * Declare a button object [page id:0,component id:1, component name: "b0"].
22  */
23 NexButton b0 = NexButton(0, 1, "b0");
24 
25 char buffer[100] = {0};
26 
27 /*
28  * Register a button object to the touch event list.
29  */
30 NexTouch *nex_listen_list[] =
31 {
32  &b0,
33  NULL
34 };
35 
36 /*
37  * Button component pop callback function.
38  * In this example,the button's text value will plus one every time when it is released.
39  */
40 void b0PopCallback(void *ptr)
41 {
42  uint16_t len;
43  uint16_t number;
44  NexButton *btn = (NexButton *)ptr;
45  dbSerialPrintln("b0PopCallback");
46  dbSerialPrint("ptr=");
47  dbSerialPrintln((uint32_t)ptr);
48  memset(buffer, 0, sizeof(buffer));
49 
50  /* Get the text value of button component [the value is string type]. */
51  btn->getText(buffer, sizeof(buffer));
52 
53  number = atoi(buffer);
54  number += 1;
55 
56  memset(buffer, 0, sizeof(buffer));
57  itoa(number, buffer, 10);
58 
59  /* Set the text value of button component [the value is string type]. */
60  btn->setText(buffer);
61 }
62 
63 void setup(void)
64 {
65  /* Set the baudrate which is for debug and communicate with Nextion screen. */
66  nexInit();
67 
68  /* Register the pop event callback function of the current button component. */
69  b0.attachPop(b0PopCallback, &b0);
70 
71  dbSerialPrintln("setup done");
72 }
73 
74 void loop(void)
75 {
76  /*
77  * When a pop or push event occured every time,
78  * the corresponding component[right page id and component id] in touch event list will be asked.
79  */
80  nexLoop(nex_listen_list);
81 }
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
void nexLoop(NexTouch *nex_listen_list[])
Listen touch event and calling callbacks attached before.
uint16_t getText(char *buffer, uint16_t len)
Get text attribute of component.
Definition: NexButton.cpp:23
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
bool setText(const char *buffer)
Set text attribute of component.
Definition: NexButton.cpp:33
The header file including all other header files provided by this library.
Father class of the components with touch events.
Definition: NexTouch.h:53