M5 Button - real time instances/disposal

  • Hi all

     My project has multiple screen pages.    
     At the moment I'm using m5button and it's working great, but if you define 2 buttons in the same screen space only one of them fires.

    (I was originally going to set up a tonne of button handlers; all of which would check and only fire if the "currentPage" was the page they were on.)
    So approach 1 doesn't work.

    So I changed my definitons to create new pointer instances and accesing the functions via "->" instead, allowing me to create and dispose of them at will.

    However of course when you invoke them from within a function or procedure in your main file the scoping is limited to the current function or procedure and they are automatically disposed of outside of that function.

    So my solutions might be:

    1. roll my own M5 button with disposal features.
    2. Amend the M5 button in the core source and submit the changes to M5stack - this will fundamentally break everyones code. although its not a particularly painful fix I doubt it would be adopted. I dont know why they didnt support creation and disposal from the outset.
    3. Just write my own button handler instead.

    Does anyone have any comments or tips? Have I missed something that I can do effectively using m5button library?

    I'm thinking I might do 1) but it's probably as much work as 3) - possibly more (as I do not need many of the features - just need touch and release.)

    Is there any option for item 3) I can use from a 3rd party thats ready to roll?

    thanks for any guidance


  • OK - I had to find my ownn solution to this:

    The problem lies with using the loop to run all the updates. If you do that - and manage your program from the loop then you need to use globally declared instances of all the buttons. If you try and create button ways dynamically via procedure they are scoped within that procedure - and they are disposed of automatically (whether you like it or not!!) when you exit the function/procedure and return to the Loop.


    I only use the Loop for switch case statement to invoke the loopProcedure for each given page.

    I create a mode variable currentPage, and a dedicated loopProcedure for every page of the project (loopMainMenu, loopPlaybackPage etc. etc.)

    Each loopProcedure draws the screen, invokes the necessary buttons and handlers for that page locally, and then starts a local do-while (currentPage=loopMainMenu){} loop to provide all the typical loop based functions like m5.update(); to keep the button system ticking and battery management going for that page. It stays in this while loop until the back button is pressed or the system navigates to another page somehow (by repointing the currentPage variable).

    It is the button handler functions that can then change the page which cause the while loop to exit back to the main Loop - and the entire loopProcedure then disposes of everything related to the old page, and a new page loopProcedure is then invoked through the switch case statement in the Loop.

    It's a bit clunky because I would prefer that the library itself simply supported dynamic disposal and invokation of global objects - and having a loop for each page is going to get quite bloaty quite quickly but wit ha little care and some carefully designed functions we can reuse most of the code not related to button invokation.

    I'm not able to post any code I'm afraid - but it works and you can leave some global buttons like button C is my back button and that wil never change, buttons a and b are also global (but all could be defined locally if required).

    I hope that helps someone in the future.