2024-05-03 12:41 AM
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:
Solved! Go to Solution.
2024-05-03 03:22 AM
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,
2024-05-03 01:26 AM
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,
2024-05-03 01:35 AM
Hello
Maybe example project on this post helps you:
There is example how to communicate between model and view.
Br JTP
2024-05-03 01:52 AM - last edited on 2024-05-03 03:21 AM by GaetanGodart
Hello @GaetanGodart
#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
2024-05-03 03:22 AM
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,
2024-05-06 11:11 PM - edited 2024-05-06 11:12 PM
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
2024-05-08 01:28 AM
Hey @ayoub ,
I am glad you were able to solve your issue!
Regards,