diff --git a/FAQ-#11.md b/FAQ-#11.md new file mode 100644 index 0000000..0e20290 --- /dev/null +++ b/FAQ-#11.md @@ -0,0 +1,78 @@ +### How do store a reference to the NeoPixelBus in another class? + +Since the NeoPixelBus is a template class; you can't just store a reference to it using older models. But there are two ways to accomplish this. + +My first recommendation is to make the containing class a template also. The following sketch demonstrates this all in one file. The actual template class can be in another file. +``` +#include + +typedef NeoPixelBus NeoPixelBusType; + +NeoPixelBusType strip(4, 2); + +// example class using a reference; requires all code to be in the include +template class PixelRef +{ + public: + PixelRef(T_PIXEL_METHOD& pixelStrip) : + _strip(pixelStrip) { + + } + + void Draw() { + _strip.Show(); + } + + private: + T_PIXEL_METHOD& _strip; +}; + +PixelRef myPixelRef(strip); + +void setup() { + strip.Begin(); + strip.Show(); +} + +void loop() { + myPixelRef.Draw(); +} +``` + +The second method to accomplish this uses a normal class definition; but has the limitation that class must be fully implemented in a header file due to the NeoPixelBus type is defined in the sketch. +``` +#include + +typedef NeoPixelBus NeoPixelBusType; + +NeoPixelBusType strip(4, 2); + +// example class using a reference; requires all code to be in the include +template class PixelRef { + public: + PixelRef(T_PIXEL_METHOD& pixelStrip) : + _strip(pixelStrip) { + } + + void Draw() { + _strip.Show(); + } + + private: + T_PIXEL_METHOD& _strip; +}; + + +PixelRef myPixelRef(strip); + +void setup() +{ + strip.Begin(); + strip.Show(); +} + +void loop() +{ + myPixelRef.Draw(); +} +``` \ No newline at end of file