X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;ds=inline;f=src%2Fkaka%2Fcakelight%2FLedFrame.java;h=ac3341d1793cc53996a83aa0b2fae4da9392f61e;hb=6a3d0edaa676312429f2f2bd6ed44e59ed07cc70;hp=a4f6293d6e2d8ecdff9bf70b00364b891a86bfaa;hpb=03b67a7377d6d23d517d33e47f338bb7859596ed;p=kaka%2Fcakelight.git
diff --git a/src/kaka/cakelight/LedFrame.java b/src/kaka/cakelight/LedFrame.java
index a4f6293..ac3341d 100644
--- a/src/kaka/cakelight/LedFrame.java
+++ b/src/kaka/cakelight/LedFrame.java
@@ -1,21 +1,99 @@
package kaka.cakelight;
-import javafx.scene.paint.Color;
+import java.util.Arrays;
public class LedFrame {
- private Color[] leds;
+ private Configuration config;
+ private byte[] bytes;
+ private int start, stride;
+ private int roff, goff, boff; // RGB offsets
public static LedFrame from(Configuration config) {
LedFrame frame = new LedFrame();
- frame.leds = new Color[config.leds.cols * 2 + config.leds.rows * 2];
+ frame.config = config;
+ 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.start = 0;
+ 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:
+ *
+ * - a start frame of 4 bytes (all zeroes)
+ * - a frame of 4 bytes for each LED (111 (3 bits) + global illumination (5 bits) + BGR)
+ * - an (optional) end frame of 4 bytes (all ones)
+ *
+ */
+ case APA102:
+ frame.start = 4;
+ frame.stride = 4;
+ 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 LedFrame fillColor(int r, int g, int b) {
+ return fillColor(Color.rgb(r, g, b));
+ }
+
+ public LedFrame fillColor(Color color) {
+ byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values
+ 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) {
- leds[led] = color;
+ 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) {
- return leds[led];
+ int offset = start + led * stride;
+ return Color.rgb(
+ bytes[offset + roff] & 0xff,
+ bytes[offset + goff] & 0xff,
+ bytes[offset + boff] & 0xff
+ );
+ }
+
+ public byte[] getBytes() {
+ return bytes;
+ }
+
+ // TODO this needs to be improved
+ /** The x position of the led from 0.0-1.0. */
+ public double xOf(int led) {
+ /* left */ if (led >= config.leds.cols * 2 + config.leds.rows) return 0;
+ /* top */ if (led >= config.leds.cols + config.leds.rows) return 1 - (double)(led - config.leds.cols - config.leds.rows) / config.leds.cols;
+ /* right */ if (led >= config.leds.cols) return 1;
+ /* bottom */ return (double)led / config.leds.cols;
+ }
+
+ /** The y position of the led from 0.0-1.0. */
+ public double yOf(int led) {
+ /* left */ if (led >= config.leds.cols * 2 + config.leds.rows) return (double)(led - config.leds.cols * 2 - config.leds.rows) / config.leds.rows;
+ /* top */ if (led >= config.leds.cols + config.leds.rows) return 0;
+ /* right */ if (led >= config.leds.cols) return 1 - (double)(led - config.leds.cols) / config.leds.rows;
+ /* bottom */ return 1;
}
}