Only update LED strip when necessary

This commit is contained in:
Thomas Kolb 2015-10-10 15:35:02 +02:00
parent 0518f0e184
commit fc33207e8f
1 changed files with 25 additions and 5 deletions

View File

@ -11,6 +11,8 @@ uint8_t numModules;
float fadestep = 1;
double nextFrame;
int somethingChanged = 0; // indicates when a ws2801 update is required
static const double interval = 0.01f;
struct Colour {
@ -59,6 +61,8 @@ void fader_set_colour(uint8_t module, uint8_t r, uint8_t g, uint8_t b)
curColour[module].red = targetColour[module].red = r;
curColour[module].green = targetColour[module].green = g;
curColour[module].blue = targetColour[module].blue = b;
somethingChanged = 1;
}
void fader_fade_colour(uint8_t module, uint8_t r, uint8_t g, uint8_t b)
@ -85,6 +89,8 @@ void fader_add_colour(uint8_t module, uint8_t r, uint8_t g, uint8_t b)
if(targetColour[module].red > 255) { targetColour[module].red = 255; }
if(targetColour[module].green > 255) { targetColour[module].green = 255; }
if(targetColour[module].blue > 255) { targetColour[module].blue = 255; }
somethingChanged = 1;
}
void fader_set_fadestep(uint8_t newFadestep)
@ -94,7 +100,14 @@ void fader_set_fadestep(uint8_t newFadestep)
fadestep = (float)newFadestep * interval / 0.04f;
}
void fade_colour(float *cur, const float *target)
/*!
* Fade the colour value in cur towards target.
*
* \param cur The colour value to update.
* \param target The target value that should be reached.
* \param changed Output value which is set to 1 if cur was changed.
*/
void fade_colour(float *cur, const float *target, int *changed)
{
float diff;
if(*cur > *target) {
@ -104,6 +117,8 @@ void fade_colour(float *cur, const float *target)
} else {
*cur -= fadestep;
}
*changed = 1;
} else if(*cur < *target) {
diff = *target - *cur;
if(diff < fadestep) {
@ -111,15 +126,17 @@ void fade_colour(float *cur, const float *target)
} else {
*cur += fadestep;
}
*changed = 1;
}
}
void fader_update(void)
{
for(uint8_t i = 0; i < numModules; i++) {
fade_colour(&(curColour[i].red), &(targetColour[i].red));
fade_colour(&(curColour[i].green), &(targetColour[i].green));
fade_colour(&(curColour[i].blue), &(targetColour[i].blue));
fade_colour(&(curColour[i].red), &(targetColour[i].red), &somethingChanged);
fade_colour(&(curColour[i].green), &(targetColour[i].green), &somethingChanged);
fade_colour(&(curColour[i].blue), &(targetColour[i].blue), &somethingChanged);
ws2801_set_colour(i,
(uint8_t)curColour[i].red,
@ -127,7 +144,10 @@ void fader_update(void)
(uint8_t)curColour[i].blue);
}
ws2801_send_update();
if(somethingChanged) {
ws2801_send_update();
somethingChanged = 0;
}
}
void fader_wait_frame(void)