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 | |
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 + ")"); | |
6e6c0f0f MS |
73 | } |
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; | |
e73ef85d MS |
78 | } |
79 | } | |
80 | } | |
81 | return flipped; | |
82 | } | |
83 | ||
84 | public final void send(int[] colors) { | |
85 | int len = 0; | |
86 | int packetNum = 0; | |
87 | for (int index : points) { | |
88 | int c = colors[index]; | |
89 | byte r = (byte) ((c >> 16) & 0xFF); | |
90 | byte g = (byte) ((c >> 8) & 0xFF); | |
91 | byte b = (byte) ((c) & 0xFF); | |
92 | if (flipped[index]) { | |
93 | byte tmp = r; | |
94 | r = g; | |
95 | g = tmp; | |
96 | } | |
97 | packet[len++] = 0; | |
98 | packet[len++] = r; | |
99 | packet[len++] = g; | |
100 | packet[len++] = b; | |
101 | ||
102 | // Flush once packet is full buffer size | |
103 | if (len >= packet.length) { | |
104 | sendPacket(packetNum++, len); | |
105 | len = 0; | |
106 | } | |
107 | } | |
108 | ||
109 | // Flush any remaining data | |
110 | if (len > 0) { | |
111 | sendPacket(packetNum++, len); | |
112 | } | |
113 | } | |
114 | ||
115 | private void sendPacket(int packetNum, int len) { | |
116 | message.clearArguments(); | |
117 | message.add(packetNum); | |
118 | message.add(len); | |
119 | message.add(packet); | |
120 | try { | |
121 | OscP5.flush(message, address); | |
122 | } catch (Exception x) { | |
123 | x.printStackTrace(); | |
124 | } | |
125 | } | |
126 | } | |
127 |