Permalink
Browse files

adding hexasimulator

  • Loading branch information...
1 parent 723d780 commit a7cdc01f312594cc17ba6744a486f171a2b0a227 @mhenstell mhenstell committed Jun 28, 2013
Showing with 139 additions and 0 deletions.
  1. +42 −0 hexasimulator/Segment.pde
  2. +97 −0 hexasimulator/hexasimulator.pde
View
@@ -0,0 +1,42 @@
+class Segment {
+
+ int num;
+ PVector pos;
+ byte[] data;
+ LinkedBlockingQueue queue;
+
+ Segment(int num, PVector pos, LinkedBlockingQueue queue) {
+ this.num = num;
+ this.pos = pos;
+ this.queue = queue;
+ this.data = new byte[121];
+ }
+
+ void draw() {
+
+ byte newData[] = (byte[]) this.queue.poll();
+ if (newData != null) this.data = newData;
+
+ pushMatrix();
+ translate(this.pos.x, this.pos.y);
+
+
+ for (int col = 0; col < 120; col++) {
+
+ byte currentByte = this.data[col + 1];
+
+ for (int row = 0; row < 8; row++) {
+
+ color fillColor;
+ if (((currentByte >>> (7 - row)) & 1) != 0) fillColor = color(255, 0, 0);
+ else fillColor = color(50, 0, 0);
+
+ fill(fillColor);
+ if (col < 60) ellipse(col * PIXEL_SPACING, row * PIXEL_SPACING, PIXEL_DIAMETER, PIXEL_DIAMETER);
+ else ellipse(col * PIXEL_SPACING + SCREEN_SPACING, row * PIXEL_SPACING, PIXEL_DIAMETER, PIXEL_DIAMETER);
+ }
+ }
+ popMatrix();
+ }
+}
+
@@ -0,0 +1,97 @@
+import hypermedia.net.*;
+import java.util.concurrent.*;
+
+final static int SEGMENT_WIDTH = 120;
+final static int SEGMENT_HEIGHT = 7;
+final static int PIXEL_SPACING = 4;
+final static int PIXEL_DIAMETER = 3;
+final static int SCREEN_SPACING = 3;
+
+UDP udp0;
+UDP udp1;
+UDP udp2;
+
+LinkedBlockingQueue imageQueue0;
+LinkedBlockingQueue imageQueue1;
+LinkedBlockingQueue imageQueue2;
+
+Segment segment0;
+Segment segment1;
+Segment segment2;
+
+ArrayList<LinkedBlockingQueue> queues;
+ArrayList<Segment> segments;
+
+void setup() {
+
+ size((SEGMENT_WIDTH * PIXEL_SPACING) * 3 + SCREEN_SPACING * 6, PIXEL_SPACING * SEGMENT_HEIGHT + (SCREEN_SPACING * 2));
+ frameRate(60);
+
+ imageQueue0 = new LinkedBlockingQueue();
+ imageQueue1 = new LinkedBlockingQueue();
+ imageQueue2 = new LinkedBlockingQueue();
+
+ queues = new ArrayList<LinkedBlockingQueue>();
+ queues.add(imageQueue0);
+ queues.add(imageQueue1);
+ queues.add(imageQueue2);
+
+ segment0 = new Segment(0, new PVector(SCREEN_SPACING, SCREEN_SPACING), imageQueue0);
+ segment1 = new Segment(1, new PVector((SCREEN_SPACING * 3) + (SEGMENT_WIDTH * PIXEL_SPACING) * 1, SCREEN_SPACING), imageQueue1);
+ segment2 = new Segment(2, new PVector((SCREEN_SPACING * 5) + (SEGMENT_WIDTH * PIXEL_SPACING) * 2, SCREEN_SPACING), imageQueue2);
+
+ segments = new ArrayList<Segment>();
+ segments.add(segment0);
+ segments.add(segment1);
+ segments.add(segment2);
+
+ udp0 = new UDP(this, 9990);
+ udp1 = new UDP(this, 9991);
+ udp2 = new UDP(this, 9992);
+ udp0.listen(true);
+ udp1.listen(true);
+ udp2.listen(true);
+}
+
+void receive(byte[] data, String ip, int port) {
+
+ int num = data[0];
+// println("Got " + (data.length - 1) + " from unit " + num);
+
+ if (num > 2 || num < 0) {
+ println("Packet header mismatch. Expected 0 or 1 or 2, got " + num);
+ return;
+ }
+
+ if (data.length != 120 + 1) {
+ println("Packet size mismatch. Expected 121, got " + data.length);
+ return;
+ }
+
+ if (queues.get(num).size() > 0) {
+// println("Buffer full, dropping frame!");
+ return;
+ }
+
+
+ try {
+ queues.get(num).put(data);
+ }
+ catch( InterruptedException e ) {
+ println("Interrupted Exception caught");
+ }
+
+}
+
+int convertByte(byte b) {
+ return (b<0) ? 256+b : b;
+}
+
+void draw() {
+ background(0);
+
+ for (Segment segment : segments) {
+ segment.draw();
+ }
+
+}

0 comments on commit a7cdc01

Please sign in to comment.