Pause/resume modes when stacking
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 1 Dec 2019 21:29:06 +0000 (22:29 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 1 Dec 2019 21:29:06 +0000 (22:29 +0100)
src/kaka/cakelight/CakeLight.java
src/kaka/cakelight/mode/AmbientMode.java
src/kaka/cakelight/mode/Mode.java
src/kaka/cakelight/mode/SingleColorMode.java
src/kaka/cakelight/mode/VideoMode.java

index 0c1aa1a..822e2ab 100644 (file)
@@ -34,7 +34,7 @@ public class CakeLight {
     public void pushMode(Mode mode) {
         Objects.requireNonNull(mode);
         if (!modes.isEmpty()) {
-            stopMode(modes.peek());
+            pauseMode(modes.peek());
         }
         modes.push(mode);
         startMode(mode);
@@ -46,7 +46,7 @@ public class CakeLight {
             Mode mode = modes.pop();
             stopMode(mode);
             if (!modes.isEmpty()) {
-                startMode(modes.peek());
+                resumeMode(modes.peek());
             }
             return true;
         }
@@ -59,6 +59,14 @@ public class CakeLight {
         mode.enter(config);
     }
 
+    private void pauseMode(Mode mode) {
+        mode.pause();
+    }
+
+    private void resumeMode(Mode mode) {
+        mode.resume();
+    }
+
     private void stopMode(Mode mode) {
         mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting
         mode.exit();
index bc8a680..77f1bc3 100644 (file)
@@ -25,6 +25,20 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
     }
 
     @Override
+    public void pause() {
+        try {
+            thread.wait();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void resume() {
+        thread.notify();
+    }
+
+    @Override
     public void exit() {
         stopThread();
     }
index 5ece5ea..a72e706 100644 (file)
@@ -9,6 +9,8 @@ public abstract class Mode {
     private Consumer<LedFrame> frameListener;
 
     public abstract void enter(Configuration config);
+    public abstract void pause();
+    public abstract void resume();
     public abstract void exit();
 
     public void setFrameListener(Consumer<LedFrame> listener) {
index 36c752a..3cf4b53 100644 (file)
@@ -6,19 +6,33 @@ import kaka.cakelight.LedFrame;
 
 public class SingleColorMode extends Mode {
     private Color color;
+    private LedFrame frame;
 
     public SingleColorMode(Color c) {
         color = c;
     }
 
-    @Override
-    public void enter(Configuration config) {
-        LedFrame frame = LedFrame.from(config);
+    private void colorizeFrame() {
         frame.fillColor(color);
         updateWithFrame(frame);
     }
 
     @Override
+    public void enter(Configuration config) {
+        frame = LedFrame.from(config);
+        colorizeFrame();
+    }
+
+    @Override
+    public void pause() {
+    }
+
+    @Override
+    public void resume() {
+        colorizeFrame();
+    }
+
+    @Override
     public void exit() {
     }
 }
index 91a9f04..1f43ffb 100644 (file)
@@ -28,6 +28,20 @@ public class VideoMode extends Mode {
     }
 
     @Override
+    public void pause() {
+        try {
+            grabberThread.wait();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void resume() {
+        grabberThread.notify();
+    }
+
+    @Override
     public void exit() {
         grabberThread.interrupt();
         deviceListener.stopListening();