Skip to content

Commit 6ebcb64

Browse files
committed
Rolled power management back into the FastLED object directly - mark the old external power management functions as deprecated
1 parent 270932a commit 6ebcb64

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

FastLED.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ CFastLED::CFastLED() {
2323
// m_nControllers = 0;
2424
m_Scale = 255;
2525
m_nFPS = 0;
26+
m_pPowerFunc = NULL;
27+
m_nPowerData = 0xFFFFFFFF;
2628
}
2729

2830
CLEDController &CFastLED::addLeds(CLEDController *pLed,
@@ -42,6 +44,11 @@ void CFastLED::show(uint8_t scale) {
4244
while(m_nMinMicros && ((micros()-lastshow) < m_nMinMicros));
4345
lastshow = micros();
4446

47+
// If we have a function for computing power, use it!
48+
if(m_pPowerFunc) {
49+
scale = (*m_pPowerFunc)(scale, m_nPowerData);
50+
}
51+
4552
CLEDController *pCur = CLEDController::head();
4653
while(pCur) {
4754
uint8_t d = pCur->getDither();
@@ -200,10 +207,10 @@ void CFastLED::countFPS(int nFrames) {
200207
}
201208

202209
void CFastLED::setMaxRefreshRate(uint16_t refresh, bool constrain) {
203-
if(constrain) {
210+
if(constrain) {
204211
// if we're constraining, the new value of m_nMinMicros _must_ be higher than previously (because we're only
205212
// allowed to slow things down if constraining)
206-
if(refresh > 0) {
213+
if(refresh > 0) {
207214
m_nMinMicros = ( (1000000/refresh) > m_nMinMicros) ? (1000000/refresh) : m_nMinMicros;
208215
}
209216
} else if(refresh > 0) {

FastLED.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#define xstr(s) str(s)
88
#define str(s) #s
99

10-
#define FASTLED_VERSION 3001000
10+
#define FASTLED_VERSION 3001001
1111
#ifndef FASTLED_INTERNAL
12-
#warning FastLED version 3.001.000 (Not really a warning, just telling you here.)
12+
#warning FastLED version 3.001.001 (Not really a warning, just telling you here.)
1313
#endif
1414

1515
#ifndef __PROG_TYPES_COMPAT__
@@ -128,6 +128,8 @@ enum EBlockChipsets {
128128
#define NUM_CONTROLLERS 8
129129
#endif
130130

131+
typedef uint8_t (*power_func)(uint8_t scale, uint32_t data);
132+
131133
/// High level controller interface for FastLED. This class manages controllers, global settings and trackings
132134
/// such as brightness, and refresh rates, and provides access functions for driving led data to controllers
133135
/// via the show/showColor/clear methods.
@@ -137,6 +139,9 @@ class CFastLED {
137139
uint8_t m_Scale; ///< The current global brightness scale setting
138140
uint16_t m_nFPS; ///< Tracking for current FPS value
139141
uint32_t m_nMinMicros; ///< minimum µs between frames, used for capping frame rates.
142+
uint32_t m_nPowerData; ///< max power use parameter
143+
power_func m_pPowerFunc; ///< function for overriding brightness when using FastLED.show();
144+
140145
public:
141146
CFastLED();
142147

@@ -396,6 +401,15 @@ class CFastLED {
396401
/// @returns the current global brightness value
397402
uint8_t getBrightness() { return m_Scale; }
398403

404+
/// Set the maximum power to be used, given in volts and milliamps.
405+
/// @param volts - how many volts the leds are being driven at (usually 5 or 12)
406+
/// @param milliamps - the maximum milliamps of power draw you want
407+
inline void setMaxPowerInVoltsAndMilliamps(uint8_t volts, uint32_t milliamps) { setMaxPowerInMilliWatts(volts * milliamps); }
408+
409+
/// Set the maximum power to be used, given in milliwatts
410+
/// @param milliwatts - the max power draw desired, in milliwatts
411+
inline void setMaxPowerInMilliWatts(uint32_t milliwatts) { m_pPowerFunc = &calculate_max_brightness_for_power_mW; m_nPowerData = milliwatts; }
412+
399413
/// Update all our controllers with the current led colors, using the passed in brightness
400414
/// @param scale temporarily override the scale
401415
void show(uint8_t scale);

power_mgt.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ static const uint8_t gDark_mW = 1 * 5; // 1mA @ 5v = 5mW
4444
// Power consumed by the MCU
4545
static const uint8_t gMCU_mW = 25 * 5; // 25mA @ 5v = 125 mW
4646

47-
48-
static uint32_t gMaxPowerInMilliwatts = (uint32_t)(400) * (uint32_t)(5); // 400mA @ 5v default to avoid USB bricking
4947
static uint8_t gMaxPowerIndicatorLEDPinNumber = 0; // default = Arduino onboard LED pin. set to zero to skip this.
5048

5149

@@ -149,32 +147,23 @@ void set_max_power_indicator_LED( uint8_t pinNumber)
149147

150148
void set_max_power_in_volts_and_milliamps( uint8_t volts, uint32_t milliamps)
151149
{
152-
gMaxPowerInMilliwatts = (uint32_t)((uint32_t)(volts) * milliamps);
150+
FastLED.setMaxPowerInVoltsAndMilliamps(volts, milliamps);
153151
}
154152

155153
void set_max_power_in_milliwatts( uint32_t powerInmW)
156154
{
157-
gMaxPowerInMilliwatts = powerInmW;
155+
FastLED.setMaxPowerInMilliWatts(powerInmW);
158156
}
159157

160158
void show_at_max_brightness_for_power()
161159
{
162-
uint8_t targetBrightness = FastLED.getBrightness();
163-
uint8_t max = calculate_max_brightness_for_power_mW( targetBrightness, gMaxPowerInMilliwatts);
164-
165-
FastLED.setBrightness( max );
166-
FastLED.show();
167-
FastLED.setBrightness( targetBrightness );
160+
// power management usage is now in FastLED.show, no need for this function
161+
FastLED.show();
168162
}
169163

170164
void delay_at_max_brightness_for_power( uint16_t ms)
171165
{
172-
uint8_t targetBrightness = FastLED.getBrightness();
173-
uint8_t max = calculate_max_brightness_for_power_mW( targetBrightness, gMaxPowerInMilliwatts);
174-
175-
FastLED.setBrightness( max );
176-
FastLED.delay( ms);
177-
FastLED.setBrightness( targetBrightness );
166+
FastLED.delay(ms);
178167
}
179168

180169
FASTLED_NAMESPACE_END

power_mgt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ FASTLED_NAMESPACE_BEGIN
1515
//
1616

1717
/// Set the maximum power used in milliamps for a given voltage
18+
/// @deprecated - use FastLED.setMaxPowerInVoltsAndMilliamps()
1819
void set_max_power_in_volts_and_milliamps( uint8_t volts, uint32_t milliamps);
1920
/// Set the maximum power used in watts
2021
void set_max_power_in_milliwatts( uint32_t powerInmW);
2122

2223
/// Select a ping with an led that will be flashed to indicate that power management
2324
/// is pulling down the brightness
25+
/// @deprecated - use FastLED.setMaxPowerInMilliWatts
2426
void set_max_power_indicator_LED( uint8_t pinNumber); // zero = no indicator LED
2527

2628

@@ -38,9 +40,11 @@ void set_max_power_indicator_LED( uint8_t pinNumber); // zero = no indicator LED
3840

3941
/// Similar to FastLED.show, but pre-adjusts brightness to keep below the power
4042
/// threshold.
43+
/// @deprecated this has now been moved to FastLED.show();
4144
void show_at_max_brightness_for_power();
4245
/// Similar to FastLED.delay, but pre-adjusts brightness to keep below the power
4346
/// threshold.
47+
/// @deprecated this has now been rolled into FastLED.delay();
4448
void delay_at_max_brightness_for_power( uint16_t ms);
4549

4650

release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ FastLED3.1.1pre
44
* Fix edge case bug w/HSV palette blending
55
* Fix power management issue w/parallel output
66
* Use static_asserts for some more useful compile time errors around bad pins
7+
* Roll power management into FastLED.show/delay directly
78

89
FastLED3.1.0
910
============

0 commit comments

Comments
 (0)