lundi 3 octobre 2016

create multiple implementations of a class, one to send Qt signals and one to work with hardware directly

I'm working on a visual debugger for an embedded application running on stm32. So the debugger will run on a PC an reuse the same code as the main application except for the low level functions triggering hardware reaction and will instead send signals for the GUI (QT).

I'm looking for a pattern or something clean that could allow me to do that without big #ifdef in the code.

So to give an example:

I have the gpio.h and gpio.c files playing with the stm32 low level stuffs (they are semi-generated by stmCube so I cannot change them completely).

(C code)
void GPIO_set_realy_state(int relay, bool state)
{
    HAL_GPIO_WritePin(port,relay,state?GPIO_PIN_SETGPIO_PIN_RESET);
}

I have a c++ wrapper above them (GpioWrapper) that is called whenever the application needed to change the state of an IO

GpioWrapper::setRealyState(int relay, bool state)
{
    GPIO_set_realy_state(relay,state);
}

In the PC application, I would like another implementation of that wrapper or something alike that would get called instead of the above one to send a signal instead of calling the low level functions making the GUI to change an icon.

GpioWrapper::setRealyState(int relay, bool state)
{
    emit RelayTriggered(relay,state);
}

The problem that I face is that to send signals, my class need to inherit from QObject and it cannot be the case in GpioWrapper.h given that that part has no clue of the Qt World when used in the embedded application and I would like to avoid #ifdef #else in my wrapper if possible.

What could be the cleaner way to solve that?

Aucun commentaire:

Enregistrer un commentaire