Added a light level config (0-31) for APA102 only
[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];
f2eedb6d
TW
40 Arrays.fill(frame.bytes, 4, frame.bytes.length - 5, (byte)(0b11100000 | config.leds.level)); // 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
aa9e49c2
TW
42 break;
43 }
03b67a73
TW
44 return frame;
45 }
46
ed56b145 47 public void fillColor(int r, int g, int b) {
38c759f8 48 fillColor(Color.rgb(r, g, b));
ed56b145
TW
49 }
50
51 public void fillColor(Color color) {
38c759f8 52 byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values
aa9e49c2 53 for (int i = 0; i < bytes.length; i += stride) {
38c759f8
TW
54 bytes[i + roff] = r;
55 bytes[i + goff] = g;
56 bytes[i + boff] = b;
57 }
ed56b145
TW
58 }
59
03b67a73 60 public void setLedColor(int led, Color color) {
aa9e49c2 61 int offset = led * stride;
ed56b145
TW
62 bytes[offset + roff] = (byte)color.r();
63 bytes[offset + goff] = (byte)color.g();
64 bytes[offset + boff] = (byte)color.b();
03b67a73
TW
65 }
66
67 public Color getLedColor(int led) {
aa9e49c2 68 int offset = led * stride;
ed56b145
TW
69 return Color.rgb(
70 bytes[offset + roff] & 0xff,
71 bytes[offset + goff] & 0xff,
72 bytes[offset + boff] & 0xff
73 );
74 }
75
76 public byte[] getBytes() {
77 return bytes;
03b67a73 78 }
0bf6c885
TW
79
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;
87 }
88
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;
95 }
03b67a73 96}