Commit | Line | Data |
---|---|---|
e73ef85d MS |
1 | import netP5.*; |
2 | import 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 | */ | |
17 | public 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 | |
e4d0d812 | 26 | private final int[] points; |
e73ef85d MS |
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"); | |
e4d0d812 MS |
37 | List<Integer> pointList = buildMappedList(model, channelList); |
38 | points = new int[pointList.size()]; | |
39 | int i = 0; | |
40 | for (int value : pointList) { | |
41 | points[i++] = value; | |
42 | } | |
e73ef85d MS |
43 | flipped = buildFlippedList(model, flippedList); |
44 | } | |
45 | ||
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) { | |
52 | points.add(0); | |
53 | } | |
54 | } else { | |
55 | Cube cube = model.getCubeByRawIndex(cubeNumber); | |
56 | if (cube == null) { | |
57 | throw new RuntimeException("Non-zero, non-existing cube specified in channel mapping (" + cubeNumber + ")"); | |
58 | } | |
59 | for (Point p : cube.points) { | |
60 | points.add(p.index); | |
61 | } | |
62 | } | |
63 | } | |
64 | } | |
65 | return points; | |
66 | } | |
67 | ||
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) { | |
71 | flipped[i] = false; | |
72 | } | |
73 | for (int[] cubeInfo : flippedRGBList) { | |
74 | int cubeNumber = cubeInfo[0]; | |
75 | Cube cube = model.getCubeByRawIndex(cubeNumber); | |
76 | if (cube == null) { | |
77 | throw new RuntimeException("Non-existing cube specified in flipped RGB mapping (" + cubeNumber + ")"); | |
6e6c0f0f MS |
78 | } |
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; | |
e73ef85d MS |
83 | } |
84 | } | |
85 | } | |
86 | return flipped; | |
87 | } | |
88 | ||
89 | public final void send(int[] colors) { | |
90 | int len = 0; | |
91 | int packetNum = 0; | |
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); | |
97 | if (flipped[index]) { | |
98 | byte tmp = r; | |
99 | r = g; | |
100 | g = tmp; | |
101 | } | |
102 | packet[len++] = 0; | |
103 | packet[len++] = r; | |
104 | packet[len++] = g; | |
105 | packet[len++] = b; | |
106 | ||
107 | // Flush once packet is full buffer size | |
108 | if (len >= packet.length) { | |
109 | sendPacket(packetNum++, len); | |
110 | len = 0; | |
111 | } | |
112 | } | |
113 | ||
114 | // Flush any remaining data | |
115 | if (len > 0) { | |
116 | sendPacket(packetNum++, len); | |
117 | } | |
118 | } | |
119 | ||
120 | private void sendPacket(int packetNum, int len) { | |
121 | message.clearArguments(); | |
122 | message.add(packetNum); | |
123 | message.add(len); | |
124 | message.add(packet); | |
125 | try { | |
126 | OscP5.flush(message, address); | |
127 | } catch (Exception x) { | |
128 | x.printStackTrace(); | |
129 | } | |
130 | } | |
131 | } | |
132 |