Skip to content

Commit 89b3f85

Browse files
authored
NeoPixelBrightnessBus (Makuna#143)
1 parent 487e0d2 commit 89b3f85

File tree

6 files changed

+347
-4
lines changed

6 files changed

+347
-4
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// NeoPixelBrightness
2+
// This example will cycle brightness from high to low of
3+
// three pixels colored Red, Green, Blue.
4+
// This demonstrates the use of the NeoPixelBrightnessBus
5+
// with integrated brightness support
6+
//
7+
// There is serial output of the current state so you can
8+
// confirm and follow along
9+
//
10+
11+
#include <NeoPixelBrightnessBus.h> // instead of NeoPixelBus.h
12+
13+
const uint16_t PixelCount = 3; // this example assumes 3 pixels, making it smaller will cause a failure
14+
const uint8_t PixelPin = 14; // make sure to set this to the correct pin, ignored for Esp8266
15+
16+
#define colorSaturation 255 // saturation of color constants
17+
RgbColor red(colorSaturation, 0, 0);
18+
RgbColor green(0, colorSaturation, 0);
19+
RgbColor blue(0, 0, colorSaturation);
20+
21+
// Make sure to provide the correct color order feature
22+
// for your NeoPixels
23+
NeoPixelBrightnessBus<NeoRgbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
24+
25+
// you loose the original color the lower the dim value used
26+
// here due to quantization
27+
const uint8_t c_MinBrightness = 8;
28+
const uint8_t c_MaxBrightness = 255;
29+
30+
int8_t direction; // current direction of dimming
31+
32+
void setup()
33+
{
34+
Serial.begin(115200);
35+
while (!Serial); // wait for serial attach
36+
37+
Serial.println();
38+
Serial.println("Initializing...");
39+
Serial.flush();
40+
41+
// this resets all the neopixels to an off state
42+
strip.Begin();
43+
strip.Show();
44+
45+
direction = -1; // default to dim first
46+
47+
Serial.println();
48+
Serial.println("Running...");
49+
50+
// set our three original colors
51+
strip.SetPixelColor(0, red);
52+
strip.SetPixelColor(1, green);
53+
strip.SetPixelColor(2, blue);
54+
55+
strip.Show();
56+
}
57+
58+
59+
void loop()
60+
{
61+
uint8_t brightness = strip.GetBrightness();
62+
Serial.println(brightness);
63+
64+
delay(100);
65+
66+
// swap diection of dim when limits are reached
67+
//
68+
if (direction < 0 && brightness <= c_MinBrightness)
69+
{
70+
direction = 1;
71+
}
72+
else if (direction > 0 && brightness >= c_MaxBrightness)
73+
{
74+
direction = -1;
75+
}
76+
// apply dimming
77+
brightness += direction;
78+
strip.SetBrightness(brightness);
79+
80+
// show the results
81+
strip.Show();
82+
}
83+

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "git",
99
"url": "https://github.com/Makuna/NeoPixelBus"
1010
},
11-
"version": "2.2.4",
11+
"version": "2.2.5",
1212
"frameworks": "arduino",
1313
"platforms": "*"
1414
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=NeoPixelBus by Makuna
2-
version=2.2.4
2+
version=2.2.5
33
author=Michael C. Miller (makuna@live.com)
44
maintainer=Michael C. Miller (makuna@live.com)
55
sentence=A library that makes controlling NeoPixels (WS2811, WS2812 & SK6812) and DotStars (APA102) easy.

