Skip to main content
NGune.1
Associate III
November 1, 2024
Solved

Creating flexbuttons with code but don't know how to implement callback

  • November 1, 2024
  • 2 replies
  • 1312 views

Hi,

I am implementing a screen with particular number of flex buttons. The buttons are generated by my code and number of buttons change depending on some other variables. So far, I can create those flex buttons by code and see and touch them on the display. They change color when I touch them, so that I realise that buttons are functional. But of course, nothing happens when I touch them, because there are no callback functions. And I couldn't find a way how to implement callback function/functions for them.

Here is a part of my code, showing how I create those buttons:

for (uint8_t i=1; i<=numberoflines; i++)

{

Buttons[i].setBoxWithBorderPosition(0, 0, 337, 30);

Buttons[i].setBorderSize(0);

Buttons[i].setBoxWithBorderColors(touchgfx::Color::getColorFromRGB(0, 0, 0), touchgfx::Color::getColorFromRGB(150, 150, 150), touchgfx::Color::getColorFromRGB(80, 80, 80), touchgfx::Color::getColorFromRGB(150, 150, 150));

Buttons[i].setPosition(2, (i*30+5), 337, 30);

Buttons[i].invalidate();

scrollableContainer1.add(Buttons[i]);



Line[i].setPosition(42, (i*30+10), 294, 19);

Line[i].setColor(touchgfx::Color::getColorFromRGB(255, 255, 255));

Line[i].setLinespacing(0);

Unicode::snprintf(TextBuffer[i], 100, "TEST TEST %d %d", i, i);

Line[i].setWildcard(TextBuffer[i]);

Line[i].setTypedText(touchgfx::TypedText(1));

Line[i].invalidate();

scrollableContainer1.add(Line[i]);

}

scrollableContainer1.invalidateContent();

scrollableContainer1.invalidate();

Could you please help me to implement callback functions for those buttons?

Many thanks.

Best answer by Osman SOYKURT

Hello @NGune.1 ,

The function that you'd probably need to use is the setAction().

If you create a flexButton within TouchGFX Designer and generate code, you'll notice this in the Screen1ViewBase.cpp  :

Screen1ViewBase::Screen1ViewBase() :
 flexButtonCallback(this, &Screen1ViewBase::flexButtonCallbackHandler)
{
 ...
 flexButton1.setAction(flexButtonCallback);
 ...
}

 And the callback :

void Screen1ViewBase::flexButtonCallbackHandler(const touchgfx::AbstractButtonContainer& src)
{
 if (&src== &flexButton1)
 {
 //Interaction1
 //When flexButton1 clicked call virtual function
 //Call function1
 function1();
 }
}


function1 being implemented in the user code.

I invite you to look at our YouTube video in which we detail the generated code of a button.

2 replies

mƎALLEm
Technical Moderator
November 4, 2024

Hello @NGune.1 ,

Please kindly use </> button to share your code. I've edited your post then ..

Please review our tips on posting a 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."
Osman SOYKURT
Osman SOYKURTBest answer
Technical Moderator
November 4, 2024

Hello @NGune.1 ,

The function that you'd probably need to use is the setAction().

If you create a flexButton within TouchGFX Designer and generate code, you'll notice this in the Screen1ViewBase.cpp  :

Screen1ViewBase::Screen1ViewBase() :
 flexButtonCallback(this, &Screen1ViewBase::flexButtonCallbackHandler)
{
 ...
 flexButton1.setAction(flexButtonCallback);
 ...
}

 And the callback :

void Screen1ViewBase::flexButtonCallbackHandler(const touchgfx::AbstractButtonContainer& src)
{
 if (&src== &flexButton1)
 {
 //Interaction1
 //When flexButton1 clicked call virtual function
 //Call function1
 function1();
 }
}


function1 being implemented in the user code.

I invite you to look at our YouTube video in which we detail the generated code of a button.

Osman SOYKURTST Software Developer | TouchGFX
NGune.1
NGune.1Author
Associate III
November 4, 2024

Hi Mr. Soykurt,

First of all, thank you for your reply.

Using the Designer, I previously generated flex buttons and assigned them functions via interactions, as you mentioned above. They work properly. 

But, I am not generating those buttons, mentioned in the question, via TouchGFX Designer, that is where my problem begins. There will be unknown number of buttons in the project. That's why I am generating those buttons in my code. Let say, in Screen1View, in a for loop. The buttons are generated in the loop without problem. And even, when I touch them on the display, I see that they respond and change color.

However, I can't assign them functions. That is exactly my question. I couldn't find a way to assign them functions in the code.

Could you please re-think about the question?

Many thanks and kind regards.

Namik

NGune.1
NGune.1Author
Associate III
November 4, 2024

Hi again,

By inspecting the answer, provided by Mr. Soykurt, more carefully, I managed to solve the issue.

What I did is (to be reference for other people):

I placed this piece of code in the beginning.

ProgView::ProgView() :

 flexButtonCallback(this, &ProgView::flexButtonCallbackHandler)
{
	for (uint8_t i=1; i<=numberoflines; i++)
		{
	 Buttons[i].setAction(flexButtonCallback);
		}
}

And then wrote the following functions:

void ProgView::flexButtonCallbackHandler(const touchgfx::AbstractButtonContainer& src)
{
	for (uint8_t i = 1; i <= numberoflines; i++)
	{
		if (&src== &Buttons[i])
		{
			SelectedLine(i);
		}
	}
}

void ProgView::SelectedLine(uint8_t line)
{
 Unicode::snprintf(DescTextBuffer, DESCTEXT_SIZE, "Line %d Selected", line);
 DescText.invalidate();
}

Of course, the function prototypes are written in the ProgView.hpp :

class ProgView : public ProgViewBase
{
public:
 virtual void SelectedLine(uint8_t line);

protected:

 touchgfx::Callback<ProgView, const touchgfx::AbstractButtonContainer&> flexButtonCallback;

 void flexButtonCallbackHandler(const touchgfx::AbstractButtonContainer& src);
}

So, the problem is solved.

Thank you very much.