Skip to content

Commit

Permalink
fixed implementation of brightness
Browse files Browse the repository at this point in the history
with the previous implementation, when brightness reached 0 the color
was lost, and when the brightness was increased or decreased to the
extremes the color could change slightly.

This new implementation fixes this by keeping the color as a base
color. And then adjusting the brightness with a separate value.

This preserves the exact color value even at 0 brightness.
  • Loading branch information
peterzuger committed Dec 3, 2020
1 parent e08df2b commit 3d8353a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tlc5947/tlc5947.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ typedef struct _pattern_base_t{
int16_t stack[MAX_STACK];
uint8_t pos;
}stack;
float brightness; // brightness from 0 -> 1
rgb12 color; // the led setting algorythm used this color,
// it is pre calculated every tick
bool visible;
Expand Down Expand Up @@ -382,6 +383,7 @@ static bool pattern_do_tick(tlc5947_tlc5947_obj_t* self, pattern_base_t* pattern
case pCOLOR:{ // change color
tprintf("pCOLOR\n\r");
pattern->color = p->color.color;
pattern->brightness = 1.0F;
self->data.changed = true;
pattern->current++;
if(pattern->current == pattern->len)
Expand Down Expand Up @@ -417,10 +419,8 @@ static bool pattern_do_tick(tlc5947_tlc5947_obj_t* self, pattern_base_t* pattern

case pBRIGHTNESS:{ // change overall brightness
tprintf("pBRIGHTNESS\n\r");
hsv c = rgbtohsv(rgb12torgb(pattern->color));
c.v = clamp(c.v + p->brighness.brightness, 0.0F, 1.0F);
pattern->color = rgbtorgb12(hsvtorgb(c));
self->data.changed = true;
pattern->brightness = clamp(pattern->brightness + p->brighness.brightness, 0.0F, 1.0F);
pattern->current++;
if(pattern->current == pattern->len)
return true; // pattern is done, no more tokens
Expand Down Expand Up @@ -596,6 +596,14 @@ static bool do_tick(tlc5947_tlc5947_obj_t* self){
for(uint16_t j = 0; j < self->data.patterns.len; j++){
if(self->data.patterns.list[j].id == pid){
color = self->data.patterns.list[j].color;

if(self->data.patterns.list[j].brightness != 1.0F){
float b = self->data.patterns.list[j].brightness;
color.r = (uint16_t)((float)color.r * b);
color.g = (uint16_t)((float)color.g * b);
color.b = (uint16_t)((float)color.b * b);
}

if(self->data.patterns.list[j].visible || (pid_pos == 0))
done = true;
--pid_pos;
Expand Down

0 comments on commit 3d8353a

Please sign in to comment.