| 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 | int pi = 0; |
| 66 | for (int[] channel : pm.channelList) { |
| 67 | for (int cubeNumber : channel) { |
| 68 | if (cubeNumber <= 0) { |
| 69 | for (int i = 0; i < Cube.POINTS_PER_CUBE; ++i) { |
| 70 | points[pi++] = 0; |
| 71 | } |
| 72 | } else { |
| 73 | Cube cube = model.getCubeByRawIndex(cubeNumber); |
| 74 | if (cube == null) { |
| 75 | throw new RuntimeException("Non-zero, non-existing cube specified in channel mapping (" + cubeNumber + ")"); |
| 76 | } |
| 77 | final int[] stripOrder = new int[] { |
| 78 | 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 |
| 79 | }; |
| 80 | for (int stripIndex : stripOrder) { |
| 81 | Strip s = cube.strips.get(stripIndex); |
| 82 | for (int j = s.points.size() - 1; j >= 0; --j) { |
| 83 | points[pi++] = s.points.get(j).index; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public final void send(int[] colors) { |
| 92 | if (!enabled) { |
| 93 | return; |
| 94 | } |
| 95 | int len = 0; |
| 96 | int packetNum = 0; |
| 97 | for (int index : points) { |
| 98 | int c = colors[index]; |
| 99 | byte r = (byte) ((c >> 16) & 0xFF); |
| 100 | byte g = (byte) ((c >> 8) & 0xFF); |
| 101 | byte b = (byte) ((c) & 0xFF); |
| 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++); |
| 110 | len = 0; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Flush any remaining data |
| 115 | if (len > 0) { |
| 116 | sendPacket(packetNum++); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private void sendPacket(int packetNum) { |
| 121 | message.clearArguments(); |
| 122 | message.add(packetNum); |
| 123 | message.add(packet.length); |
| 124 | message.add(packet); |
| 125 | try { |
| 126 | OscP5.flush(message, address); |
| 127 | } catch (Exception x) { |
| 128 | x.printStackTrace(); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |