Skip to main content
Associate II
January 8, 2025
Solved

Hardware button with TouchGFX for screen transition

  • January 8, 2025
  • 5 replies
  • 2506 views

Hi,

I have been trying to implement the functionality to change the currently active screen on TouchGFX using a hardware button with no luck so far.

I'm using an STM32H743zit6.

Here are the resources I've looked at but haven't managed to get working:

https://www.youtube.com/watch?v=ufvJ5bcesL8

https://www.youtube.com/watch?v=QgEDSjvGAlk

https://support.touchgfx.com/docs/development/scenarios/example-gpio

https://support.touchgfx.com/docs/development/board-bring-up/how-to/10-physical-buttons

https://support.touchgfx.com/docs/development/ui-development/touchgfx-engine-features/screen-transitions

https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/hardware-button/td-p/640357

 

Any help would be greatly appreciated,

Thanks.

Best answer by Josh_W020

HI,

Thought I'd update this incase anyone else is having the same issue.
In my project I am using an internal framebuffer. In order to set this up I followed this guide made by STM: https://support.touchgfx.com/docs/development/board-bring-up/how-to/03-display-internal
In the guide it specifically states to add the line:

 HAL_LTDC_ProgramLineEvent(&hltdc,0);

to the stm32h7xx_it.c file. However, adding this line will prevent tick from updating? So my tick function was only called once on startup and this if statement never checked: 

if (HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_2) == GPIO_PIN_RESET)
	{
		static_cast<FrontendApplication*>(Application::getInstance())->handleKeyEvent('A');
	}

 Removing that line from stm32h7xx_it.c fixed the issue and now tick updates as expected. Just out of curiosity if anyone can explain a bit of the reasoning behind this that would be great.

Thank you for the help,
Josh.

5 replies

Andrew Neil
Super User
January 8, 2025

@Josh_W020 wrote:

Here are the resources I've looked at


What we really need to see is what you've done.

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 


@Josh_W020 wrote:

I have been trying to implement the functionality to change the currently active screen on TouchGFX using a hardware button .


Which part are you stuck on:

  • change the currently active screen?
  • detecting a hardware button?
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Josh_W020Author
Associate II
January 9, 2025

Hi,

I already have a different hardware button implemented for a separate function so I believe this is working correctly.

Sorry if I haven't provided enough info, I'll drop some code in below that i have tried so far.

Button setup in TouchGFX:

Screenshot 2025-01-09 093106.png

This is the cpp file as created in the youtube video provided by STM.

#include <newButtonController.hpp>
#include <main.h>
#include <touchgfx/hal/HAL.hpp>

extern "C" uint8_t User_ButtonState;

void newButtonController::init()
{
	previousState = 0xFF;
}

bool newButtonController::sample(uint8_t& key)
{
	if(HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_2) == GPIO_PIN_RESET && previousState == 0x00)
	{
		previousState = 0xFF;
		key = 1; // here key determines which key will be trigger in TouchGFX
		return true;
	}
	previousState = 0x00;
	return false;
}

And this the header file:

#ifndef NEWBUTTONCONTROLLER_HPP_
#define NEWBUTTONCONTROLLER_HPP_

#include <platform/driver/button/ButtonController.hpp>

class newButtonController : public touchgfx::ButtonController
{
	virtual void init();
	virtual bool sample(uint8_t& key);

private:
	uint8_t previousState;
};



#endif /* NEWBUTTONCONTROLLER_HPP_ */

Finally the modified TouchGFXHAL.cpp file:

#include <TouchGFXHAL.hpp>

/* USER CODE BEGIN TouchGFXHAL.cpp */

#include "main.h"
#include <newButtonController.hpp>
newButtonController bc;

using namespace touchgfx;
extern "C" uint8_t framebuffer[800 * 480];

void TouchGFXHAL::initialize()
{
 // Calling parent implementation of initialize().
 //
 // To overwrite the generated implementation, omit the call to the parent function
 // and implement the needed functionality here.
 // Please note, HAL::initialize() must be called to initialize the framework.
	/*

	*/
 TouchGFXGeneratedHAL::initialize();

 // Set the framebuffer start address
 HAL::getInstance()->setFrameBufferStartAddresses(
 framebuffer, // Framebuffer for the first layer (internal memory)
 nullptr, // Framebuffer for the second layer (if unused, set to nullptr)
 nullptr // Framebuffer for the third layer (if unused, set to nullptr)
 );

 setButtonController(&bc);
}

