New two-color noise mode + initial draft on commands
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 24 Nov 2019 21:58:42 +0000 (22:58 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 24 Nov 2019 21:58:42 +0000 (22:58 +0100)
src/kaka/cakelight/Console.java
src/kaka/cakelight/mode/AmbientMode.java
src/kaka/cakelight/mode/TwoColorNoiseMode.java [new file with mode: 0644]

index caf8195..3b66959 100644 (file)
@@ -2,6 +2,7 @@ package kaka.cakelight;
 
 import kaka.cakelight.mode.AmbientMode;
 import kaka.cakelight.mode.SingleColorMode;
+import kaka.cakelight.mode.TwoColorNoiseMode;
 import kaka.cakelight.mode.VideoMode;
 
 import java.io.BufferedReader;
@@ -60,6 +61,8 @@ public class Console extends Thread {
                    String[] split = input.split("\\s+");
                    config.video.saturation = Double.parseDouble(split[1]);
                    System.out.println("setting saturation to " + config.video.saturation);
+               } else if (input.matches("(n|noise)")) {
+                   TwoColorNoiseMode.getCommand().activate(cakelight, config, input.split("\\s+"));
                }
             } catch (IOException e) {
                 System.out.println("Error reading from command line");
@@ -67,4 +70,9 @@ public class Console extends Thread {
             }
         }
     }
+
+    public interface Command {
+        String[] getNames();
+        void activate(CakeLight cakelight, Configuration config, String[] args);
+    }
 }
index afe36ab..bc8a680 100644 (file)
@@ -7,9 +7,11 @@ import kaka.cakelight.util.SimplexNoise3D;
 
 public class AmbientMode extends Mode { // TODO split into DynamicAmbient and StaticAmbient?
     private Thread thread; // TODO move to a dynamic sub class
-    private Configuration config;
+    protected Configuration config;
     private int type = 0;
 
+    AmbientMode() {}
+
     public AmbientMode(String[] args) {
         if (args.length > 0) {
             type = Integer.parseInt(args[0]);
@@ -56,7 +58,7 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
      * @param time  Time in milliseconds since start
      * @param count Goes from 0 to number of LEDs - 1
      */
-    private void updateFrame(LedFrame frame, long time, int count) {
+    protected void updateFrame(LedFrame frame, long time, int count) {
         if (type == 0) {
             for (int i = 0; i < config.leds.getCount(); i++) {
                 double r = Math.sin(2 * i * Math.PI / config.leds.getCount() + time * 0.001) * 0.5 + 0.5;
diff --git a/src/kaka/cakelight/mode/TwoColorNoiseMode.java b/src/kaka/cakelight/mode/TwoColorNoiseMode.java
new file mode 100644 (file)
index 0000000..6d2dabe
--- /dev/null
@@ -0,0 +1,64 @@
+package kaka.cakelight.mode;
+
+import kaka.cakelight.*;
+import kaka.cakelight.util.SimplexNoise3D;
+
+public class TwoColorNoiseMode extends AmbientMode {
+    private final Color primary, secondary;
+    private SimplexNoise3D noise = new SimplexNoise3D(0);
+
+    public static Console.Command getCommand() {
+        return new Console.Command() {
+            public String[] getNames() {
+                return new String[] {"n", "noise"};
+            }
+
+            public void activate(CakeLight cakelight, Configuration config, String[] args) {
+                if (args.length == 3) { // cmd + col1 + col2
+                    cakelight.setMode(new TwoColorNoiseMode(
+                            parseColor(args[1]),
+                            parseColor(args[2])
+                    ));
+                }
+            }
+
+            private Color parseColor(String s) {
+                switch (s.toLowerCase()) {
+                    case "r": return Color.rgb(255, 0, 0);
+                    case "g": return Color.rgb(0, 255, 0);
+                    case "b": return Color.rgb(0, 0, 255);
+                    default: // assume hexadecimal
+                        if (s.length() == 3) {
+                            return Color.rgb(
+                                    Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
+                                    Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
+                                    Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
+                            );
+                        } else if (s.length() == 6) {
+                            return Color.rgb(
+                                    Integer.parseInt(s.substring(0, 2), 16),
+                                    Integer.parseInt(s.substring(2, 4), 16),
+                                    Integer.parseInt(s.substring(4, 6), 16)
+                            );
+                        }
+                }
+                return Color.BLACK;
+            }
+        };
+    }
+
+    public TwoColorNoiseMode(Color primary, Color secondary) {
+        this.primary = primary;
+        this.secondary = secondary;
+    }
+
+    @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))), 1.5);
+            frame.setLedColor(i, primary.interpolate(secondary, v));
+        }
+    }
+}