As the OP suggested, I'm posting a summing up of the comments under his question since the answer was found.
In order to make a common define for those parameters in different source files, the easiest solution is to make a shared header. To do this, just create a new file called FastLedInclude.h
and, inside of that, write
#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 6
Then in every source file you will just have to #include "FastLedInclude.h"
to be able to use that library.
The FastLED library also requires the programmer to create a struct. Now, placing the definition of the struct (i.e. struct CRGB leds[NUM_LEDS];
) into the header file leads to compilation errors, since the compiler writes the code to allocate it once for every object file.
The correct way to share this among different object files is to declare it extern
in the header, then define it just once in one of the cpp or ino files:
// into FastLedInclude.h
extern struct CRGB leds[];
// into any cpp or ino file (but just once)
struct CRGB leds[NUM_LEDS];
This can also be done in the main ino sketch file, but personally I prefer to define it into another one (it depends, however, on how you choose to divide the program).
If you want to create a library, some other problems arise. The easiest solution is to ask the user to manually modify the header file which will be in the library folder. This solution is a bit uncomfortable, but it is the safest one.
Another solution is to force the user to create a header file into the sketch folder. Then the library should know the sketch folder path to back-include it. But.. This is a very discouraged option, since it will require the library to know the sketch location, thus violating the consistency principle at the base of a library-based approach.
The third solution is to modify the approach, from a compile-time static definition to a dynamic one. In order to do this, instead of defining NUM_LEDS
and DATA_PIN
at compile time you save them as variables in the library class.
For instance, in your library class definition you have
class yourclass
{
public:
yourclass(uint8_t data_pin, uint8_t num_leds);
~yourclass();
private:
uint8_t _data_pin;
uint8_t _num_leds;
struct CRGB *leds = NULL;
};
And the implementation can be
yourclass::yourclass(uint8_t data_pin, uint8_t num_leds)
{
_data_pin = data_pin;
_num_leds = num_leds;
// Dynamic allocation of the array
leds = (struct CRGB *) malloc(num_leds * sizeof(struct CRGB));
}
yourclass::~yourclass()
{
// you should free the leds array when done
free(leds);
}
Then the user can write
yourclass *obj = new yourclass(6, 60);
// or, if he wants to define,
#define NUM_LEDS 60
#define DATA_PIN 6
...
yourclass *obj = new yourclass(DATA_PIN, NUM_LEDS);
or, in the case of a statically allocated object,
yourclass obj(6, 60);
// or, if he wants to define,
#define NUM_LEDS 60
#define DATA_PIN 6
...
yourclass obj(DATA_PIN, NUM_LEDS);
FastLedInclude.h
) and then, in each file you need the FastLed library, write#include "FastLedInclude.h"
instead of#include <FastLed.h>
– frarugi87 Mar 18 '16 at 12:32DATA_PIN
you can store a uint8_t variable in your class containing the data pin. You can do the same withNUM_LEDS
, and then allocate the matrix dynamically (withmalloc
s ornew
s) in the code. It can have lower performances, but it will work better – frarugi87 Mar 18 '16 at 12:47const byte numberOfLeds = NUM_LEDS
to their sketch and then usenumberOfLeds
in my library rather thanNUM_LEDS
? – Rob Hilken Mar 18 '16 at 12:56