cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a flex button keep its state through screen change

ArendZA
Associate II

Hello,

I'm busy working on my first touchGFX project and I am using flex buttons instead of toggle buttons. I set the trigger to toggle and change the color of the button which works really well. The issue I am facing is that if I set a button to the off state when its default state in on and change screens the buttons will all reset to on when returning to the screen.

I notice the same thing happens even with toggle buttons. Is there any way to save the state of the button when leaving the screen so that when I return it will still be in the correct state?

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello,

This is my trial. This is something I used before to save the status of a toggle button:

Create a .hpp file that will manage the persistance of the button status and name it persistance.hpp.

Its content will be:

 

#ifndef PERSISTANCE_HPP
#define PERSISTANCE_HPP
using namespace touchgfx;
class Persistance
{

  public:

    Persistance()
    {
    }
    static int GetButtonStatus(void)
    {
        return button_status;
    }
    static void SetButtonStatus(int temp)
    {
        button_status = temp;
    }
 

  private:
    static int button_status;
};

#endif // PERSISTANCE_HPP

 

Then put that file in the path: TouchGFX\gui\include\gui\common\

Include: persistance.hpp in your screenview.cpp (xxxScreenView.cpp )

 

#include <gui/common/persistance.hpp>

 

In Setup Screen, set the value of the button:

 

xxxScreenView::setupScreen()
{
button.forceState(Persistance::GetButtonStatus);
}

 

you call Persistance::SetButtonStatus() to save the status of the button when the button status changed: 

 

Persistance::SetButtonStatus(<set the status of the button>)

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

9 REPLIES 9
SofLit
ST Employee

Hello,

This is my trial. This is something I used before to save the status of a toggle button:

Create a .hpp file that will manage the persistance of the button status and name it persistance.hpp.

Its content will be:

 

#ifndef PERSISTANCE_HPP
#define PERSISTANCE_HPP
using namespace touchgfx;
class Persistance
{

  public:

    Persistance()
    {
    }
    static int GetButtonStatus(void)
    {
        return button_status;
    }
    static void SetButtonStatus(int temp)
    {
        button_status = temp;
    }
 

  private:
    static int button_status;
};

#endif // PERSISTANCE_HPP

 

Then put that file in the path: TouchGFX\gui\include\gui\common\

Include: persistance.hpp in your screenview.cpp (xxxScreenView.cpp )

 

#include <gui/common/persistance.hpp>

 

In Setup Screen, set the value of the button:

 

xxxScreenView::setupScreen()
{
button.forceState(Persistance::GetButtonStatus);
}

 

you call Persistance::SetButtonStatus() to save the status of the button when the button status changed: 

 

Persistance::SetButtonStatus(<set the status of the button>)

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
ArendZA
Associate II

Awesome, just implemented this. Works good, busy working on a solution for multiple buttons thats not just creating 20 of the same functions and variables.


@ArendZA wrote:

busy working on a solution for multiple buttons thats not just creating 20 of the same functions and variables.


You can manage that by a table:

 

    static int GetButtonStatus(int button_index)
    {
        return button_status[button_index];
    }
    static void SetButtonStatus(int button_value, int button_index)
    {
        button_status[button_index] = button_value;
    }
 

  private:
    static int button_status[20]; /* 20 buttons*/

 

But need to manage the button_index that should not exceed 20 in your case. 

Also use enumeration to set/get button status index for code visibility.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Thanks I was busy doing something similar when i saw your message.

For people who may find this thread in the future, here's the code for different types of widgets that I have now gotten working.
Create the header file like above and in that file is where you will be storing your values. You'll need 2 functions for each widget 1 to set the value the other to fetch the value when you open the screen.

Flex buttons:

static int getButtonState(int buttonIndex)

{

int temp = button_states[buttonIndex];

return temp;

}



static void setButtonState(int buttonIndex, int buttonState)

{

button_states[buttonIndex] = buttonState;

}

This can be used like this:

void Screen2View::setupScreen()

{

Screen2ViewBase::setupScreen();

int temp = Persistance::getButtonState( 0 ); /* This matches with the correct button in the array */

*Button*.forceState(temp);

}

And to set the value use something like this in the callback:

int buttonState = ofBtn.getPressed();

if(buttonState == pressed){

Persistance::setButtonState(Button, pressed /* 1 */);

}
else

{

Persistance::setButtonState(Button, unpressed /* 0 */);

}

For sliders its easy as well, 2 functions you'll need:

static void setSpeedStatus ( int speed)

{

speed_status = speed;

}



static int getSpeedStatus()

{

return speed_status;

}

In the callback:

void Screen2View::speedUpdate(int value)
{
  Persistance::setSpeedStatus(value);
}

In void Screen2View::setupScreen()
Use this:

int speed = Persistance::getSpeedStatus();

speedSlider.setValue(speed);

Radio buttons 2 functions again:

static void setRadioGroup1 (int selectedRadio){

radio_group1_status = selectedRadio;

}



static int getRadioGroup1 ( void )

{

return radio_group1_status;

}
private:
static int radio_group1_status;

In the callback:

Persistance::setRadioGroup2(0Button);

To set the value:

void Screen2View::setupScreen()

{

Screen2ViewBase::setupScreen();

int radioGroup1Button = Persistance::getRadioGroup1();

switch(radioGroup1Button)

{

case fwdButton:

Btn.setSelected(pressed);

break;

case revButton:

Btn.setSelected(pressed);

break;

case faultButton:

Btn.setSelected(pressed);

break;

}

One thing to remember for radio buttons, if a button has the value of 0 it will now be the default value unless you initialize the storage variable to another number

Horrible formatting sorry 

 

Hello @ArendZA and thank you for the sharing.

In next time please kindly use </> button to share your code. I've edited your comment then (was not simple with many blocks).

See tips on posting.

Thank you again for your contrubution.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
ArendZA
Associate II

Yeah sorry about that was excited for my lunch break and typed it out quick

Marc_LM
Associate III

@SofLit, where is Persistance constructed (memory mapped) ?
Also isn't this the same as adding members in FrontendApplication/Model to have them globally accessible?

FJB2069
Associate II

Doesn't this demo show how to do this using MVP?   

https://youtu.be/RJlwEMCTgsM?si=wvrITwf52WXhQBYO 

 

seems like creating a Persistance class method may be easier?