From b16cda106b6df80b666c0194f02419bacbdd93b8 Mon Sep 17 00:00:00 2001 From: Bart Nagel Date: Thu, 18 Oct 2018 15:21:06 -0700 Subject: [PATCH 1/3] Tweak blazing trail decay - Rewrite decay code to fade brightness smoothly with an x^4 relationship - Fade hue linearly from orange to red rather than for a subset of time from red to orange Signed-off-by: Bart Nagel --- src/kaleidoscope/plugin/LED-Stalker.cpp | 29 +++++++++++++------------ 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/kaleidoscope/plugin/LED-Stalker.cpp b/src/kaleidoscope/plugin/LED-Stalker.cpp index e18598f0..c913e896 100644 --- a/src/kaleidoscope/plugin/LED-Stalker.cpp +++ b/src/kaleidoscope/plugin/LED-Stalker.cpp @@ -105,27 +105,28 @@ cRGB Haunt::compute(uint8_t *step) { // BlazingTrail BlazingTrail::BlazingTrail(void) { } +uint8_t hue_start = 50.0 / 360 * 0xff; +uint8_t hue_end = 0; cRGB BlazingTrail::compute(uint8_t *step) { cRGB color; - if (*step >= 0xff - 30) { - color = hsvToRgb(0xff - *step, 255, 255); - } else { - color = hsvToRgb(30, 255, 255); + // Get a float from 0 to 1 for our position in the animation + float pos = (0xff - *step) / (float) 0xff; - color.r = min(*step * color.r / 255, 255); - color.g = min(*step * color.g / 255, 255); - } + // Fade hue linearly from orange to red + uint8_t hue = hue_start + pos * (hue_end - hue_start); - if (*step >= 0xff - 30) - *step -= 1; - else if (*step >= 0x40) - *step -= 16; - else if (*step >= 32) - *step -= 32; - else + // Fade value from full following a -x^4+1 curve + uint8_t val = 0xff * (1 - pos * pos * pos * pos); + + color = hsvToRgb(hue, 0xff, val); + + if (*step < 4) { *step = 0; + } else { + *step -= 4; + } return color; } From 9a78f8f887bb0fc7ab9c6f66cd32c851bec2ffde Mon Sep 17 00:00:00 2001 From: Bart Nagel Date: Sun, 22 Dec 2019 16:57:38 -0800 Subject: [PATCH 2/3] Eliminate floating-point logic Thanks to @noseglasses for the guidance! Signed-off-by: Bart Nagel --- src/kaleidoscope/plugin/LED-Stalker.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/kaleidoscope/plugin/LED-Stalker.cpp b/src/kaleidoscope/plugin/LED-Stalker.cpp index c913e896..3705947a 100644 --- a/src/kaleidoscope/plugin/LED-Stalker.cpp +++ b/src/kaleidoscope/plugin/LED-Stalker.cpp @@ -111,14 +111,18 @@ uint8_t hue_end = 0; cRGB BlazingTrail::compute(uint8_t *step) { cRGB color; - // Get a float from 0 to 1 for our position in the animation - float pos = (0xff - *step) / (float) 0xff; + // Get the position in the animation, where 0 is start and 0xff is end + uint8_t pos255 = 0xff - *step; // Fade hue linearly from orange to red - uint8_t hue = hue_start + pos * (hue_end - hue_start); - - // Fade value from full following a -x^4+1 curve - uint8_t val = 0xff * (1 - pos * pos * pos * pos); + uint8_t hue = hue_start + (pos255 * (hue_end - hue_start) >> 8); + + // Fade value from full following a 1-x^4 curve + uint8_t val = + 0xff // Maximum brightness + - ((uint32_t) pos255 * pos255 * pos255 * pos255 // Animation position to 4th power + >> 24) // ...pulled down to 8-bit range (but this has a maximum of 0xfc rather than 0xff) + - pos255 / (0x100 / 4); // Correction to bring the end result into a full 0 to 0xff range color = hsvToRgb(hue, 0xff, val); From 39a9f8700bfea990b8b3f65a4184e2597a762210 Mon Sep 17 00:00:00 2001 From: Bart Nagel Date: Sun, 22 Dec 2019 23:47:40 -0800 Subject: [PATCH 3/3] Mark constants as constexpr Signed-off-by: Bart Nagel --- src/kaleidoscope/plugin/LED-Stalker.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kaleidoscope/plugin/LED-Stalker.cpp b/src/kaleidoscope/plugin/LED-Stalker.cpp index 3705947a..9a59f93f 100644 --- a/src/kaleidoscope/plugin/LED-Stalker.cpp +++ b/src/kaleidoscope/plugin/LED-Stalker.cpp @@ -105,8 +105,8 @@ cRGB Haunt::compute(uint8_t *step) { // BlazingTrail BlazingTrail::BlazingTrail(void) { } -uint8_t hue_start = 50.0 / 360 * 0xff; -uint8_t hue_end = 0; +constexpr uint8_t hue_start = 50.0 / 360 * 0xff; +constexpr uint8_t hue_end = 0; cRGB BlazingTrail::compute(uint8_t *step) { cRGB color;