FastLED 3.6.0
Loading...
Searching...
No Matches
power_mgt.cpp
Go to the documentation of this file.
1
3
5#define FASTLED_INTERNAL
6#include "FastLED.h"
7#include "power_mgt.h"
8
9FASTLED_NAMESPACE_BEGIN
10
11// POWER MANAGEMENT
12
30static const uint8_t gRed_mW = 16 * 5;
31static const uint8_t gGreen_mW = 11 * 5;
32static const uint8_t gBlue_mW = 15 * 5;
33static const uint8_t gDark_mW = 1 * 5;
35
36// Alternate calibration by RAtkins via pre-PSU wattage measurments;
37// these are all probably about 20%-25% too high due to PSU heat losses,
38// but if you're measuring wattage on the PSU input side, this may
39// be a better set of calibrations. (WS2812B)
40// static const uint8_t gRed_mW = 100;
41// static const uint8_t gGreen_mW = 48;
42// static const uint8_t gBlue_mW = 100;
43// static const uint8_t gDark_mW = 12;
44
45
48#define POWER_LED 1
49
51#define POWER_DEBUG_PRINT 0
52
53
54// Power consumed by the MCU
55static const uint8_t gMCU_mW = 25 * 5; // 25mA @ 5v = 125 mW
56
57static uint8_t gMaxPowerIndicatorLEDPinNumber = 0; // default = Arduino onboard LED pin. set to zero to skip this.
58
59
60uint32_t calculate_unscaled_power_mW( const CRGB* ledbuffer, uint16_t numLeds ) //25354
61{
62 uint32_t red32 = 0, green32 = 0, blue32 = 0;
63 const CRGB* firstled = &(ledbuffer[0]);
64 uint8_t* p = (uint8_t*)(firstled);
65
66 uint16_t count = numLeds;
67
68 // This loop might benefit from an AVR assembly version -MEK
69 while( count) {
70 red32 += *p++;
71 green32 += *p++;
72 blue32 += *p++;
73 --count;
74 }
75
76 red32 *= gRed_mW;
77 green32 *= gGreen_mW;
78 blue32 *= gBlue_mW;
79
80 red32 >>= 8;
81 green32 >>= 8;
82 blue32 >>= 8;
83
84 uint32_t total = red32 + green32 + blue32 + (gDark_mW * numLeds);
85
86 return total;
87}
88
89
90uint8_t calculate_max_brightness_for_power_vmA(const CRGB* ledbuffer, uint16_t numLeds, uint8_t target_brightness, uint32_t max_power_V, uint32_t max_power_mA) {
91 return calculate_max_brightness_for_power_mW(ledbuffer, numLeds, target_brightness, max_power_V * max_power_mA);
92}
93
94uint8_t calculate_max_brightness_for_power_mW(const CRGB* ledbuffer, uint16_t numLeds, uint8_t target_brightness, uint32_t max_power_mW) {
95 uint32_t total_mW = calculate_unscaled_power_mW( ledbuffer, numLeds);
96
97 uint32_t requested_power_mW = ((uint32_t)total_mW * target_brightness) / 256;
98
99 uint8_t recommended_brightness = target_brightness;
100 if(requested_power_mW > max_power_mW) {
101 recommended_brightness = (uint32_t)((uint8_t)(target_brightness) * (uint32_t)(max_power_mW)) / ((uint32_t)(requested_power_mW));
102 }
103
104 return recommended_brightness;
105}
106
107// sets brightness to
108// - no more than target_brightness
109// - no more than max_mW milliwatts
110uint8_t calculate_max_brightness_for_power_mW( uint8_t target_brightness, uint32_t max_power_mW)
111{
112 uint32_t total_mW = gMCU_mW;
113
115 while(pCur) {
116 total_mW += calculate_unscaled_power_mW( pCur->leds(), pCur->size());
117 pCur = pCur->next();
118 }
119
120#if POWER_DEBUG_PRINT == 1
121 Serial.print("power demand at full brightness mW = ");
122 Serial.println( total_mW);
123#endif
124
125 uint32_t requested_power_mW = ((uint32_t)total_mW * target_brightness) / 256;
126#if POWER_DEBUG_PRINT == 1
127 if( target_brightness != 255 ) {
128 Serial.print("power demand at scaled brightness mW = ");
129 Serial.println( requested_power_mW);
130 }
131 Serial.print("power limit mW = ");
132 Serial.println( max_power_mW);
133#endif
134
135 if( requested_power_mW < max_power_mW) {
136#if POWER_LED > 0
137 if( gMaxPowerIndicatorLEDPinNumber ) {
138 Pin(gMaxPowerIndicatorLEDPinNumber).lo(); // turn the LED off
139 }
140#endif
141#if POWER_DEBUG_PRINT == 1
142 Serial.print("demand is under the limit");
143#endif
144 return target_brightness;
145 }
146
147 uint8_t recommended_brightness = (uint32_t)((uint8_t)(target_brightness) * (uint32_t)(max_power_mW)) / ((uint32_t)(requested_power_mW));
148#if POWER_DEBUG_PRINT == 1
149 Serial.print("recommended brightness # = ");
150 Serial.println( recommended_brightness);
151
152 uint32_t resultant_power_mW = (total_mW * recommended_brightness) / 256;
153 Serial.print("resultant power demand mW = ");
154 Serial.println( resultant_power_mW);
155
156 Serial.println();
157#endif
158
159#if POWER_LED > 0
160 if( gMaxPowerIndicatorLEDPinNumber ) {
161 Pin(gMaxPowerIndicatorLEDPinNumber).hi(); // turn the LED on
162 }
163#endif
164
165 return recommended_brightness;
166}
167
168
169void set_max_power_indicator_LED( uint8_t pinNumber)
170{
171 gMaxPowerIndicatorLEDPinNumber = pinNumber;
172}
173
174void set_max_power_in_volts_and_milliamps( uint8_t volts, uint32_t milliamps)
175{
176 FastLED.setMaxPowerInVoltsAndMilliamps(volts, milliamps);
177}
178
179void set_max_power_in_milliwatts( uint32_t powerInmW)
180{
182}
183
185{
186 // power management usage is now in FastLED.show, no need for this function
187 FastLED.show();
188}
189
191{
192 FastLED.delay(ms);
193}
194
195FASTLED_NAMESPACE_END
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:17
central include file for FastLED, defines the CFastLED class/object
void delay(unsigned long ms)
Delay for the given number of milliseconds.
Definition FastLED.cpp:132
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:54
void setMaxPowerInVoltsAndMilliamps(uint8_t volts, uint32_t milliamps)
Set the maximum power to be used, given in volts and milliamps.
Definition FastLED.h:562
void setMaxPowerInMilliWatts(uint32_t milliwatts)
Set the maximum power to be used, given in milliwatts.
Definition FastLED.h:566
Base definition for an LED controller.
Definition controller.h:61
CLEDController * next()
Get the next controller in the linked list after this one.
Definition controller.h:145
virtual int size()
How many LEDs does this controller manage?
Definition controller.h:165
static CLEDController * head()
Get the first LED controller in the linked list of controllers.
Definition controller.h:141
CRGB * leds()
Pointer to the CRGB array for this controller.
Definition controller.h:173
Naive fallback solution for low level pin access.
Definition fastpin.h:38
void hi()
Set the pin state to HIGH
Definition fastpin.h:67
void lo()
Set the pin state to LOW
Definition fastpin.h:69
void set_max_power_in_milliwatts(uint32_t powerInmW)
Set the maximum power used in watts.
void show_at_max_brightness_for_power()
Similar to CFastLED::show(), but pre-adjusts brightness to keep below the power threshold.
void delay_at_max_brightness_for_power(uint16_t ms)
Similar to CFastLED::delay(), but pre-adjusts brightness to keep below the power threshold.
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:60
void set_max_power_in_volts_and_milliamps(uint8_t volts, uint32_t milliamps)
Set the maximum power used in milliamps for a given voltage.
void set_max_power_indicator_LED(uint8_t pinNumber)
Select a pin with an LED that will be flashed to indicate that power management is pulling down the b...
uint8_t calculate_max_brightness_for_power_vmA(const CRGB *ledbuffer, uint16_t numLeds, uint8_t target_brightness, uint32_t max_power_V, uint32_t max_power_mA)
Determines the highest brightness level you can use and still stay under the specified power budget f...
Definition power_mgt.cpp:90
uint8_t calculate_max_brightness_for_power_mW(const CRGB *ledbuffer, uint16_t numLeds, 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 f...
Definition power_mgt.cpp:94
Functions to limit the power used by FastLED.
Representation of an RGB pixel (Red, Green, Blue)
Definition pixeltypes.h:120