cancel
Showing results for 
Search instead for 
Did you mean: 

How to set the slider position from code.

radioman
Associate II

I have been able to get the slider position using a callback function but need to set the slider position when the page is shown from a stored value. I don't need continuous updates only when page is activated.

Thanks

7 REPLIES 7
MM..1
Chief III

Simply read docu Slider | TouchGFX Documentation

slider.setValue(stored);

radioman
Associate II

I did see that but wasn't sure how to implement it in my code. I will try using that

Thanks

radioman
Associate II

For anyone needing help with this I did the following and it works as expected:

This was for a screen brightness setting slider with a saved value called Current_Brightness

I used callback functions the same as getting a value from a widget.

Create a function in the screen with the slider.

int scrBrightnessView::getCurrentBrightness(void)

{

return getBrightness_callback();

}

also add in the setupScreen function:

sliderName.setValue(getCurrentBrightness());

In screen header public add:

virtual int getCurrentBrightness(void);

In gfx_callbacks.c I added:

extern int Current_Brightness;

int getBrightness_callback(void)

{

return Current_Brightness;

}

In gfx_callbacks.h I added:

int getBrightness_callback(void);

Not sure there is an easier way but this works.

wired
Senior III

Before you go any further, you need to stop what you're doing and do some more reading about the MVP architecture. You should have methods that get and save the value in the screen's presenter class that pass the data back and forth from the model. Then, the value should be initialized in the View's setupScreen() method, after the base call. This is all described in the documentation. There are also some Youtube videos that take you through the whole process.

radioman
Associate II

Thanks. I originally started with that arch but got lost. I watched a video and was able to setup MVP as here (minimal code shown) and it does work:

void Model::tick()

{

modelListener->UpdateValue(CurrentBrightness);

}

void Model::ValueChanged(int value)

{

CurrentBrightness = value;

}

void scrBrightnessView::BrightnessChanged(int value)

{

presenter->ValueChanged(value);

}

void scrBrightnessView::UpdateValue(int CurrentBrightness)

{

sldBrightness.setValue(CurrentBrightness);

sldBrightness.invalidate();

}

void scrBrightnessPresenter::ValueChanged(int value)

{

model->ValueChanged(value);

}

void scrBrightnessPresenter::UpdateValue(int CurrentBrightness)

{

view.UpdateValue(CurrentBrightness);

}

My only question is because it only needs to updated when the screen is first displayed i don't need the tick() function to be constantly updating the value.

How do I call modelListener->UpdateValue(CurrentBrightness); outside of tick()?

wired
Senior III

You don't need to go through the modelListener if you are just trying to maintain state variables. You can make public setter and getter functions in the model class for each variable, and declare private state variables. For example, I have a structure with program settings, and to set/get one of the elements I do this:

    void saveCh1Freq(int16_t saveFreq)
    {
        PgmSettings.ch1Freq = saveFreq;
    }
 
    int16_t getCh1Freq()
    {
        return PgmSettings.ch1Freq;
    }

PgmSettings is a private structure in the Model class.

In the presenter class HPP file for the screen, I have these functions:

    void saveCh1Freq(int16_t saveFreq)
    {
        model->saveCh1Freq(saveFreq);
    }
 
    int16_t getCh1Freq()
    {
        return model->getCh1Freq();
    }
 

And in the setupScreen() function for the screen's ...View.cpp file, I have this after the base class setupScreen() call:

    ch1Freq = presenter->getCh1Freq();
    Unicode::snprintf(textAreaParamPRBuffer, 20, "%d", ch1Freq);
    sliderAdjustPR.setValue(ch1Freq);

radioman
Associate II

That worked like a champ.

I save the value in EEPROM and after a power cycle I read the EEPROM and set the value back to what was saved.

I did have to remember to set the external EEPROM read/write functions as extern "C".

Thanks a million.