Bugfix - use pause flag
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 1 Dec 2019 22:11:32 +0000 (23:11 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 1 Dec 2019 22:11:32 +0000 (23:11 +0100)
src/kaka/cakelight/mode/AmbientMode.java
src/kaka/cakelight/mode/VideoMode.java

index 77f1bc3..ba816d5 100644 (file)
@@ -9,6 +9,7 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
     private Thread thread; // TODO move to a dynamic sub class
     protected Configuration config;
     private int type = 0;
     private Thread thread; // TODO move to a dynamic sub class
     protected Configuration config;
     private int type = 0;
+    private boolean isPaused = false;
 
     AmbientMode() {}
 
 
     AmbientMode() {}
 
@@ -26,15 +27,12 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
 
     @Override
     public void pause() {
 
     @Override
     public void pause() {
-        try {
-            thread.wait();
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
+        isPaused = true;
     }
 
     @Override
     public void resume() {
     }
 
     @Override
     public void resume() {
+        isPaused = false;
         thread.notify();
     }
 
         thread.notify();
     }
 
@@ -43,13 +41,16 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
         stopThread();
     }
 
         stopThread();
     }
 
-    public void startThread() {
+    private void startThread() {
         thread = new Thread() {
             public void run() {
                 try {
                     long start = System.currentTimeMillis();
                     int index = 0;
                     while (!isInterrupted()) {
         thread = new Thread() {
             public void run() {
                 try {
                     long start = System.currentTimeMillis();
                     int index = 0;
                     while (!isInterrupted()) {
+                        if (isPaused) {
+                            wait();
+                        }
                         LedFrame frame = LedFrame.from(config);
                         updateFrame(frame, System.currentTimeMillis() - start, index);
                         updateWithFrame(frame);
                         LedFrame frame = LedFrame.from(config);
                         updateFrame(frame, System.currentTimeMillis() - start, index);
                         updateWithFrame(frame);
@@ -57,13 +58,14 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
                         Thread.sleep(20);
                     }
                 } catch (InterruptedException e) {
                         Thread.sleep(20);
                     }
                 } catch (InterruptedException e) {
+                    e.printStackTrace();
                 }
             }
         };
         thread.start();
     }
 
                 }
             }
         };
         thread.start();
     }
 
-    public void stopThread() {
+    private void stopThread() {
         thread.interrupt();
     }
 
         thread.interrupt();
     }
 
index 1f43ffb..c7985e3 100644 (file)
@@ -15,6 +15,7 @@ public class VideoMode extends Mode {
     private Thread grabberThread;
     private Consumer<VideoFrame> frameConsumer;
     private VideoDeviceListener deviceListener;
     private Thread grabberThread;
     private Consumer<VideoFrame> frameConsumer;
     private VideoDeviceListener deviceListener;
+    private boolean isPaused = false;
 
     public VideoMode() {
         deviceListener = new VideoDeviceListener();
 
     public VideoMode() {
         deviceListener = new VideoDeviceListener();
@@ -29,15 +30,12 @@ public class VideoMode extends Mode {
 
     @Override
     public void pause() {
 
     @Override
     public void pause() {
-        try {
-            grabberThread.wait();
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
+        isPaused = true;
     }
 
     @Override
     public void resume() {
     }
 
     @Override
     public void resume() {
+        isPaused = false;
         grabberThread.notify();
     }
 
         grabberThread.notify();
     }
 
@@ -54,11 +52,14 @@ public class VideoMode extends Mode {
                 try (FrameGrabber grabber = FrameGrabber.from(videoDevice, config)) {
                     while (!isInterrupted()) {
                         Optional<VideoFrame> frame = grabber.grabFrame();
                 try (FrameGrabber grabber = FrameGrabber.from(videoDevice, config)) {
                     while (!isInterrupted()) {
                         Optional<VideoFrame> frame = grabber.grabFrame();
+                        if (isPaused) {
+                            wait();
+                        }
                         if (frameConsumer != null) frame.ifPresent(frameConsumer);
                         frame.ifPresent(VideoMode.this::onVideoFrame);
 //                        timeIt("frame", grabber::grabFrame);
                     }
                         if (frameConsumer != null) frame.ifPresent(frameConsumer);
                         frame.ifPresent(VideoMode.this::onVideoFrame);
 //                        timeIt("frame", grabber::grabFrame);
                     }
-                } catch (IOException e) {
+                } catch (IOException | InterruptedException e) {
                     e.printStackTrace();
                 }
             }
                     e.printStackTrace();
                 }
             }