cancel
Showing results for 
Search instead for 
Did you mean: 

How to call virtual cpp function from main.c

erkanc
Associate III

 

 

Hi everyone.

There is a my page. I defined a virtual function there. And I changed the image's location. But I can not call this function from C. I research many example but can not arrive the success.

 

otherPageView.hpp

 

#ifndef OTHERPAGEVIEW_HPP
#define OTHERPAGEVIEW_HPP

#include <gui_generated/otherpage_screen/otherPageViewBase.hpp>
#include <gui/otherpage_screen/otherPagePresenter.hpp>

class otherPageView : public otherPageViewBase
{
public:
    otherPageView();
    virtual ~otherPageView() {}
    virtual void setupScreen();
    virtual void tearDownScreen();
    virtual void handleTickEvent();

    virtual void function1();
protected:

};

#endif // OTHERPAGEVIEW_HPP

 

 

 

otherPageView.cpp

 

#include <gui/otherpage_screen/otherPageView.hpp>
#include <touchgfx/Color.hpp>
#include <texts/TextKeysAndLanguages.hpp>
#include <math.h>
#include <touchgfx/hal/OSWrappers.hpp>

extern "C" void call_C_f(otherPageView* u) // wrapper function
{
     return u->function1();
}

void otherPageView::function1()
{
    image1.setXY(0, 50);
    image1.invalidate();
}

otherPageView::otherPageView()
{
}

void otherPageView::setupScreen()
{
    otherPageViewBase::setupScreen();
}

void otherPageView::tearDownScreen()
{
    otherPageViewBase::tearDownScreen();
}

 

 

 

main.c

 

// I stayed in this point how to call like function1?
void call_C_f(struct C* o);

int main(void)
{
    // bla, bla init...

    while(1)
    {
        MX_TouchGFX_Process();
    }
}

 

 

 

Thank you for your helps.

 

Best regards.

 

1 ACCEPTED SOLUTION

Accepted Solutions
GaetanGodart
ST Employee

This line

otherPageView* u = new otherPageView;

seems problematic to me.
You are trying to create a new view for your "page" (usually called screen).
You are not supposed to do that, how is the compiler supposed to know which view your are referencing. The one you just created doesn't knows about your image1.

It is normal that when calling the function from handleTickEvent everything works fine as the view knows about the image.

Hence, the simplest implementation if to use the handleTickEven function as you would use your infinite while loop in your main.
Instead of calling the function from main, try to instanciate a bool to true, then in your handleTickEvent, check for the value of this bool, if it is true, do your modifications then reset the value.

This would look somehitng like that :

main.c :

bool myBool = false;

// after some time, when you want to modify your GUI do this
myBool = true;

 

view.hpp :

#ifndef OTHERPAGEVIEW_HPP
#define OTHERPAGEVIEW_HPP

#include <gui_generated/otherpage_screen/otherPageViewBase.hpp>
#include <gui/otherpage_screen/otherPagePresenter.hpp>

class otherPageView : public otherPageViewBase
{
public:
    otherPageView();
    virtual ~otherPageView() {}
    virtual void setupScreen();
    virtual void tearDownScreen();
    virtual void handleTickEvent();

    virtual void function1();
protected:

};

#endif // OTHERPAGEVIEW_HPP

 

view.cpp :

#include <gui/otherpage_screen/otherPageView.hpp>
#include <touchgfx/Color.hpp>
#include <texts/TextKeysAndLanguages.hpp>
#include <math.h>
#include <touchgfx/hal/OSWrappers.hpp>

extern "C"
{
#include "main.h"
}

otherPageView::otherPageView()
{

}

void otherPageView::setupScreen()
{
    otherPageViewBase::setupScreen();
}

void otherPageView::tearDownScreen()
{
    otherPageViewBase::tearDownScreen();
}

void otherPageView::handleTickEvent()
{
	if(myBool)
	{
		function1();
		myBool = false;
	}
}

void otherPageView::function1()
{
	image1.setAlpha(0);
	image1.invalidate();
	selectedMenu_u8++;
}

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

View solution in original post

4 REPLIES 4
GaetanGodart
ST Employee

Hello @erkanc ,

 

Let me clarify a few things.
You want to call a function defined in TouchGFX's files during runtime so TouchGFX should have already started.
I assume you want to call a function that will modify what will be displayed on your GUI. So in other words you want to updated the screen based on what happens in the main.c.

If you are using FreeRTOS, the right way of doing so it to use the tasks and what a RTOS offers such as queues or semaphore based on your needs.
Here is a video showing how to add a message to a queue when a function in main.c is executed and how to receive this message in the TouchGFX application : update TouchGFX from main.c 
Furthermore, you might want to have a look at how our MVP architecture works : MVP introduction 

