-4

I'm looking at some Arduino code and encountering a construct I haven't seen before:

CRGBPalette16 currentPalette( CRGB::Black );

CRGB::Black is a constant, which, as some have pointed out is a number.

Later on in the code the author appears to write directly to currentPalette (or other similar variables) like this: currentPalette[12] = CRGB::Black;

Is currentPalette an object instance? If so then how can you access it as if it were an array?

5
  • It is not valid C. It could be C++, though. Commented Sep 26, 2018 at 10:39
  • It could be a macro in C as well. EDIT: No, it cannot. But, having looked at the rest of the code, I do believe it is indeed C++. Commented Sep 26, 2018 at 10:51
  • Are you sure CRGB::Black is actually a struct and not an enum constant? Commented Sep 26, 2018 at 10:51
  • 1
    @wildplasser: It is Arduino Language, which is an unspecified, undocumented (basically "whatever the Arduino IDE will accept") close relative of C++. The actual compiler is a fork of GCC, but the IDE does some preprocessing and wrapping of the code (for example generating a suitable main method). Commented Sep 26, 2018 at 11:04
  • CRGB::Black is a number, not a struct. There are many constructors for CRGBPalette16, with varying numbers of parameters. Related: FastLED reference. Commented Sep 26, 2018 at 11:05

1 Answer 1

4

Having had a look at the rest of the file, I believe you are looking at some C++ code.

CRGBPalette16 currentPalette( CRGB::Black );

This line initialises an object of type CRGBPalette16 with the enum parameter CRGB::Black.

You can see the object passed as a reference into other functions, such as here on line 72:

leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i*16), brightness);

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. So CRGBPalette16 is an object and we're calling its constructor here? It's weird, because you can assign directly to it like: currentPalette[12] = 0xffcc88;
Not weird at all because you can define an overload operator for objects. Have a look at learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator
EDIT: Just realised my mistake there in my last comment. It should say "You can define an overload to the subscript operator for objects".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.