Disable a global object

FanController fan0(GPIO_FAN_TACH0 , SENSOR_THRESHOLD, GPIO_FAN_PWM0);
FanController fan1(GPIO_FAN_TACH1,  SENSOR_THRESHOLD, GPIO_FAN_PWM1);

i have these two objects declared globally, at the beginning of the setup routine the configuration for the device is loaded, is there any way i can disable to objects? so if i didnt want those hardware resources to be consumed could how would i globally shut this off. or is there a way to load my configuration globally and then if it is disabled just never initialize it ?

like

    if (FAN0_ENABLED) {
      FanController fan0(GPIO_FAN_TACH0 , SENSOR_THRESHOLD, GPIO_FAN_PWM0);
    }
    if (FAN1_ENABLED) {
      FanController fan1(GPIO_FAN_TACH1,  SENSOR_THRESHOLD, GPIO_FAN_PWM1);
    }

or would it matter if i declared these globally and then just never called begin()?

You can create a global pointer to a FanController object then only use the constructor if you need to use it.

Example

#include <FanController.h>

FanController * fan0;

void setup()
{
  fan0 = new FanController(2, 1000, 5);

  fan0->begin();
}

void loop()
{
  fan0->getSpeed();
}

Just comment them out


// FanController fan0(GPIO_FAN_TACH0 , SENSOR_THRESHOLD, GPIO_FAN_PWM0);
// FanController fan1(GPIO_FAN_TACH1,  SENSOR_THRESHOLD, GPIO_FAN_PWM1);

Or use conditional compilation:

#ifdef FAN0_ENABLED
FanController fan0(GPIO_FAN_TACH0 , SENSOR_THRESHOLD, GPIO_FAN_PWM0);
#endif
#ifdef FAN1_ENABLED
FanController fan1(GPIO_FAN_TACH1,  SENSOR_THRESHOLD, GPIO_FAN_PWM1);
#endif



void setup()
{
#ifdef FAN0_ENABLED
  fan0.begin();
#endif
#ifdef FAN1_ENABLED
  fan1.begin();
#endif

in this case if i exit the void setup does the object still exist? so i can call on it by the name i set using new, until the system is restarted?

nah because then they would be excluded form the program entirely. i may have not been entirely clear, this code runs on a sub processor and that processor receives a json config file for the hardware that is attached and then reconfigures the inputs and outputs to match the hard ware that is attached based on user input, therefore some people may have a button attached where the fan is declared and that would function differently than the button.

the reconfiguration only happens once in the cycle at the time of system start up. so if i can create the object based on the config and then use it until the object is shut off, then that will suffice. this hardware will not be power cycled that often in its use case.

i thought about this but the way i understand this if you don't define it as declared at compile time then the program doesn't include it at all i need the ability encoded in the firmware, but based on the users use case they may not want it like that or even on that pin. i am trying to open options to the user without having 900 firmware versions or without creating an external tool that will create random firmware based on use case.

Yes it does.. because your pointer is global. In the example you can see that it can be accessed within loop().

The pointer still exists because it is global.

The object still exists because the memory for it was explicitly allocated dynamically (with new) and it has not been explicitly freed.

If the object had been local to setup():

  FanController myFan(2, 1000, 5);
  fan0 = &myFan;

Then the object would stop existing at the end of setup() and the pointer would be left pointing to unallocated memory.

for the completness of my knowledge how do you free it after new?

I think the keyword is 'delete'. If the pointer points to an array of objects, use 'delete[]'.