-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?

Tom Auger
  • 19,421
  • 22
  • 81
  • 104
  • It is not valid C. It *could be* C++, though. – wildplasser Sep 26 '18 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++. – John Go-Soco Sep 26 '18 at 10:51
  • Are you sure `CRGB::Black` is actually a struct and not an enum constant? – HolyBlackCat Sep 26 '18 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). – Jörg W Mittag Sep 26 '18 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](http://fastled.io/docs/3.1/index.html). – molbdnilo Sep 26 '18 at 11:05

1 Answers1

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

John Go-Soco
  • 886
  • 1
  • 9
  • 20
  • 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;` – Tom Auger Sep 26 '18 at 11:15
  • 2
    Not weird at all because you can define an overload operator for objects. Have a look at https://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/ – John Go-Soco Sep 26 '18 at 11:25
  • EDIT: Just realised my mistake there in my last comment. It should say "You can define an overload to the subscript operator for objects". – John Go-Soco Sep 26 '18 at 12:08