Add mappings for panda board UI and PandaDriver class
[SugarCubes.git] / _PandaDriver.pde
CommitLineData
e73ef85d
MS
1import netP5.*;
2import oscP5.*;
3
4/**
5 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
6 *
7 * //\\ //\\ //\\ //\\
8 * ///\\\ ///\\\ ///\\\ ///\\\
9 * \\\/// \\\/// \\\/// \\\///
10 * \\// \\// \\// \\//
11 *
12 * EXPERTS ONLY!! EXPERTS ONLY!!
13 *
14 * This class implements the output function to the Panda Boards. It
15 * will be moved into GLucose once stabilized.
16 */
17public class PandaDriver {
18
19 // Address to send to
20 private final NetAddress address;
21
22 // OSC message
23 private final OscMessage message;
24
25 // List of point indices on the board
26 private final List<Integer> points;
27
28 // Bit for flipped status of each point index
29 private final boolean[] flipped;
30
31 // Packet data
32 private final byte[] packet = new byte[4*352]; // TODO: de-magic-number
33
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);
39 }
40
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) {
47 points.add(0);
48 }
49 } else {
50 Cube cube = model.getCubeByRawIndex(cubeNumber);
51 if (cube == null) {
52 throw new RuntimeException("Non-zero, non-existing cube specified in channel mapping (" + cubeNumber + ")");
53 }
54 for (Point p : cube.points) {
55 points.add(p.index);
56 }
57 }
58 }
59 }
60 return points;
61 }
62
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) {
66 flipped[i] = false;
67 }
68 for (int[] cubeInfo : flippedRGBList) {
69 int cubeNumber = cubeInfo[0];
70 Cube cube = model.getCubeByRawIndex(cubeNumber);
71 if (cube == null) {
72 throw new RuntimeException("Non-existing cube specified in flipped RGB mapping (" + cubeNumber + ")");
73 } else {
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;
78 }
79 }
80 }
81 }
82 return flipped;
83 }
84
85 public final void send(int[] colors) {
86 int len = 0;
87 int packetNum = 0;
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);
93 if (flipped[index]) {
94 byte tmp = r;
95 r = g;
96 g = tmp;
97 }
98 packet[len++] = 0;
99 packet[len++] = r;
100 packet[len++] = g;
101 packet[len++] = b;
102
103 // Flush once packet is full buffer size
104 if (len >= packet.length) {
105 sendPacket(packetNum++, len);
106 len = 0;
107 }
108 }
109
110 // Flush any remaining data
111 if (len > 0) {
112 sendPacket(packetNum++, len);
113 }
114 }
115
116 private void sendPacket(int packetNum, int len) {
117 message.clearArguments();
118 message.add(packetNum);
119 message.add(len);
120 message.add(packet);
121 try {
122 OscP5.flush(message, address);
123 } catch (Exception x) {
124 x.printStackTrace();
125 }
126 }
127}
128