Skip to content

Commit

Permalink
Merge pull request #346 from adafruit/pb-str2order
Browse files Browse the repository at this point in the history
Add str2order()
  • Loading branch information
PaintYourDragon committed Feb 7, 2023
2 parents 47f5892 + cffb79f commit 9a9104c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
33 changes: 33 additions & 0 deletions Adafruit_NeoPixel.cpp
Expand Up @@ -3439,3 +3439,36 @@ void Adafruit_NeoPixel::rainbow(uint16_t first_hue, int8_t reps,
setPixelColor(i, color);
}
}

/*!
@brief Convert pixel color order from string (e.g. "BGR") to NeoPixel
color order constant (e.g. NEO_BGR). This may be helpful for code
that initializes from text configuration rather than compile-time
constants.
@param v Input string. Should be reasonably sanitized (a 3- or 4-
character NUL-terminated string) or undefined behavior may
result (output is still a valid NeoPixel order constant, but
might not present as expected). Garbage in, garbage out.
@return One of the NeoPixel color order constants (e.g. NEO_BGR).
NEO_KHZ400 or NEO_KHZ800 bits are not included, nor needed (all
NeoPixels actually support 800 KHz it's been found, and this is
the default state if no KHZ bits set).
@note This function is declared static in the class so it can be called
without a NeoPixel object (since it's not likely been declared
in the code yet). Use Adafruit_NeoPixel::str2order().
*/
neoPixelType Adafruit_NeoPixel::str2order(const char *v) {
int8_t r = 0, g = 0, b = 0, w = -1;
if (v) {
char c;
for (uint8_t i=0; ((c = tolower(v[i]))); i++) {
if (c == 'r') r = i;
else if (c == 'g') g = i;
else if (c == 'b') b = i;
else if (c == 'w') w = i;
}
r &= 3;
}
if (w < 0) w = r; // If 'w' not specified, duplicate r bits
return (w << 6) | (r << 4) | ((g & 3) << 2) | (b & 3);
}
4 changes: 3 additions & 1 deletion Adafruit_NeoPixel.h
Expand Up @@ -87,7 +87,7 @@
// 0bRRRRGGBB for RGB

// RGB NeoPixel permutations; white and red offsets are always same
// Offset: W R G B
// Offset: W R G B
#define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2)) ///< Transmit as R,G,B
#define NEO_RBG ((0 << 6) | (0 << 4) | (2 << 2) | (1)) ///< Transmit as R,B,G
#define NEO_GRB ((1 << 6) | (1 << 4) | (0 << 2) | (2)) ///< Transmit as G,R,B
Expand Down Expand Up @@ -371,6 +371,8 @@ class Adafruit_NeoPixel {
uint8_t saturation = 255, uint8_t brightness = 255,
bool gammify = true);

static neoPixelType str2order(const char *v);

private:
#if defined(ARDUINO_ARCH_RP2040)
void rp2040Init(uint8_t pin, bool is800KHz);
Expand Down
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=Adafruit NeoPixel
version=1.10.7
version=1.11.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for controlling single-wire-based LED pixels and strip.
Expand Down

0 comments on commit 9a9104c

Please sign in to comment.