Skip to content

Commit 6f18d1c

Browse files
committed
Some optimisations (O2)
1 parent 9a76beb commit 6f18d1c

3 files changed

Lines changed: 16 additions & 15 deletions

File tree

wled00/bus_manager.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ void BusDigital::setPixelColor(unsigned pix, uint32_t c) {
248248
if (!_valid || pix >= _len) return;
249249
if (hasWhite()) c = autoWhiteCalc(c);
250250
if (Bus::_cct >= 1900) c = colorBalanceFromKelvin(Bus::_cct, c); //color correction from CCT
251-
uint8_t adjBri = _scale != 100 ? scaleBri(_bri, _scale) : _bri;
252-
c = gamma32Func(color_fade(c, adjBri, true));
251+
c = gamma32Func(color_fade(c, scaleBri(_bri, _scale), true));
253252

254253
// pre-calcualte power usage for per-output ABL (a single bus should never have over 2000 LEDs so uint32_t is enough for _busPowerSum)
255254
// WARNING: assumes pixel is not modified agin until show() is called
@@ -466,7 +465,7 @@ void BusPwm::show() {
466465

467466
// use CIE brightness formula (linear + cubic) to approximate human eye perceived brightness
468467
// see: https://en.wikipedia.org/wiki/Lightness
469-
unsigned pwmBri = _scale != 100 ? scaleBri(_bri, _scale) : _bri;
468+
unsigned pwmBri = scaleBri(_bri, _scale);
470469
if (pwmBri < 21) { // linear response for values [0-20]
471470
pwmBri = (pwmBri * maxBri + 2300 / 2) / 2300 ; // adding '0.5' before division for correct rounding, 2300 gives a good match to CIE curve
472471
} else { // cubic response for values [21-255]
@@ -642,8 +641,7 @@ void BusNetwork::setPixelColor(unsigned pix, uint32_t c) {
642641
void BusNetwork::show() {
643642
if (!_valid || !canShow()) return;
644643
_broadcastLock = true;
645-
uint8_t adjBri = _scale != 100 ? scaleBri(_bri, _scale) : _bri;
646-
realtimeBroadcast(_UDPtype, _client, _len, _data, adjBri, hasWhite());
644+
realtimeBroadcast(_UDPtype, _client, _len, _data, scaleBri(_bri, _scale), hasWhite());
647645
_broadcastLock = false;
648646
}
649647

@@ -1013,8 +1011,7 @@ void BusHub75Matrix::setPixelColor(unsigned pix, uint32_t c) {
10131011
void BusHub75Matrix::setBrightness(uint8_t b) {
10141012
Bus::setBrightness(b);
10151013
if (!_valid) return;
1016-
uint8_t adjBri = _scale != 100 ? scaleBri(_bri, _scale) : _bri;
1017-
display->setBrightness(adjBri);
1014+
display->setBrightness(scaleBri(_bri, _scale));
10181015
}
10191016

10201017
void BusHub75Matrix::show(void) {

wled00/bus_manager.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ class Bus {
242242
static uint8_t _cctBlend;
243243

244244
uint32_t autoWhiteCalc(uint32_t c) const;
245-
static inline uint8_t scaleBri(uint8_t bri, uint8_t multiplier) { unsigned b = ((unsigned)bri*multiplier)/100; return b > 255 ? 255 : b; }
245+
static inline unsigned __attribute__((optimize("O2"))) scaleBri(unsigned bri, unsigned multiplier) {
246+
if (multiplier == 100) return bri;
247+
unsigned b = (bri * multiplier) / 100;
248+
return b > 255 ? 255 : b;
249+
}
246250
};
247251

248252

wled00/colors.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CRGBA& CRGBA::nscale8(uint8_t scale) {
1515
return *this;
1616
}
1717

18-
CRGBA& CRGBA::nadd(CRGBA c, bool preserveCR) {
18+
CRGBA& __attribute__((optimize("O2"))) CRGBA::nadd(CRGBA c, bool preserveCR) {
1919
uint32_t c2 = c.color32 & 0x00FFFFFF; // ignore alpha/white of color2
2020
if (c.a < 255) fast_color_scale(c2, c.a); // scale color2 by its alpha
2121
uint32_t c1 = color32 & 0x00FFFFFF; // ignore alpha/white of color1
@@ -48,7 +48,7 @@ CRGBA& CRGBA::nadd(CRGBA c, bool preserveCR) {
4848
* color blend function, based on FastLED blend function
4949
* the calculation for each color is: result = (A*(amountOfA) + A + B*(amountOfB) + B) / 256 with amountOfA = 255 - amountOfB
5050
*/
51-
uint32_t color_blend(uint32_t color1, uint32_t color2, uint8_t blend) {
51+
uint32_t __attribute__((optimize("O2"))) color_blend(uint32_t color1, uint32_t color2, uint8_t blend) {
5252
// min / max blend checking is omitted: calls with 0 or 255 are rare, checking lowers overall performance
5353
uint32_t rb1 = color1 & TWO_CHANNEL_MASK; // extract R & B channels from color1
5454
uint32_t wg1 = (color1 >> 8) & TWO_CHANNEL_MASK; // extract W & G channels from color1 (shifted for multiplication later)
@@ -64,7 +64,7 @@ uint32_t color_blend(uint32_t color1, uint32_t color2, uint8_t blend) {
6464
* original idea: https://github.com/wled/WLED/pull/2465 by https://github.com/Proto-molecule
6565
* speed optimisations by @dedehai
6666
*/
67-
uint32_t color_add(uint32_t c1, uint32_t c2, bool preserveCR) {
67+
uint32_t __attribute__((optimize("O2"))) color_add(uint32_t c1, uint32_t c2, bool preserveCR) {
6868
if (preserveCR) { fast_color_add(c1, c2); return c1; }
6969
if (c1 == BLACK) return c2;
7070
if (c2 == BLACK) return c1;
@@ -78,15 +78,15 @@ uint32_t color_add(uint32_t c1, uint32_t c2, bool preserveCR) {
7878
}
7979

8080
// fast color scale function (scales c1 as c1 * scale / 256)
81-
void fast_color_scale(uint32_t &c1, uint8_t scale) {
81+
void __attribute__((optimize("O2"))) fast_color_scale(uint32_t &c1, uint8_t scale) {
8282
uint32_t s = scale + 1;
8383
uint32_t rb = ((( c1 & TWO_CHANNEL_MASK) * s) >> 8) & TWO_CHANNEL_MASK;
8484
uint32_t wg = (((c1>>8) & TWO_CHANNEL_MASK) * s) & ~TWO_CHANNEL_MASK;
8585
c1 = rb | wg;
8686
}
8787

8888
// fast color add function that preserves ratio
89-
void fast_color_add(uint32_t &c1, uint32_t c2, uint8_t scale) {
89+
void __attribute__((optimize("O2"))) fast_color_add(uint32_t &c1, uint32_t c2, uint8_t scale) {
9090
if (c2 == BLACK) return; // adding black does nothing
9191
if (scale < 255) fast_color_scale(c2, scale); // scale added color
9292
if (c1 == BLACK) { c1 = c2; return; } // source is black, just assign c2
@@ -108,7 +108,7 @@ void fast_color_add(uint32_t &c1, uint32_t c2, uint8_t scale) {
108108
* fades color toward black
109109
* if using "video" method the resulting color will never become black unless it is already black
110110
*/
111-
uint32_t color_fade(uint32_t c1, uint8_t amount, bool video) {
111+
uint32_t __attribute__((optimize("O2"))) color_fade(uint32_t c1, uint8_t amount, bool video) {
112112
if (amount == 255) return c1; // no fading
113113
uint32_t addRemains = 0;
114114
if (video && amount) { // video scaling: make sure colors do not dim to zero if they started non-zero
@@ -126,7 +126,7 @@ uint32_t color_fade(uint32_t c1, uint8_t amount, bool video) {
126126
// Blending also occurs between the 16th and 1st elements when blendType is LINEARBLEND, producing wrap-around palette.
127127
// If you do not want wrap-around, use LINEARBLEND_NOWRAP which effectively reduces color entris count to 240.
128128
// If you do not want any blending at all, use NOBLEND which effectively reduces color entries count to 16.
129-
CRGBA ColorFromPaletteWLED(const CRGBPalette16& pal, uint8_t index, uint8_t brightness, TBlendType blendType) {
129+
CRGBA __attribute__((optimize("O2"))) ColorFromPaletteWLED(const CRGBPalette16& pal, uint8_t index, uint8_t brightness, TBlendType blendType) {
130130
if (blendType == LINEARBLEND_NOWRAP) {
131131
index = (index*241) >> 8; // Blend range is affected by lo4 blend of values, remap to avoid wrapping
132132
}

0 commit comments

Comments
 (0)