mirror of
https://github.com/Makuna/NeoPixelBus.git
synced 2025-08-05 11:54:27 +02:00
Page:
FAQ #11
Pages
A method for resolving this
API Reference
Advanced Topics Tutorials
Color objects
Concentric Rings Support
DotStar Features
DotStar Methods
ESP32 DotStar Methods
ESP32 NeoMethods
ESP32 and RTOS Tasks
ESP8266 NeoMethods
Examples
FAQ #0
FAQ #1
FAQ #10
FAQ #11
FAQ #12
FAQ #2
FAQ #3
FAQ #4
FAQ #42
FAQ #5
FAQ #6
FAQ #7
FAQ #8
FAQ #9
FAQ
Home
HsbColor object API
HslColor object API
HtmlColor object API
Layout objects
LedSegment enum
Library Comercial Use
Library Comparisons
Matrix Panels Support
Nano 33 BLE NeoMethods
Neo Features
Neo Methods
NeoBitmapFile object API
NeoBitmapFile object
NeoBuffer object API
NeoBuffer object
NeoGamma object
NeoHueBlend objects
NeoMosaic object
NeoPixelAnimator object API
NeoPixelAnimator object
NeoPixelBrightnessBus object API
NeoPixelBrightnessBus object
NeoPixelBus object API
NeoPixelBus object
NeoPixelBusLg object API
NeoPixelBusLg object
NeoPixelSegmentBus object API
NeoPixelSegmentBus object
NeoTiles object
NeoTopology object
NeoVerticalSpriteSheet object API
NeoVerticalSpriteSheet object
Project References
Quick Start Guide
Quick Start Using One Wire LEDs
Quick Start Using Two Wire LEDs
Raster Image Support
Rgb16Color object API
Rgb48Color object API
RgbColor object API
Rgbw64Color object API
RgbwColor object API
RgbwwColor object API
SevenSegDigit object API
Smaller Code
Spiral Topography
T_COLOR_FEATURE
T_GAMMA
T_METHOD
Wiki Work List
enum AlarmAddError
enum AlarmPeriod
Clone
Table of Contents
How do I 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 or library.
#include <NeoPixelBus.h>
typedef NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> NeoPixelBusType;
NeoPixelBusType strip(4, 2);
// example template class using a reference
template<typename T_PIXEL_METHOD> class PixelRef {
public:
PixelRef(T_PIXEL_METHOD& pixelStrip) :
_strip(pixelStrip) {
}
void Draw() {
_strip.Show();
}
private:
T_PIXEL_METHOD& _strip;
};
PixelRef<NeoPixelBusType> 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 <NeoPixelBus.h>
typedef NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> NeoPixelBusType;
NeoPixelBusType strip(4, 2);
// example class using a reference; requires all code to be in the include
class PixelRef {
public:
PixelRef(NeoPixelBusType& pixelStrip) :
_strip(pixelStrip) {
}
void Draw() {
_strip.Show();
}
private:
NeoPixelBusType& _strip;
};
PixelRef myPixelRef(strip);
void setup() {
strip.Begin();
strip.Show();
}
void loop() {
myPixelRef.Draw();
}