Page 1 of 1

Pass argument from epprom to a class constructor

Posted: Mon Mar 07, 2022 9:35 pm
by Aragornale
Helow, i´m using the simplified subsequent code to initate a NeoPixel Strip in a ESP32 on arduino framework

Code: Select all

//creates the  class "strip" at the begin code
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(EEPROM.read(MNPIXEL), PinNeoPixel);
....
void setup  ()
 {
   EEPROM.begin ();
   strip.Begin ();
 }
void loop {}......
I nedd to pass the 1 byte parameter from eeprom to the class constructor, cause this is the Leds qtty attached to the strip.
The issue is when i declare the class, can´t read the eeprom, cause, it isnt initialized yet, but i cant declare the class into "setup", after eeprom.begin (), cause this must be a global class. I need to be able to read the eeprom during global class and variables declaration or bypass this and declare a global class inside a function. idk if this can be possible, or there is any way to jump this. In other words, i need to read eeprom during global variables initialization, like this.

Code: Select all

byte qtty=EEPROM.read(MNPIXEL);
......
void setup () {....}
void loop () {......}
Best Regards.

Re: Pass argument from epprom to a class constructor

Posted: Wed Mar 09, 2022 2:37 pm
by vanBassum
First option:

Code: Select all

class NeoPixel
{
public:
	NeoPixel(uint8_t byteFromEEPROM)
	{
		//bla bla
	}

	void DoSomething()
	{
		//bla bla
	}
}

//main.cpp

NeoPixel *neoPixel;


void Setup()
{
	uint8_t eepromVal = EEPROM_READ(...);
	neoPixel = new NeoPixel(eepromVal);
}


void Loop()
{
	neoPixel->DoSomething();
}


Second option:

Code: Select all


class NeoPixel
{
public:
	NeoPixel()
	{
		
	}

	void Init(uint8_t eepromVal)
	{
		//bla bla
	}

	void DoSomething()
	{
		//bla bla
	}
}

//main.cpp

NeoPixel neoPixel;


void Setup()
{
	uint8_t eepromVal = EEPROM_READ(...);
	neoPixel.Init(eepromVal);
}


void Loop()
{
	neoPixel.DoSomething();
}