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 int[] 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 List<Integer> pointList = buildMappedList(model, channelList);
38 points = new int[pointList.size()];
40 for (int value : pointList) {
43 flipped = buildFlippedList(model, flippedList);
46 private ArrayList<Integer> buildMappedList(Model model, int[][] channelList) {
47 ArrayList<Integer> points = new ArrayList<Integer>();
48 for (int[] channel : channelList) {
49 for (int cubeNumber : channel) {
50 if (cubeNumber == 0) {
51 for (int i = 0; i < (Cube.CLIPS_PER_CUBE*Clip.STRIPS_PER_CLIP*Strip.POINTS_PER_STRIP); ++i) {
55 Cube cube = model.getCubeByRawIndex(cubeNumber);
57 throw new RuntimeException("Non-zero, non-existing cube specified in channel mapping (" + cubeNumber + ")");
59 for (Point p : cube.points) {
68 private boolean[] buildFlippedList(Model model, int[][] flippedRGBList) {
69 boolean[] flipped = new boolean[model.points.size()];
70 for (int i = 0; i < flipped.length; ++i) {
73 for (int[] cubeInfo : flippedRGBList) {
74 int cubeNumber = cubeInfo[0];
75 Cube cube = model.getCubeByRawIndex(cubeNumber);
77 throw new RuntimeException("Non-existing cube specified in flipped RGB mapping (" + cubeNumber + ")");
79 for (int i = 1; i < cubeInfo.length; ++i) {
80 int stripIndex = cubeInfo[i];
81 for (Point p : cube.strips.get(stripIndex-1).points) {
82 flipped[p.index] = true;
89 public final void send(int[] colors) {
92 for (int index : points) {
93 int c = colors[index];
94 byte r = (byte) ((c >> 16) & 0xFF);
95 byte g = (byte) ((c >> 8) & 0xFF);
96 byte b = (byte) ((c) & 0xFF);
107 // Flush once packet is full buffer size
108 if (len >= packet.length) {
109 sendPacket(packetNum++, len);
114 // Flush any remaining data
116 sendPacket(packetNum++, len);
120 private void sendPacket(int packetNum, int len) {
121 message.clearArguments();
122 message.add(packetNum);
126 OscP5.flush(message, address);
127 } catch (Exception x) {