Add fire mode
authorTomas Wenström <tomas.wenstrom@gmail.com>
Mon, 14 Jun 2021 19:19:06 +0000 (21:19 +0200)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Mon, 14 Jun 2021 19:19:06 +0000 (21:19 +0200)
Untested, because I can't get JavaFX to run

src/kaka/cakelight/Commands.java
src/kaka/cakelight/Console.java
src/kaka/cakelight/mode/FireMode.java [new file with mode: 0644]

index e665cf1..8f61713 100644 (file)
@@ -168,6 +168,19 @@ class Commands {
        });
     }
 
+    static Console.Command fireMode() {
+        return modeCommand(new String[] {"f", "fire"}, (console, args) -> {
+           if (args.length > 1) {
+               console.out("setting multi-color fire mode");
+               return new FireMode(Stream.of(args)
+                       .map(console::parseColor)
+                       .toArray(Color[]::new)
+               );
+           }
+           return null;
+       });
+    }
+
     static Console.Command sunriseMode() {
         return modeCommand(new String[] {"sunrise"}, (console, args) -> {
            if (args.length == 1) {
index d7a29a3..960cc45 100644 (file)
@@ -37,6 +37,7 @@ public class Console extends Thread {
        register(Commands.saturation());
        register(Commands.ambientMode());
        register(Commands.noiseMode());
+       register(Commands.fireMode());
        register(Commands.sunriseMode());
     }
 
diff --git a/src/kaka/cakelight/mode/FireMode.java b/src/kaka/cakelight/mode/FireMode.java
new file mode 100644 (file)
index 0000000..02db5b4
--- /dev/null
@@ -0,0 +1,42 @@
+package kaka.cakelight.mode;
+
+import kaka.cakelight.Color;
+import kaka.cakelight.LedFrame;
+import kaka.cakelight.util.SimplexNoise3D;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+public class FireMode extends AmbientMode {
+    private final Color[] colors;
+    private SimplexNoise3D noise = new SimplexNoise3D(0);
+
+    public FireMode(Color... colors) {
+       assert colors.length > 1;
+       List<Color> list = Arrays.asList(colors);
+       Collections.reverse(list);
+       this.colors = list.toArray(new Color[colors.length]);
+    }
+
+    @Override
+    protected void updateFrame(LedFrame frame, long time, int count) {
+       for (int i = 0; i < config.leds.getCount(); i++) {
+           double x = frame.xOf(i);
+           double y = frame.yOf(i);
+           double v = Math.pow(Math.min(1, Math.max(0, noise.getr(0.0, 1.0, 1, x, y - (time / 7000.0), time / 3500.0))), 1.5);
+           frame.setLedColor(i, getColorAt(v * y));
+       }
+    }
+
+    private Color getColorAt(double value) { // 0.0 to 1.0
+       double localRange = 1.0 / (colors.length - 1);
+       int index = (int)(value / localRange);
+       double localValue = (value / localRange) - index;
+       if (index == colors.length - 1) {
+           return colors[colors.length - 1];
+       } else {
+           return colors[index].interpolate(colors[index + 1], localValue);
+       }
+    }
+}