cancel
Showing results for 
Search instead for 
Did you mean: 

How to save a circle color when change to another screen

ayoub
Associate II

Hello all, 

I have made a GUI with some circles that show as a LED. When the LED must be turned on the color of the circle shall be green and LED is OFF then the color must be changed to gray. So when I change the screen to another and back again, the color changes to a default value. As I understand I need to save the status to the Presenter and then to the model afterward I should get the status from the model and then the presenter. So now I want to know how to save the circle's color when leaving the screen. 

here is the screen shot for screenview: 

ayoub_0-1714722025210.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
GaetanGodart
ST Employee

TouchGFX/gui/include/gui/model/Model.hpp:21:30: error: 'colortype' has not been declared  

means that colortype is unknown in the file model.hpp.

 

You need to include <touchgfx/Color.hpp> in model.hpp.

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

View solution in original post

6 REPLIES 6
GaetanGodart
ST Employee

Hello @ayoub ,

 

You are right, you need to save the value (or a simple boolean would work in your case) to the model.

To do so, I would create a variable storing the color in the model, then creating two functions: a getColor and a setColor and make them reachable from the screen so you can manipulate your variable.

To learn how to do that, I advise you to read our tutorial 3 which will teach you everything you need to use the model vie presenter architecture : TouchGFX tutorial 03 

Furthermore, I advise you to read more about how MVP works : TouchGFX MVP article 

 

If the comment answer your question, I advise you to select it as "best answer".

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)
JTP1
Lead

Hello

Maybe example project on this post helps you:

https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/communication-from-screen-to-backend/m-p/572290

There is example how to communicate between model and view.

 

Br JTP

 

ayoub
Associate II

Hello @GaetanGodart

 

Thank you for your information. I have created it as you mentioned it. I am trying to save the color of the painter with the function getColor().
Now I have a function to save to the color with a colortype in presenter and this is OK and there is no error compilation.  
In Model.cpp I need to create the function that I created in presenter and also another function to retrieve the color value.
These two functions need colortype as input argument and return argument. The issue is that I got compilation error that show as below:

 ../../TouchGFX/gui/include/gui/model/Model.hpp:21:30: error: 'colortype' has not been declared  

I include the following header files in Model.cpp

 

 

#include <touchgfx/Color.hpp>
#include <touchgfx/hal/Types.hpp>

 

 

Here is some code from ScreenView, Presenter and model: 

 

//--------------HomeScreenView.cpp------------------------------

 

#include <gui/homescreen_screen/HomeScreenView.hpp>
#include <touchgfx/Color.hpp>
#include <touchgfx/hal/Types.hpp>
//#include "main.h"

#ifndef SIMULATOR
extern "C"
{
#include "main.h"
}
#endif

HomeScreenView::HomeScreenView()
{
//input1_LEDPainter.setColor(touchgfx::Color::getColorFromRGB(120, 120, 120));
//input1_LED.invalidate();
}

void HomeScreenView::setupScreen()
{
HomeScreenViewBase::setupScreen();
}

void HomeScreenView::tearDownScreen()
{
HomeScreenViewBase::tearDownScreen();
}

void HomeScreenView::uart_Data (char *data)
{
#ifndef SIMULATOR
if (*data == '1') {
HAL_GPIO_WritePin(LD_USER2_GPIO_Port, LD_USER2_Pin, GPIO_PIN_SET);
input1_LEDPainter.setColor(touchgfx::Color::getColorFromRGB(0, 255, 0));
input1_LED.invalidate();
test = input1_LEDPainter.getColor();
presenter->save_input1_LED( input1_LEDPainter.getColor());
}

if (*data == '0') {
HAL_GPIO_WritePin(LD_USER2_GPIO_Port, LD_USER2_Pin, GPIO_PIN_RESET);
input1_LEDPainter.setColor(touchgfx::Color::getColorFromRGB(120, 120, 120));
input1_LED.invalidate();
test = input1_LEDPainter.getColor();
presenter->save_input1_LED( input1_LEDPainter.getColor());
}

#endif

}

 

 

//-----------------HomeScreenView.hpp----------------------

 

 

#ifndef HOMESCREENVIEW_HPP
#define HOMESCREENVIEW_HPP

#include <gui_generated/homescreen_screen/HomeScreenViewBase.hpp>
#include <gui/homescreen_screen/HomeScreenPresenter.hpp>

class HomeScreenView : public HomeScreenViewBase
{
public:
HomeScreenView();
virtual ~HomeScreenView() {}
virtual void setupScreen();
virtual void tearDownScreen();
virtual void uart_Data (char *data);

protected:
colortype test;
};

#endif // HOMESCREENVIEW_HPP 

 

 

//---------------HomeScreenPresenter.cpp--------------

 

#include <gui/homescreen_screen/HomeScreenView.hpp>
#include <gui/homescreen_screen/HomeScreenPresenter.hpp>

HomeScreenPresenter::HomeScreenPresenter(HomeScreenView& v)
: view(v)
{
}

void HomeScreenPresenter::activate()
{
}

void HomeScreenPresenter::deactivate()
{
}

void HomeScreenPresenter::uart_Data (char *data)
{
view.uart_Data (data);
}

void HomeScreenPresenter::save_input1_LED(colortype painter)
{
//model->saveInput1_LEDStatus( colorLED);
}

 

 

//---------------HomeScreenPresenter.hpp--------------

 

#ifndef HOMESCREENPRESENTER_HPP
#define HOMESCREENPRESENTER_HPP

#include <gui/model/ModelListener.hpp>
#include <mvp/Presenter.hpp>
#include <touchgfx/Color.hpp>
#include <touchgfx/hal/Types.hpp>

using namespace touchgfx;

class HomeScreenView;
class HomeScreenPresenter : public touchgfx::Presenter, public ModelListener
{
public:
HomeScreenPresenter(HomeScreenView& v);

/**
* The activate function is called automatically when this screen is "switched in"
* (ie. made active). Initialization logic can be placed here.
*/
virtual void activate();

/**
* The deactivate function is called automatically when this screen is "switched out"
* (ie. made inactive). Teardown functionality can be placed here.
*/
virtual void deactivate();

virtual ~HomeScreenPresenter() {}
virtual void uart_Data(char *data);
virtual void save_input1_LED(colortype painter);

private:
HomeScreenPresenter();
HomeScreenView& view;
};

#endif // HOMESCREENPRESENTER_HPP

 

 

//----Model.cpp--------------------

 

#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>

#ifndef SIMULATOR
extern "C" {
#include "cmsis_os2.h"
#include <cstring>
#include "main.h"
extern osMessageQueueId_t uartQueueHandle;
uartData_t *uartData_r;
uint16_t count = 0;
}
#endif

Model::Model() :
modelListener(0), toggleButtonState(false)
{
}

void Model::tick() {
#ifndef SIMULATOR
if (osMessageQueueGetCount(uartQueueHandle) > 0) {
if (osMessageQueueGet(uartQueueHandle, &uartData_r, 0, 0) == osOK) {
strncpy(RData, uartData_r->Data, uartData_r->size);
modelListener->uart_Data(RData);
}
}
#endif
}

void Model::saveToggelButton(bool State )
{
toggleButtonState = State;
}

bool Model::getToggleButton()
{
return toggleButtonState;
}

void Model::save_input1_LED(colortype painter)
colorStatus_LED = painter;
}

colortype Model::getStatus_input1_LED()()
{
return colorStatus_LED;
}

 

 

 //----Model.hpp--------------------

STM32F769I_DISCO

 



GaetanGodart
ST Employee

TouchGFX/gui/include/gui/model/Model.hpp:21:30: error: 'colortype' has not been declared  

means that colortype is unknown in the file model.hpp.

 

You need to include <touchgfx/Color.hpp> in model.hpp.

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)
ayoub
Associate II

Hello @ GaetanGodart

I have added the touchgfx/colortype (Check Model.hpp) .  Now the problem is solved by adding or casting the class in Model.cpp when I am using a colortype variable. (adding touchgfx::colortype in Model.hpp)

Thank you all for your help and input! 

Here is the copy of the code that worked for me: 

 

//-----------------HomeScreenView.cpp-------------------------

#include <gui/homescreen_screen/HomeScreenView.hpp>

#include <touchgfx/Color.hpp>

#include <touchgfx/hal/Types.hpp>

//#include "main.h"

#ifndef SIMULATOR

extern "C"

{

#include "main.h"

#include <stdio.h>

 

}

#endif

 

HomeScreenView::HomeScreenView()

{

 

}

 

void HomeScreenView::setupScreen()

{

HomeScreenViewBase::setupScreen();

 

}

 

void HomeScreenView::tearDownScreen()

{

HomeScreenViewBase::tearDownScreen();

}

 

void HomeScreenView::uart_Data (char *data)

{

#ifndef SIMULATOR

if (*data == '1') {

HAL_GPIO_WritePin(LD_USER2_GPIO_Port, LD_USER2_Pin, GPIO_PIN_SET);

input1_LEDPainter.setColor(touchgfx::Color::getColorFromRGB(0, 255, 0));

input1_LED.invalidate();

 

snprintf(debugStringBuffer, sizeof(debugStringBuffer), "tick: %d", count);

count++;

Application::getDebugPrinter()->setString(debugStringBuffer);

Application::invalidateDebugRegion();

 

//save the current color for LEDs.

presenter->save_input1_LED( touchgfx::Color::getColorFromRGB(0, 255, 0));

}

if (*data == '0') {

HAL_GPIO_WritePin(LD_USER2_GPIO_Port, LD_USER2_Pin, GPIO_PIN_RESET);

input1_LEDPainter.setColor(touchgfx::Color::getColorFromRGB(120, 120, 120));

input1_LED.invalidate();

 

//save the current color for LEDs.

presenter->save_input1_LED( touchgfx::Color::getColorFromRGB(0, 255, 0));

}

#endif

}

 

