| 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 | // IP address |
| 20 | public final String ip; |
| 21 | |
| 22 | // Address to send to |
| 23 | private final NetAddress address; |
| 24 | |
| 25 | // Whether board output is enabled |
| 26 | private boolean enabled = false; |
| 27 | |
| 28 | // OSC message |
| 29 | private final OscMessage message; |
| 30 | |
| 31 | // List of point indices on the board |
| 32 | private final int[] points; |
| 33 | |
| 34 | // Packet data |
| 35 | private final byte[] packet = new byte[4*352]; // TODO: de-magic-number, UDP related? |
| 36 | |
| 37 | public PandaDriver(String ip) { |
| 38 | this.ip = ip; |
| 39 | this.address = new NetAddress(ip, 9001); |
| 40 | message = new OscMessage("/shady/pointbuffer"); |
| 41 | points = new int[PandaMapping.PIXELS_PER_BOARD]; |
| 42 | for (int i = 0; i < points.length; ++i) { |
| 43 | points[i] = 0; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public PandaDriver(String ip, int[] pointList) { |
| 48 | this(ip); |
| 49 | for (int i = 0; i < pointList.length && i < points.length; ++i) { |
| 50 | this.points[i] = pointList[i]; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public PandaDriver(String ip, Model model, PandaMapping pm) { |
| 55 | this(ip); |
| 56 | buildPointList(model, pm); |
| 57 | } |
| 58 | |
| 59 | public void toggle() { |
| 60 | enabled = !enabled; |
| 61 | println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF")); |
| 62 | } |
| 63 | |
| 64 | private void buildPointList(Model model, PandaMapping pm) { |
| 65 | final int[][] stripOrderings = new int[][] { |
| 66 | { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT |
| 67 | { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT |
| 68 | { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT |
| 69 | { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT |
| 70 | }; |
| 71 | |
| 72 | int pi = 0; |
| 73 | for (int[] channel : pm.channelList) { |
| 74 | for (int cubeNumber : channel) { |
| 75 | if (cubeNumber <= 0) { |
| 76 | for (int i = 0; i < Cube.POINTS_PER_CUBE; ++i) { |
| 77 | points[pi++] = 0; |
| 78 | } |
| 79 | } else { |
| 80 | Cube cube = model.getCubeByRawIndex(cubeNumber); |
| 81 | if (cube == null) { |
| 82 | throw new RuntimeException("Non-zero, non-existing cube specified in channel mapping (" + cubeNumber + ")"); |
| 83 | } |
| 84 | int stripOrderIndex = 0; |
| 85 | switch (cube.wiring) { |
| 86 | case FRONT_LEFT: stripOrderIndex = 0; break; |
| 87 | case FRONT_RIGHT: stripOrderIndex = 1; break; |
| 88 | case REAR_LEFT: stripOrderIndex = 2; break; |
| 89 | case REAR_RIGHT: stripOrderIndex = 3; break; |
| 90 | } |
| 91 | for (int stripIndex : stripOrderings[stripOrderIndex]) { |
| 92 | Strip s = cube.strips.get(stripIndex); |
| 93 | for (int j = s.points.size() - 1; j >= 0; --j) { |
| 94 | points[pi++] = s.points.get(j).index; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public final void send(int[] colors) { |
| 103 | if (!enabled) { |
| 104 | return; |
| 105 | } |
| 106 | int len = 0; |
| 107 | int packetNum = 0; |
| 108 | for (int index : points) { |
| 109 | int c = colors[index]; |
| 110 | byte r = (byte) ((c >> 16) & 0xFF); |
| 111 | byte g = (byte) ((c >> 8) & 0xFF); |
| 112 | byte b = (byte) ((c) & 0xFF); |
| 113 | packet[len++] = 0; |
| 114 | packet[len++] = r; |
| 115 | packet[len++] = g; |
| 116 | packet[len++] = b; |
| 117 | |
| 118 | // Flush once packet is full buffer size |
| 119 | if (len >= packet.length) { |
| 120 | sendPacket(packetNum++); |
| 121 | len = 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Flush any remaining data |
| 126 | if (len > 0) { |
| 127 | sendPacket(packetNum++); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | private void sendPacket(int packetNum) { |
| 132 | message.clearArguments(); |
| 133 | message.add(packetNum); |
| 134 | message.add(packet.length); |
| 135 | message.add(packet); |
| 136 | try { |
| 137 | OscP5.flush(message, address); |
| 138 | } catch (Exception x) { |
| 139 | x.printStackTrace(); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |