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

◆ calculate_max_brightness_for_power_mW() [2/2]

uint8_t calculate_max_brightness_for_power_mW ( uint8_t target_brightness,
uint32_t 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 111 of file power_mgt.cpp.

112{
113 uint32_t total_mW = gMCU_mW;
114
116 while(pCur) {
117 total_mW += calculate_unscaled_power_mW( pCur->leds(), pCur->size());
118 pCur = pCur->next();
119 }
120
121#if POWER_DEBUG_PRINT == 1
122 Serial.print("power demand at full brightness mW = ");
123 Serial.println( total_mW);
124#endif
125
126 uint32_t requested_power_mW = ((uint32_t)total_mW * target_brightness) / 256;
127#if POWER_DEBUG_PRINT == 1
128 if( target_brightness != 255 ) {
129 Serial.print("power demand at scaled brightness mW = ");
130 Serial.println( requested_power_mW);
131 }
132 Serial.print("power limit mW = ");
133 Serial.println( max_power_mW);
134#endif
135
136 if( requested_power_mW < max_power_mW) {
137#if POWER_LED > 0
139 Pin(gMaxPowerIndicatorLEDPinNumber).lo(); // turn the LED off
140 }
141#endif
142#if POWER_DEBUG_PRINT == 1
143 Serial.print("demand is under the limit");
144#endif
145 return target_brightness;
146 }
147
148 uint8_t recommended_brightness = (uint32_t)((uint8_t)(target_brightness) * (uint32_t)(max_power_mW)) / ((uint32_t)(requested_power_mW));
149#if POWER_DEBUG_PRINT == 1
150 Serial.print("recommended brightness # = ");
151 Serial.println( recommended_brightness);
152
153 uint32_t resultant_power_mW = (total_mW * recommended_brightness) / 256;
154 Serial.print("resultant power demand mW = ");
155 Serial.println( resultant_power_mW);
156
157 Serial.println();
158#endif
159
160#if POWER_LED > 0
162 Pin(gMaxPowerIndicatorLEDPinNumber).hi(); // turn the LED on
163 }
164#endif
165
166 return recommended_brightness;
167}
CLEDController * next()
Get the next controller in the linked list after this one.
virtual int size()
How many LEDs does this controller manage?
static CLEDController * head()
Get the first LED controller in the linked list of controllers.
CRGB * leds()
Pointer to the CRGB array for this controller.
Base definition for an LED controller.
void hi()
Set the pin state to HIGH
Definition fastpin.h:79
void lo()
Set the pin state to LOW
Definition fastpin.h:81
Naive fallback solution for low level pin access.
Definition fastpin.h:47
uint32_t calculate_unscaled_power_mW(const CRGB *ledbuffer, uint16_t numLeds)
Determines how many milliwatts the current LED data would draw at max brightness (255)
Definition power_mgt.cpp:61
static uint8_t gMaxPowerIndicatorLEDPinNumber
Definition power_mgt.cpp:58
static const uint8_t gMCU_mW
Definition power_mgt.cpp:56

References calculate_unscaled_power_mW(), gMaxPowerIndicatorLEDPinNumber, gMCU_mW, CLEDController::head(), Pin::hi(), CLEDController::leds(), Pin::lo(), CLEDController::next(), and CLEDController::size().

+ Here is the call graph for this function: