Commit 1048f78c authored by Mathias Haage's avatar Mathias Haage
Browse files

Add ht15 washing

parent 653895e6
......@@ -2,3 +2,4 @@
.recommenders
alarmclock/bin
lift/bin
washing/bin
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="resources/img.jar"/>
<classpathentry kind="lib" path="/ljrt/ljrt.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>washing</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
#Mon Aug 20 20:33:19 CEST 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
/*
* Real-time and concurrent programming course, laboratory 3
* Department of Computer Science, Lund Institute of Technology
*
* PP 980728 Created
* PP 990924 Revised
*/
package done;
/**
* Abstract description of a washing machine. Specialized
* into simulations and actual hardware implementations.
*/
public abstract class AbstractWashingMachine {
/**
* Read the water temperature.
* @return Temperature in Centigrades (0-100).
*/
public abstract double getTemperature();
/**
* Read the water level in the machine.
* @return A real number between 0 and 1, where 1 indicates an
* absolutely full (i.e. overflowing) machine and 0
* indicates a (practically) empty one.
*/
public abstract double getWaterLevel();
/**
* Check if the front door is open.
* @return True if the door is locked, false if it is open.
*/
public abstract boolean isLocked();
/**
* Turns the heating on/off.
* @param on True means "heat on", false means "heat off".
*/
public abstract void setHeating(boolean on);
/**
* Open/close the water input tap.
* @param on True means "open tap", false means "close tap".
*/
public abstract void setFill(boolean on);
/**
* Start/stop the water drain pump.
* @param on True means "start pump", false means "stop pump".
*/
public abstract void setDrain(boolean on);
/**
* Lock/unlock the front door.
* @param on True means "lock door", false means "unlock door".
*/
public abstract void setLock(boolean on);
/**
* Control the turning motor.
* @param direction SPIN_OFF, SPIN_LEFT, SPIN_RIGHT, or SPIN_FAST.
*/
public abstract void setSpin(int direction);
/**
* Activate the washing machine. Called early during initialization
* by main().
*/
public abstract void start();
// ------------------------------------------------- PACKAGE PRIVATE METHODS
/**
* Set button listener. The listener's processButton() method will be
* called whenever a button is pressed.
*/
abstract void setButtonListener(ButtonListener l);
// ------------------------------------------------------- PRIVATE CONSTANTS
/**
* Stop spin.
*/
public static final int SPIN_OFF = 0;
/**
* Slow left spin.
*/
public static final int SPIN_LEFT = 1;
/**
* Slow right spin.
*/
public static final int SPIN_RIGHT = 2;
/**
* Fast left spin.
*/
public static final int SPIN_FAST = 3;
};
/*
* Real-time and concurrent programming course, laboratory 3
* Department of Computer Science, Lund Institute of Technology
*
* PP 980730
*/
package done;
/**
* Interface for handling interrupts due to button presses.
*/
public interface ButtonListener {
/**
* Called whenever one of the front buttons is pressed.
*
* @param theButton Indicates pressed button (0, 1, 2, or 3)
*/
void processButton(int theButton);
}
/*
* Real-time and concurrent programming course, laboratory 3
* Department of Computer Science, Lund Institute of Technology
*
* PP 980728 Created
* PP 990924 Revised
* MH 110927 Revised
*/
package done;
import java.awt.*;
import java.awt.event.*;
/**
* A little panel window with a few buttons to click. Used
* to manipulate washing machine parameters directly.
*/
class HackerPanel
extends Frame
implements ActionListener {
private static final long serialVersionUID = 1L;
// ---------------------------------------------------------- PUBLIC METHODS
public HackerPanel(AbstractWashingMachine mach) {
super("Washing Machine Hacker's Toolbox");
myMachine = mach;
setSize(PANEL_WIDTH, PANEL_HEIGHT);
setLayout(new GridLayout(4, 3));
Button b;
b = new Button("Heat on");
b.addActionListener(this);
add(b);
b = new Button("Heat off");
b.addActionListener(this);
add(b);
b = new Button("Fill on");
b.addActionListener(this);
add(b);
b = new Button("Fill off");
b.addActionListener(this);
add(b);
b = new Button("Drain on");
b.addActionListener(this);
add(b);
b = new Button("Drain off");
b.addActionListener(this);
add(b);
b = new Button("Lock");
b.addActionListener(this);
add(b);
b = new Button("Unlock");
b.addActionListener(this);
add(b);
b = new Button("Spin off");
b.addActionListener(this);
add(b);
b = new Button("Spin left");
b.addActionListener(this);
add(b);
b = new Button("Spin right");
b.addActionListener(this);
add(b);
b = new Button("Spin fast");
b.addActionListener(this);
add(b);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
setVisible(true);
}
/**
* Listener method; automatically called by AWT upon
* window events, such as when a button is clicked.
*
* @param e An ActionEvent describing the event.
*/
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("Heat on"))
myMachine.setHeating(true);
else if(command.equals("Heat off"))
myMachine.setHeating(false);
else if(command.equals("Fill on"))
myMachine.setFill(true);
else if(command.equals("Fill off"))
myMachine.setFill(false);
else if(command.equals("Drain on"))
myMachine.setDrain(true);
else if(command.equals("Drain off"))
myMachine.setDrain(false);
else if(command.equals("Lock"))
myMachine.setLock(true);
else if(command.equals("Unlock"))
myMachine.setLock(false);
else if(command.equals("Spin off"))
myMachine.setSpin(AbstractWashingMachine.SPIN_OFF);
else if(command.equals("Spin left"))
myMachine.setSpin(AbstractWashingMachine.SPIN_LEFT);
else if(command.equals("Spin right"))
myMachine.setSpin(AbstractWashingMachine.SPIN_RIGHT);
else if(command.equals("Spin fast"))
myMachine.setSpin(AbstractWashingMachine.SPIN_FAST);
}
/**
* Handle window closing.
* @param e The WindowEvent provided by AWT.
*/
public void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING)
System.exit(0);
}
// ---------------------------------------------- PRIVATE INSTANCE VARIABLES
private AbstractWashingMachine myMachine;
// ------------------------------------------------------- PRIVATE CONSTANTS
private static final int PANEL_WIDTH = 300;
private static final int PANEL_HEIGHT = 200;
}
/*
* Real-time and concurrent programming course, laboratory 3
* Department of Computer Science, Lund Institute of Technology
*
* PP 980925 Created
* PP 990924 Revised
*/
package done;
import java.awt.*;
import java.awt.event.*;
/**
* A dialog for sarcastic messages when things go wrong.
*
* @param parent Parent Frame
* @param title Dialog title
* @param msg Sarcastic message to print in dialog window
* @param image Image to display in window center
* @param imageW Image width (pixels)
* @param imageH Image height (pixels)
* @param bgColor Dialog window background color
* @param fgColor Dialog window foreground color
*/
class SarcasticDialog extends Dialog implements ActionListener {
private static final long serialVersionUID = 1L;
// --------------------------------------------- PACKAGE PRIVATE CONSTRUCTOR
/**
*
*/
SarcasticDialog(Frame parent,
String title,
String msg,
Image image,
int imageW,
int imageH,
Color bgColor,
Color fgColor) {
super(parent, title, true);
setBackground(bgColor);
setForeground(fgColor);
Label l = new Label(msg, Label.CENTER);
add("North", l);
add("East", new ImageCanvas(image, imageW, imageH, bgColor));
Button b = new Button("OK");
add("South", b);
b.addActionListener(this);
setSize(WIDTH, HEIGHT);
}
// ------------------------------------------------------ OVERRIDDEN METHODS
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("OK"))
System.exit(0);
}
// ----------------------------------------------------------- INNER CLASSES
/**
* A little thing for putting an image in a container.
*/
private class ImageCanvas extends Canvas {
private static final long serialVersionUID = 1L;
ImageCanvas(Image i, // Image to draw
int w, // Width of image
int h, // Height
Color c) { // Color for transp. background
super();
myImage = i;
setBackground(myColor = c);
setSize(myWidth = w, myHeight = h);
setVisible(true);
}
// Overridden paint method
public void paint(Graphics g) {
g.drawImage(myImage, 0, 0, myWidth, myHeight, myColor, this);
}
// Private stuff
private Image myImage;
private int myWidth, myHeight;
private Color myColor;
}
// ------------------------------------------------------- PRIVATE CONSTANTS
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
}
/*
* Real-time and concurrent programming course, washing machine lab.
* Department of Computer Science, Lund Institute of Technology
*
* PP 980923 Created
* PP 990924 Revised
*/
package done;
import todo.WashingController;
/**
* Main program. Basically just parses the command line and then
* creates a WashingMachineSimulation and a WashingController.
*
* @see todo.WashingController
*/
public class Wash {
// ------------------------------------------------------------- MAIN METHOD
/**
* Parses command line arguments and creates a WashingController.
*
* Possible arguments:
* <DL>
* <DT>-speed N <DD>Speeds up simulation N times.
* <DT>-hackermode <DD>Adds a panel with a few useful buttons.
* <DT>-help <DD>Does NOT provide any help.
* </DL>
*
* @exception NumberFormatException When the numeric speed argument fails.
* @exception InterruptedException Won't happen, I'm just too lazy
* to catch it.
*/
public static void main(String[] args)
throws NumberFormatException, InterruptedException {
// Options
double theSpeed = 50.0;
boolean useHackerPanel = false;
double freakShowProbability = 0.00333333; // Once a minute
// Read options
for(int i = 0; i < args.length; i++) {
if (args[i].equals("-speed") && i < (args.length - 1)) {
theSpeed = (new Double(args[i + 1])).doubleValue();
i++; // Don't consider the speed anymore
}
else if (args[i].equals("-freakshowprobability")) {
freakShowProbability = (new Double(args[i + 1])).doubleValue();
i++; // Don't consider the probability anymore
}
else if (args[i].equals("-hackermode"))
useHackerPanel = true;
else if (args[i].equals("-help")) {
System.exit(0);
}
else
System.out.println("Unrecognized command line argument '"
+ args[i] + "'. Ignored.");
}
// Create a washing machine simulation and a controller
AbstractWashingMachine theMachine
= new WashingMachineSimulation(theSpeed, freakShowProbability);
@SuppressWarnings("unused")
HackerPanel p;
if (useHackerPanel) {
p = new HackerPanel(theMachine);
}
theMachine.setButtonListener(new WashingController(theMachine, theSpeed));
theMachine.start();
// Wait for hell to freeze over
Object o = new Object();
synchronized(o) {
o.wait();
}
}
};
/*
* Real-time and concurrent programming course, laboratory 3
* Department of Computer Science, Lund Institute of Technology
*
* PP 980813 Created
* PP 990924 Revised, now uses OngoingThread instead of java.lang.Thread
*/
package done;
import se.lth.cs.realtime.*;
/**
* Implementation of abstract AbstractWashingMachine class.
* Interfaces to the simulation.
*/
class WashingMachineSimulation
extends AbstractWashingMachine
implements Runnable, java.awt.event.ActionListener {
// ------------------------------------------------------------- CONSTRUCTOR
/**
* @param speed Simulation speed (1 for normal speed, >1 for faster)
*/
public WashingMachineSimulation(double speed,
double freakShowProbability) {
mySpeed = speed;
myTemperature = AMBIENT_TEMP;
myWaterLevel = 0;
iAmLocked =
iAmHeating =
iAmFilling =
iAmDraining =
iAmOverHeating =
iAmOverFlowing = false;
mySpin = SPIN_OFF;
myButtonListener = null;
myThread = new OngoingThread(this);
myView = new WashingView(this, mySpeed, freakShowProbability);
}
// ------------------------------------------------- PACKAGE PRIVATE METHODS
/**
* Set button listener. The listener's processButton() method will be
* called whenever a button is pressed.
*/
void setButtonListener(ButtonListener l) {
myButtonListener = l;
}
/**
* @return True if currently overheating, false otherwise.
*/
synchronized boolean isOverHeating() {
return iAmOverHeating;
}
/**
* @return True if currently overflowing, false otherwise.
*/
synchronized boolean isOverFlowing() {
return iAmOverFlowing;
}
/**
* @return True if currently draining, false otherwise.
*/
synchronized boolean isDraining() {
return iAmDraining;
}
/**
* @return True if currently filling, false otherwise.
*/
synchronized boolean isFilling() {
return iAmFilling;
}
/**
* @return Current spin (0..3)
*/
synchronized int getSpin() {
return mySpin;
}
// ------------------------------------------------------ OVERRIDDEN METHODS
/**
* Read the water temperature.
* @return Temperature in Centigrades (0-100).
*/
public synchronized double getTemperature() {
return myTemperature;
}
/**
* Read the water level in the machine.
* @return A real number between 0 and 1, where 1 indicates an
* absolutely full (i.e. overflowing) machine and 0
* indicates a (practically) empty one.
*/
public synchronized double getWaterLevel() {
return myWaterLevel / MAX_WATER_LEVEL;
}
/**
* Check if the front door is open.
* @return True if the door is locked, false if it is open.
*/
public synchronized boolean isLocked() {
return iAmLocked;
}
/**
* Check whether currently heating.
* @return True if currently heating, false otherwise.