Start a new topic

Nextion Arduino Sketch Generator

Hi,

 

i am very new to Netion screen and i noticed that it's very hard to use the Official library and the non official ones usually is designed to fit the author purpose only and no updates and fixes is applied.

 

so we have to use the official library :) ,

i was designing my first HMI file to use a 2.8" screen as Home Automation control screen , it has 5 pages and 32 component which need interaction with my ESP8266

so writing the declaration and the callback is too much to do.


first i list up my components in excel file to write it back in my sketch i fund my self writing a VBA code to help me doing that step by step i have done almost the entire sketch auto generated by VBA code. and add a text file to your desktop named NextionCode.text open it to find you code


important


i have only added three components which is Text , button and dual state button.

i have no planed to add more , feel free to edit it you self i kept it without password

to allow others to modify it.


i will add it later to github and i will share the repo here


attached the excel sheet which will normally show a security yellow tab on top when you run it as it has VBA code to warn you , don't be afraid it has noting bad


this is an example of my components


Page Name Page Number Comp ID Comp Name
page0 0 1 p0temp
page0 0 2 p0humd
Reception 1 5 R1
Reception 1 4 R2
Reception 1 3 R3
Reception 1 8 R4
Reception 1 7 R5
Reception 1 6 R6
Reception 1 20 p1temp
Reception 1 21 p1humd
LivingRoom 2 4 R1
LivingRoom 2 5 R2
LivingRoom 2 6 R3
LivingRoom 2 9 p2temp
LivingRoom 2 10 p2humd
KidsRoom 3 4 R1
KidsRoom 3 5 R2
KidsRoom 3 6 R3
KidsRoom 3 9 p3temp
KidsRoom 3 10 p3humd
BedRoom 4 4 R1
BedRoom 4 5 R2
BedRoom 4 6 R3
BedRoom 4 9 p4temp
BedRoom 4 10 p4humd
Settings 5 6 p5ssid
Settings 5 7 p5vcc
Settings 5 8 p5bedroom
Settings 5 9 p5recep1
Settings 5 10 p5recep2
Settings 5 11 p5livingroom
Settings 5 12 p5kidsroom

and here is the generated code



//##############################################################
//this part is the components Declare in the top of your sketch
//##############################################################


NexText p0temp = NexText(0,1,"p0temp");
NexText p0humd = NexText(0,2,"p0humd");
NexDSButton p1R1 = NexDSButton(1,5,"p1R1");
NexDSButton p1R2 = NexDSButton(1,4,"p1R2");
NexDSButton p1R3 = NexDSButton(1,3,"p1R3");
NexDSButton p1R4 = NexDSButton(1,8,"p1R4");
NexDSButton p1R5 = NexDSButton(1,7,"p1R5");
NexDSButton p1R6 = NexDSButton(1,6,"p1R6");
NexText p1temp = NexText(1,20,"p1temp");
NexText p1humd = NexText(1,21,"p1humd");
NexDSButton p2R1 = NexDSButton(2,4,"p2R1");
NexDSButton p2R2 = NexDSButton(2,5,"p2R2");
NexDSButton p2R3 = NexDSButton(2,6,"p2R3");
NexText p2temp = NexText(2,9,"p2temp");
NexText p2humd = NexText(2,10,"p2humd");
NexDSButton p3R1 = NexDSButton(3,4,"p3R1");
NexDSButton p3R2 = NexDSButton(3,5,"p3R2");
NexDSButton p3R3 = NexDSButton(3,6,"p3R3");
NexText p3temp = NexText(3,9,"p3temp");
NexText p3humd = NexText(3,10,"p3humd");
NexDSButton p4R1 = NexDSButton(4,4,"p4R1");
NexDSButton p4R2 = NexDSButton(4,5,"p4R2");
NexDSButton p4R3 = NexDSButton(4,6,"p4R3");
NexText p4temp = NexText(4,9,"p4temp");
NexText p4humd = NexText(4,10,"p4humd");
NexText p5ssid = NexText(5,6,"p5ssid");
NexText p5vcc = NexText(5,7,"p5vcc");
NexText p5bedroom = NexText(5,8,"p5bedroom");
NexText p5recep1 = NexText(5,9,"p5recep1");
NexText p5recep2 = NexText(5,10,"p5recep2");
NexText p5livingroom = NexText(5,11,"p5livingroom");
NexText p5kidsroom = NexText(5,12,"p5kidsroom");


