From 5c8619eb4de57834d1bfda3ab2574c99802b0d1c Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sun, 22 Oct 2017 13:20:45 -0400 Subject: [PATCH] ws2812 mix: add all, then divide (#2032) This achieves rounding between multiple summed frame buffers --- app/modules/ws2812.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/modules/ws2812.c b/app/modules/ws2812.c index 869cbcb0..88b71970 100644 --- a/app/modules/ws2812.c +++ b/app/modules/ws2812.c @@ -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;