Skip to main content
kvkhekale
Associate III
March 28, 2019
Solved

How to implement "SwitchScreen()" in touchgfx?

  • March 28, 2019
  • 8 replies
  • 12814 views

Hello all,

I am trying to switch the screen throuch code in touchgfx.

If I use SwitchScreen, how shall I state the argument (screen* newScreen)?

If, for example I want to switch to Screen7.

Shall I call the function with SwitchScreen(scr7)?

where scr7 is declared as follows:

ScreenView7 scr7;

Best answer by Martin KJELDSEN

Hi @HP​,

Yes, this is the way you transition to other screens. Using the designer, they're generated for you when you have a "change screen" interaction defined. Here's an example - From screen 2 i defined an interaction to go to screen1 with NO special transition animation. The possibilibies are:

  • No transition
  • Slide Transition
  • Cover Transition

This interaction will cause the following methods to be generated:

/*
 * Screen Transition Declarations
 */
// Screen1
 
void FrontendApplicationBase::gotoScreen1ScreenNoTransition()
{
 transitionCallback = touchgfx::Callback<FrontendApplicationBase>(this, &FrontendApplication::gotoScreen1ScreenNoTransitionImpl);
 pendingScreenTransitionCallback = &transitionCallback;
}
 
void FrontendApplicationBase::gotoScreen1ScreenNoTransitionImpl()
{
 makeTransition<Screen1View, Screen1Presenter, touchgfx::NoTransition, Model >(&currentScreen, &currentPresenter, frontendHeap, &currentTransition, &model);
}
 

If you'd chosen another transition then the methods and arguments would be different. If you want to transition to a screen programatically, e.g. if you get a certain message from the backend, you will call it like in the code in the previous post:

static_cast<FrontendApplication*>(Application::getInstance())->gotoScreen1ScreenNoTransition();

The methods generated by the designer are in gui_generated/src/common/FrontEndApplicationBase.cpp

You can add your own, manually, in gui/src/common/FrontEndApplication.cpp

/Martin

8 replies

Martin KJELDSEN
Principal III
March 28, 2019

Hi,

Just use the generated goto methods:

e.g. you may define a method void gotoScreen() that takes some enum, switch on the argument value to transition to a screen (Depending on the screen transition you will have goto methods with or without transitions):

void BasePresenter::gotoScreen(ScreenType _type)
{
...
 case SETTINGS:
 static_cast<FrontendApplication*>(Application::getInstance())->gotoSettingsScreen();
 break;
...
}

Does that make sense?

kvkhekale
kvkhekaleAuthor
Associate III
March 29, 2019

Yes, definitely. Thank you very much for the prompt response.

But, the parameters on which I have to decide whether to switch to a new screen or not depend wholly on code.

So, in the designer, I cant decide when to switch the screen.

Thats the reason, I wont be able to use the generated code, from thre designer.

Can you please tell me another alternativethan using the generated goto method?

Martin KJELDSEN
Principal III
April 2, 2019

There is a "problem" right now where you will not have your goto methods generated if you don't have an interaction to do so specified. You can get some inspiration from how to the designer generates gotomethods and define them yourself in your concrete user code version of FrontEndApplication ( Check out FrontEndApplicationBase in a project with a screen transition).

Once you have those, you can use the method i mentioned above.

/Martin

HP_it
Senior II
September 5, 2019

Sorry for bringing this thread back to life..

Is this still the preferred way to change screens or is there another way?

I must admit that I'm not all that familiar with those goto methods - where are they located?

I too need a way to programatically change the screen withouth button presses.

Martin KJELDSEN
Martin KJELDSENBest answer
Principal III
September 6, 2019

Hi @HP​,

Yes, this is the way you transition to other screens. Using the designer, they're generated for you when you have a "change screen" interaction defined. Here's an example - From screen 2 i defined an interaction to go to screen1 with NO special transition animation. The possibilibies are:

  • No transition
  • Slide Transition
  • Cover Transition

This interaction will cause the following methods to be generated:

/*
 * Screen Transition Declarations
 */
// Screen1
 
void FrontendApplicationBase::gotoScreen1ScreenNoTransition()
{
 transitionCallback = touchgfx::Callback<FrontendApplicationBase>(this, &FrontendApplication::gotoScreen1ScreenNoTransitionImpl);
 pendingScreenTransitionCallback = &transitionCallback;
}
 
void FrontendApplicationBase::gotoScreen1ScreenNoTransitionImpl()
{
 makeTransition<Screen1View, Screen1Presenter, touchgfx::NoTransition, Model >(&currentScreen, &currentPresenter, frontendHeap, &currentTransition, &model);
}
 

If you'd chosen another transition then the methods and arguments would be different. If you want to transition to a screen programatically, e.g. if you get a certain message from the backend, you will call it like in the code in the previous post:

static_cast<FrontendApplication*>(Application::getInstance())->gotoScreen1ScreenNoTransition();

The methods generated by the designer are in gui_generated/src/common/FrontEndApplicationBase.cpp

You can add your own, manually, in gui/src/common/FrontEndApplication.cpp

/Martin

HP_it
Senior II
September 6, 2019

Great, thanks for the pointers!

MPhil.1
Visitor II
February 6, 2020

Can someone please help with providing a better example of creating custom methods that were not created by designer?

I need to enter a screen when a user presses a hardware button.

Andreea
Associate
February 21, 2020

I'm wondering about the same thing.

The TouchGFX team promised to come back with some video demos in the near future (as in the end of this month or so).

Please share if you found a solution for this problem.

ABeck.1
Associate III
February 13, 2020

0690X00000DA17AQAT.pngFor me while screen transition, drag I can see, completely first screen is not going, when second screen comes . What to do to resolve this

Martin KJELDSEN
Principal III
February 21, 2020

Second screen has no background = you're seeing garbage memory on target, and some weird state in the simulator.

FDeLu.1
Visitor II
April 9, 2020

Martin,

Have you released the video yet? If so please supply a link.

Thanks ​

Frank​

Martin KJELDSEN
Principal III
April 9, 2020

Hi @FDeLu.1​,

I already shot the video, yes, but i haven't edited it yet. Has to follow company protocols, etc. I did it as a one-take to save time and i'm just considering if i'm happy with releasing it.

/Martin

FDeLu.1
Visitor II
April 10, 2020

​OK please post the link once released. 

Also is it OK to call another screen from the view or should it be done from the presenter or model?  In a splash screen tick handler we have a timing loop that keeps the screen active for a couple seconds then calls "static_cast<FrontendApplication*>(Application::getInstance())_>gotoHomeScreenNoTransition();" from the handler.  Is that the correct place to call for the screen change or should it be done elsewhere?

Thanks,

Frank

scottSD
Senior III
May 8, 2020

@FDeLu.1​ 

Where did you end up doing calling your screen transitions? I am attempting doing it in a message handler in FrontendApplication.cpp.

@Martin KJELDSEN​  I am currently looking at implementing a message queue (from main.c) to send the TouchGFX task messages. These messages will be different dependent on the current screen being viewed. I was thinking putting this message handler in a common area such as FrontendApplication.cpp. Or would the model class be best place to do this?

Martin KJELDSEN
Principal III
May 11, 2020

You should probably use Model::tick() to inspect message queues and react accordingly, in order to have the logic bundled nicely together.

/Martin

mibra.1
Associate II
May 21, 2020
  • HI All,

I'm having a similar issue regarding switching screen.

in My GUI, i have Screen one and Screen two,

1- in screen one i set an interaction for changing the current screen (to screen two) by hardware button and i generated the code correctly and i found the next API

//Handles when a key is pressed

void Screen1ViewBase::handleKeyEvent(uint8_t key)

{

  if(0 == key)

  {

    application().gotoScreen2CoverTransitionEast();

  }

}

2- i used this handleKeyEvent(0) (in certain event) , in the screen1 view, but i didn't succeed to switch the screen2.

3- after debugging , i found that i actually go to the screen two setupScreen, the screen doesn't appear on the lcd.

Q: i want how ask how to use handleKeyEvent() properly ? do i use it in the screen view or the presenter or the model?

GBert.2
Senior
November 16, 2021

I don't think I have the perfect answer, but here's my workaround:

How do you switch between screens from the Model.cpp?

Using the Application handle, I was able to change screens.

#include <gui/common/FrontendApplication.hpp>
 
void Model::forceScreenTransition(eScreenIndex_t const eScreenIndex)
{
 FrontendApplication *const poApp = static_cast<FrontendApplication *>(Application::getInstance());
 switch (eScreenIndex)
 {
 default:
 case SCREEN_UNKNOWN: break; //< Do nothing.
 case SCREEN_SELF_TEST: poApp->gotoSelfTestScreenNoTransition(); break;
 case SCREEN_WAITING_CONNECTION: poApp->gotoWaitingConnectionScreenNoTransition(); break;
 // etc ...
 }
}

But what if the gotoMyScreen() function has not been generated?

For some reason, TouchGFX does not generate the transition functions for a screen without an interaction.

My workaround was to add a useless interaction (When hardware button is clicked, switch to <my screen>) to force TouchGFX Designer to generate the code in FrontendApplicationBase.cpp.

Hope it helps.

Sharing is caring.