Only update LED strip when necessary
This commit is contained in:
parent
0518f0e184
commit
fc33207e8f
28
src/fader.c
28
src/fader.c
|
@ -11,6 +11,8 @@ uint8_t numModules;
|
||||||
float fadestep = 1;
|
float fadestep = 1;
|
||||||
double nextFrame;
|
double nextFrame;
|
||||||
|
|
||||||
|
int somethingChanged = 0; // indicates when a ws2801 update is required
|
||||||
|
|
||||||
static const double interval = 0.01f;
|
static const double interval = 0.01f;
|
||||||
|
|
||||||
struct Colour {
|
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].red = targetColour[module].red = r;
|
||||||
curColour[module].green = targetColour[module].green = g;
|
curColour[module].green = targetColour[module].green = g;
|
||||||
curColour[module].blue = targetColour[module].blue = b;
|
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)
|
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].red > 255) { targetColour[module].red = 255; }
|
||||||
if(targetColour[module].green > 255) { targetColour[module].green = 255; }
|
if(targetColour[module].green > 255) { targetColour[module].green = 255; }
|
||||||
if(targetColour[module].blue > 255) { targetColour[module].blue = 255; }
|
if(targetColour[module].blue > 255) { targetColour[module].blue = 255; }
|
||||||
|
|
||||||
|
somethingChanged = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fader_set_fadestep(uint8_t newFadestep)
|
void fader_set_fadestep(uint8_t newFadestep)
|
||||||
|
@ -94,7 +100,14 @@ void fader_set_fadestep(uint8_t newFadestep)
|
||||||
fadestep = (float)newFadestep * interval / 0.04f;
|
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;
|
float diff;
|
||||||
if(*cur > *target) {
|
if(*cur > *target) {
|
||||||
|
@ -104,6 +117,8 @@ void fade_colour(float *cur, const float *target)
|
||||||
} else {
|
} else {
|
||||||
*cur -= fadestep;
|
*cur -= fadestep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*changed = 1;
|
||||||
} else if(*cur < *target) {
|
} else if(*cur < *target) {
|
||||||
diff = *target - *cur;
|
diff = *target - *cur;
|
||||||
if(diff < fadestep) {
|
if(diff < fadestep) {
|
||||||
|
@ -111,15 +126,17 @@ void fade_colour(float *cur, const float *target)
|
||||||
} else {
|
} else {
|
||||||
*cur += fadestep;
|
*cur += fadestep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*changed = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void fader_update(void)
|
void fader_update(void)
|
||||||
{
|
{
|
||||||
for(uint8_t i = 0; i < numModules; i++) {
|
for(uint8_t i = 0; i < numModules; i++) {
|
||||||
fade_colour(&(curColour[i].red), &(targetColour[i].red));
|
fade_colour(&(curColour[i].red), &(targetColour[i].red), &somethingChanged);
|
||||||
fade_colour(&(curColour[i].green), &(targetColour[i].green));
|
fade_colour(&(curColour[i].green), &(targetColour[i].green), &somethingChanged);
|
||||||
fade_colour(&(curColour[i].blue), &(targetColour[i].blue));
|
fade_colour(&(curColour[i].blue), &(targetColour[i].blue), &somethingChanged);
|
||||||
|
|
||||||
ws2801_set_colour(i,
|
ws2801_set_colour(i,
|
||||||
(uint8_t)curColour[i].red,
|
(uint8_t)curColour[i].red,
|
||||||
|
@ -127,7 +144,10 @@ void fader_update(void)
|
||||||
(uint8_t)curColour[i].blue);
|
(uint8_t)curColour[i].blue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(somethingChanged) {
|
||||||
ws2801_send_update();
|
ws2801_send_update();
|
||||||
|
somethingChanged = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void fader_wait_frame(void)
|
void fader_wait_frame(void)
|
||||||
|
|
Loading…
Reference in a new issue