First cut at an Open Pixel Control server
[SugarCubes.git] / OpenPixelControl.pde
1 import processing.net.*;
2
3 // Server for Open Pixel Control patterns (http://openpixelcontrol.org/)
4 class OpenPixelControl extends SCPattern {
5
6 int port = 7890;
7
8 Server server;
9 byte[] buffer;
10 int bufferedByteCount;
11
12 public OpenPixelControl(LX lx, PApplet parent) {
13 super(lx);
14 server = new Server(parent, port);
15 println("Listening for Open Pixel Control data on port " + port);
16
17 // Buffer space for two frames, worst case
18 buffer = new byte[0x10004 * 2];
19 bufferedByteCount = 0;
20
21 // Save a JSON layout file that some Open Pixel Control clients can use
22 writeMappingFile("openpixelcontrol-layout.json");
23 }
24
25 public void run(double deltaMs) {
26 readFromClient();
27 }
28
29 void readFromClient() {
30 Client client = server.available();
31
32 if (client == null) {
33 // No client; flush any stored partial frames
34 bufferedByteCount = 0;
35 return;
36 }
37
38 while (true) {
39 int available = client.available();
40 if (available <= 0) {
41 return;
42 }
43
44 if (bufferedByteCount == 0) {
45 // Read directly to buffer
46 bufferedByteCount = client.readBytes(buffer);
47 } else {
48 // Append to an earlier partial frame
49 byte[] additional = client.readBytes();
50 arrayCopy(additional, 0, buffer, bufferedByteCount, additional.length);
51 bufferedByteCount += additional.length;
52 }
53
54 // Extract OPC packets from buffer
55 int offset = 0;
56 while (bufferedByteCount - offset >= 4) {
57 int channel = buffer[offset + 0] & 0xFF;
58 int command = buffer[offset + 1] & 0xFF;
59 int length = ((buffer[offset + 2] & 0xFF) << 8) | (buffer[offset + 3] & 0xFF);
60
61 if (bufferedByteCount - offset < length + 4) {
62 // Not enough data for a full packet yet
63 break;
64 }
65
66 // Handle the packet in-place
67 offset += 4;
68 opcPacket(channel, command, offset, length);
69 offset += length;
70 }
71
72 // If we didn't use the whole buffer, save remainder for later
73 bufferedByteCount -= offset;
74 arrayCopy(buffer, offset, buffer, 0, bufferedByteCount);
75 }
76 }
77
78 void opcPacket(int channel, int command, int offset, int length) {
79 // Only look at "Set Pixel Colors" to channel 0.
80 if (channel == 0 && command == 0) {
81
82 // Unpack colors directly into framebuffer
83 for (int i = 0; i < length / 3; i++) {
84 if (i >= colors.length) {
85 break;
86 }
87
88 colors[i] =
89 ((buffer[offset + 0] & 0xFF) << 16) |
90 ((buffer[offset + 1] & 0xFF) << 8) |
91 (buffer[offset + 2] & 0xFF) ;
92
93 offset += 3;
94 }
95 }
96 }
97
98 void writeMappingFile(String filename) {
99 PrintWriter output;
100 output = createWriter(filename);
101
102 // Rearrange points by color buffer index
103 LXPoint[] orderedPoints;
104 int maxIndex = 0;
105 for (LXPoint p : model.points) {
106 maxIndex = max(maxIndex, p.index);
107 }
108 orderedPoints = new LXPoint[maxIndex + 1];
109 for (LXPoint p : model.points) {
110 orderedPoints[p.index] = p;
111 }
112
113 float xCenter = (model.xMax + model.xMin) / 2.0;
114 float yCenter = (model.yMax + model.yMin) / 2.0;
115 float zCenter = (model.zMax + model.zMin) / 2.0;
116 float xSize = model.xMax - model.xMin;
117 float zSize = model.zMax - model.zMin;
118 float maxSize = max(xSize, zSize);
119 float scale = 4.0 / maxSize;
120
121 output.print("[");
122 for (int i = 0; i < orderedPoints.length; i++) {
123 boolean isLast = i == orderedPoints.length - 1;
124 String comma = isLast ? "" : ",";
125 LXPoint p = orderedPoints[i];
126
127 if (p == null) {
128 // Unused index
129 output.print("null" + comma);
130 } else {
131 // Transform coordinates to make sense in the OPC visualizer
132 float x = (p.x - xCenter) * scale;
133 float y = (p.z - zCenter) * scale;
134 float z = (p.y - yCenter) * scale;
135
136 output.print("{\"point\":[" + x + "," + y + "," + z + "]}" + comma);
137 }
138 }
139 output.print("]");
140
141 output.flush();
142 output.close();
143 println("Saved OpenPixelControl model to " + filename);
144 }
145 };