X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2FLedFrame.java;h=cb92dabb823aa2a2635bde6f10aecbc966729b9c;hb=f2eedb6d98ae638d65c9bfd1a9a339a060235592;hp=4d6b68add12d99313bfaaedcf0d407434fea0d57;hpb=ed56b145bc04170b4129f4ff92b9eab0e6e4dc74;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/LedFrame.java b/src/kaka/cakelight/LedFrame.java index 4d6b68a..cb92dab 100644 --- a/src/kaka/cakelight/LedFrame.java +++ b/src/kaka/cakelight/LedFrame.java @@ -1,36 +1,71 @@ 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.bytes = new byte[config.leds.getCount() * 3]; + 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.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: + *
    + *
  1. a start frame of 4 bytes (all zeroes)
  2. + *
  3. a frame of 4 bytes for each LED (111 (3 bits) + global illumination (5 bits) + BGR)
  4. + *
  5. an (optional) end frame of 4 bytes (all ones)
  6. + *
+ */ + 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 - 5, (byte)(0b11100000 | config.leds.level)); // 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 + break; + } return frame; } public void fillColor(int r, int g, int b) { - for (int i = 0; i < bytes.length; i += 3) { - bytes[i + roff] = (byte)r; - bytes[i + goff] = (byte)g; - bytes[i + boff] = (byte)b; - } + fillColor(Color.rgb(r, g, b)); } public void fillColor(Color color) { - fillColor(color.r(), color.g(), color.b()); + 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) { + bytes[i + roff] = r; + bytes[i + goff] = g; + bytes[i + boff] = b; + } } 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, @@ -41,4 +76,21 @@ public class LedFrame { 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; + } }