FastLED 3.10.10
Loading...
Searching...
No Matches

◆ brightness_within_budget()

static fl::u8 brightness_within_budget ( fl::u32 fixed_mW,
fl::u32 controllable_mW,
fl::u8 target_brightness,
fl::u32 max_power_mW )
static

Definition at line 434 of file power_mgt.cpp.hpp.

436 {
437 const fl::u32 requested_mW =
438 fixed_mW + scale_power_for_brightness(controllable_mW, target_brightness);
439 if (requested_mW <= max_power_mW) {
440 return target_brightness;
441 }
442 if (controllable_mW == 0) {
443 // Nothing left to scale: every brightness draws the same baseline.
444 // Also keeps the division below from dividing by zero.
445 return 0;
446 }
447 if (max_power_mW <= fixed_mW) {
448 // The baseline alone meets or exceeds the budget: no brightness fits,
449 // and the budget is an upper bound. Dark is also the monotone answer,
450 // since just above the baseline the headroom buys almost nothing.
451 // Also keeps the subtraction below from underflowing.
452 FL_WARN_EVERY(1000, "power: budget " << max_power_mW
453 << " mW is at or under the " << fixed_mW
454 << " mW baseline (MCU + LED dark current); LEDs blanked."
455 << " Raise the budget or set FL_MCU_POWER_MW.");
456 return 0;
457 }
458 // The ratio is taken over the whole 0-255 scaled range, because
459 // `controllable_mW` is the demand at *full* brightness. Scaling it by the
460 // requested brightness first would answer "what fraction of the request
461 // fits", which is smaller than "what brightness fits" by exactly that
462 // fraction -- and the step-down below only ever decreases, so it cannot
463 // recover it. The cap is where the request comes back in.
464 //
465 // The cap cannot actually fire from here: the caller only reaches this
466 // branch when the request is already over budget, which is the same
467 // inequality. It is kept because that argument runs through
468 // `map_power_value`'s rounding, and a helper that clamps to its own
469 // argument is cheaper than depending on that.
470 const fl::u32 headroom_mW = max_power_mW - fixed_mW;
471 const fl::u8 target_scaled = map_power_value(target_brightness);
472 fl::u32 allowed_scaled = power_ratio_of_255(headroom_mW, controllable_mW);
473 if (allowed_scaled > target_scaled) {
474 allowed_scaled = target_scaled;
475 }
476 const fl::u32 recommended_scaled = static_cast<fl::u32>(allowed_scaled);
477 fl::u8 recommended = unmap_power_value(static_cast<fl::u8>(recommended_scaled));
478
479 // Then check the answer instead of trusting the division. Inverting the
480 // ratio in scaled space is off by the rounding of whichever `scale32by8`
481 // the platform compiled -- the FASTLED_SCALE8_FIXED form computes
482 // `i * (s + 1) >> 8`, so the closed form lands about `controllable/256`
483 // over the budget, which was 176 mW of a 5000 mW budget on the default
484 // model. Demand is monotone in brightness, so stepping down until it fits
485 // is exact whatever the rounding, and terminates: 0 always fits here,
486 // because the branch above already returned for a budget at or under
487 // baseline. The result is the largest brightness whose modeled draw fits,
488 // so it never exceeds the budget and never falls as the budget rises.
489 while (recommended > 0 &&
490 fixed_mW + scale_power_for_brightness(controllable_mW, recommended) >
491 max_power_mW) {
492 --recommended;
493 }
494 return recommended;
495}
fl::u32 scale_power_for_brightness(fl::u32 total_mW, fl::u8 brightness) FL_NO_EXCEPT
Applies the configured power-scaling response to a total power value.
fl::u32 power_ratio_of_255(fl::u32 num, fl::u32 den) FL_NO_EXCEPT
min(255, floor(255 * num / den)) for den > 0, computed in 32-bit arithmetic.
#define FL_WARN_EVERY(MILLIS,...)
Definition log.h:442
unsigned char u8
Definition stdint.h:132
static fl::u8 map_power_value(fl::u8 brightness) FL_NO_EXCEPT
static fl::u8 unmap_power_value(fl::u8 scaled_brightness) FL_NO_EXCEPT

References FL_NO_EXCEPT, FL_WARN_EVERY, map_power_value(), power_ratio_of_255(), scale_power_for_brightness(), and unmap_power_value().

Referenced by calculate_max_brightness_for_power_mW(), and calculate_max_brightness_for_power_mW().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: