Start a new topic

Read Button State

 Hi!


I am trying to undertstand, how i can read the boolean state of the button on my 4,3" Nextion Panel. I have opend the example Compbutton.ino, but  i can't understand where i can read the state of the example.

Can anyone help me?


BTW: i am missing kind of a quick-start guide, i have saw the reference guide, but some easy examples are missing.


Thanks alot!


Here the example:





#include "NexButton.h"

NexButton b0 = NexButton(0, 1, "b0");
char buffer[100] = {0};

NexTouch *nexListenList[] =
{
&b0,
NULL
};

void b0PopCallback(void *ptr)
{
uint16_t len;
uint16_t number;
NexButton *btn = (NexButton *)ptr;

dbSerial.println("b0PopCallback");
dbSerial.print("ptr=");
dbSerial.println((uint32_t)ptr);

memset(buffer, 0, sizeof(buffer));
btn->getText(buffer, sizeof(buffer));

number = atoi(buffer);
number += 1;

memset(buffer, 0, sizeof(buffer));
itoa(number, buffer, 10);

btn->setText(buffer);
}

void setup(void)
{
dbSerial.begin(9600);
nexInit();
b0.attachPop(b0PopCallback, &b0);
dbSerial.println("setup done");
}

void loop(void)
{
dbSerial.println("nexLoop");
nexLoop(nexListenList);
}




I'm not sure if I understand what you mean with "boolean state of the button".

Here the display sends a serial message when you release the button b0, since 'Send key value' is checked. See picture :



As I understand the code, the list "nexListenList" defines a list of components to listen to.

The line "b0.attachPop(b0PopCallback, &b0);" in main setup defines which event to listen to (Touch Release, not Touch Press).

The line "nexLoop(nexListenList);" in main loop listens to the Touch Release Event in each loop of the program.


I think it is a little easier to read the code of the Progress bar example, because you have two buttons btn_up and btn_down, which have an action on progress bar j0.

https://github.com/itead/ITEADLIB_Arduino_Nextion/tree/master/examples/CompProgressBar


I recommend you to insert code in your posts using this icon, it makes it easier to read :


 HI RaPhael!


Thanks for your help, so when i understand that corrcet then i can set a variable in the fuction

buttonUpPopCallback and buttonDownPopCallback true or false,

like the following:

 

 

void buttonDownPopCallback(void *ptr)
{
   testvariable=true;
}


void buttonUpPopCallback(void *ptr)
{
    
testvariable=false;

    }



 


Thanks for the tipps with the code!




 


 

 

 

 

 

 

 

I don't understand the nexLoop function within the "arduino main loop" ( void loop(void) ) shown by the examples.

If I follow up the code I find the class "NexTouch::mainEventLoop" within the "NexTouch.cpp".
there you can find a while(true) statement...
How can the program ever "can get out" of this while(true) endless loop (to execute my other statements within my "arduino main loop") ?

In the same class "NexTouch::mainEventLoop" is a delay(10) statement.
Does this mean that my whole "arduino main loop" ( void loop(void) ) is as well delayed by 10ms every loop ?

(sorry I am a layman in object orientated programming ...)

 


2 people like this

@Daniel Meyer

Yes, it looks OK.


@Dirk

Good questions !!

@Dirk: the program never gets out from the while(true) endless loop. So the other statements within the arduino main loop are not executed. I don't know what's the intention of ITEAD ... hope they will shortly explain ...


2 people like this

I just modified the NexTouch.cpp:

uint8_t NexTouch::mainEventLoop(NexTouch **list)
{
    uint16_t i;
    uint8_t c;
    
    // modified by Uwe Wilts while (true)
    // modified by Uwe Wilts  {
        while (nexSerial.available() > 0)
        {   
            delay(10);
            c = nexSerial.read();
            
            if (NEX_RET_EVENT_TOUCH_HEAD == c)
            {
                if (nexSerial.available() >= 6)
                {
                    //memset(__buffer, 0, sizeof(__buffer));
                    __buffer[0] = c;  
                    for (i = 1; i < 7; i++)
                    {
                        __buffer[i] = nexSerial.read();
                    }
                    __buffer[i] = 0x00;
                    
                    if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6])
                    {
                        iterate(list, (NexPid)__buffer[1], (NexCid)__buffer[2], (NexEventType)__buffer[3]);
                    }
                    
                }
            }
        }
    // modified by Uwe Wilts }        
    return 0;
}

 So the function mainEventLoop() returns and the arduino main loop will be run through.



