Added support for APA102 LED strips
[kaka/cakelight.git] / src / kaka / cakelight / LedFrame.java
CommitLineData
03b67a73
TW
1package kaka.cakelight;
2
aa9e49c2
TW
3import java.util.Arrays;
4
03b67a73 5public class LedFrame {
0bf6c885 6 private Configuration config;
ed56b145 7 private byte[] bytes;
aa9e49c2
TW
8 private int stride;
9 private int roff, goff, boff; // RGB offsets
03b67a73
TW
10
11 public static LedFrame from(Configuration config) {
12 LedFrame frame = new LedFrame();
0bf6c885 13 frame.config = config;
aa9e49c2
TW
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.stride = 3;
20 frame.roff = 0;
21 frame.goff = 2;
22 frame.boff = 1;
23 frame.bytes = new byte[config.leds.getCount() * frame.stride];
24 break;
25
26 /*
27 * The APA102 strip takes its input as:
28 * <ol>
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>
32 * </ol>
33 */
34 case APA102:
35 frame.stride = 4;
36 frame.roff = 3 + 4;
37 frame.goff = 2 + 4;
38 frame.boff = 1 + 4;
39 frame.bytes = new byte[4 + config.leds.getCount() * frame.stride + 4];
40 Arrays.fill(frame.bytes, 4, frame.bytes.length - 1, (byte)0xff); // Initiate the first byte of each LED + the end frame with ones
41 break;
42 }
03b67a73
TW
43 return frame;
44 }
45
ed56b145 46 public void fillColor(int r, int g, int b) {
38c759f8 47 fillColor(Color.rgb(r, g, b));
ed56b145
TW
48 }
49
50 public void fillColor(Color color) {
38c759f8 51 byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values
aa9e49c2 52 for (int i = 0; i < bytes.length; i += stride) {
38c759f8
TW
53 bytes[i + roff] = r;
54 bytes[i + goff] = g;
55 bytes[i + boff] = b;
56 }
ed56b145
TW
57 }
58
03b67a73 59 public void setLedColor(int led, Color color) {
aa9e49c2 60 int offset = led * stride;
ed56b145
TW
61 bytes[offset + roff] = (byte)color.r();
62 bytes[offset + goff] = (byte)color.g();
63 bytes[offset + boff] = (byte)color.b();
03b67a73
TW
64 }
65
66 public Color getLedColor(int led) {
aa9e49c2 67 int offset = led * stride;
ed56b145
TW
68 return Color.rgb(
69 bytes[offset + roff] & 0xff,
70 bytes[offset + goff] & 0xff,
71 bytes[offset + boff] & 0xff
72 );
73 }
74
75 public byte[] getBytes() {
76 return bytes;
03b67a73 77 }
0bf6c885
TW
78
79 // TODO this needs to be improved
80 /** The x position of the led from 0.0-1.0. */
81 public double xOf(int led) {
82 /* left */ if (led >= config.leds.cols * 2 + config.leds.rows) return 0;
83 /* top */ if (led >= config.leds.cols + config.leds.rows) return 1 - (double)(led - config.leds.cols - config.leds.rows) / config.leds.cols;
84 /* right */ if (led >= config.leds.cols) return 1;
85 /* bottom */ return (double)led / config.leds.cols;
86 }
87
88 /** The y position of the led from 0.0-1.0. */
89 public double yOf(int led) {
90 /* left */ if (led >= config.leds.cols * 2 + config.leds.rows) return (double)(led - config.leds.cols * 2 - config.leds.rows) / config.leds.rows;
91 /* top */ if (led >= config.leds.cols + config.leds.rows) return 0;
92 /* right */ if (led >= config.leds.cols) return 1 - (double)(led - config.leds.cols) / config.leds.rows;
93 /* bottom */ return 1;
94 }
03b67a73 95}