src/NeoPixelBrightnessBus.h

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*-------------------------------------------------------------------------
2+
NeoPixelBus library wrapper template class that provides overall brightness control
3+
4+
Written by Michael C. Miller.
5+
6+
I invest time and resources providing this open source code,
7+
please support me by dontating (see https://github.com/Makuna/NeoPixelBus)
8+
9+
-------------------------------------------------------------------------
10+
This file is part of the Makuna/NeoPixelBus library.
11+
12+
NeoPixelBus is free software: you can redistribute it and/or modify
13+
it under the terms of the GNU Lesser General Public License as
14+
published by the Free Software Foundation, either version 3 of
15+
the License, or (at your option) any later version.
16+
17+
NeoPixelBus is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
GNU Lesser General Public License for more details.
21+
22+
You should have received a copy of the GNU Lesser General Public
23+
License along with NeoPixel. If not, see
24+
<http://www.gnu.org/licenses/>.
25+
-------------------------------------------------------------------------*/
26+
27+
#pragma once
28+
29+
#include "NeoPixelBus.h"
30+
31+
template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoPixelBrightnessBus :
32+
public NeoPixelBus<T_COLOR_FEATURE, T_METHOD>
33+
{
34+
public:
35+
NeoPixelBrightnessBus(uint16_t countPixels, uint8_t pin) :
36+
NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(countPixels, pin),
37+
_brightness(0)
38+
{
39+
}
40+
41+
NeoPixelBrightnessBus(uint16_t countPixels, uint8_t pinClock, uint8_t pinData) :
42+
NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(countPixels, pinClock, pinData),
43+
_brightness(0)
44+
{
45+
}
46+
47+
NeoPixelBrightnessBus(uint16_t countPixels) :
48+
NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(countPixels),
49+
_brightness(0)
50+
{
51+
}
52+
53+
void SetBrightness(uint8_t brightness)
54+
{
55+
// Due to using fixed point math, we modifiy the brightness
56+
// before storing making the math faster
57+
uint8_t newBrightness = brightness + 1;
58+
59+
// Only update if there is a change
60+
if (newBrightness != _brightness)
61+
{
62+
// calculate a scale to modify from old brightness to new brightness
63+
//
64+
uint8_t oldBrightness = _brightness - 1; // unmodify brightness value
65+
uint16_t scale;
66+
67+
if (oldBrightness == 0)
68+
{
69+
scale = 0; // Avoid divide by 0
70+
}
71+
else if (brightness == 255)
72+
{
73+
scale = 65535 / oldBrightness;
74+
}
75+
else
76+
{
77+
scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness;
78+
}
79+
80+
// re-scale existing pixels
81+
//
82+
uint8_t* ptr = this->Pixels();
83+
uint8_t* ptrEnd = ptr + this->PixelsSize();
84+
while (ptr != ptrEnd)
85+
{
86+
uint16_t value = *ptr;
87+
*ptr++ = (value * scale) >> 8;
88+
}
89+
90+
_brightness = newBrightness;
91+
this->Dirty();
92+
}
93+
}
94+
95+
uint8_t GetBrightness() const
96+
{
97+
return _brightness - 1;
98+
}
99+
100+
void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color)
101+
{
102+
if (_brightness)
103+
{
104+
uint8_t* ptr = (uint8_t*)&color;
105+
uint8_t* ptrEnd = ptr + T_COLOR_FEATURE::PixelSize;
106+
107+
while (ptr != ptrEnd)
108+
{
109+
uint16_t value = *ptr;
110+
*ptr++ = (value * _brightness) >> 8;
111+
}
112+
}
113+
NeoPixelBus<T_COLOR_FEATURE, T_METHOD>::SetPixelColor(indexPixel, color);
114+
}
115+
116+
typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const
117+
{
118+
typename T_COLOR_FEATURE::ColorObject color = NeoPixelBus<T_COLOR_FEATURE, T_METHOD>::GetPixelColor(indexPixel);
119+
120+
if (_brightness)
121+
{
122+
uint8_t* ptr = (uint8_t*)&color;
123+
uint8_t* ptrEnd = ptr + T_COLOR_FEATURE::PixelSize;
124+
125+
while (ptr != ptrEnd)
126+
{
127+
uint16_t value = *ptr;
128+
*ptr++ = (value << 8) / _brightness;
129+
}
130+
}
131+
return color;
132+
}
133+
134+
protected:
135+
uint8_t _brightness;
136+
};
137+
138+

src/NeoPixelBus.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoPixelBus
152152
_state &= ~NEO_DIRTY;
153153
};
154154

155-
uint8_t* Pixels() const
155+
uint8_t* Pixels()
156156
{
157157
return _method.getPixels();
158158
};
@@ -307,7 +307,7 @@ template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoPixelBus
307307

308308

309309

310-
private:
310+
protected:
311311
const uint16_t _countPixels; // Number of RGB LEDs in strip
312312

