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.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/patterns/twinkle.c b/src/patterns/twinkle.c
new file mode 100644
index 0000000..0c3f985
--- /dev/null
+++ b/src/patterns/twinkle.c
@@ -0,0 +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
+}
+
+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)
+{
+ 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;
+
+ 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;
+
+ return blend(twinkle(time, x), bg);
+}
+
+static rgb_t warm_twinkle(uint32_t time, size_t x)
+{
+ 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 = twink.a;
+
+ 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;
+
+ return bg;
+}
+
+static void reset(void)
+{
+}
+
+__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});