5 // Server for Open Pixel Control patterns (http://openpixelcontrol.org/)
6 class OpenPixelControl extends SCPattern implements Runnable {
12 int bufferedByteCount;
14 public OpenPixelControl(LX lx, PApplet parent) {
16 parent.registerMethod("dispose", this);
18 // Save a JSON layout file that some Open Pixel Control clients can use
19 writeMappingFile("openpixelcontrol-layout.json");
21 // Buffer space for two frames, worst case
22 buffer = new byte[0x10004 * 2];
23 bufferedByteCount = 0;
26 public void dispose() {
31 } catch (IOException e) {
39 // Thread run function; handle OPC traffic.
40 while (Thread.currentThread() == thread) {
43 Socket cli = server.accept();
44 InputStream input = cli.getInputStream();
45 bufferedByteCount = 0;
47 while (Thread.currentThread() == thread) {
49 int r = input.read(buffer, bufferedByteCount, buffer.length - bufferedByteCount);
53 bufferedByteCount += r;
55 // Extract OPC packets from buffer
57 while (bufferedByteCount - offset >= 4) {
58 int channel = buffer[offset + 0] & 0xFF;
59 int command = buffer[offset + 1] & 0xFF;
60 int length = ((buffer[offset + 2] & 0xFF) << 8) | (buffer[offset + 3] & 0xFF);
62 if (bufferedByteCount - offset < length + 4) {
63 // Not enough data for a full packet yet
67 // Handle the packet in-place
69 opcPacket(channel, command, offset, length);
73 // If we didn't use the whole buffer, save remainder for later
74 bufferedByteCount -= offset;
75 arrayCopy(buffer, offset, buffer, 0, bufferedByteCount);
80 } catch (IOException e) {
86 public void run(double deltaMs) {
87 // SCPattern run function; nothing to do except start our thread the first time.
91 server = new ServerSocket(this.port);
92 thread = new Thread(this);
94 println("Listening for Open Pixel Control data on port " + port);
96 } catch (IOException e) {
103 void opcPacket(int channel, int command, int offset, int length) {
104 // Only look at "Set Pixel Colors" to channel 0.
105 if (channel == 0 && command == 0) {
107 // Unpack colors directly into framebuffer
108 for (int i = 0; i < length / 3; i++) {
109 if (i >= colors.length) {
114 ((buffer[offset + 0] & 0xFF) << 16) |
115 ((buffer[offset + 1] & 0xFF) << 8) |
116 (buffer[offset + 2] & 0xFF) ;
123 void writeMappingFile(String filename) {
125 output = createWriter(filename);
127 // Rearrange points by color buffer index
128 LXPoint[] orderedPoints;
130 for (LXPoint p : model.points) {
131 maxIndex = max(maxIndex, p.index);
133 orderedPoints = new LXPoint[maxIndex + 1];
134 for (LXPoint p : model.points) {
135 orderedPoints[p.index] = p;
138 float xCenter = (model.xMax + model.xMin) / 2.0;
139 float yCenter = (model.yMax + model.yMin) / 2.0;
140 float zCenter = (model.zMax + model.zMin) / 2.0;
141 float xSize = model.xMax - model.xMin;
142 float zSize = model.zMax - model.zMin;
143 float maxSize = max(xSize, zSize);
144 float scale = 4.0 / maxSize;
147 for (int i = 0; i < orderedPoints.length; i++) {
148 boolean isLast = i == orderedPoints.length - 1;
149 String comma = isLast ? "" : ",";
150 LXPoint p = orderedPoints[i];
154 output.print("null" + comma);
156 // Transform coordinates to make sense in the OPC visualizer
157 float x = (p.x - xCenter) * scale;
158 float y = (p.z - zCenter) * scale;
159 float z = (p.y - yCenter) * scale;
161 output.print("{\"point\":[" + x + "," + y + "," + z + "]}" + comma);
168 println("Saved OpenPixelControl model to " + filename);