From 8b4c6f303af08fd52fd0debd936a36ed522abc43 Mon Sep 17 00:00:00 2001 From: Greger Date: Mon, 2 Nov 2015 19:15:01 +0100 Subject: [PATCH] added function to set only one color channel without touching the others --- Adafruit_NeoPixel.cpp | 30 ++++++++++++++++++++++++++++++ Adafruit_NeoPixel.h | 1 + 2 files changed, 31 insertions(+) diff --git a/Adafruit_NeoPixel.cpp b/Adafruit_NeoPixel.cpp index 916e721d..7cf7ab71 100644 --- a/Adafruit_NeoPixel.cpp +++ b/Adafruit_NeoPixel.cpp @@ -1195,6 +1195,36 @@ void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) { } } +// Set the r, g or b value of a single pixel +void Adafruit_NeoPixel::setPixelChannel(uint16_t n, uint8_t v, uint8_t c) { + // set channel (red, green or blue) 'c' of pixel n to value v + if(n < numLEDs) { + uint8_t *p; + uint8_t offset; + if(c == 0) { + offset = rOffset; + } else if(c == 1) { + offset = gOffset; + } else if(c == 2) { + offset = bOffset; + } else { + return; + } + + if(wOffset == rOffset) { + p = &pixels[n * 3]; + } else { + p = &pixels[n * 4]; + } + if(brightness) { + v = (v * brightness) >> 8; + } + + p[offset] = v; + + } +} + // Convert separate R,G,B into packed 32-bit RGB color. // Packed format is always RGB, regardless of LED strand color order. uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) { diff --git a/Adafruit_NeoPixel.h b/Adafruit_NeoPixel.h index c32e3bf0..b9edd1fd 100644 --- a/Adafruit_NeoPixel.h +++ b/Adafruit_NeoPixel.h @@ -129,6 +129,7 @@ class Adafruit_NeoPixel { setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w), setPixelColor(uint16_t n, uint32_t c), + setPixelChannel(uint16_t n, uint8_t v, uint8_t c), setBrightness(uint8_t), clear(), updateLength(uint16_t n),