jeudi 14 septembre 2023

Embedded C++: how to develop a good structure for programs [closed]

I am writing some embedded C++ code. Currently I only interface with a uart, some leds and some general IO.

My question comes here, what's the best way to structure the code?

My current approach: Each peripheral has a separate source, header and class. Then all the classes are instantiated in a class called Env.

// main.cc
int main()
{   
    Env env;
    
    while(1)
    {
        env.leds.allOn();
        util::counter(100000);
        
        env.leds.allOff();
        util::counter(100000);
    }
}

Then within the environment the rest is instantiated.

// Env.h
class Env 
{
public:
    Env();
    ~Env();
    
    UartPC pc;
    DebugLeds leds{&pc};
    GenIo gio{&pc};
    
private:
    
};

I got to this point where I am constantly passing a reference to the uart to the rest of the classes (this is because the classes will print some "Init successful" message).

Is this good/bad practice? Is there a good book / webpage describing how to structure code?

Thanks.

Aucun commentaire:

Enregistrer un commentaire