FastLED 3.9.15
Loading...
Searching...
No Matches

◆ calculate_max_brightness_for_power_mW() [2/2]

fl::u8 calculate_max_brightness_for_power_mW ( fl::u8 target_brightness,
fl::u32 max_power_mW )

Determines the highest brightness level you can use and still stay under the specified power budget for all sets of LEDs.

Unlike the other internal power functions which use a pointer to a specific set of LED data, this function uses the CFastLED linked list of LED controllers and their attached data.

Parameters
target_brightnessthe brightness you'd ideally like to use
max_power_mWthe max power draw desired, in milliwatts
Returns
a limited brightness value. No higher than the target brightness, but may be lower depending on the power limit.

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

211{
212 fl::u32 total_mW = gMCU_mW;
213
215 while(pCur) {
216 total_mW += calculate_unscaled_power_mW( pCur->leds(), pCur->size());
217 pCur = pCur->next();
218 }
219
220#if POWER_DEBUG_PRINT == 1
221 Serial.print("power demand at full brightness mW = ");
222 Serial.println( total_mW);
223#endif
224
225 fl::u8 target_brightness_scaled = map_power_value(target_brightness);
226 fl::u32 requested_power_mW = scale_power_for_brightness(total_mW, target_brightness);
227#if POWER_DEBUG_PRINT == 1
228 if( target_brightness != 255 ) {
229 Serial.print("power demand at scaled brightness mW = ");
230 Serial.println( requested_power_mW);
231 }
232 Serial.print("power limit mW = ");
233 Serial.println( max_power_mW);
234#endif
235
236 if( requested_power_mW < max_power_mW) {
237#if POWER_LED > 0
239 Pin(gMaxPowerIndicatorLEDPinNumber).lo(); // turn the LED off
240 }
241#endif
242#if POWER_DEBUG_PRINT == 1
243 Serial.print("demand is under the limit");
244#endif
245 return target_brightness;
246 }
247
248 fl::u8 recommended_scaled = (fl::u32)(target_brightness_scaled * (fl::u32)(max_power_mW)) / ((fl::u32)(requested_power_mW));
249 fl::u8 recommended_brightness = unmap_power_value(recommended_scaled);
250#if POWER_DEBUG_PRINT == 1
251 Serial.print("recommended brightness # = ");
252 Serial.println( recommended_brightness);
253
254 fl::u32 resultant_power_mW = scale_power_for_brightness(total_mW, recommended_brightness);
255 Serial.print("resultant power demand mW = ");
256 Serial.println( resultant_power_mW);
257
258 Serial.println();
259#endif
260
261#if POWER_LED > 0
263 Pin(gMaxPowerIndicatorLEDPinNumber).hi(); // turn the LED on
264 }
265#endif
266
267 return recommended_brightness;
268}
CLEDController * next() FL_NOEXCEPT
Get the next controller in the linked list after this one.
CRGB * leds() FL_NOEXCEPT
Pointer to the CRGB array for this controller.
virtual int size() const FL_NOEXCEPT
How many LEDs does this controller manage?
static CLEDController * head() FL_NOEXCEPT
Get the first LED controller in the linked list of controllers.
fl::CLEDController CLEDController
fl::u32 scale_power_for_brightness(fl::u32 total_mW, fl::u8 brightness)
Applies the configured power-scaling response to a total power value.
fl::u32 calculate_unscaled_power_mW(fl::span< const CRGB > leds)
unsigned char u8
Definition stdint.h:131
static fl::u8 unmap_power_value(fl::u8 scaled_brightness)
static fl::u8 gMaxPowerIndicatorLEDPinNumber
static fl::u8 map_power_value(fl::u8 brightness)
static const fl::u8 gMCU_mW
#define Serial
Definition serial.h:304

References calculate_unscaled_power_mW(), gMaxPowerIndicatorLEDPinNumber, gMCU_mW, fl::CLEDController::head(), fl::CLEDController::leds(), map_power_value(), fl::CLEDController::next(), scale_power_for_brightness(), Serial, fl::CLEDController::size(), and unmap_power_value().

+ Here is the call graph for this function: