cancel
Showing results for 
Search instead for 
Did you mean: 

Calling different functions when a button is pressed or released (Partially Solved).

Lagodolio
Senior

OK, it is probably a stupid question, but I'm no able to find a solution...

I need to call a function when a button is pressed, and a function when it is released.

I can handle pressed/released status by:

void Screen1View::handleClickEvent(const ClickEvent& event)
{
  if((event.getType() == ClickEvent::PRESSED))
  {
  	Instruction1();
  	HAL_Delay(50);
  }
if((event.getType() == ClickEvent::RELEASED))
 {
	Instruction2();
	HAL_Delay(50);
 }
}

but this is called every time I touch/release the screen and not the single button.

If I use flexButtonCallbackHandler(),

void Screen1ViewBase::flexButtonCallbackHandler(const touchgfx::AbstractButtonContainer& src)
{
  if (&src == &Down_Button)
  {
   //Interaction1
   //When Down_Button clicked call virtual function
   //Call function1
    Instruction1();
    HAL_Delay(50);
  }
}

I can determine the button pressed, but I haven't a call when the button is released.

Any solution?

Thanks and best regards to all the community!

6 REPLIES 6
ChahinezC
Lead

Hello @MM.22aino​,

I recommend you using a radio button, you add it in the GUI first, then create an interaction on the right section as presented in the photo attached. 

In the menu, you can select "Radio button is selected" or " Radio button is deselected " as Trigger, then you have the action menu, you can either select one of the proposed options or select “Call new virtual function�? and develop it in the code generated by TouchGFX in the STM32CubeIDE.

Please note that the base files should not be modified since they are generated directly from the created interface and widgets.

Chahinez.

0693W000007DYxkQAG.png

Lagodolio
Senior

Hello ChahinezC,

I thought of using a Radio Button, but I wasn't able to achieve my purpose.

I have a button with "FLOW": when I press it a pump starts and a valve stay opened, when I released it the pump stops and the valve was closed.

So, when the button is pressed, I need to send an instruction for pump starting + valve opening and another command when the button is released.

ChahinezC
Lead

Hi @MM.22aino​,

I am sorry but I am not really sure about the issue you are having.

But, I think this might be a Selection problem in the Proprieties section; selecting the "Selected" or the "Deselectable" can solve your problem.

Also you can refer to the TouchGFX documentation and especially the RadioButton part.

I hope this helps.

Chahinez.

Lagodolio
Senior

My problem with radio buttons is that I have a radioButtonSelectedCallbackHandler and a radioButtonDeselectedCallbackHandler, the first is called when I select the RadioButton (it works like a pushed button) and the second when the operator deselect the RadioButton by clicking on it (if it is Deselectable) or by clicking another RadioButton.

My goal is that my button sends a call when it is pressed and another call when it is released.

Imagine a brake: if I push my button it sends the command "Brake!", when I maintain it pushed no commands are sent (the button status isn't changed), when I release the button it sends the command "Unbrake!".

Thanks a lot,

Lagodolio
Senior

Another case is a two-color LED: if the button is pressed it glows up red, else green.

Lagodolio
Senior

Hello!

I partially solved the problem by modifying the class ClickButtonTrigger (@ ClickButtonTrigger.hpp):

class ClickButtonTrigger : public AbstractButtonContainer
{
public:
    /**
     * Handles a ClickAvent. The action callback is called when the ClickButtonTrigger
     * receives a ClickEvent::RELEASED event in PRESSED state. Function setPressed() will
     * be called with the new button state.
     *
     * @param  event The click event.
     *
     * @see setAction, setPressed, getPressed
     */
    virtual void handleClickEvent(const ClickEvent& event)
    {
        bool wasPressed = getPressed();
        bool newPressedValue = (event.getType() == ClickEvent::PRESSED);
        if ((newPressedValue && !wasPressed) || (!newPressedValue && wasPressed))
        {
            setPressed(newPressedValue);
            invalidate();
        }
        if (wasPressed && (event.getType() == ClickEvent::RELEASED) && action)
        {
            if (action->isValid())
            {
                action->execute(*this);
            }
        }
       if ( (event.getType() == ClickEvent::PRESSED) && action)
 
              {
                if (action->isValid())
                  {
                      action->execute(*this);
                  }
              }
    }
};

So Screen1ViewBase :: flexButtonCallbackHandler (..) is called both when a button is pressed and released.

I modified AbstractButtonContainer.hpp (but probably it is not necessary and there is a better way to do it) with a boolean that stores the button status

 /** Handles what should happen when the pressed state is updated. */
    virtual void handlePressedUpdated()
    {
          if (pressed==true)
          {
        	button_pressed=true;
          }
          else{
        	button_pressed=false;
          }
    }

For some reason, flexButtonCallbackHandler() is called only from Screen1ViewBase and not from Screen1View, but it probably my own mistake...😳