OpenSprinkler Forums OpenSprinkler Unified Firmware main.cpp is very big Reply To: main.cpp is very big

#52165

testuser
Participant

While there are a lot of #ifdef’s, I don’t think that is what will make splitting things hard. Finding functions is not much of a problem with modern tools (Visual Studio, VScode, Eclipse and others) since they all can find definitions and declarations in other files. Including headers should not increase the size of the binary image unless you are doing something very strange in the headers.

I have found that refactoring code like this often results in the end in much more structured code with fewer interdependencies although there may be a certain amount of pain getting there. I guess I’d say that the OpenSprinkler code might benefit from more extensive use of classes. For example one way to deal with hardware differences is to define an abstract base and specialize the hardware, e.g.

class I2C
{
public:
  virtual int read(...) = 0;
  virtual byte write(...) = 0;
  ...
};
 
class RPiI2C : public I2C
{
public:
  int read(...); //implementation in cpp file
  byte write(...); //implementation in cpp file
  ...
};

Then where you want to use it:

#ifdef RPI
RPiI2C i2c;
#else
ArduinoI2C i2c;
...
#endif
 
i2c.read(...);

I admit I am partial to classes since my preferred language is C#, where all code must be in a class. Anyhow, anything you can do to shrink main will be appreciated.

I have forked your project in order to add a moisture sensor to it and have considered doing some refactoring myself. The problem with that is it will make it difficult for me to merge any changes you make back into it.