gfurtadoalmeida / esp32-driver-nextion

ESP32 driver for Nextion displays.
MIT License
17 stars 7 forks source link

How to know page change #6

Closed kiralikbeyin closed 1 year ago

kiralikbeyin commented 1 year ago

When the user changes the page, how can I send a data from the nextion editor, esp will know that the page has changed?

gfurtadoalmeida commented 1 year ago

As far as I know, Nextion does not fire an specific event when a page is changed.

What you could do is listen to the nextion_event_callback_set_on_touch event and read the page_id property from the event callback structure. The event will be fired for every user touch and will give you the current page. It will be up to you to create the logic to verify a page was changed.

kiralikbeyin commented 1 year ago

I found sendme in nextion guide:

Sends number of currently loaded page over serial (0x66 Return Data)– number of currently loaded page is stored in system variable dp– used in a page’s initialize event will auto-send as page loadsusage: sendme

sendme // sends the value of dp in 0x66 Return Data Format

..

In your lib:

https://github.com/gfurtadoalmeida/esp32-driver-nextion/blob/master/components/nextion/src/nextion/page.c and

/**
 * "Sendme" command result.
 * Format: 0x66 {0x01} 0xFF 0xFF 0xFF
 * Composition:
 *   - {0x01}: page number
 */
#define NEX_DVC_RSP_SENDME_RESULT 0x66U

Maybe something can be done with these but I don't understand..

gfurtadoalmeida commented 1 year ago

Indeed, nextion_page_get will return the current page id. It is a sync command that you'll need to invoke from the ESP32 side. Would that suffice?

kiralikbeyin commented 1 year ago

ESP should be informed when there is a change by the screen.

I added this: 
(code == NEX_DVC_RSP_SENDME_RESULT)

 #define NEX_DVC_CODE_IS_EVENT(code, length) ( (code == NEX_DVC_RSP_SENDME_RESULT)|| (code >= NEX_DVC_EVT_TOUCH_OCCURRED && code != NEX_DVC_RSP_SENDME_RESULT && code != NEX_DVC_RSP_GET_STRING && code != NEX_DVC_RSP_GET_NUMBER && code != NEX_DVC_RSP_TRANSPARENT_DATA_READY && code != NEX_DVC_RSP_TRANSPARENT_DATA_FINISHED) || (code == NEX_DVC_EVT_HARDWARE_START_RESET && length == 6))

added         
case NEX_DVC_RSP_SENDME_RESULT:

    bool nextion_core_event_dispatch(nextion_handle_t handle, const uint8_t *buffer, const size_t buffer_length)
    {
        NEX_CHECK((buffer_length >= NEX_DVC_CMD_ACK_LENGTH), "buffer length error(<NEX_DVC_CMD_ACK_LENGTH)", false)

        const uint8_t code = buffer[0];

        NEX_LOGD("preparing event %d", code);

        switch (code)
        {

        case NEX_DVC_RSP_SENDME_RESULT:
            if (buffer_length == 5)
            {
              nextion_on_touch_event_t event = {
                  .handle = handle,
                  .page_id = buffer[1],
                  .component_id = buffer[2], //no need
                  .state = buffer[3]}; //no need

              NEX_LOGD("dispatching 'sendme' event %x", buffer[1]);

              handle->event_callback_on_touch(event);
            }
            break;
        case NEX_DVC_EVT_TOUCH_OCCURRED:

....

now i can get page id in my code:

void callback_touch_event(nextion_on_touch_event_t event)
{

printf("event.page_id ...  %d ",event.page_id);
...

Thank you