FastLED 3.9.3
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#include "namespace.h"
9
10FASTLED_NAMESPACE_BEGIN
11
12// POWER MANAGEMENT
13
31static const uint8_t gRed_mW = 16 * 5;
32static const uint8_t gGreen_mW = 11 * 5;
33static const uint8_t gBlue_mW = 15 * 5;
34static const uint8_t gDark_mW = 1 * 5;
36
37// Alternate calibration by RAtkins via pre-PSU wattage measurments;
38// these are all probably about 20%-25% too high due to PSU heat losses,
39// but if you're measuring wattage on the PSU input side, this may
40// be a better set of calibrations. (WS2812B)
41// static const uint8_t gRed_mW = 100;
42// static const uint8_t gGreen_mW = 48;
43// static const uint8_t gBlue_mW = 100;
44// static const uint8_t gDark_mW = 12;
45
46
49#define POWER_LED 1
50
52#define POWER_DEBUG_PRINT 0
53
54
55// Power consumed by the MCU
56static const uint8_t gMCU_mW = 25 * 5; // 25mA @ 5v = 125 mW
57
58static uint8_t gMaxPowerIndicatorLEDPinNumber = 0; // default = Arduino onboard LED pin. set to zero to skip this.
59
60
61uint32_t calculate_unscaled_power_mW( const CRGB* ledbuffer, uint16_t numLeds ) //25354
62{
63 uint32_t red32 = 0, green32 = 0, blue32 = 0;
64 const CRGB* firstled = &(ledbuffer[0]);
65 uint8_t* p = (uint8_t*)(firstled);
66
67 uint16_t count = numLeds;
68
69 // This loop might benefit from an AVR assembly version -MEK
70 while( count) {
71 red32 += *p++;
72 green32 += *p++;
73 blue32 += *p++;
74 --count;
75 }
76
77 red32 *= gRed_mW;
78 green32 *= gGreen_mW;
79 blue32 *= gBlue_mW;
80
81 red32 >>= 8;
82 green32 >>= 8;
83 blue32 >>= 8;
84
85 uint32_t total = red32 + green32 + blue32 + (gDark_mW * numLeds);
86
87 return total;
88}
89
90
91uint8_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) {
92 return calculate_max_brightness_for_power_mW(ledbuffer, numLeds, target_brightness, max_power_V * max_power_mA);
93}
94
95uint8_t calculate_max_brightness_for_power_mW(const CRGB* ledbuffer, uint16_t numLeds, uint8_t target_brightness, uint32_t max_power_mW) {
96 uint32_t total_mW = calculate_unscaled_power_mW( ledbuffer, numLeds);
97
98 uint32_t requested_power_mW = ((uint32_t)total_mW * target_brightness) / 256;
99
100 uint8_t recommended_brightness = target_brightness;
101 if(requested_power_mW > max_power_mW) {
102 recommended_brightness = (uint32_t)((uint8_t)(target_brightness) * (uint32_t)(max_power_mW)) / ((uint32_t)(requested_power_mW));
103 }
104
105 return recommended_brightness;
106}
107
108// sets brightness to
109// - no more than target_brightness
110// - no more than max_mW milliwatts
111uint8_t calculate_max_brightness_for_power_mW( uint8_t target_brightness, uint32_t max_power_mW)
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
138 if( gMaxPowerIndicatorLEDPinNumber ) {
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
161 if( gMaxPowerIndicatorLEDPinNumber ) {
162 Pin(gMaxPowerIndicatorLEDPinNumber).hi(); // turn the LED on
163 }
164#endif
165
166 return recommended_brightness;
167}
168
169
170void set_max_power_indicator_LED( uint8_t pinNumber)
171{
172 gMaxPowerIndicatorLEDPinNumber = pinNumber;
173}
174
175void set_max_power_in_volts_and_milliamps( uint8_t volts, uint32_t milliamps)
176{
177 FastLED.setMaxPowerInVoltsAndMilliamps(volts, milliamps);
178}
179
180void set_max_power_in_milliwatts( uint32_t powerInmW)
181{
183}
184
186{
187 // power management usage is now in FastLED.show, no need for this function
188 FastLED.show();
189}
190
192{
193 FastLED.delay(ms);
194}
195
196FASTLED_NAMESPACE_END
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:33
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:178
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:82
void setMaxPowerInVoltsAndMilliamps(uint8_t volts, uint32_t milliamps)
Set the maximum power to be used, given in volts and milliamps.
Definition FastLED.h:731
void setMaxPowerInMilliWatts(uint32_t milliwatts)
Set the maximum power to be used, given in milliwatts.
Definition FastLED.h:735
Base definition for an LED controller.
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.
Naive fallback solution for low level pin access.
Definition fastpin.h:43
void hi()
Set the pin state to HIGH
Definition fastpin.h:72
void lo()
Set the pin state to LOW
Definition fastpin.h:74
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:61
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:91
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:95
Functions to limit the power used by FastLED.
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39