And here is how the button is configured in the ioc file:

Screenshot 2025-01-09 093623.png

Thanks again,

Josh

mƎALLEm
Technical Moderator
January 8, 2025

Hello,

I think you need to use application().gotoScreen2CoverTransitionxxx() API from the button callback.

See for example this thread.

 

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
Josh_W020Author
Associate II
January 9, 2025

Hi, thanks for the response.

I have checked my StartUpScreenViewBAse.cpp file and it appears to have generated correctly.

void StartUpScreenViewBase::handleKeyEvent(uint8_t key)
{
 if(1 == key)
 {
 //Interaction1
 //When hardware button 1 clicked change screen to FFTScreen
 //Go to FFTScreen with no screen transition
 application().gotoFFTScreenScreenNoTransition();
 
 }
}

Is it that I need to call "application().gotoFFTScreenScreenNoTransition();" from another place in my code?

MM..1
Chief III
January 8, 2025

Ohh boy zero info... Try this

MM1_0-1736362775916.png

run simultor and press A key on keyboard Work transition ?

Interaction1 in my example is add on main screen (FYI your screen marked to start) and transition go to Screen1...

When this works ok you can step to physical button code ...

 

Josh_W020Author
Associate II
January 9, 2025

Hi thanks for the reply,

I have tried setting the hardware button up in this way and the screen changes as expected in the simulator.

I also know the hardware button is working as I have another button used for saving images to a USB that works correctly.

However, if you have any suggestions on how to call the change screen functions through code even without pressing a button that would be great too (for example calling it after a short delay in the main while loop).

Josh

mƎALLEm
Technical Moderator
January 9, 2025

The transition is done using the call of application().gotoScreenXXXTransitionYYY() 

See the link I share previously.

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
GaetanGodart
Technical Moderator
January 9, 2025

Hello @Josh_W020 ,

 

From what I understood, you are sure that the button itself works and you are sure that you are able to do a screen transition.
So the issue must be between connecting these 2 actions.

We provide examples of boards and UI using hardware buttons with the GFX01M2 display extension.
One of the GUI using hardware button is the "Aircon remote demo":

GaetanGodart_0-1736424458944.png

Instead of adding a hardware button to TouchGFX Designer (which is possible), we mapped it to a keyboard key.
This way, we can execute an interaction from pressing a key on the keyboard (when simulating for instance) and we are also able to map our physical button to execute the interaction.

Here you can see that the interaction is executed when pressing the key '8' :

GaetanGodart_1-1736424597788.png

And here you can see how we mapped the hardware button to a key in MB1642ButtonController.cpp :

GaetanGodart_2-1736424667818.png

 

I hope this example is helpful to you!

 

Regards,

Josh_W020Author
Associate II
January 9, 2025

Hi,

Thanks for the response.

Is the MB1642ButtonController.cpp a file that you have created or one that is generated?

the reason I ask is that in my project I have ButtonController.hpp generated but don't have a file called ButtonController.cpp

Kind Regards,

Josh

GaetanGodart
Technical Moderator
January 10, 2025

Hello @Josh_W020,

 

MB1642ButtonController.cpp was created, not generated.
This is because it has to be done by hand to link the correct GPIOs that are used for the hardware buttons.

Every project has the ButtonController.hpp file.

 

Regards,

Josh_W020AuthorBest answer
Associate II
January 16, 2025

HI,

Thought I'd update this incase anyone else is having the same issue.
In my project I am using an internal framebuffer. In order to set this up I followed this guide made by STM: https://support.touchgfx.com/docs/development/board-bring-up/how-to/03-display-internal
In the guide it specifically states to add the line:

 HAL_LTDC_ProgramLineEvent(&hltdc,0);

to the stm32h7xx_it.c file. However, adding this line will prevent tick from updating? So my tick function was only called once on startup and this if statement never checked: 

if (HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_2) == GPIO_PIN_RESET)
	{
		static_cast<FrontendApplication*>(Application::getInstance())->handleKeyEvent('A');
	}

 Removing that line from stm32h7xx_it.c fixed the issue and now tick updates as expected. Just out of curiosity if anyone can explain a bit of the reasoning behind this that would be great.

Thank you for the help,
Josh.