cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass an event from a hardware driver in C to a C++ model of TouchGFX?

HTD
Senior III

What I've tried:

I made a C module that talks to a sensor and reads data from it. It's done in interrupts, my callback is called when the data is received.

I can debug received data as they come by sending them over UART and read it on PuTTY.

The problem is, the Visual Studio solution for the GUI part doesn't see all the hardware part.

I discovered, than I can extern C functions from main or all included drivers and call them from the TouchGFX model.

What I couldn't do - is to extern data. I have a variable in C, extern keyword used - any reference to it from C++ file produces compile error like "unknown symbol". C++ compiler doesn't complain about functions, it calls them.

So I made something like this:

static float value;

extern "C" float h_get_value()

{

return value;

}

It works.

Long story short - I can pass the data in this ugly way to my model, though it's more like polling, my model just calls the function on every GUI frame.

Now I wonder:

Would it be possible to call Model or ModelListener instance from my C code? How? What and where should I include? Would it even work if my GUI function be called from an interrupt handler?

I suspect it could not work. So polling the hardware state every GUI frame is not that bad. But is there a way to avoid those wrapper function calls and just see a struct written in C part by my C++ GUI model?

I think the C++ code sees everything that is declared in ".h" files, I can include them. It doesn't see anything from ".c" files. I can put a variable only in ".c" file. When I try to place it within a header file, the code does not compile, I get "multiple definition" error, like the code from the header was processed multiple times, which is weird because there is a guard at the beginning of the file.

So - I have an ugly workaround and I'm looking for a proper way to make communication between my hardware part and GUI part. I want my all time critical logic in C part. So I send a command from GUI to start the process, the C code does everything, and my GUI just check on every displayed frame the state of the C part to report it to the user.

If there's a way to trigger event from C - that would be best. If not - just have one shared struct that contains state of the hardware inputs and outputs. The roadblock is I can't access a variable defined in C from my C++ code other, than using an externed C function, that returns it. I'm almost sure there is a more straight forward solution.

1 REPLY 1
MM..1
Chief III

You can use RTOS message queue ,or include your h file in cpp file .

h file need add on start and end

#ifdef __cplusplus
 extern "C" {
#endif
 
 
... normal header file
extern struct variable ....
extern uint8_t fooo; ...
 
void funcxx(....)
 
 
#ifdef __cplusplus
 }
#endif