davetcc / tcMenu

Menu library for Arduino, mbed and ESP with designer UI and remote control capabilities.
https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/
Apache License 2.0
275 stars 25 forks source link

SubMenuItem: Callback support #293

Closed vzahradnik closed 1 year ago

vzahradnik commented 1 year ago

I have a submenu where I dynamically pull the data from the sensors; it's called service screen.

I'd like to define a callback function which is called after user enters the menu. Currently, SubMenuItems don't support callback functions.

My workaround is to periodically check whether user is or is not in this menu:

    if (menuServiceMenu.isActive()) {
        ...
    }

This code works however is not efficient. Instead of executing the code only once (as in the case of callback version), it is called periodically while user stays in the screen.

davetcc commented 1 year ago

https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/menumanager-and-iteration/#listening-for-changes-on-menu-manager

Can you take a look and see if the menu manager observer does what you want. It sounds like it should work for you and you can even prevent the menu from showing if you wanted.

davetcc commented 1 year ago

In fact, if you could take a look at the below as it does pretty much what you want:

https://github.com/davetcc/tcMenuLib/blob/master/examples/arduino32/dynamicMenuItems/dynamicMenuItems.ino#L48

vzahradnik commented 1 year ago

Thanks, I will look into it.

vzahradnik commented 1 year ago

Your solution works great. I'll close this issue. I just have one question. What is the purpose of menuMgr.getCurrentMenu() == item? When I had this clause in the if statement, my code was not triggered. Once I removed this part, my code started working.

if(item->getId() == menuList.getId() && menuMgr.getCurrentMenu() == item) {

}
davetcc commented 1 year ago

The examples sometimes go overboard with things to show all the possible functions that you can use. Checking by ID is fine.