Merge remote-tracking branch 'origin/master'
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 5 Nov 2017 19:00:52 +0000 (20:00 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 5 Nov 2017 19:00:52 +0000 (20:00 +0100)
src/kaka/cakelight/AmbientMode.java
src/kaka/cakelight/CakeLight.java
src/kaka/cakelight/Console.java
src/kaka/cakelight/LedFrame.java

index 2b10e31..7836b90 100644 (file)
@@ -77,6 +77,20 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
                 double value = Math.abs((ledOffset * 2 - ledCount) / ledCount); // 1 to 0 to 1
                 frame.setLedColor(i, Color.hsv(value * hueLength + hueOffset, 1, 1));
             }
+        } else if (type == 3) {
+            for (int i = 0; i < config.leds.getCount(); i++) {
+                double x = frame.xOf(i);
+                double y = frame.yOf(i);
+                double g = 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, Color.rgb(0, g, 1 - g * 0.5));
+            }
+        } else if (type == 4) {
+            for (int i = 0; i < config.leds.getCount(); i++) {
+                double x = frame.xOf(i);
+                double y = frame.yOf(i);
+                double g = 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, Color.rgb(1, g, 0));
+            }
         }
     }
 
index 69f6677..95a3be1 100644 (file)
@@ -20,6 +20,7 @@ public class CakeLight {
 
     public void cleanup() {
         if (this.mode != null) {
+           this.mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting
             this.mode.exit();
         }
     }
@@ -42,6 +43,6 @@ public class CakeLight {
 
     public void turnOff() {
         cleanup();
-        ledController.onFrame(LedFrame.from(config));
+        ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));
     }
 }
index fef4301..6339138 100644 (file)
@@ -25,7 +25,7 @@ public class Console extends Thread {
             System.out.print("> ");
             try {
                 String input = reader.readLine();
-                if (input.equals("0") || input.equals("1") || input.equals("2")) {
+                if (input.equals("0") || input.equals("1") || input.equals("2") || input.equals("3") || input.equals("4")) {
                    cakelight.setMode(new AmbientMode(new String[] {input}));
                    System.out.println("setting ambient mode to " + input);
                } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
index 18b8f22..ac3341d 100644 (file)
@@ -5,12 +5,9 @@ import java.util.Arrays;
 public class LedFrame {
     private Configuration config;
     private byte[] bytes;
-    private int stride;
+    private int start, stride;
     private int roff, goff, boff; // RGB offsets
 
-    /**
-     * @return a frame initiated to black
-     */
     public static LedFrame from(Configuration config) {
         LedFrame frame = new LedFrame();
         frame.config = config;
@@ -19,6 +16,7 @@ public class LedFrame {
              * The WS2801 strip takes its input as a plain list of 24-bit colors in RBG order (at least mine did).
              */
             case WS2801:
+               frame.start = 0;
                 frame.stride = 3;
                 frame.roff = 0;
                 frame.goff = 2;
@@ -35,40 +33,42 @@ public class LedFrame {
              * </ol>
              */
             case APA102:
+               frame.start = 4;
                 frame.stride = 4;
-                frame.roff = 3 + 4;
-                frame.goff = 2 + 4;
-                frame.boff = 1 + 4;
-                frame.bytes = new byte[4 + config.leds.getCount() * frame.stride + 4];
-                Arrays.fill(frame.bytes, 4, frame.bytes.length - 5, (byte)(0b11100000 | config.leds.brightness)); // Initiate the first byte of each LED
-                Arrays.fill(frame.bytes, frame.bytes.length - 5, frame.bytes.length - 1, (byte)0xff); // Initiate the end frame with ones
+                frame.roff = 3;
+                frame.goff = 2;
+                frame.boff = 1;
+                frame.bytes = new byte[4 + config.leds.getCount() * frame.stride + 4 * 2]; // 1 end frame doesn't seem to work, so we add another
+                Arrays.fill(frame.bytes, 4, frame.bytes.length - 9, (byte)(0b11100000 | config.leds.brightness)); // Initiate the first byte of each LED
+                Arrays.fill(frame.bytes, frame.bytes.length - 9, frame.bytes.length, (byte)0xff); // Initiate the end frame(s) with ones
                 break;
         }
         return frame;
     }
 
-    public void fillColor(int r, int g, int b) {
-        fillColor(Color.rgb(r, g, b));
+    public LedFrame fillColor(int r, int g, int b) {
+        return fillColor(Color.rgb(r, g, b));
     }
 
-    public void fillColor(Color color) {
+    public LedFrame fillColor(Color color) {
         byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values
-        for (int i = 0; i < bytes.length; i += stride) {
+        for (int i = start, max = start + config.leds.getCount() * stride; i < max; i += stride) {
             bytes[i + roff] = r;
             bytes[i + goff] = g;
             bytes[i + boff] = b;
         }
+       return this;
     }
 
     public void setLedColor(int led, Color color) {
-        int offset = led * stride;
+        int offset = start + led * stride;
         bytes[offset + roff] = (byte)color.r();
         bytes[offset + goff] = (byte)color.g();
         bytes[offset + boff] = (byte)color.b();
     }
 
     public Color getLedColor(int led) {
-        int offset = led * stride;
+        int offset = start + led * stride;
         return Color.rgb(
                 bytes[offset + roff] & 0xff,
                 bytes[offset + goff] & 0xff,