aboutsummaryrefslogtreecommitdiff
path: root/src/patterns/twinkle.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/patterns/twinkle.c')
-rw-r--r--src/patterns/twinkle.c148
1 files changed, 119 insertions, 29 deletions
diff --git a/src/patterns/twinkle.c b/src/patterns/twinkle.c
index 564a30a..0c3f985 100644
--- a/src/patterns/twinkle.c
+++ b/src/patterns/twinkle.c
@@ -1,54 +1,144 @@
#include <stdio.h>
+
#include "byte_math.h"
#include "pattern.h"
-static uint8_t pseudo_random(uint8_t x) {
- return (1103515245 * x + 12345) & 0xFF; // & 0xFF ensures the result is 0-255
+static uint8_t pseudo_random(uint8_t x)
+{
+ return (1103515245 * x + 12345) & 0xFF; // & 0xFF ensures the result is 0-255
+}
+
+rgb_t twinkle(uint32_t time, size_t x)
+{
+ uint8_t time_offset = pseudo_random(x);
+
+ uint32_t adj_time = time - time_offset;
+ uint32_t time8 = adj_time & 0xff;
+ uint32_t speed = pseudo_random((adj_time >> 8) + x) & 0x3;
+
+ time8 *= speed;
+ uint8_t w = 0;
+ if (time8 <= 255) {
+ w = calc_w(time8);
+ } else {
+ w = 0;
+ }
+
+ rgb_t r;
+ r.r = 0xff;
+ r.g = 0xff;
+ r.b = 0xff;
+ r.a = 0xff - w;
+ return r;
}
+rgb_t more_twinkle(uint32_t time, size_t x)
+{
+ uint8_t time_offset = pseudo_random(x);
+
+#define N 91
+
+ uint32_t adj_time = time - time_offset;
+ uint32_t time8 = adj_time % N;
+ uint32_t speed = pseudo_random((adj_time / N) + x) % 3;
+
+ time8 *= speed;
+ time8 += (255 - N);
+ uint8_t w = 0;
+ if (time8 <= 255) {
+ w = calc_w(time8);
+ } else {
+ w = 0;
+ }
+
+ rgb_t r;
+ r.r = 0xff;
+ r.g = 0xff;
+ r.b = 0xff;
+ r.a = 0xff - w;
+ return r;
+}
static rgb_t get_rgb(uint32_t time, size_t x)
{
- int r = 0, g = 0, b = 0;
+ uint8_t time8 = time & 0xff;
+
+ rgb_t twink = twinkle(time, x);
+ rgb_t bg;
+ bg.color = 0;
+ bg.r = 0xff;
+ bg.g = byte_scale(byte_sin(time8 + x * 2), 0x60);
+ bg.b = 0;
+ bg.a = 0xa0;
+
+ return blend(twink, bg);
+}
+
+static rgb_t blue_twinkle(uint32_t time, size_t x)
+{
+ rgb_t bg;
uint8_t time8 = time & 0xff;
- uint8_t time_offset = pseudo_random(x);
- uint32_t adjusted_time = time - time_offset;
+ bg.color = 0;
+ bg.r = byte_sin((time8 * 2 + x * 2 + 0x80) * 2);
+ bg.g = byte_sin(time8 + x * 2);
+ bg.b = 0xff;
+ bg.a = 0xc0;
- // The random number for this "metaframe." The metaframe is enough to show 1
- // flash. It's used to keep the same LED's from consistently repeating.
- uint32_t metaframe = adjusted_time >> 8;
- uint8_t rand = pseudo_random(x + metaframe);
- uint8_t speed = pseudo_random(rand) % 16;
- uint8_t off = pseudo_random(rand + speed);
+ return blend(twinkle(time, x), bg);
+}
- if (speed > 4) {
- speed = 0;
- }
+static rgb_t warm_twinkle(uint32_t time, size_t x)
+{
+ uint8_t time8 = time & 0xff;
- uint32_t w_index = ((adjusted_time - off) * speed) & 0xff;
- int w = 0;
- if (w_index < 255) {
- w = calc_w(w_index);
- }
+ rgb_t twink = twinkle(time, x);
+ rgb_t bg;
+ bg.color = 0;
+ bg.r = 0xff;
+ bg.g = byte_scale(byte_sin(time8 + x * 2), 0x60);
+ bg.b = 0;
+ bg.a = twink.a;
- r = 0xff;
- g = byte_scale(byte_sin(time8 + x * 2), 0x60);
- b = 0;
+ return bg;
+}
+
+static rgb_t cool_twinkle(uint32_t time, size_t x)
+{
+ rgb_t bg;
+ uint8_t time8 = time & 0xff;
+ rgb_t twink = twinkle(time, x);
+
+ bg.color = 0;
+ bg.r = byte_sin((time8 * 2 + x * 2 + 0x80) * 2);
+ bg.g = byte_sin(time8 + x * 2);
+ bg.b = 0xff;
+ bg.a = twink.a;
- rgb_t color;
- color.color = 0;
- color.r = clip(r + w);
- color.g = clip(byte_scale(g, AS_BYTE(0.75)) + w);
- color.b = clip(byte_scale(b, AS_BYTE(0.75)) + w);
- return color;
+ return bg;
}
static void reset(void)
{
}
-__attribute__((__section__(".patterns"))) volatile static pattern_t pat =
+__attribute__((__section__(".patterns"))) volatile static pattern_t p08_pat =
((pattern_t){.get_rgb = get_rgb, .name = "Twinkle", .reset = reset});
+
+__attribute__((
+ __section__(".patterns"))) volatile static pattern_t p09_naked_pat =
+ ((pattern_t){.get_rgb = twinkle, .name = "Naked Twinkle", .reset = reset});
+
+__attribute__((__section__(
+ ".patterns"))) volatile static pattern_t p10_naked_pat = ((pattern_t){
+ .get_rgb = warm_twinkle, .name = "Warm Twinkle", .reset = reset});
+
+__attribute__((__section__(".patterns"))) volatile static pattern_t p11_cool_pat =
+ ((pattern_t){
+ .get_rgb = cool_twinkle, .name = "Cool Twinkle", .reset = reset});
+
+__attribute__((__section__(".patterns"))) volatile static pattern_t blue_pat =
+ ((pattern_t){
+ .get_rgb = blue_twinkle, .name = "Blue Twinkle", .reset = reset});