2 people like this
I think this loop is just an example for demonstration purposes (?)
otherwise it would not make sense.

I will not use this loop but write my own loop.

>>"So the function mainEventLoop() returns and the arduino main loop will be run through."
yes ... in some cases ... ;-)
because I think the condition
"while (nexSerial.available() > 0)"
is very often "true", isn't it ?
or is it only "true" if the display actually sends something ?

 

@Dirk: The function available() returns the number of bytes available for reading from a serial port. So "while (nexSerial.available() > 0)" will only be true if the display actually sends something. Also the delay(10) statement will only be executed when there are data from the display.


1 person likes this

> I think this loop is just an example for demonstration purposes (?) otherwise it would not make sense.


I hope ITEAD will shortly explain!!!


1 person likes this
Thanks for the help so far!!
I just tryd to change the example code from the Library of the "compprogressbar.cpp" so that only the buttons are requestet from the Arduino, i also changed the serial connection to Serial1 in Serialconfig.

See the code:

 

/**
 * @example CompProgressBar.ino
 *
 * @par Show how to use API of class NexProgressBar.  
 *
 * @author  Wu Pengfei (email:<pengfei.wu@itead.cc>)
 * @date    2015/7/10
 * @copyright 
 * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

#include "NexProgressBar.h"
#include "NexButton.h"

NexProgressBar n1  = NexProgressBar(0, 3, "n1");
NexButton b0   = NexButton(0, 1, "b0");
NexButton b1 = NexButton(0, 2, "b1");

NexTouch *nexListenList[] = 
{
    &b0,
    &b1,
    NULL
};



void setup(void)
{
    uint32_t brightness = 0;
    
    dbSerial.begin(9600);
    nexInit();
    b0.attachPop(buttonUpPopCallback);
    b1.attachPop(buttonDownPopCallback);
    dbSerial.println("setup done");
}


void buttonUpPopCallback(void *ptr)
{
   
  dbSerial.println("Button1");
}
void buttonDownPopCallback(void *ptr)
{
  
  dbSerial.println("Button2");
   
}


void loop(void)
{

    delay(500);
    nexLoop(nexListenList);
    
    
    
}

 

My problem is now, that i can't see the printed out messages: Button1 or Button2. But when i press the 2 buttons in the touchscreen, then i can see the RX TX led flashing on my Board,so my feeling is that the communication is OK. Where else could the problem be that the two functions: void buttonDownPopCallback(void *ptr) and void buttonUpPopCallback(void *ptr) aren't working?
I also changed the NexTouch.cpp: as posted from Uwe Wilts.

Does anybody has some other ideas what i can try?

Thanks!

BR

Daniel

 

Do you get the message "setup done" ?

Hi,all.Here is a good news.

@ITEAD_SUPPORT has opened a blog for Quick start of ITEADLIB_Arduino_Nextion_library.

Later I will update the tutorial link here.

I don't understand the nexLoop function within the "arduino main loop" ( void loop(void) ) shown by the examples.

If I follow up the code I find the class "NexTouch::mainEventLoop" within the "NexTouch.cpp".
there you can find a while(true) statement...
How can the program ever "can get out" of this while(true) endless loop (to execute my other statements within my "arduino main loop") ?

In the same class "NexTouch::mainEventLoop" is a delay(10) statement.
Does this mean that my whole "arduino main loop" ( void loop(void) ) is as well delayed by 10ms every loop ?

@ITEAD:
Can you explain this ?

 

Login or Signup to post a comment