public void pushMode(Mode mode) {
Objects.requireNonNull(mode);
if (!modes.isEmpty()) {
- stopMode(modes.peek());
+ pauseMode(modes.peek());
}
modes.push(mode);
startMode(mode);
Mode mode = modes.pop();
stopMode(mode);
if (!modes.isEmpty()) {
- startMode(modes.peek());
+ resumeMode(modes.peek());
}
return true;
}
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();
}
@Override
+ public void pause() {
+ try {
+ thread.wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void resume() {
+ thread.notify();
+ }
+
+ @Override
public void exit() {
stopThread();
}
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) {
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() {
}
}
}
@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();