//############################################
//Register a Components to the touch event list.
//############################################


NexTouch *nex_listen_list[] =
{
&p1R1,
&p1R2,
&p1R3,
&p1R4,
&p1R5,
&p1R6,
&p2R1,
&p2R2,
&p2R3,
&p3R1,
&p3R2,
&p3R3,
&p4R1,
&p4R2,
&p4R3,
NULL
};



//############################################
//### the pop Up callback for touch Components
//############################################



void p1R1PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p1R1PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p1R1.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p1R2PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p1R2PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p1R2.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p1R3PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p1R3PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p1R3.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p1R4PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p1R4PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p1R4.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p1R5PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p1R5PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p1R5.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p1R6PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p1R6PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p1R6.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p2R1PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p2R1PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p2R1.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p2R2PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p2R2PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p2R2.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p2R3PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p2R3PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p2R3.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p3R1PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p3R1PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p3R1.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p3R2PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p3R2PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p3R2.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p3R3PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p3R3PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p3R3.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p4R1PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p4R1PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p4R1.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p4R2PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p4R2PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p4R2.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
void p4R3PopCallback(void * ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln("p4R3PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
p4R3.getValue(&dual_state);
     /* write you code here to run on touch event */
}
 
 
//########################################
//### this part paste it in the setup top
//########################################



nexInit();
p1R1.attachPop(p1R1PopCallback,&p1R1);
p1R2.attachPop(p1R2PopCallback,&p1R2);
p1R3.attachPop(p1R3PopCallback,&p1R3);
p1R4.attachPop(p1R4PopCallback,&p1R4);
p1R5.attachPop(p1R5PopCallback,&p1R5);
p1R6.attachPop(p1R6PopCallback,&p1R6);
p2R1.attachPop(p2R1PopCallback,&p2R1);
p2R2.attachPop(p2R2PopCallback,&p2R2);
p2R3.attachPop(p2R3PopCallback,&p2R3);
p3R1.attachPop(p3R1PopCallback,&p3R1);
p3R2.attachPop(p3R2PopCallback,&p3R2);
p3R3.attachPop(p3R3PopCallback,&p3R3);
p4R1.attachPop(p4R1PopCallback,&p4R1);
p4R2.attachPop(p4R2PopCallback,&p4R2);
p4R3.attachPop(p4R3PopCallback,&p4R3);

 

i hope you guys like it

xlsm

Just noting in your Callback routines

You have localized component for talk back to Serial Monitor

- this was not already done in pop/push without need to localize?


Localization of dual_state

- will dual_state not lose its value as soon as function ends.

Maybe a globalized dual_state would perhaps save some code.


I invite you to post in the Hobby Lobby Gallery of the New Forum

 - this gallery will not last that much longer


yes patrick all the Dual state buttons are global not local i set this in the HMI file already.

the callback routines is empty and there is a comment so the user can write his code.

Again, just trying to assist


If a dual-state button in an HMI is global wont effect the MCU side tracking.


The MCU side value for the dual-state component (declared line 8)

   seen on line 75 has been localized to the function only.


This means that if one make changes to the dual-state variable of line 75

within the function, such changes are lost when leaving the function line 83


Further to examine is the declaration of the dual-state component line 8

if the function p1R1.setValue() is called, can fail

looking at the library code setValue(no) will parse to p1R1.val=1ÿÿÿ

for a globally declared component, this is not valid if issued say on page 3

Declaring the component as below

NexDSButton p1R1 = NexDSButton(1,5,"p1R1");

will resolve the parse for HMI side globals where page 1 is named page1


Login or Signup to post a comment