Added a light level config (0-31) for APA102 only
[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 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.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 - 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
42                 break;
43         }
44         return frame;
45     }
46
47     public void fillColor(int r, int g, int b) {
48         fillColor(Color.rgb(r, g, b));
49     }
50
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) {
54             bytes[i + roff] = r;
55             bytes[i + goff] = g;
56             bytes[i + boff] = b;
57         }
58     }
59
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();
65     }
66
67     public Color getLedColor(int led) {
68         int offset = led * stride;
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;
78     }
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     }
96 }