1 package kaka.cakelight;
3 import java.util.Arrays;
5 public class LedFrame {
6 private Configuration config;
9 private int roff, goff, boff; // RGB offsets
11 public static LedFrame from(Configuration config) {
12 LedFrame frame = new LedFrame();
13 frame.config = config;
14 switch (config.leds.type) {
16 * The WS2801 strip takes its input as a plain list of 24-bit colors in RBG order (at least mine did).
23 frame.bytes = new byte[config.leds.getCount() * frame.stride];
27 * The APA102 strip takes its input as:
29 * <li>a start frame of 4 bytes (all zeroes)</li>
30 * <li>a frame of 4 bytes for each LED (111 (3 bits) + global illumination (5 bits) + BGR)</li>
31 * <li>an (optional) end frame of 4 bytes (all ones)</li>
39 frame.bytes = new byte[4 + config.leds.getCount() * frame.stride + 4];
40 Arrays.fill(frame.bytes, 4, frame.bytes.length - 5, (byte)(0b11100000 | config.leds.brightness)); // Initiate the first byte of each LED
41 Arrays.fill(frame.bytes, frame.bytes.length - 5, frame.bytes.length - 1, (byte)0xff); // Initiate the end frame with ones
47 public void fillColor(int r, int g, int b) {
48 fillColor(Color.rgb(r, g, b));
51 public void fillColor(Color color) {
52 byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values
53 for (int i = 0; i < bytes.length; i += stride) {
60 public void setLedColor(int led, Color color) {
61 int offset = led * stride;
62 bytes[offset + roff] = (byte)color.r();
63 bytes[offset + goff] = (byte)color.g();
64 bytes[offset + boff] = (byte)color.b();
67 public Color getLedColor(int led) {
68 int offset = led * stride;
70 bytes[offset + roff] & 0xff,
71 bytes[offset + goff] & 0xff,
72 bytes[offset + boff] & 0xff
76 public byte[] getBytes() {
80 // TODO this needs to be improved
81 /** The x position of the led from 0.0-1.0. */
82 public double xOf(int led) {
83 /* left */ if (led >= config.leds.cols * 2 + config.leds.rows) return 0;
84 /* top */ if (led >= config.leds.cols + config.leds.rows) return 1 - (double)(led - config.leds.cols - config.leds.rows) / config.leds.cols;
85 /* right */ if (led >= config.leds.cols) return 1;
86 /* bottom */ return (double)led / config.leds.cols;
89 /** The y position of the led from 0.0-1.0. */
90 public double yOf(int led) {
91 /* left */ if (led >= config.leds.cols * 2 + config.leds.rows) return (double)(led - config.leds.cols * 2 - config.leds.rows) / config.leds.rows;
92 /* top */ if (led >= config.leds.cols + config.leds.rows) return 0;
93 /* right */ if (led >= config.leds.cols) return 1 - (double)(led - config.leds.cols) / config.leds.rows;
94 /* bottom */ return 1;