Instantiation of classes, etc. before setup()

Hi
We have a Nano handling up to 8 MCP23017 expanders, instantiated this way before the call to setup():

MCP23017 mcps[BOARDCOUNT ] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27};

However, In Setup, we read from EEPROM the number of actual devices to configure, their addresses, and a mode for each. So, obviously, if the EEPROM content designates 4 MCP23017, we have allocated 4 for nothing. Additionally, it would be good to be able to configure a 'sparse' list, leaving some addresses in the 0x20-0x27 range for other devices(like LCDs...). However, I don't see a way to do the allocation dynamically within setup().
Here's the stripped down example of what works, with a note about what we'd prefer to do:

[code]
#include <CMRI.h>
#include <Auto485.h>
#include <EEPROM.h>
#include <Wire.h>
#include <MCP23017.h>
#include <ACR.h>

#define CODEVERSION 1.3
ACR_Node thisNode;      // holder for config data from EEPROM; def'n in ACR.h; populated in Setup.
#define BOARDCOUNT 8
//This works, but is a static allocation.  I'd like to do it dynamically, in setup, based on info
//in the EEPROM
MCP23017 mcps[BOARDCOUNT] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27};
 //----------------------------------------------------------------------------
void setup() {
  EEPROM.get(0, thisNode);  //fetch our configuration struct
  //okay.  Now we know we have MCP boards at several addresses; create the handlers
  //and init CMRI per info
} //----------------------------------------------------------------------------
void loop() {
} //----------------------------------------------------------------------------
[/code]
MCP23017 *mcps[BOARDCOUNT] ;
 //----------------------------------------------------------------------------
void setup() {
  EEPROM.get(0, thisNode);  //fetch our configuration struct
  mcps[0] = new MCP23017();
  mcps[0]->begin(0x22);
  mcps[1] = new MCP23017();
  mcps[1]->begin(0x27);
 
} 

Thanks for that. Going a bit further, what I can do is malloc() the array once I know how many elements I need; pushing the boundaries of a Nano, I know, but it is what it is.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.