void HomeScreenView::set_input1_LED(touchgfx::colortype in1_Painter)

{

input1_LEDPainter.setColor(in1_Painter);

input1_LED.invalidate();

 

}

//------------------HomeScreenViewBase.hpp-------------------

#ifndef HOMESCREENVIEW_HPP

#define HOMESCREENVIEW_HPP

#include <gui_generated/homescreen_screen/HomeScreenViewBase.hpp>

#include <gui/homescreen_screen/HomeScreenPresenter.hpp>

 

class HomeScreenView : public HomeScreenViewBase

{

public:

HomeScreenView();

virtual ~HomeScreenView() {}

virtual void setupScreen();

virtual void tearDownScreen();

virtual void uart_Data (char *data);

virtual void set_input1_LED(touchgfx::colortype in1_Painter);

 

protected:

colortype test;

char debugStringBuffer[30];

int count = 10;

};

 

#endif // HOMESCREENVIEW_HPP

 

 

//-----------------------HomeScreenPresenter.hpp-------------------------

 

#ifndef HOMESCREENPRESENTER_HPP

#define HOMESCREENPRESENTER_HPP

 

#include <gui/model/ModelListener.hpp>

#include <mvp/Presenter.hpp>

#include <touchgfx/Color.hpp>

#include <touchgfx/hal/Types.hpp>

 

 

using namespace touchgfx;

 

class HomeScreenView;

 

class HomeScreenPresenter : public touchgfx::Presenter, public ModelListener

{

public:

HomeScreenPresenter(HomeScreenView& v);

 

/**

* The activate function is called automatically when this screen is "switched in"

* (ie. made active). Initialization logic can be placed here.

*/

virtual void activate();

 

/**

* The deactivate function is called automatically when this screen is "switched out"

* (ie. made inactive). Teardown functionality can be placed here.

*/

virtual void deactivate();

 

virtual ~HomeScreenPresenter() {}

virtual void uart_Data(char *data);

virtual void save_input1_LED(colortype painter);

 

private:

HomeScreenPresenter();

 

HomeScreenView& view;

};

 

#endif // HOMESCREENPRESENTER_HPP

//----------------Modle.cpp--------------------

 

#include <gui/model/Model.hpp>

#include <gui/model/ModelListener.hpp>

 

#ifndef SIMULATOR

 

extern "C" {

#include "cmsis_os2.h"

#include <cstring>

#include "main.h"

 

extern osMessageQueueId_t uartQueueHandle;

 

uartData_t *uartData_r;

uint16_t count = 0;

}

 

#endif

 

Model::Model() :

modelListener(0), toggleButtonState(false), colorStatus_LED{touchgfx::Color::getColorFromRGB(120, 120, 120)}

{

 

}

 

void Model::tick() {

#ifndef SIMULATOR

 

if (osMessageQueueGetCount(uartQueueHandle) > 0) {

if (osMessageQueueGet(uartQueueHandle, &uartData_r, 0, 0) == osOK) {

strncpy(RData, uartData_r->Data, uartData_r->size);

modelListener->uart_Data(RData);

}

}

 

#endif

 

}

 

 

void Model::saveToggelButton(bool State )

{

toggleButtonState = State;

 

}

 

bool Model::getToggleButton()

{

return toggleButtonState;

 

}

 

 

void Model::save_input1_LED(touchgfx::colortype painter)

{

colorStatus_LED = painter;

 

}

 

touchgfx::colortype Model::getStatus_input1_LED()

{

return colorStatus_LED;

}

 

 

//-----------------Model.hpp--------------------

#ifndef MODEL_HPP

#define MODEL_HPP

 

#include <touchgfx/Color.hpp>

#include <touchgfx/hal/Types.hpp>

 

 

class ModelListener;

 

class Model {

public:

Model();

 

void bind(ModelListener *listener) {

modelListener = listener;

}

 

void tick();

void saveToggelButton(bool State );

bool getToggleButton();

void save_input1_LED(touchgfx::colortype painter);

touchgfx::colortype getStatus_input1_LED();

 

 

protected:

ModelListener *modelListener;

char RData[257];

int inputLEDS;

bool toggleButtonState;

touchgfx::colortype colorStatus_LED; //casting the class to using colortype

 

};

 

#endif // MODEL_HPP

 

Hey @ayoub ,

 

I am glad you were able to solve your issue!

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)