Permalink
Browse files

simulator now sleeps when no data is recieved

  • Loading branch information...
mhenstell committed Jan 5, 2014
1 parent 6b8d1a1 commit 4f55521c995699cdd685f9eb2670754ea723a87b
Showing with 50 additions and 36 deletions.
  1. +50 −36 hexasimulator/hexasimulator.pde
@@ -22,42 +22,50 @@ Segment segment2;
ArrayList<LinkedBlockingQueue> queues;
ArrayList<Segment> segments;
+boolean stopped;
+long lastRecieved;
+long timeout = 5000; // Turn off draw loop when no data recieved
+
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);
+
+ 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);
+void receive(byte[] data, String ip, int port) {
+ // Restart draw loop
+ lastRecieved = millis();
+ loop();
+
+ 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;
@@ -69,7 +77,7 @@ void receive(byte[] data, String ip, int port) {
}
if (queues.get(num).size() > 0) {
-// println("Buffer full, dropping frame!");
+ // println("Buffer full, dropping frame!");
return;
}
@@ -80,7 +88,6 @@ void receive(byte[] data, String ip, int port) {
catch( InterruptedException e ) {
println("Interrupted Exception caught");
}
-
}
int convertByte(byte b) {
@@ -89,9 +96,16 @@ int convertByte(byte b) {
void draw() {
background(0);
-
+
for (Segment segment : segments) {
- segment.draw();
+ segment.draw();
}
+ // Turn off draw loop when no data is being recieved
+ if (millis() - lastRecieved > timeout) {
+ stopped = true;
+ noLoop();
+ println("Paused.");
+ }
}
+

0 comments on commit 4f55521

Please sign in to comment.