Pass argument from epprom to a class constructor

Aragornale
Posts: 1
Joined: Mon Mar 07, 2022 9:14 pm

Pass argument from epprom to a class constructor

Postby Aragornale » Mon Mar 07, 2022 9:35 pm

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.

vanBassum
Posts: 68
Joined: Sun Jan 17, 2021 11:59 am

Re: Pass argument from epprom to a class constructor

Postby vanBassum » Wed Mar 09, 2022 2:37 pm

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();
}

Who is online

Users browsing this forum: cfyronald96 and 83 guests