If you do not use a RTOS, you could simply include main.h in your model.cpp to be able to access variables. From there you can set a variable as a state machine to know if something has been required to be updated, then update your screen and finally reset your variable.
Note that you would have to do that in the handleTickEvent function of the model.cpp as shown in the previously shared video.

I hope it helps you. If it does not, I invite you to share more information about what you want to achieve exactly.

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

 

 

 From there you can set a variable as a state machine to know if something has been required to be updated, then update your screen and finally reset your variable.
Note that you would have to do that in the handleTickEvent function of the model.cpp as shown in the previously shared video.

 

 

Yes, I normally use this.

My purpose is 

 

 

I assume you want to call a function that will modify what will be displayed on your GUI. So in other words you want to updated the screen based on what happens in the main.c.

 

 

 

Finally I try the following code;

 

#ifndef OTHERPAGEVIEW_HPP
#define OTHERPAGEVIEW_HPP

#include <gui_generated/otherpage_screen/otherPageViewBase.hpp>
#include <gui/otherpage_screen/otherPagePresenter.hpp>

class otherPageView : public otherPageViewBase
{
public:
    otherPageView();
    virtual ~otherPageView() {}
    virtual void setupScreen();
    virtual void tearDownScreen();
    virtual void handleTickEvent();
    void f(int);
    virtual void function1();
protected:

};

#endif // OTHERPAGEVIEW_HPP
#include <gui/otherpage_screen/otherPageView.hpp>
#include <touchgfx/Color.hpp>
#include <texts/TextKeysAndLanguages.hpp>
#include <math.h>
#include <touchgfx/hal/OSWrappers.hpp>

extern "C"
{
#include "ram.h"
}

extern "C" void func(int);

void func(int i)
{
	otherPageView* u = new otherPageView;
	u->function1();
}

void otherPageView::function1()
{
	image1.setAlpha(0);   // not work
	image1.invalidate();  // not work
	selectedMenu_u8++;    // This line works.
}

otherPageView::otherPageView()
{

}

void otherPageView::setupScreen()
{
    otherPageViewBase::setupScreen();
}

void otherPageView::tearDownScreen()
{
    otherPageViewBase::tearDownScreen();
}
void func(int);

int main(void)
{
    func(0);
    while(1)
    {
         MX_TouchGFX_Process();
    }
}

 

 

I called func from main.c. But image1.setAlpha(0); and image.invalidate(); command didn't work.

erkanc_1-1709725984603.png

If I call the function1() from handleTickEvent all of commands is worked. Is this normal? Or is there any explanation about this?

GaetanGodart
ST Employee

This line

otherPageView* u = new otherPageView;

seems problematic to me.
You are trying to create a new view for your "page" (usually called screen).
You are not supposed to do that, how is the compiler supposed to know which view your are referencing. The one you just created doesn't knows about your image1.

It is normal that when calling the function from handleTickEvent everything works fine as the view knows about the image.

Hence, the simplest implementation if to use the handleTickEven function as you would use your infinite while loop in your main.
Instead of calling the function from main, try to instanciate a bool to true, then in your handleTickEvent, check for the value of this bool, if it is true, do your modifications then reset the value.

This would look somehitng like that :

main.c :

bool myBool = false;

// after some time, when you want to modify your GUI do this
myBool = true;

 

view.hpp :

#ifndef OTHERPAGEVIEW_HPP
#define OTHERPAGEVIEW_HPP

#include <gui_generated/otherpage_screen/otherPageViewBase.hpp>
#include <gui/otherpage_screen/otherPagePresenter.hpp>

class otherPageView : public otherPageViewBase
{
public:
    otherPageView();
    virtual ~otherPageView() {}
    virtual void setupScreen();
    virtual void tearDownScreen();
    virtual void handleTickEvent();

    virtual void function1();
protected:

};

#endif // OTHERPAGEVIEW_HPP

 

view.cpp :

#include <gui/otherpage_screen/otherPageView.hpp>
#include <touchgfx/Color.hpp>
#include <texts/TextKeysAndLanguages.hpp>
#include <math.h>
#include <touchgfx/hal/OSWrappers.hpp>

extern "C"
{
#include "main.h"
}

otherPageView::otherPageView()
{

}

void otherPageView::setupScreen()
{
    otherPageViewBase::setupScreen();
}

void otherPageView::tearDownScreen()
{
    otherPageViewBase::tearDownScreen();
}

void otherPageView::handleTickEvent()
{
	if(myBool)
	{
		function1();
		myBool = false;
	}
}

void otherPageView::function1()
{
	image1.setAlpha(0);
	image1.invalidate();
	selectedMenu_u8++;
}

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)
GaetanGodart
ST Employee

Hello @erkanc ,

 

Were you able to fix your problem and achieved your desired result?

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)