Reworked spin controller
This commit is contained in:
parent
20fa65c926
commit
81ee61c779
1 changed files with 45 additions and 61 deletions
|
@ -1,13 +1,17 @@
|
||||||
package wash.control;
|
package wash.control;
|
||||||
|
|
||||||
import actor.ActorThread;
|
import actor.ActorThread;
|
||||||
import wash.io.WashingIO;
|
|
||||||
import wash.control.WashingMessage.Order;
|
import wash.control.WashingMessage.Order;
|
||||||
|
import wash.io.WashingIO;
|
||||||
|
import wash.io.WashingIO.Spin;
|
||||||
|
|
||||||
public final class ControllerSpin extends ActorThread<WashingMessage> {
|
public final class ControllerSpin extends ActorThread<WashingMessage> {
|
||||||
private WashingIO io;
|
private WashingIO io;
|
||||||
private Spin spin = new Spin();
|
|
||||||
private WashingMessage m;
|
private WashingMessage m;
|
||||||
|
private WashingMessage temp;
|
||||||
|
private boolean ackSent = true;
|
||||||
|
private Order spinState = Order.SPIN_OFF;
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
public ControllerSpin(WashingIO io) {
|
public ControllerSpin(WashingIO io) {
|
||||||
this.io = io;
|
this.io = io;
|
||||||
|
@ -15,70 +19,50 @@ public final class ControllerSpin extends ActorThread<WashingMessage> {
|
||||||
|
|
||||||
protected void sendAck() {
|
protected void sendAck() {
|
||||||
m.sendAck(this);
|
m.sendAck(this);
|
||||||
|
ackSent = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
spin.start();
|
long elapsed; // "Virtual" seconds elapsed, used for modulo ops in spin direction
|
||||||
listener(); // Drop into a recursive listener method
|
while (true) {
|
||||||
}
|
|
||||||
|
|
||||||
/** 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() {
|
|
||||||
try {
|
try {
|
||||||
loop();
|
temp = receiveWithTimeout(10000 / Settings.SPEEDUP);
|
||||||
} catch (InterruptedException e) {
|
elapsed = ((System.currentTimeMillis() - start) * (Settings.SPEEDUP) / 1000 * 60);
|
||||||
return;
|
|
||||||
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue