This way VS17 underlines "ueb" in cpp file and says cannot be used as a constant value. However, it compiles without errors.
Calling begin then says
Funktionen.cpp: In member function void Function::begin()
Function.cpp: 37:45: error: 'leds' was not declared in this scope
FastLED.addLeds<WS2812B, DATA_PIN_S1, GRB>(leds, NUM_LEDS_S1)
Whole code is a bit longer btw, I cuttet off the unnecessary part
Creates an array of CRGB object which IMMEDIATELY goes out of scope once the function ends and ceases to exist. Hardly useful.
You will need to (choose one):
have a member variable that is a pointer to a CRGB and points to the first element of an array that is created in the sketch and another member variable that holds the total number of elements in that array.
Have a member array that is sized for the longest strip you might ever have and a member variable that tells how many slots of that array you actually used.
A member pointer and malloc memory for it in the constructor. This is NOT a solution for the beginner.
Creates an array of CRGB object which IMMEDIATELY goes out of scope once the function ends and ceases to exist. Hardly useful.
You will need to (choose one):
1. have a member variable that is a pointer to a CRGB and points to the first element of an array that is created in the sketch and another member variable that holds the total number of elements in that array.
2. Have a member array that is sized for the longest strip you might ever have and a member variable that tells how many slots of that array you actually used.
3. A member pointer and malloc memory for it in the constructor. This is NOT a solution for the beginner.
Wow - okay, this is two steps ahead of my knowledge^^.
As far as I understand, C++ has no garbage collector - why does the array cease to exist?
If I get 1) right,
byte arrSize = sizeof(ueb);
would hold the length of the array.
Next problem is to declare a variable which could hold leds[0]..
Inso:
Wow - okay, this is two steps ahead of my knowledge^^.
As far as I understand, C++ has no garbage collector - why does the array cease to exist?
Google "C++ scope" and do some reading.
If I get 1) right,
byte arrSize = sizeof(ueb);
would hold the length of the array.
ueb is a byte. sizeof a byte would be 1.
Next problem is to declare a variable which could hold leds[0]..
No, you misunderstand. Define the array in the .ino file just as you would if the class wasn't there. Just like you would have in your first example code in the OP. Then pass a pointer to that array into your class and have a member variable that holds that pointer and another that holds the number of elements in the array.
gfvalvo:
It's a local variable on the stack. It's removed (actually the stack pointer is adjusted) when the function exits.
Think I get it - it is not declared in .h public or private. Omg, yes. And I cannot declare it in .h, because the size is not known at this point. Damn..
Delta_G:
Google "C++ scope" and do some reading.
ueb is a byte. sizeof a byte would be 1.
No, you misunderstand. Define the array in the .ino file just as you would if the class wasn't there. Just like you would have in your first example code in the OP. Then pass a pointer to that array into your class and have a member variable that holds that pointer and another that holds the number of elements in the array.
Ok, now it makes sense, I just tell the object array where the global created array is located in ram, so i can use it within the object without creating it there..
read a lot about pointers the last days, did research, tested, and was ~sure the problem is solved.
It is not :-\
Simple example:
I declare two int, create a pointer array, put in the pointer to s and t. Then I call a function of another class, using the array:
int s = 5;
int t = 10;
int *testArray[] = { &s, &t };
[...]
aTest::testTheA(*testArray);
On the other class, I create two int values, set the value to the two values I have the pointer of. First is fine. Second is not.
int u = 0;
int v = 0;
void aTest::testTheA(int *testArray)
{
u = testArray[0];
v = testArray[1];
Serial.print("S = ");
Serial.println(u);
Serial.print("T = ");
Serial.println(v);
}
Output is:
S = 5
T = 731
Setting S to 15 also prints S= 15. But changing t to whatever does not change anything, it is always 731.
is a correct way to call a function with a pointer array as parameter ?
Yes, but then you pass it a pointer to a pointer to an int. An int** if you will. Remember, the array name without any braces is itself a pointer to the first element.
What you want to do here is not to have an array of pointers, but an array of int. Then you want to pass a pointer to that array. Not a pointer to a pointer.
int *testArray[] = { &s, &t };
Should just be:
int testArray[] = { s, t };
Then it will work with the code you had before.
If not then stop posting contrived examples and give me a MCVE so I can play with it too.
Delta_G:
If not then stop posting contrived examples and give me a MCVE so I can play with it too.
No problem
The project: I have a LED sketch running for a couple of WS2812b on several pins.
I want to be able to f.e. set a seperate pin to a color, and also set them all to a color.
Therefor, I create an object for every stripe (here T1 and T2) and also a multiobject, a virtual stripe (here Multi).
The multiobject holds an array of pointers to every stripe, so it can call functions on every stripe, while I have just to call one function.
Calling the function with multi now only works for the first stripe.
I made a new sketch only focusing on two stripes, the multiobject and the set color method:
arrayVerify.ino:
// arrayVerify.ino
#include "ledFunction.h"
ledFunction T1; // create first stripe
ledFunction T2; // create second stripe
ledFunction *objectPointers[] = { &T1, &T2 }; // create array with pointers to stripes
ledFunction Multi(objectPointers); // create a virtual stripe to access all
void setup() // only for verification
{
Serial.begin(115200);
Multi.setColor(255); // color should be set to T1 and T2
T1.printColor(); // verification for T1: true : Color code is: 255
T2.printColor(); // verification for T2: false: Color code is: 0
T1.setColor(200); // color should be set to T1 seperate
T2.setColor(200); // color should be set to T2 seperate
T1.printColor(); // verification for T1: true : Color code is: 255
T2.printColor(); // verification for T2: true : Color code is: 255
}
void loop(){}
ledFunction.h:
// ledFunction.h
#ifndef _LEDFUNCTION_h
#define _LEDFUNCTION_h
#include "arduino.h"
class ledFunction
{
private:
byte colorValue; // color value of stripes leds (or of all stripes if multi)
ledFunction *multiStripe; // the array used after creating the multistripe object
boolean isMulti; // helper to decide if it is a call of multi or singlestripe (used in setColor() )
public:
ledFunction(); // create single stripe object
ledFunction(ledFunction **objectPointers); // create multistripe object
void setColor(byte color); // function for single and multi
void printColor(); // serial print actual value of byte colorValue
};
#endif
ledFunction.cpp:
// ledFunction.cpp
#include "ledFunction.h"
ledFunction::ledFunction()
{
colorValue = 0;
isMulti = false;
}
ledFunction::ledFunction(ledFunction **objectPointers)
{
multiStripe = *objectPointers;
colorValue = 0;
isMulti = true;
}
void ledFunction::setColor(byte color)
{
if (isMulti == true) // function is called by multi
{
multiStripe[0].setColor(color); // should be same as T1.setColor(byte color)
multiStripe[1].setColor(color); // should be same as T2.setColor(byte color)
}
else // function is called by single stripe
{
colorValue = color;
}
}
void ledFunction::printColor()
{
Serial.print("Color code is: ");
Serial.println(colorValue);
}
If I can point out anything better or something else, just let me know
And again, after three hours of crashes and frustration, I´m back
With the formentioned sketch, I was able to set my values. No I try to call a sketch with the pointer. Within the sketch, I need to access the variables of the object (f.e. the amount of LEDs). No way.
I have revised the sketch to show in a "simple" example what is wrong.
Again, two objects, T1 and T2, with byte "identifier" 1 and 2. The third object called Multi with "identifier" = 9 and the array of pointers.
Multiobject calls a function. Within, I use first and second array pointer to call another function, just printing the identifier. Both times the identifier is 9 instead of 1 and 2.
The code:
arrayVerify.ino
// arrayVerify.ino
#include "ledFunction.h"
ledFunction T1(1); // create first stripe
ledFunction T2(2); // create second stripe
ledFunction objectPointers[] = { &T1, &T2 }; // create array with pointers to stripes
ledFunction Multi(objectPointers); // create a virtual stripe to access all
void setup()
{
Serial.begin(115200);
Multi.example();
}
void loop(){}
ledFunction.h
// ledFunction.h
#ifndef _LEDFUNCTION_h
#define _LEDFUNCTION_h
#include "arduino.h"
class ledFunction
{
private:
ledFunction *multiStripe; // the array used after creating the multistripe object
byte identifier;
public:
ledFunction(int ident); // create single stripe object
ledFunction(ledFunction *objectPointers); // create multistripe object
void printIdentifier();
void example();
};
#endif
Seems to me that your 'Multi' object and your T1 / T2 objects are different animals and maybe should be of different classes. I'd consider defining a 'multiLedHandler' class with appropriate friendship so it can access the private members of objects instantiated from the ledFunction class.
Also, your functions for handling multiple stripes are rather inflexible since you hard-coded the number of stripes as 2. I'd pass the 'multiLedHandler' constructor both the array of pointers and the number of elements in the array. That way it could handle an arbitrary number of stripes.
Also, your functions for handling multiple stripes are rather inflexible since you hard-coded the number of stripes as 2. I'd pass the 'multiLedHandler' constructor both the array of pointers and the number of elements in the array. That way it could handle an arbitrary number of stripes.
Yeah, that was my next question. How will this class know how many strips have been connected?
I'm with you that these should be two separate classes.