FastLED 3.10.6
Loading...
Searching...
No Matches
auto_brightness.cpp
Go to the documentation of this file.
1// auto_brightness.cpp
2//
3// NOTE: a twin of this file lives in examples/MoodRing/auto_brightness.cpp.
4// Examples cannot include across directories, so the file is duplicated.
5// Fix both, or neither.
6#include "auto_brightness.h"
7
9 if (leds.empty()) {
10 return 0.0f;
11 }
12 uint32_t total = 0;
13 for (const CRGB &led : leds) {
14 total += led.r + led.g + led.b;
15 }
16 float avgValue = float(total) / float(leds.size() * 3);
17 return (avgValue / 255.0f) * 100.0f;
18}
19
20uint8_t applyBrightnessCompression(float inputBrightnessPercent,
21 uint8_t maxBrightness, float lowThreshold,
22 float highThreshold) {
23 float maxBrightnessPercent = (maxBrightness / 255.0f) * 100.0f;
24 if (inputBrightnessPercent < lowThreshold) {
25 return 255;
26 } else if (inputBrightnessPercent < highThreshold) {
27 float range = highThreshold - lowThreshold;
28 float progress = (inputBrightnessPercent - lowThreshold) / range;
29 float targetPercent =
30 100.0f - (progress * (100.0f - maxBrightnessPercent));
31 return static_cast<uint8_t>((targetPercent / 100.0f) * 255.0f);
32 } else {
33 return maxBrightness;
34 }
35}
CRGB leds[1]
uint8_t applyBrightnessCompression(float inputBrightnessPercent, uint8_t maxBrightness, float lowThreshold, float highThreshold)
float getAverageBrightness(fl::span< const CRGB > leds)
fl::CRGB CRGB
Definition crgb.h:25
unsigned char uint8_t
Definition stdint.h:208
fl::u32 uint32_t
Definition stdint.h:218