diff --git a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java index 7a2f165b5..ed73197a2 100644 --- a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java +++ b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java @@ -9,7 +9,7 @@ public class TrainControllerImpl implements TrainController { private int step = 0; private int referenceSpeed = 0; private int speedLimit = 0; - + private Thread trainThickThread; @Override public void followSpeed() { if (referenceSpeed < 0) { @@ -24,6 +24,33 @@ public void followSpeed() { enforceSpeedLimit(); } + @Override + public void startTrainTickThread(int ThickInterval) throws + IllegalArgumentException{ + if (trainThickThread != null){ + throw new IllegalArgumentException("Thread exists already"); + } + this.trainThickThread= new Thread(()->{ + while (true){ + synchronized(this){ + followSpeed(); + } + try{ + Thread.sleep(ThickInterval); + }catch(InterruptedException e){ + System.out.println("Thread interrupted"); + } + } + }); + } + @Override + public void stopTrainTickThread(){ + if(trainThickThread!=null){ + trainThickThread.interrupt(); + trainThickThread=null; + System.out.println("Thread stopped"); + } + } @Override public int getReferenceSpeed() { @@ -51,4 +78,5 @@ public void setJoystickPosition(int joystickPosition) { public void setEmergencyBrake(boolean EmergencyBrake){referenceSpeed=0;} + } diff --git a/train-interfaces/src/main/java/hu/bme/mit/train/interfaces/TrainController.java b/train-interfaces/src/main/java/hu/bme/mit/train/interfaces/TrainController.java index c8ede325a..d3a7ba34f 100644 --- a/train-interfaces/src/main/java/hu/bme/mit/train/interfaces/TrainController.java +++ b/train-interfaces/src/main/java/hu/bme/mit/train/interfaces/TrainController.java @@ -12,4 +12,8 @@ public interface TrainController { public void setEmergencyBrake(boolean EmergencyBrake); + public void startTrainTickThread(int ThickInterval); + + public void stopTrainTickThread(); + }