| 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 int[] points; |
| 27 | |
| 28 | // Packet data |
| 29 | private final byte[] packet = new byte[4*352]; // TODO: de-magic-number |
| 30 | |
| 31 | public PandaDriver(NetAddress address, Model model, int[][] channelList) { |
| 32 | this.address = address; |
| 33 | message = new OscMessage("/shady/pointbuffer"); |
| 34 | List<Integer> pointList = buildMappedList(model, channelList); |
| 35 | points = new int[pointList.size()]; |
| 36 | int i = 0; |
| 37 | for (int value : pointList) { |
| 38 | points[i++] = value; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | private ArrayList<Integer> buildMappedList(Model model, int[][] channelList) { |
| 43 | ArrayList<Integer> points = new ArrayList<Integer>(); |
| 44 | for (int[] channel : channelList) { |
| 45 | for (int cubeNumber : channel) { |
| 46 | if (cubeNumber == 0) { |
| 47 | for (int i = 0; i < (Cube.FACES_PER_CUBE*Face.STRIPS_PER_FACE*Strip.POINTS_PER_STRIP); ++i) { |
| 48 | points.add(0); |
| 49 | } |
| 50 | } else { |
| 51 | Cube cube = model.getCubeByRawIndex(cubeNumber); |
| 52 | if (cube == null) { |
| 53 | throw new RuntimeException("Non-zero, non-existing cube specified in channel mapping (" + cubeNumber + ")"); |
| 54 | } |
| 55 | final int[] stripOrder = new int[] { |
| 56 | 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 |
| 57 | }; |
| 58 | for (int stripIndex : stripOrder) { |
| 59 | Strip s = cube.strips.get(stripIndex); |
| 60 | for (int j = s.points.size() - 1; j >= 0; --j) { |
| 61 | points.add(s.points.get(j).index); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return points; |
| 68 | } |
| 69 | |
| 70 | public final void send(int[] colors) { |
| 71 | int len = 0; |
| 72 | int packetNum = 0; |
| 73 | for (int index : points) { |
| 74 | int c = colors[index]; |
| 75 | byte r = (byte) ((c >> 16) & 0xFF); |
| 76 | byte g = (byte) ((c >> 8) & 0xFF); |
| 77 | byte b = (byte) ((c) & 0xFF); |
| 78 | packet[len++] = 0; |
| 79 | packet[len++] = r; |
| 80 | packet[len++] = g; |
| 81 | packet[len++] = b; |
| 82 | |
| 83 | // Flush once packet is full buffer size |
| 84 | if (len >= packet.length) { |
| 85 | sendPacket(packetNum++); |
| 86 | len = 0; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Flush any remaining data |
| 91 | if (len > 0) { |
| 92 | sendPacket(packetNum++); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private void sendPacket(int packetNum) { |
| 97 | message.clearArguments(); |
| 98 | message.add(packetNum); |
| 99 | message.add(packet.length); |
| 100 | message.add(packet); |
| 101 | try { |
| 102 | OscP5.flush(message, address); |
| 103 | } catch (Exception x) { |
| 104 | x.printStackTrace(); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |