Skip to content

Commit

Permalink
ws2812 mix: add all, then divide
Browse files Browse the repository at this point in the history
This achieves rounding between multiple summed frame buffers
  • Loading branch information
Nathaniel Wesley Filardo committed Sep 30, 2017
1 parent 3e60fa8 commit 3a926e8
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/modules/ws2812.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,19 @@ static int ws2812_buffer_mix(lua_State* L) {

size_t i;
for (i = 0; i < cells; i++) {
int val = 0;
int32_t val = 0;
for (src = 0; src < n_sources; src++) {
val += ((int)(source[src].values[i] * source[src].factor) >> 8);
val += (int32_t)(source[src].values[i] * source[src].factor);
}

val >>= 8;

if (val < 0) {
val = 0;
} else if (val > 255) {
val = 255;
}
buffer->values[i] = val;
buffer->values[i] = (uint8_t)val;
}

return 0;
Expand Down

0 comments on commit 3a926e8

Please sign in to comment.