From 81ee61c7797ceb2fbd55d836aa8bec6e571ad263 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 15 Nov 2024 10:56:04 +0100 Subject: [PATCH] Reworked spin controller --- wash/src/wash/control/ControllerSpin.java | 106 +++++++++------------- 1 file changed, 45 insertions(+), 61 deletions(-) diff --git a/wash/src/wash/control/ControllerSpin.java b/wash/src/wash/control/ControllerSpin.java index 7ec645b..5396334 100644 --- a/wash/src/wash/control/ControllerSpin.java +++ b/wash/src/wash/control/ControllerSpin.java @@ -1,13 +1,17 @@ package wash.control; import actor.ActorThread; -import wash.io.WashingIO; import wash.control.WashingMessage.Order; +import wash.io.WashingIO; +import wash.io.WashingIO.Spin; public final class ControllerSpin extends ActorThread { private WashingIO io; - private Spin spin = new Spin(); private WashingMessage m; + private WashingMessage temp; + private boolean ackSent = true; + private Order spinState = Order.SPIN_OFF; + long start = System.currentTimeMillis(); public ControllerSpin(WashingIO io) { this.io = io; @@ -15,70 +19,50 @@ public final class ControllerSpin extends ActorThread { protected void sendAck() { m.sendAck(this); + ackSent = true; } @Override public void run() { - spin.start(); - listener(); // Drop into a recursive listener method - } - - /** Recursive listener method */ - public void listener() { - if(isInterrupted()) { - spin.interrupt(); - return; - } - - try { - m = take(); - } catch (Exception e) { - System.exit(1); - } - - switch (m.order()) { - case Order.SPIN_OFF -> spin.setMode(WashingIO.Spin.IDLE); - case Order.SPIN_FAST -> spin.setMode(WashingIO.Spin.FAST); - case Order.SPIN_SLOW -> spin.setMode(WashingIO.Spin.LEFT); - default -> { - listener(); - } - } - - listener(); - } - - private final class Spin extends Thread { - private WashingIO.Spin spinMode; - boolean changed = false; - - public Spin() { - spinMode = WashingIO.Spin.IDLE; - } - - public synchronized void setMode(WashingIO.Spin m) { - spinMode = m; - changed = true; - } - - private void loop() throws InterruptedException { - io.setSpinMode(spinMode); - - if (changed) { - sendAck(); - changed = false; - } - - Thread.sleep(60000 / Settings.SPEEDUP); - loop(); - } - - @Override - public void run() { + long elapsed; // "Virtual" seconds elapsed, used for modulo ops in spin direction + while (true) { try { - loop(); - } catch (InterruptedException e) { - return; + temp = receiveWithTimeout(10000 / Settings.SPEEDUP); + elapsed = ((System.currentTimeMillis() - start) * (Settings.SPEEDUP) / 1000 * 60); + + // If there is a new message, swap + if (temp != null) { + m = temp; + ackSent = false; // We have a new order + } + + // If this message is not the same as last time + if (m != null && m.order() != spinState) { + spinState = m.order(); + } + + switch (spinState) { + case Order.SPIN_OFF -> { + io.setSpinMode(Spin.IDLE); + } + case Order.SPIN_FAST -> { + io.setSpinMode(Spin.FAST); + } + case Order.SPIN_SLOW -> { + if (elapsed % 2 == 0) + io.setSpinMode(Spin.RIGHT); + else + io.setSpinMode(Spin.LEFT); + } + default -> { + continue; + } + } + + if (m != null && !ackSent) + sendAck(); + } catch (Exception e) { + System.out.println(e); } } }