Initializing global object (LCD) from setup().

I have some code for legacy hardware with a LCD display. The code to do this is in my main sketch and comes before setup(). It looks something like this:

static const uint8_t En_pin         = 4;
static const uint8_t Rw_pin         = 5;
static const uint8_t Rs_pin         = 6;
static const uint8_t D4_pin         = 0;
static const uint8_t D5_pin         = 1;
static const uint8_t D6_pin         = 2;
static const uint8_t D7_pin         = 3;
LiquidCrystal_I2C lcd(I2C_ADDR_LCD,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

Unfortunately, I need to support two different I2C to LCD modules which use different values for the pins. I would like to store which variant the device has in EEPROM, but I think I won't have access to this information until I get to setup(). If I instantiate it in setup() it isn't global.

I know I could use #ifdef, #else,#endif, but I'd rather not do it this way

Looking at the LiquidCrystal code, when the object is instantiated, it just sets a bunch of private attributes using a private method config(). I think I could make it work if I changed LiquidCrystal .config() to protected and created my own object which inherited from LiquidCrystal_I2C . I'd rather not have a special version of this library. Is there a better way?

I don't see any better way. If you instantiate the object in setup, it's going to go out of scope when setup ends and that will leave you unable to use it at all.

I need to support two different I2C to LCD modules which use different values for the pins.

Do both use the LiquidCrystal_I2C library? If so, make a global pointer. Then, in setup(), use ptr = LiquidCrystal_I2C() with the appropriate pin numbers.

In the rest of the code, use ptr-> instead of instance. to call the methods.

They both do use the same exact library, just with different initialization parameters.