a0e14049d392528ae131cc62cfae187a726ecad2
[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 - 1, (byte)0xff); // Initiate the first byte of each LED + the end frame with ones
41                 break;
42         }
43         return frame;
44     }
45
46     public void fillColor(int r, int g, int b) {
47         fillColor(Color.rgb(r, g, b));
48     }
49
50     public void fillColor(Color color) {
51         byte r = (byte)color.r(), g = (byte)color.g(), b = (byte)color.b(); // Gamma corrected values
52         for (int i = 0; i < bytes.length; i += stride) {
53             bytes[i + roff] = r;
54             bytes[i + goff] = g;
55             bytes[i + boff] = b;
56         }
57     }
58
59     public void setLedColor(int led, Color color) {
60         int offset = led * stride;
61         bytes[offset + roff] = (byte)color.r();
62         bytes[offset + goff] = (byte)color.g();
63         bytes[offset + boff] = (byte)color.b();
64     }
65
66     public Color getLedColor(int led) {
67         int offset = led * stride;
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;
77     }
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     }
95 }