Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code for static arrays #370

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions Adafruit_NeoPixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@
NeoPixels expecting an 800 KHz (vs 400 KHz) data stream
with color bytes expressed in green, red, blue order per
pixel.
@param s Static Array that can be used to store the RGB values.
Default value is a nullptr
@return Adafruit_NeoPixel object. Call the begin() function before use.
*/
Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, int16_t p, neoPixelType t)
Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, int16_t p, neoPixelType t, uint8_t *s)
: begun(false), brightness(0), pixels(NULL), endTime(0) {
s ? isStatic = true : isStatic = false;
updateType(t);
updateLength(n);
updateLength(n, s);
setPin(p);
#if defined(ARDUINO_ARCH_RP2040)
// Find a free SM on one of the PIO's
Expand Down Expand Up @@ -117,7 +120,9 @@ Adafruit_NeoPixel::Adafruit_NeoPixel()
@brief Deallocate Adafruit_NeoPixel object, set data pin back to INPUT.
*/
Adafruit_NeoPixel::~Adafruit_NeoPixel() {
free(pixels);
if(!isStatic){
free(pixels);
}
if (pin >= 0)
pinMode(pin, INPUT);
}
Expand All @@ -138,22 +143,32 @@ void Adafruit_NeoPixel::begin(void) {
strip object. Old data is deallocated and new data is cleared.
Pin number and pixel format are unchanged.
@param n New length of strip, in pixels.
@param s Pointer to the static array for the LED Values. If there is not
static array, default arguement is nullptr
@note This function is deprecated, here only for old projects that
may still be calling it. New projects should instead use the
'new' keyword with the first constructor syntax (length, pin,
type).
*/
void Adafruit_NeoPixel::updateLength(uint16_t n) {
free(pixels); // Free existing data (if any)
void Adafruit_NeoPixel::updateLength(uint16_t n, uint8_t *s) {

// Allocate new data -- note: ALL PIXELS ARE CLEARED
numBytes = n * ((wOffset == rOffset) ? 3 : 4);
if ((pixels = (uint8_t *)malloc(numBytes))) {
memset(pixels, 0, numBytes);
if(s){
numLEDs = n;
numBytes = n * ((wOffset == rOffset) ? 3 : 4);
pixels = s;
} else {
numLEDs = numBytes = 0;
free(pixels); // Free existing data (if any)

// Allocate new data -- note: ALL PIXELS ARE CLEARED
numBytes = n * ((wOffset == rOffset) ? 3 : 4);
if ((pixels = (uint8_t *)malloc(numBytes))) {
memset(pixels, 0, numBytes);
numLEDs = n;
} else {
numLEDs = numBytes = 0;
}
}

}

/*!
Expand Down Expand Up @@ -186,10 +201,12 @@ void Adafruit_NeoPixel::updateType(neoPixelType t) {

// If bytes-per-pixel has changed (and pixel data was previously
// allocated), re-allocate to new size. Will clear any data.
if (pixels) {
bool newThreeBytesPerPixel = (wOffset == rOffset);
if (newThreeBytesPerPixel != oldThreeBytesPerPixel)
updateLength(numLEDs);
if(!isStatic){
if (pixels) {
bool newThreeBytesPerPixel = (wOffset == rOffset);
if (newThreeBytesPerPixel != oldThreeBytesPerPixel)
updateLength(numLEDs);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions Adafruit_NeoPixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Adafruit_NeoPixel {
public:
// Constructor: number of LEDs, pin number, LED type
Adafruit_NeoPixel(uint16_t n, int16_t pin = 6,
neoPixelType type = NEO_GRB + NEO_KHZ800);
neoPixelType type = NEO_GRB + NEO_KHZ800, uint8_t *s = nullptr);
Adafruit_NeoPixel(void);
~Adafruit_NeoPixel();

Expand All @@ -230,7 +230,7 @@ class Adafruit_NeoPixel {
void fill(uint32_t c = 0, uint16_t first = 0, uint16_t count = 0);
void setBrightness(uint8_t);
void clear(void);
void updateLength(uint16_t n);
void updateLength(uint16_t n, uint8_t *s = nullptr);
void updateType(neoPixelType t);
/*!
@brief Check whether a call to show() will start sending data
Expand Down Expand Up @@ -383,6 +383,7 @@ class Adafruit_NeoPixel {
#ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled...
bool is800KHz; ///< true if 800 KHz pixels
#endif
bool isStatic; ///< true if a static array is used
bool begun; ///< true if begin() previously called
uint16_t numLEDs; ///< Number of RGB LEDs in strip
uint16_t numBytes; ///< Size of 'pixels' buffer below
Expand Down