Skip to content

Commit

Permalink
Added setBrightness() method
Browse files Browse the repository at this point in the history
  • Loading branch information
PaintYourDragon committed Jul 5, 2013
1 parent 37440e5 commit 7e752eb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
54 changes: 38 additions & 16 deletions Adafruit_NeoPixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,14 @@

#include "Adafruit_NeoPixel.h"

Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint8_t p, uint8_t t) {
numBytes = n * 3;
Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint8_t p, uint8_t t) : numLEDs(n), numBytes(n * 3), pin(p), type(t), pixels(NULL)
#ifdef AVR
,port(portOutputRegister(digitalPinToPort(p))),
pinMask(digitalPinToBitMask(p))
#endif
{
if((pixels = (uint8_t *)malloc(numBytes))) {
memset(pixels, 0, numBytes);
numLEDs = n;
type = t;
pin = p;
#ifdef __AVR__
port = portOutputRegister(digitalPinToPort(p));
pinMask = digitalPinToBitMask(p);
#endif
endTime = 0L;
} else {
numLEDs = 0;
}
}

Expand All @@ -69,7 +63,7 @@ void Adafruit_NeoPixel::begin(void) {

void Adafruit_NeoPixel::show(void) {

if(!numLEDs) return;
if(!pixels) return;

// Data latch = 50+ microsecond pause in the output stream. Rather than
// put a delay at the end of the function, the ending time is noted and
Expand Down Expand Up @@ -835,6 +829,11 @@ void Adafruit_NeoPixel::show(void) {
void Adafruit_NeoPixel::setPixelColor(
uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
if(n < numLEDs) {
if(brightness) { // See notes in setBrightness()
r = (r * brightness) >> 8;
g = (g * brightness) >> 8;
b = (b * brightness) >> 8;
}
uint8_t *p = &pixels[n * 3];
if((type & NEO_COLMASK) == NEO_GRB) { *p++ = g; *p++ = r; }
else { *p++ = r; *p++ = g; }
Expand All @@ -845,10 +844,19 @@ void Adafruit_NeoPixel::setPixelColor(
// Set pixel color from 'packed' 32-bit RGB color:
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {
if(n < numLEDs) {
uint8_t
r = (uint8_t)(c >> 16),
g = (uint8_t)(c >> 8),
b = (uint8_t)c;
if(brightness) { // See notes in setBrightness()
r = (r * brightness) >> 8;
g = (g * brightness) >> 8;
b = (b * brightness) >> 8;
}
uint8_t *p = &pixels[n * 3];
if((type & NEO_COLMASK) == NEO_GRB) { *p++ = c >> 8; *p++ = c >> 16; }
else { *p++ = c >> 16; *p++ = c >> 8; }
*p = c;
if((type & NEO_COLMASK) == NEO_GRB) { *p++ = g; *p++ = r; }
else { *p++ = r; *p++ = g; }
*p = b;
}
}

Expand Down Expand Up @@ -878,3 +886,17 @@ uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) {
uint16_t Adafruit_NeoPixel::numPixels(void) {
return numLEDs;
}

// Adjust output brightness; 0=darkest (off), 255=brightest. This does
// NOT affect what's currently displayed, nor will another call to show().
// It only affects subsequent setPixelColor() calls. To change the
// brightness of existing data on the strip, it must be re-rendered.
void Adafruit_NeoPixel::setBrightness(uint8_t b) {
// Stored brightness value is different than what's passed.
// This simplifies the actual scaling math later, allowing a fast
// 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t,
// adding 1 here may (intentionally) roll over...so 0 = max brightness
// (color values are interpreted literally; no scaling), 1 = min
// brightness (off), 255 = just below max brightness.
brightness = b + 1;
}
15 changes: 9 additions & 6 deletions Adafruit_NeoPixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class Adafruit_NeoPixel {
begin(void),
show(void),
setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b),
setPixelColor(uint16_t n, uint32_t c);
setPixelColor(uint16_t n, uint32_t c),
setBrightness(uint8_t);
uint16_t
numPixels(void);
uint32_t
Expand All @@ -53,19 +54,21 @@ class Adafruit_NeoPixel {

private:

uint16_t
const uint16_t
numLEDs, // Number of RGB LEDs in strip
numBytes; // Size of 'pixels' buffer below
uint8_t
*pixels, // Holds LED color values (3 bytes each)
const uint8_t
pin, // Output pin number
type; // Pixel flags (400 vs 800 KHz, RGB vs GRB color)
uint8_t
brightness,
*pixels; // Holds LED color values (3 bytes each)
uint32_t
endTime; // Latch timing reference
#ifdef __AVR__
volatile uint8_t
const volatile uint8_t
*port; // Output PORT register
uint8_t
const uint8_t
pinMask; // Output PORT bitmask
#endif

Expand Down
2 changes: 1 addition & 1 deletion examples/strandtest/strandtest.pde
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 6, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, 6, NEO_GRB + NEO_KHZ800);

void setup() {
strip.begin();
Expand Down

0 comments on commit 7e752eb

Please sign in to comment.