Added support for APA102 LED strips
authorTomas Wenström <tomas.wenstrom@gmail.com>
Mon, 14 Aug 2017 12:04:15 +0000 (14:04 +0200)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Mon, 14 Aug 2017 12:04:15 +0000 (14:04 +0200)
config.properties
config.properties.template
src/kaka/cakelight/Configuration.java
src/kaka/cakelight/LedFrame.java

index 7e04ee2..7732340 100644 (file)
@@ -12,6 +12,8 @@ video.crop.right=29
 video.crop.top=18
 video.crop.bottom=18
 
+# Supported types: apa102, ws2801
+leds.type=apa102
 leds.cols=32
 leds.rows=17
 
index 7e04ee2..7732340 100644 (file)
@@ -12,6 +12,8 @@ video.crop.right=29
 video.crop.top=18
 video.crop.bottom=18
 
+# Supported types: apa102, ws2801
+leds.type=apa102
 leds.cols=32
 leds.rows=17
 
index 86ade8e..21c2072 100644 (file)
@@ -98,14 +98,27 @@ public class Configuration {
     public class LedConfiguration {
         public int cols;
         public int rows;
+        public LedType type;
 
         private LedConfiguration(Properties prop) {
             cols = Integer.parseInt(get(prop, "leds.cols"));
             rows = Integer.parseInt(get(prop, "leds.rows"));
+            switch (get(prop, "leds.type", "").toUpperCase()) {
+                case "WS2801":
+                    type = LedType.WS2801;
+                    break;
+                case "APA102":
+                default:
+                    type = LedType.APA102;
+            }
         }
 
         public int getCount() {
             return cols * 2 + rows * 2;
         }
     }
+
+    public enum LedType {
+        WS2801, APA102
+    }
 }
index ab4ef5c..a0e1404 100644 (file)
@@ -1,14 +1,45 @@
 package kaka.cakelight;
 
+import java.util.Arrays;
+
 public class LedFrame {
     private Configuration config;
     private byte[] bytes;
-    private int roff = 0, goff = 2, boff = 1; // Color values are stored as RBG, which is what the LED list takes.
+    private int stride;
+    private int roff, goff, boff; // RGB offsets
 
     public static LedFrame from(Configuration config) {
         LedFrame frame = new LedFrame();
         frame.config = config;
-        frame.bytes = new byte[config.leds.getCount() * 3];
+        switch (config.leds.type) {
+            /*
+             * The WS2801 strip takes its input as a plain list of 24-bit colors in RBG order (at least mine did).
+             */
+            case WS2801:
+                frame.stride = 3;
+                frame.roff = 0;
+                frame.goff = 2;
+                frame.boff = 1;
+                frame.bytes = new byte[config.leds.getCount() * frame.stride];
+                break;
+
+            /*
+             * The APA102 strip takes its input as:
+             * <ol>
+             * <li>a start frame of 4 bytes (all zeroes)</li>
+             * <li>a frame of 4 bytes for each LED (111 (3 bits) + global illumination (5 bits) + BGR)</li>
+             * <li>an (optional) end frame of 4 bytes (all ones)</li>
+             * </ol>
+             */
+            case APA102:
+                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 - 1, (byte)0xff); // Initiate the first byte of each LED + the end frame with ones
+                break;
+        }
         return frame;
     }
 
@@ -18,7 +49,7 @@ public class LedFrame {
 
     public void 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 += 3) {
+        for (int i = 0; i < bytes.length; i += stride) {
             bytes[i + roff] = r;
             bytes[i + goff] = g;
             bytes[i + boff] = b;
@@ -26,14 +57,14 @@ public class LedFrame {
     }
 
     public void setLedColor(int led, Color color) {
-        int offset = led * 3;
+        int offset = 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 * 3;
+        int offset = led * stride;
         return Color.rgb(
                 bytes[offset + roff] & 0xff,
                 bytes[offset + goff] & 0xff,