313313
uint8_t _state; // internal state
@@ -381,3 +381,4 @@ template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoPixelBus
381381
}
382382
};
383383

384+

src/internal/NeoBrightnessBus.h

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*-------------------------------------------------------------------------
2+
NeoPixel library helper template class that provides overall brightness control
3+
4+
Written by Michael C. Miller.
5+
6+
I invest time and resources providing this open source code,
7+
please support me by dontating (see https://github.com/Makuna/NeoPixelBus)
8+
9+
-------------------------------------------------------------------------
10+
This file is part of the Makuna/NeoPixelBus library.
11+
12+
NeoPixelBus is free software: you can redistribute it and/or modify
13+
it under the terms of the GNU Lesser General Public License as
14+
published by the Free Software Foundation, either version 3 of
15+
the License, or (at your option) any later version.
16+
17+
NeoPixelBus is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
GNU Lesser General Public License for more details.
21+
22+
You should have received a copy of the GNU Lesser General Public
23+
License along with NeoPixel. If not, see
24+
<http://www.gnu.org/licenses/>.
25+
-------------------------------------------------------------------------*/
26+
27+
#pragma once
28+
29+
template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoBrightnessBus :
30+
public NeoPixelBus<T_COLOR_FEATURE, T_METHOD>
31+
{
32+
public:
33+
void SetBrightness(uint8_t brightness)
34+
{
35+
// Stored brightness value is different than what's passed.
36+
// This simplifies the actual scaling math later, allowing a fast
37+
// 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t,
38+
// adding 1 here may (intentionally) roll over...so 0 = max brightness
39+
// (color values are interpreted literally; no scaling), 1 = min
40+
// brightness (off), 255 = just below max brightness.
41+
uint8_t newBrightness = brightness + 1;
42+
// Only update if there is a change
43+
if (newBrightness != _brightness)
44+
{
45+
// calculate a scale to modify from old brightness to new brightness
46+
//
47+
uint8_t oldBrightness = _brightness - 1; // De-wrap old brightness value
48+
uint16_t scale;
49+
50+
if (oldBrightness == 0)
51+
{
52+
scale = 0; // Avoid divide by 0
53+
}
54+
else if (brightness == 255)
55+
{
56+
scale = 65535 / oldBrightness;
57+
}
58+
else
59+
{
60+
scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness;
61+
}
62+
63+
// re-scale existing data in RAM
64+
//
65+
uint8_t* ptr = pixels;
66+
uint8_t* ptrEnd = pixels + PixelsSize();
67+
while (ptr != ptrEnd)
68+
{
69+
uint16_t value = *ptr;
70+
*ptr++ = (value * scale) >> 8;
71+
}
72+
73+
_brightness = newBrightness;
74+
Dirty();
75+
}
76+
}
77+
78+
uint8_t GetBrightness() const
79+
{
80+
return _brightness;
81+
}
82+
83+
void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color)
84+
{
85+
if (_brightness)
86+
{
87+
uint8_t* ptr = (uint8_t*)&color;
88+
uint8_t* ptrEnd = ptr + T_COLOR_FEATURE::PixelSize;
89+
90+
while (ptr != ptrEnd)
91+
{
92+
uint16_t value = *ptr;
93+
*ptr++ = (value * _brightness) >> 8;
94+
}
95+
}
96+
NeoPixelBus<T_COLOR_FEATURE, T_METHOD>::SetPixelColor(indexPixel, color);
97+
}
98+
99+
typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const
100+
{
101+
T_COLOR_FEATURE::ColorObject color = NeoPixelBus<T_COLOR_FEATURE, T_METHOD>::GetPixelColor(indexPixel);
102+
103+
if (_brightness)
104+
{
105+
uint8_t* ptr = (uint8_t*)&color;
106+
uint8_t* ptrEnd = ptr + T_COLOR_FEATURE::PixelSize;
107+
108+
while (ptr != ptrEnd)
109+
{
110+
uint16_t value = *ptr;
111+
*ptr++ = (value << 8) / _brightness);
112+
}
113+
}
114+
return color;
115+
}
116+
117+
protected:
118+
uint8_t _brightness;
119+
};
120+
121+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy