2024-11-06 05:38 AM
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.
Solved! Go to Solution.
2024-11-07 06:33 AM - edited 2024-11-07 06:33 AM
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>)
2024-11-07 06:33 AM - edited 2024-11-07 06:33 AM
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>)
2024-11-13 01:33 AM
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.
2024-11-13 01:46 AM - edited 2024-11-13 02:46 AM
@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.
2024-11-13 03:50 AM - last edited on 2024-11-13 03:58 AM by SofLit
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
2024-11-13 03:51 AM
Horrible formatting sorry
2024-11-13 04:00 AM
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.
2024-11-13 04:14 AM
Yeah sorry about that was excited for my lunch break and typed it out quick
2024-11-13 09:10 AM
@SofLit, where is Persistance constructed (memory mapped) ?
Also isn't this the same as adding members in FrontendApplication/Model to have them globally accessible?
2024-11-22 04:03 PM
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?