[Logo] TCC discussion forum
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Top Downloads] Top Downloads   [Groups] Back to home page 
[Register] Register /  [Login] Login 


This forum is read only and new users cannot register, please ask all new questions either using GitHub discussions, or in Arduino forum tagging @davetcc.

Is there a way to compile a history of all pressed buttons? RSS feed
Forum Index » tcMenu Arduinio library
Author Message
Mr.Robot


Joined: Sep 3, 2021
Messages: 8
Offline
Hi again,

here goes my second question. I am not sure though, whether it belongs to the tcMenu or IoAbstraction subform (do not hesitate to move the post accordingly).

I am using tcMenu with regular pushbuttons - which is working great. What I would like, however, is, in addition to navigating the menu with the buttons, some sort of history which buttons have been pressed. I want this in order to implement some sort of 'cheat code' (e.g., KONAMI code). What I would need for this is a callback whenever a button was pressed. This would enable me to compile my own history of all pressed buttons. Is there a way to do this?

I browsed the API references of TcMenu but couldn't find anything. IO Abstraction seems to have what I am looking for (the callback given to the addSwitch method). Since I am using tcMenu to setup my buttons, however, I am not sure whether I can still register a callback that way in addition to what tcMenu (presumedly) internally already registers. Is that possible or would it break things?

Thanks!
davetcc


Joined: Jan 19, 2019
Messages: 686
Offline
Hi there,

I think the best way to achieve what you want is to manage the button presses yourself in this case and pass them to tcMenu after recording the button press. IE you would use IO abstraction switches to register the callback for each switch.

Then you could record the value of each switch. The only complexity in this is that you would need to manage the encoder yourself. However, if you look at the RotaryEncoder classes, writing an extension that recorded the press and then updated the current value would be quite trivial. Most of the work is done in the encoder base class.

Take a look at these methods:

https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/menumanager-and-iteration/#controlling-the-menu-items-manually

and take a look at the UpDownRotaryEncoder here:

https://github.com/davetcc/IoAbstraction/blob/7faa97239b564a294ef644ba78095baac76a3efe/src/SwitchInput.h#L270

Along with the source in the attached CPP file. Should give you a good starting point on how you'd write your custom one.
Mr.Robot


Joined: Sep 3, 2021
Messages: 8
Offline
Thanks!

I will put it on my ToDo list. Have to implement some other stuff beforehand. In case I succeed, I will let you know.
Mr.Robot


Joined: Sep 3, 2021
Messages: 8
Offline
Related to my other post, I am currently looking into handling the button interrupts on my own and passing them to the corresponding menuMgr function.

I am having trouble with menuMgr.valueChanged(int value), however. This is my code, which (obviously) is not working:

void onButtonPressedClb(uint8_t pin, bool heldDown) {
    switch (pin) {
      case UP_BUTTON_GPIO:
        menuMgr.valueChanged(-1);
        break;
      case DOWN_BUTTON_GPIO:
        menuMgr.valueChanged(+1);
        break;
      case LEFT_BUTTON_GPIO:
        if(!heldDown) menuMgr.performDirectionMove(true);
        break;
      case RIGHT_BUTTON_GPIO:
        if(!heldDown) menuMgr.performDirectionMove(false);
        break;
      case A_BUTTON_GPIO:
        menuMgr.onMenuSelect(heldDown);
        break;
      case B_BUTTON_GPIO:
        if(!heldDown) menuMgr.performDirectionMove(true);
        break;
    }
}


After reading through the source code of tcmenu and IoAbstraction, I now understand why simply passing -1 and 1 as values does not work. My idea then was to get the current state of the menu and add or substract 1, but I cannot find a way to get/read the current state of the menu.

Do I have to setup a dummy encoder and manipulate it in my callback? Is there no other way?
davetcc


Joined: Jan 19, 2019
Messages: 686
Offline
Do I have to setup a dummy encoder and manipulate it in my callback? Is there no other way?


As per the other topic, creating your own class based on the EncoderUpDownButtons is absolutely trivial, the base class does most of the work for you. If we look at the code in SwitchInput.cpp/h it literally adds up to a few lines.

To be honest, for what you want to do, it's the easiest way to move forward.

So step by step:

1. Copy the above class definition out of SwitchInput.h, rename the class so there is no name collision.
2. Copy the constructor definition out of SwitchInput.cpp and modify it to meet your needs.
3. At this point tell TcMenu Designer not to define any inputs, and manually configure the inputs so you have more control, read https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/tcmenu-plugins/customise-menu-input-display-plugin/#more-detail-on-how-menu-input-works
davetcc


Joined: Jan 19, 2019
Messages: 686
Offline
I mean there are other ways, but they are more complicated. As you'll have to do some of what menuMgr does for you when using an encoder. If you can use the encoder interface it usually means very little code is needed.

You could look at the keyboard example and also the touch examples and take a look at what they do in their _menu.cpp/h files that are different. End of the day, you could completely control the menu outside of the normal menuMgr arrangements, but then you'll still need to integrate enough to get the change notifications to the renderer etc.
 
Forum Index » tcMenu Arduinio library
Go to:   
Mobile view
Powered by JForum 2.7.0 © 2020 JForum Team • Maintained by Andowson Chang and Ulf Dittmer

This site uses cookies to analyse traffic, serve ads by Google AdSense (non-personalized in EEA/UK), and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.