How to check for current page in LVGL lambda

Hi, I am making an alarm clock and I want swipes to work only on the clock face that will take the UI to 4 different pages. Going back will be with a physical button or timeout. I am struggling how to check for a current page in a lambda. I am struggling reading the C++ documentation. LVGL api docs. I asked chatgpt to help me with that and first try it did not compile

      - lambda: |-
          if (lv_scr_act() == id(clock_page)) {
            // Handle swipe gesture only on 'clock_page'
            ESP_LOGD("swipe", "Swipe up detected on clock page!");
          }

That threw this error:

error: comparison between distinct pointer types 'lv_obj_t*' {aka '_lv_obj_t*'} and 'esphome::lvgl::LvPageType*' lacks a cast

So I asked GPT again and it suggested this:

lambda:
  - if (reinterpret_cast<lv_obj_t*>(lv_scr_act()) == reinterpret_cast<lv_obj_t*>(id(clock_page))) {
      // Swipe actions will be here if 'clock_page' is the active screen
      // Perform swipe handling code here
  }

This compiles but it does not work. The if statement is resolved as False when it is supposed to be True.

In my LVGL I do have a clock_page defined like so:

lvgl:
  pages:
    - id: clock_page
      scrollable: false
      widgets:
        - obj: # clock container
            height: SIZE_CONTENT
            ...

If anyone could point me in the right direction, that would be greatly appreciated. The esphome LVGL documentation only mentions the yaml side of things and not the lambda in-line. Thank you in advance.

There’s a condition for that and if you really need to call it in a lambda you can - see ESPHome: esphome::lvgl::LvPageType Class Reference

        - lambda: |-
            if (id(main_page).is_showing()) ESP_LOGD("main", "Is showing");
1 Like

Yes, I know that the condition exists, but then I would have to shoehorn it into my touchscreen swipe code and that felt like a workaround. I did not realise I can use the page ID id(<page_name>) as an object like that! I did see that class reference but I didn’t link those things in my mind. I will try it today after work and I’ll let you know (and mark your answer as the solution if it works). Thank you so much!

Yes, that is the answer. Thank you!