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 public final String ip;
23 private final NetAddress address;
25 // Whether board output is enabled
26 private boolean enabled = false;
29 private final OscMessage message;
31 // List of point indices on the board
32 private final int[] points;
35 private final byte[] packet = new byte[4*352]; // TODO: de-magic-number, UDP related?
37 private static final int NO_POINT = -1;
39 public PandaDriver(String ip) {
41 this.address = new NetAddress(ip, 9001);
42 message = new OscMessage("/shady/pointbuffer");
43 points = new int[PandaMapping.PIXELS_PER_BOARD];
44 for (int i = 0; i < points.length; ++i) {
49 public PandaDriver(String ip, int[] pointList) {
51 for (int i = 0; i < pointList.length && i < points.length; ++i) {
52 this.points[i] = pointList[i];
56 public PandaDriver(String ip, Model model, PandaMapping pm) {
58 buildPointList(model, pm);
61 public void toggle() {
63 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
66 private void buildPointList(Model model, PandaMapping pm) {
67 final int[][] stripOrderings = new int[][] {
68 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
69 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
70 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
71 { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
75 for (ChannelMapping channel : pm.channelList) {
76 switch (channel.mode) {
77 case ChannelMapping.MODE_CUBES:
78 for (int rawCubeIndex : channel.objectIndices) {
79 if (rawCubeIndex < 0) {
80 for (int i = 0; i < Cube.POINTS_PER_CUBE; ++i) {
81 points[pi++] = NO_POINT;
84 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
85 int stripOrderIndex = 0;
86 switch (cube.wiring) {
87 case FRONT_LEFT: stripOrderIndex = 0; break;
88 case FRONT_RIGHT: stripOrderIndex = 1; break;
89 case REAR_LEFT: stripOrderIndex = 2; break;
90 case REAR_RIGHT: stripOrderIndex = 3; break;
92 for (int stripIndex : stripOrderings[stripOrderIndex]) {
93 Strip s = cube.strips.get(stripIndex);
94 for (int j = s.points.size() - 1; j >= 0; --j) {
95 points[pi++] = s.points.get(j).index;
102 case ChannelMapping.MODE_BASS:
103 // TODO(mapping): figure out how these strips are wired
104 for (int i = 0; i < ChannelMapping.PIXELS_PER_CHANNEL; ++i) {
105 points[pi++] = NO_POINT;
109 case ChannelMapping.MODE_FLOOR:
110 // TODO(mapping): figure out how these strips are wired
111 for (int i = 0; i < ChannelMapping.PIXELS_PER_CHANNEL; ++i) {
112 points[pi++] = NO_POINT;
116 case ChannelMapping.MODE_SPEAKER:
117 // TODO(mapping): figure out how these strips are wired
118 for (int i = 0; i < ChannelMapping.PIXELS_PER_CHANNEL; ++i) {
119 points[pi++] = NO_POINT;
123 case ChannelMapping.MODE_NULL:
124 for (int i = 0; i < ChannelMapping.PIXELS_PER_CHANNEL; ++i) {
125 points[pi++] = NO_POINT;
130 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
135 public final void send(int[] colors) {
141 for (int index : points) {
142 int c = (index < 0) ? 0 : colors[index];
143 byte r = (byte) ((c >> 16) & 0xFF);
144 byte g = (byte) ((c >> 8) & 0xFF);
145 byte b = (byte) ((c) & 0xFF);
151 // Flush once packet is full buffer size
152 if (len >= packet.length) {
153 sendPacket(packetNum++);
158 // Flush any remaining data
160 sendPacket(packetNum++);
164 private void sendPacket(int packetNum) {
165 message.clearArguments();
166 message.add(packetNum);
167 message.add(packet.length);
170 OscP5.flush(message, address);
171 } catch (Exception x) {