5 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
8 * ///\\\ ///\\\ ///\\\ ///\\\
9 * \\\/// \\\/// \\\/// \\\///
12 * EXPERTS ONLY!! EXPERTS ONLY!!
14 * This class implements the output function to the Panda Boards. It
15 * will be moved into GLucose once stabilized.
17 public class PandaDriver {
20 private final NetAddress address;
23 private final OscMessage message;
25 // List of point indices on the board
26 private final List<Integer> points;
28 // Bit for flipped status of each point index
29 private final boolean[] flipped;
32 private final byte[] packet = new byte[4*352]; // TODO: de-magic-number
34 public PandaDriver(NetAddress address, Model model, int[][] channelList, int[][] flippedList) {
35 this.address = address;
36 message = new OscMessage("/shady/pointbuffer");
37 points = buildMappedList(model, channelList);
38 flipped = buildFlippedList(model, flippedList);
41 private ArrayList<Integer> buildMappedList(Model model, int[][] channelList) {
42 ArrayList<Integer> points = new ArrayList<Integer>();
43 for (int[] channel : channelList) {
44 for (int cubeNumber : channel) {
45 if (cubeNumber == 0) {
46 for (int i = 0; i < (Cube.CLIPS_PER_CUBE*Clip.STRIPS_PER_CLIP*Strip.POINTS_PER_STRIP); ++i) {
50 Cube cube = model.getCubeByRawIndex(cubeNumber);
52 throw new RuntimeException("Non-zero, non-existing cube specified in channel mapping (" + cubeNumber + ")");
54 for (Point p : cube.points) {
63 private boolean[] buildFlippedList(Model model, int[][] flippedRGBList) {
64 boolean[] flipped = new boolean[model.points.size()];
65 for (int i = 0; i < flipped.length; ++i) {
68 for (int[] cubeInfo : flippedRGBList) {
69 int cubeNumber = cubeInfo[0];
70 Cube cube = model.getCubeByRawIndex(cubeNumber);
72 throw new RuntimeException("Non-existing cube specified in flipped RGB mapping (" + cubeNumber + ")");
74 for (int i = 1; i < cubeInfo.length; ++i) {
75 int stripIndex = cubeInfo[i];
76 for (Point p : cube.strips.get(stripIndex-1).points) {
77 flipped[p.index] = true;
85 public final void send(int[] colors) {
88 for (int index : points) {
89 int c = colors[index];
90 byte r = (byte) ((c >> 16) & 0xFF);
91 byte g = (byte) ((c >> 8) & 0xFF);
92 byte b = (byte) ((c) & 0xFF);
103 // Flush once packet is full buffer size
104 if (len >= packet.length) {
105 sendPacket(packetNum++, len);
110 // Flush any remaining data
112 sendPacket(packetNum++, len);
116 private void sendPacket(int packetNum, int len) {
117 message.clearArguments();
118 message.add(packetNum);
122 OscP5.flush(message, address);
123 } catch (Exception x) {