From ca89075cc5091a06ac5e5f162a467b877f95f00c Mon Sep 17 00:00:00 2001 From: Phillip Burgess Date: Tue, 7 Feb 2023 09:03:50 -0800 Subject: [PATCH 1/2] Add str2order(), fix comment, bump version --- Adafruit_NeoPixel.cpp | 34 ++++++++++++++++++++++++++++++++++ Adafruit_NeoPixel.h | 4 +++- library.properties | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Adafruit_NeoPixel.cpp b/Adafruit_NeoPixel.cpp index 540bcf7..76cd197 100644 --- a/Adafruit_NeoPixel.cpp +++ b/Adafruit_NeoPixel.cpp @@ -3439,3 +3439,37 @@ 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) { + uint8_t i; + 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); +} diff --git a/Adafruit_NeoPixel.h b/Adafruit_NeoPixel.h index 53d29f7..ba022f6 100644 --- a/Adafruit_NeoPixel.h +++ b/Adafruit_NeoPixel.h @@ -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 @@ -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); diff --git a/library.properties b/library.properties index 22df9e6..cec3e14 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit NeoPixel -version=1.10.7 +version=1.11.0 author=Adafruit maintainer=Adafruit sentence=Arduino library for controlling single-wire-based LED pixels and strip. From cffb79f7fdabc026002147cbae91b36c9091989d Mon Sep 17 00:00:00 2001 From: Phillip Burgess Date: Tue, 7 Feb 2023 09:12:27 -0800 Subject: [PATCH 2/2] Fix unused var --- Adafruit_NeoPixel.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Adafruit_NeoPixel.cpp b/Adafruit_NeoPixel.cpp index 76cd197..a555438 100644 --- a/Adafruit_NeoPixel.cpp +++ b/Adafruit_NeoPixel.cpp @@ -3460,7 +3460,6 @@ void Adafruit_NeoPixel::rainbow(uint16_t first_hue, int8_t reps, neoPixelType Adafruit_NeoPixel::str2order(const char *v) { int8_t r = 0, g = 0, b = 0, w = -1; if (v) { - uint8_t i; char c; for (uint8_t i=0; ((c = tolower(v[i]))); i++) { if (c == 'r') r = i;