Bugfixes
[kaka/cakelight.git] / src / kaka / cakelight / LedFrame.java
1 package kaka.cakelight;
2
3 import java.util.Arrays;
4
5 public class LedFrame {
6     private Configuration config;
7     private byte[] bytes;
8     private int start, stride;
9     private int roff, goff, boff; // RGB offsets
10
11     public static LedFrame from(Configuration config) {
12         LedFrame frame = new LedFrame();
13         frame.config = config;
14         switch (config.leds.type) {
15             /*
16              * The WS2801 strip takes its input as a plain list of 24-bit colors in RBG order (at least mine did).
17              */
18             case WS2801:
19                 frame.start = 0;
20                 frame.stride = 3;
21                 frame.roff = 0;
22                 frame.goff = 2;
23                 frame.boff = 1;
24                 frame.bytes = new byte[config.leds.getCount() * frame.stride];
25                 break;
26
27             /*
28              * The APA102 strip takes its input as:
29              * <ol>
30              * <li>a start frame of 4 bytes (all zeroes)</li>
31              * <li>a frame of 4 bytes for each LED (111 (3 bits) + global illumination (5 bits) + BGR)</li>
32              * <li>an (optional) end frame of 4 bytes (all ones)</li>
33              * </ol>
34              */
35             case APA102:
36                 frame.start = 4;
37                 frame.stride = 4;
38                 frame.roff = 3;
39                 frame.goff = 2;
40                 frame.boff = 1;
41                 frame.bytes = new byte[4 + config.leds.getCount() * frame.stride + 4 * 2]; // 1 end frame doesn't seem to work, so we add another
42                 Arrays.fill(frame.bytes, 4, frame.bytes.length - 9, (byte)(0b11100000 | config.leds.brightness)); // Initiate the first byte of each LED
43                 Arrays.fill(frame.bytes, frame.bytes.length - 9, frame.bytes.length, (byte)0xff); // Initiate the end frame(s) with ones
44                 break;
45         }
46         return frame;
47     }
48
49     public LedFrame fillColor(int r, int g, int b) {
50         return fillColor(Color.rgb(r, g, b));
51     }
52
53     public LedFrame fillColor(Color color) {
54         byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values
55         for (int i = start, max = start + config.leds.getCount() * stride; i < max; i += stride) {
56             bytes[i + roff] = r;
57             bytes[i + goff] = g;
58             bytes[i + boff] = b;
59         }
60         return this;
61     }
62
63     public void setLedColor(int led, Color color) {
64         int offset = start + led * stride;
65         bytes[offset + roff] = (byte)color.r();
66         bytes[offset + goff] = (byte)color.g();
67         bytes[offset + boff] = (byte)color.b();
68     }
69
70     public Color getLedColor(int led) {
71         int offset = start + led * stride;
72         return Color.rgb(
73                 bytes[offset + roff] & 0xff,
74                 bytes[offset + goff] & 0xff,
75                 bytes[offset + boff] & 0xff
76         );
77     }
78
79     public byte[] getBytes() {
80         return bytes;
81     }
82
83     // TODO this needs to be improved
84     /** The x position of the led from 0.0-1.0. */
85     public double xOf(int led) {
86         /* left   */ if (led >= config.leds.cols * 2 + config.leds.rows) return 0;
87         /* top    */ if (led >= config.leds.cols + config.leds.rows) return 1 - (double)(led - config.leds.cols - config.leds.rows) / config.leds.cols;
88         /* right  */ if (led >= config.leds.cols) return 1;
89         /* bottom */ return (double)led / config.leds.cols;
90     }
91
92     /** The y position of the led from 0.0-1.0. */
93     public double yOf(int led) {
94         /* left   */ if (led >= config.leds.cols * 2 + config.leds.rows) return (double)(led - config.leds.cols * 2 - config.leds.rows) / config.leds.rows;
95         /* top    */ if (led >= config.leds.cols + config.leds.rows) return 0;
96         /* right  */ if (led >= config.leds.cols) return 1 - (double)(led - config.leds.cols) / config.leds.rows;
97         /* bottom */ return 1;
98     }
99 }