Commit d86e97d1 authored by Mathias Haage's avatar Mathias Haage
Browse files

Add ht16 alarmclock

parent 5f937c51
......@@ -36,12 +36,12 @@ public class ClockCanvas extends Canvas {
public void paint(Graphics g) {
backbuff.drawImage(clockimg, 0, 0, this);
if (parent.b1) {
if (parent.bs) {
backbuff.drawImage(btnblue_p, 90, 120, this);
} else {
backbuff.drawImage(btnblue, 90, 120, this);
}
if (parent.b2) {
if (parent.bc) {
backbuff.drawImage(btnblue_p, 90, 150, this);
} else {
backbuff.drawImage(btnblue, 90, 150, this);
......
......@@ -9,27 +9,27 @@ import todo.AlarmClock;
public class ClockGUI extends Applet implements KeyListener, ClockTimeDisplay, ItemListener {
private static final long serialVersionUID = 1L;
// Clock input mode
static final int MODE_NONE =0;
static final int MODE_TSHOW =1;
static final int MODE_ASET =2;
static final int MODE_TSET =3;
static final int MODE_ATOGGLE =4;
boolean b1 = false;
boolean b2 = false;
private boolean b1p = false;
private boolean b2p = false;
boolean bl = false;
boolean bu = false;
boolean bd = false;
boolean br = false;
static final int MODE_NONE = 0;
static final int MODE_TSHOW = 1;
static final int MODE_ASET = 2;
static final int MODE_TSET = 3;
static final int MODE_ATOGGLE = 4;
// Key states, current and previous, also used by canvas
boolean bs, bsp; // shift
boolean bc, bcp; // control
boolean bl, blp; // left
boolean br, brp; // right
boolean bu, bup; // up
boolean bd, bdp; // down
ClockInput clkinput;
private ClockOutput clkoutput;
int cursor = 0;
int clockmode = MODE_NONE;
int clocktime = 0;// Clock time
int alarmtime = 0;// Alarm time
int clocktime = 0; // Clock time
int alarmtime = 0; // Alarm time
private int inputval = 0;// Input value
private boolean hiding = false;
private AlarmClock control;
......@@ -84,17 +84,17 @@ public class ClockGUI extends Applet implements KeyListener, ClockTimeDisplay, I
int prevmode = clockmode;
// Read push button states
if (b1 != b1p || b2 != b2p) {
if (b1 && b2) {
if (bs != bsp || bc != bcp) {
if (bs && bc) {
clkinput.alarmOn = !clkinput.alarmOn;
chb_alarmon.setState(clkinput.alarmOn);
chb_tshow.setState(true);
clockmode = MODE_ATOGGLE;
} else if (b1) {
} else if (bs) {
clkinput.choice = ClockInput.SET_TIME;
chb_tset.setState(true);
clockmode = MODE_TSET;
} else if (b2) {
} else if (bc) {
clkinput.choice = ClockInput.SET_ALARM;
chb_aset.setState(true);
clockmode = MODE_ASET;
......@@ -104,10 +104,28 @@ public class ClockGUI extends Applet implements KeyListener, ClockTimeDisplay, I
clockmode = MODE_TSHOW;
}
}
b1p = b1;
b2p = b2;
// If any button changed, signal ClockInput
if(bs != bsp || bc != bcp
|| !bl && bl != blp
|| !br && br != brp
|| !bu && bu != bup
|| !bd && bd != bdp ) {
// Above if logic for avoiding bug JDK-4153069
clkinput.lastValueSet = inputval;
clkinput.getSemaphoreInstance().give();
}
// State update
bsp = bs;
bcp = bc;
blp = bl;
brp = br;
bup = bu;
bdp = bd;
// Any button changed?
// Editing to be completed?
if (clockmode == MODE_TSET) {
TimeSet(prevmode);
} else if (clockmode == MODE_ASET) {
......@@ -117,10 +135,6 @@ public class ClockGUI extends Applet implements KeyListener, ClockTimeDisplay, I
}
clkcanvas.repaint();
// Signal ClockInput
clkinput.lastValueSet = inputval;
clkinput.getSemaphoreInstance().give();
}
private void TimeSet(int prevmode)
......@@ -168,10 +182,10 @@ public class ClockGUI extends Applet implements KeyListener, ClockTimeDisplay, I
public void keyPressed(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_SHIFT:
b1 = true;
bs = true;
break;
case KeyEvent.VK_CONTROL:
b2 = true;
bc = true;
break;
case KeyEvent.VK_LEFT:
bl = true;
......@@ -196,10 +210,10 @@ public class ClockGUI extends Applet implements KeyListener, ClockTimeDisplay, I
public void keyReleased(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_SHIFT:
b1 = false;
bs = false;
break;
case KeyEvent.VK_CONTROL:
b2 = false;
bc = false;
break;
case KeyEvent.VK_LEFT:
bl = false;
......@@ -278,6 +292,7 @@ public class ClockGUI extends Applet implements KeyListener, ClockTimeDisplay, I
// Control software already running, activate GUI updates:
hiding = false;
} else {
System.out.println("AlarmClock start...");
control.start();
}
}
......@@ -293,7 +308,11 @@ public class ClockGUI extends Applet implements KeyListener, ClockTimeDisplay, I
// however, not waste time on calling GUI methods. Set flag:
hiding = true;
}
public void destroy() {
control.terminate();
}
public void setAlarmPulse(boolean onoff) {
alarmpulse = onoff;
clkcanvas.repaint();
......
package done;
import java.awt.Toolkit;
public class ClockOutput {
......@@ -13,7 +14,8 @@ public class ClockOutput {
* Wake-up clock user.
*/
public void doAlarm() {
System.out.println("Beep!");
System.out.print("Beep! \u0007 "); // BELL char, or ctrl-G.
Toolkit.getDefaultToolkit().beep(); // Dito modern/AWT style.
theDisplay.setAlarmPulse(true);
try {Thread.sleep(300);} catch (InterruptedException e) {}
theDisplay.setAlarmPulse(false);
......@@ -27,4 +29,31 @@ public class ClockOutput {
public void showTime(int hhmmss) {
theDisplay.setTime(hhmmss);
}
/**
* Display debug/logging message on hardware-dependent serial out.
* @param msg The string to display.
*/
public void console(String msg) {
System.out.println(msg);
}
/**
* Format and display ClockInput data.
* @param time The value as hh:mm:ss
* @param flag The state of the check box, T if checked.
* @param mode The ClockInput state.
*/
public void console(long curr, int time, boolean flag, int mode) {
String choice = null;
switch (mode) {
case ClockInput.SHOW_TIME: choice = "SHOW_TIME"; break;
case ClockInput.SET_ALARM: choice = "SET_ALARM"; break;
case ClockInput.SET_TIME: choice = "SET_TIME "; break;
}
System.out.printf("At ms=%05d: ", curr%10000); // ms within a minute
System.out.printf("Value=%02d:%02d:%02d AlarmFlag=%s ",
time/100/100, time/100,time, flag?"T":"F");
System.out.printf("Choice=%s%n", choice);
}
}
package todo;
import done.*;
import se.lth.cs.realtime.semaphore.Semaphore;
import se.lth.cs.realtime.semaphore.MutexSem;
public class AlarmClock extends Thread {
/**
* Main class of alarm-clock application.
* Constructor providing access to IO.
* Method start corresponding to main,
* with closing down done in terminate.
*/
public class AlarmClock {
private static ClockInput input;
private static ClockOutput output;
private static Semaphore sem;
private ClockInput input;
private ClockOutput output;
private Semaphore signal;
// Declare thread objects here..
/**
* Create main application and bind attributes to device drivers.
* @param i The input from simulator/emulator/hardware.
* @param o Dito for output.
*/
public AlarmClock(ClockInput i, ClockOutput o) {
input = i;
output = o;
sem = input.getSemaphoreInstance();
signal = input.getSemaphoreInstance();
}
// The AlarmClock thread is started by the simulator. No
// need to start it by yourself, if you do you will get
// an IllegalThreadStateException. The implementation
// below is a simple alarmclock thread that beeps upon
// each keypress. To be modified in the lab.
public void run() {
while (true) {
sem.take();
output.doAlarm();
/**
* Tell threads to terminate and wait until they are dead.
*/
public void terminate() {
// Do something more clever here...
output.console("AlarmClock exit.");
}
/**
* Create thread objects, and start threads
*/
public void start() {
// Delete/replace the following test/demo code;
// make something happen by exercising the IO:
// Create thread objects here...
Thread removeMeFromApplication = new Thread(new InputOutputTest());
// Create threads of execution by calling start...
removeMeFromApplication.start();
}
class InputOutputTest implements Runnable {
public void run() {
long curr; int time, mode; boolean flag;
output.console("Click on GUI to obtain key presses!");
while (!Thread.currentThread().isInterrupted()) {
curr = System.currentTimeMillis();
time = input.getValue();
flag = input.getAlarmFlag();
mode = input.getChoice();
output.doAlarm();
output.console(curr, time, flag, mode);
if (time == 120000) break; // Swe: Bryter fr middag
signal.take();
}
output.console("IO-test terminated #");
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment