Skip to main content
KDyrb.1
Associate II
July 30, 2024
Question

How to get a state of flex button in TouchGFX?

  • July 30, 2024
  • 2 replies
  • 4219 views

Hi,
I am writing here because I need a help.
I want to use flex button this way - when I hold pressed button then virtual function is executed, so when I release button then the virtual function stops.
I try for example .getPressed() in handleTickEvent, but it doesn't working. I try a sample with CustomButton from this forum but I can't use it enough.


Can somebody help me ?

I will be very grateful for help.

Best regards,
Kamil

2 replies

Graduate II
July 31, 2024

Hello

Have you make sure your handleTickEvent is working and called every tick ? 

If your button is located to customContainer, check this 

https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/handletickevent-not-working-with-container/m-p/263835#M20364

 

Here is also example how to use getPressed in handleTick:

https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/how-do-i-trigger-a-new-virtual-function-when-a-touch-button/m-p/52581#M100

 

Hope these helps you. If not, maybe show us your code which is not working.

Br JTP

KDyrb.1
KDyrb.1Author
Associate II
July 31, 2024

I use handleTickEvent in MainView - not in ButtonContainer.
Here a code of my handleTickEvent in MainView:

 

void MainView::handleTickEvent()
{
if(zliczoneFlaga == 1)
 {
 dlugosc_materialu_View = dlugosc_ustawiona2;
 }

Unicode::snprintfFloat(textArea_dlugosc_ustawionaBuffer, TEXTAREA_DLUGOSC_USTAWIONA_SIZE, "%0.1f", dlugosc_ustawiona2 );

textArea_dlugosc_ustawiona.invalidate();

 WysylamBigosFloat(dlugosc_materialu_View);

boxProgress1.setValue(dlugosc_materialu_View);

boxProgress1.invalidate();

Unicode::snprintfFloat(textArea_dlugosc_materialuBuffer, TEXTAREA_DLUGOSC_MATERIALU_SIZE, "%0.1f", dlugosc_materialu_View );

textArea_dlugosc_materialu.invalidate();

boxProgress1.setRange(0, dlugosc_ustawiona2);

boxProgress1.invalidate();

if((dlugosc_ustawiona2 - dlugosc_materialu_View) <= 0.01)

 {

scalableImage_start.setVisible (true);

scalableImage_start.invalidate();

scalableImage_start_1.setVisible (false);

scalableImage_start_1.invalidate();

 }
if(flexButtonCofnij.getPressed() == 1)
 {
 HAL_GPIO_WritePin(GPIOB, P2_Pin, GPIO_PIN_SET);
 }
else
 {
 HAL_GPIO_WritePin(GPIOB, P2_Pin, GPIO_PIN_RESET);
 }
}

 

FlexButtonCofnij is use to control P2_Pin output.
In previous post I expressed myself incorrectly because when I hold a button then the P2_pin output change a state all time but I want to that when I hold button then P2_pin is SET, when I release then P2_pin is RESET.

Do you understand evrything?

Thanks for our help!

ST Employee
July 31, 2024

Hello @KDyrb.1 ,

Based on your last comment, if you want to set or reset a pin, you should handle that function inside Model.cpp to be able to access HAL_GPIO_WritePin() , and you don't want to set or reset the pin every frame.
Instead, follow the comment made by @JTP1 (especially, the second forum thread he mentioned), and call HAL_GPIO_WritePin(GPIOB, P2_Pin, GPIO_PIN_SET) when the button is pressed and call reset when it is released.
Your code should look like below:

void Screen1View::flexButtonClickHandler(const touchgfx::BoxWithBorderButtonStyle< touchgfx::ClickButtonTrigger >& b, const ClickEvent& event)
{
 if (&b == &flexButton1)
 {
 if (event.getType() == ClickEvent::PRESSED)
 {
 touchgfx_printf("Button is pressed..\n");
 //presenter->setPin();
 }
 else if (event.getType() == ClickEvent::RELEASED)
 {
 touchgfx_printf("Button is released..\n");
 //presenter->resetPin();
 }
 }
}

Please refer to Backend Communication to read more about how to call hardware related functions.

I hope this helps you, don't hesitate to ask more questions!

tdecker2
Associate II
July 31, 2024

Make sure your virtual function is non-blocking. From your description it seems like you have a while-loop or similar checking for the release of the FlexButton. But TouchGFX will freeze when the called function is blocking.