cancel
Showing results for 
Search instead for 
Did you mean: 

How to determine how long a user has pressed a hardware buttons in Touchgfx?

TuanLe
Associate II

I follow the guidelines in this article
https://support.touchgfx.com/docs/development/scenarios/example-gpio
and my Touchgfx program can react to a single hardware button pressed event.

Now I need to add 1 more hardware button and calculate how long the user has been pressing a button in order to direct a user to the relevant setup screen.
I have a few questions I would like to ask:

1. The HAL class has a single pointer to a ButtonController-inherited class object: ButtonController* buttonController, does it imply that I need to write a single class to handle all pressed events of all hardware buttons?

2. How often the method ButtonController::sample(uint8_t &key) is called andby which class?

3. How should I calculate the duration of a button press with the current Touchgfx's classes (ButtonController for example) and methods?

Thank you for your help.

2 REPLIES 2
GaetanGodart
ST Employee

Hello @TuanLe ,

 

1. The HAL class has a single pointer to a ButtonController-inherited class object: ButtonController* buttonController, does it imply that I need to write a single class to handle all pressed events of all hardware buttons?

Yes, you can simply add button's checking one after the other in their own if statement to check their state (pressed or not).

 

2. How often the method ButtonController::sample(uint8_t &key) is called and by which class?

The method is called every tick (60 times per second) but I don't know by which class, to know that, you could do a global search in your project for the function name (sample) to find where it is called.

 

3. How should I calculate the duration of a button press with the current Touchgfx's classes (ButtonController for example) and methods?

This is very simple, when the conditions you are looking for are filled, simple set a variable ("state") to true.
Add a handleTickEvent function that will either increment a counter if the state is true or will send the value (by calling a function with the value as parameter) and then reset it to 0 if the state is false.

 

Bonus

I have already tested clicking on 2 hardware button at once in TouchGFX.
I did not use the buttonController class, instead I directly added my if statement in a handleTickFunction in the model.cpp file.
Please find that example attached (it uses a nucleoH563 with a AZ2 display).
Note that :
 - you won't be able to run that example if you do not have that specific hardware (you could adapt it to yours however)
 - the example test 2 button click in "parallel" which means that each button have a specific action but I later tried to have an action happening only if both buttons were clicked (either by using a nested if statement of by putting a double condition in the if statement).

 

If this comment answers your question, I invite you to select it as "best answer".

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)
Andrew Neil
Evangelist III

Do you actually need to measure the time, or just distinguish a "short" press from a "long" press?

For the latter, see:

https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/repeat-button-triggers-immediately-bug-or-feature/m-p/652072