cancel
Showing results for 
Search instead for 
Did you mean: 

Get Button Status (Pressed and Released)

PCu1
Senior

Hello,

I have to send send the status of one flexbutton(even a group of flexbuttons...) to a communication task.

I created a Queue in Model.cpp via the Vien-Presenter chain. And while the button is pressed com task receives '1'. So far so good.

But what I want is to send is the pressed status but also released, because it could be used for actuator.

The status could be send one time or multiple time.

I tried a solution with 3 interactions:

1.Button is clicked call virtual function

2.Timer

3.When timer is done call the same virtual function.

In the virtual function, I check the button status with getPressed() function.

But this method doesn't always work.

Is there a more elegant method to do this?

Pierre

8 REPLIES 8
Mon2
Senior III
PCu1
Senior

Thank you Mon2 for your answer but in the example I didn't find the method to get the Pressed and Release state.

I tried with TRIGGER as touch or repeat but no success.

I don't know if I can add a polling virtual function that runs continously with getPressed() for all of the buttons on the screen.

Thank you,

Pierre

Martin KJELDSEN
Chief III

What do you mean by "The method doesn't always work" ?

How did you make it work for when the button is being pressed? You should be able to do the exact same thing for released?

I'd create a subclass and overwrite the internal handler that sends the pressed callback to also support a released callback. If you create your own handler, then you can also modify it to support a "long press" as you described - To send a signal every x seconds the button is being held down.

/Martin

PCu1
Senior

Hi Martin,

I mean that sometimes I get the new status of the button, sometimes not.

This is the virtual function for my screen:

void MainView::sendstate()
{
	boutonState = flexButton1.getPressed();
	presenter->sendstate(boutonState);
}

As mentionned above, there is an interaction with a timer that calls 'sendstate()' again after a period to check if the button is released, but not really good.

Is there an example on how to create a subclass and add it to the project?

Thank you,

Pierre

Martin KJELDSEN
Chief III

Simply inherit from FlexButton:

class MyButton : public FlexButton
{
public:
    MyButton();
    ....
};

Since this is not accessible through the designer, simply add this widget to your screen in the user code version of your screen class. Add it to your project for compilation. Does that make sense?

class MainView : public MainViewBase
{
public:
    MainView();
    virtual ~MainView() {}
    virtual void setupScreen();
    virtual void tearDownScreen();
private:
      MyButton button;
};
 
...
 
void MainView::setupScreen()
{
   add(button);
}

/Martin

Alexandre RENOUX
Principal

Another possibility that is not very clean but works is shown in following community thread ;

https://community.st.com/s/question/0D50X0000C9fTH2SQM/how-can-i-access-all-3-callbacks-of-the-flexbutton-widget-or-any-button

Basically you create your own Button class based on the implementation of a Flex Button. Some code is shown if you click the link above.

/Alexandre

PCu1
Senior

Hi Alexandre,

Thank you for your code.

I tried it and it works. I have a question and I will ask in the other thread.

It would nice if this function was native in the future.

Pierre

davidpoh
Associate II

Hi PCu1,

Are you able to read the status of the flexbutton ? 

You manged to get it working ? 

Possible to share your solution. 

Thanks in advance.