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

Add ht15 alarmclock

parent bbe90042
.metadata
.recommenders
alarmclock/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>alarmclock</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 21:08:50 CEST 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.4
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=warning
org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning
org.eclipse.jdt.core.compiler.source=1.3
<applet
name="AlarmClock"
code="done/ClockGUI"
codebase="."
archive="csrtsem.jar"
width="400"
height="275"
align="Top"
alt="If you had a java-enabled browser, you would see an applet here."
>
<hr>
If your browser recognized the applet tag, you see an applet here.
<hr>
</applet>
package done;
import java.awt.*;
public class ClockCanvas extends Canvas {
private static final long serialVersionUID = 1L;
private Graphics backbuff;
private Image buff;
private Image clockimg;
private Image btnblue;
private Image btnblue_p;
private Image btnyellow;
private Image btnyellow_p;
private ClockGUI parent;
private char[] lcdcache = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
private char[] lcdcache2 = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
public ClockCanvas(ClockGUI parent) {
setSize(400, 189);
buff = parent.createImage(400, 189);
backbuff = buff.getGraphics();
this.parent = parent;
clockimg = parent.getImage(parent.getClass().getResource("/clock.png"));
btnblue = parent.getImage(parent.getClass().getResource("/btnblue.png"));
btnblue_p = parent.getImage(parent.getClass().getResource("/btnblue_p.png"));
btnyellow = parent.getImage(parent.getClass().getResource("/btnyellow.png"));
btnyellow_p = parent.getImage(parent.getClass().getResource("/btnyellow_p.png"));
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
backbuff.drawImage(clockimg, 0, 0, this);
if (parent.b1) {
backbuff.drawImage(btnblue_p, 90, 120, this);
} else {
backbuff.drawImage(btnblue, 90, 120, this);
}
if (parent.b2) {
backbuff.drawImage(btnblue_p, 90, 150, this);
} else {
backbuff.drawImage(btnblue, 90, 150, this);
}
if (parent.bl) {
backbuff.drawImage(btnyellow_p, 177, 150, this);
} else {
backbuff.drawImage(btnyellow, 177, 150, this);
}
if (parent.bu) {
backbuff.drawImage(btnyellow_p, 228, 120, this);
} else {
backbuff.drawImage(btnyellow, 228, 120, this);
}
if (parent.bd) {
backbuff.drawImage(btnyellow_p, 228, 150, this);
} else {
backbuff.drawImage(btnyellow, 228, 150, this);
}
if (parent.br) {
backbuff.drawImage(btnyellow_p, 279, 150, this);
} else {
backbuff.drawImage(btnyellow, 279, 150, this);
}
backbuff.setFont(new Font("Courier", Font.PLAIN, 28));
backbuff.setColor(new Color(0, 68, 85));
int hhmmss;
hhmmss = parent.clocktime;
lcdcache[7] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache[6] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache[5] = ':';
lcdcache[4] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache[3] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache[2] = ':';
lcdcache[1] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache[0] = (char) (hhmmss % 10 + '0');
backbuff.drawString(new String(lcdcache), 50, 60);
hhmmss = parent.alarmtime;
lcdcache2[7] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache2[6] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache2[4] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache2[3] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache2[1] = (char) (hhmmss % 10 + '0');
hhmmss /= 10;
lcdcache2[0] = (char) (hhmmss % 10 + '0');
if (parent.alarmpulse) {
lcdcache2[5] = ' ';
lcdcache2[2] = ' ';
} else {
if (parent.clkinput.alarmOn) {
lcdcache2[5] = ':';
lcdcache2[2] = ':';
} else {
lcdcache2[5] = '_';
lcdcache2[2] = '_';
}
}
backbuff.drawString(new String(lcdcache2), 50, 85);
if (parent.clockmode == ClockGUI.MODE_TSET || parent.clockmode == ClockGUI.MODE_ASET) {
int cur = 5 - parent.cursor;
if (parent.cursor < 4) {
cur++;
if (parent.cursor < 2) {
cur++;
}
}
char[] cur$ = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
cur$[cur] = '_';
if (parent.clockmode == ClockGUI.MODE_ASET) {
backbuff.drawString(new String(cur$), 50, 85);
} else {
backbuff.drawString(new String(cur$), 50, 60);
}
}
// Paint the back buffer
g.drawImage(buff, 0, 0, this);
}
}
package done;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
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;
ClockInput clkinput;
private ClockOutput clkoutput;
int cursor = 0;
int clockmode = MODE_NONE;
int clocktime = 0;// Clock time
int alarmtime = 0;// Alarm time
private int inputval = 0;// Input value
private boolean hiding = false;
private AlarmClock control;
boolean alarmpulse = false;
private Checkbox chb_alarmon;
private ClockCanvas clkcanvas;
private Checkbox chb_tshow;
private Checkbox chb_tset;
private Checkbox chb_aset;
private CheckboxGroup chg_mode;
/**
* Initializes the applet. You never need to call this directly; it is
* called automatically by the system once the applet is created.
*/
public void init() {
setSize(400, 250);
setLayout(new FlowLayout());
clkcanvas = new ClockCanvas(this);
add(clkcanvas);
clkcanvas.addKeyListener(this);
chb_alarmon = new Checkbox("Alarm on");
chb_alarmon.addItemListener(this);
add(chb_alarmon);
chb_tshow = new Checkbox("Show Time");
chb_tshow.setState(true);
chb_tshow.setEnabled(false);
chb_tset = new Checkbox("Set Time");
chb_tset.setEnabled(false);
chb_aset = new Checkbox("Set Alarm");
chb_aset.setEnabled(false);
chg_mode = new CheckboxGroup();
chb_tshow.setCheckboxGroup(chg_mode);
chb_tset.setCheckboxGroup(chg_mode);
chb_aset.setCheckboxGroup(chg_mode);
add(chb_tshow);
add(chb_tset);
add(chb_aset);
clkinput = new ClockInput();
clkoutput = new ClockOutput(this);
// Instantiate alarm-clock software using application interfaces.
control = new AlarmClock(clkinput, clkoutput);
}
private void onInput() {
int prevmode = clockmode;
// Read push button states
if (b1 != b1p || b2 != b2p) {
if (b1 && b2) {
clkinput.alarmOn = !clkinput.alarmOn;
chb_alarmon.setState(clkinput.alarmOn);
chb_tshow.setState(true);
clockmode = MODE_ATOGGLE;
} else if (b1) {
clkinput.choice = ClockInput.SET_TIME;
chb_tset.setState(true);
clockmode = MODE_TSET;
} else if (b2) {
clkinput.choice = ClockInput.SET_ALARM;
chb_aset.setState(true);
clockmode = MODE_ASET;
} else {
clkinput.choice = ClockInput.SHOW_TIME;
chb_tshow.setState(true);
clockmode = MODE_TSHOW;
}
}
b1p = b1;
b2p = b2;
// Any button changed?
if (clockmode == MODE_TSET) {
TimeSet(prevmode);
} else if (clockmode == MODE_ASET) {
AlarmSet(prevmode);
} else {
cursor = 0;
}
clkcanvas.repaint();
// Signal ClockInput
clkinput.lastValueSet = inputval;
clkinput.getSemaphoreInstance().give();
}
private void TimeSet(int prevmode)
{
if (prevmode != MODE_TSET) {
inputval = clocktime;
}
if (bl && cursor < 5) {
cursor++;
} else if (br && cursor > 0) {
cursor--;
} else if (bu) {
inputval = IncTime(inputval, cursor);
} else if (bd) {
if (GetDigit(inputval, cursor) > 0) {
inputval = DecDigit(inputval, cursor);
}
}
clocktime = inputval;
}
private void AlarmSet(int prevmode)
{
if (prevmode != MODE_ASET) {
inputval = alarmtime;
}
if (bl && cursor < 5) {
cursor++;
} else if (br && cursor > 0) {
cursor--;
} else if (bu) {
inputval = IncTime(inputval, cursor);
} else if (bd) {
if (GetDigit(inputval, cursor) > 0) {
inputval = DecDigit(inputval, cursor);
}
}
alarmtime = inputval;
}
public void keyPressed(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_SHIFT:
b1 = true;
break;
case KeyEvent.VK_CONTROL:
b2 = true;
break;
case KeyEvent.VK_LEFT:
bl = true;
break;
case KeyEvent.VK_UP:
bu = true;
break;
case KeyEvent.VK_DOWN:
bd = true;
break;
case KeyEvent.VK_RIGHT:
br = true;
break;
default:
return;
}
clkcanvas.repaint();
onInput();
}
public void keyReleased(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_SHIFT:
b1 = false;
break;
case KeyEvent.VK_CONTROL:
b2 = false;
break;
case KeyEvent.VK_LEFT:
bl = false;
break;
case KeyEvent.VK_UP:
bu = false;
break;
case KeyEvent.VK_DOWN:
bd = false;
break;
case KeyEvent.VK_RIGHT:
br = false;
break;
default:
return;
}
clkcanvas.repaint();
onInput();
}
public void keyTyped(KeyEvent event) {
}
public void setTime(int hhmmss) {
if (!hiding && clockmode != MODE_TSET) {
clocktime = hhmmss;
}
clkcanvas.repaint();
}
private int GetDigit(int number, int pos)
{
int i;
for (i = 0; i < pos; i++) number /= 10;
return number % 10;
}
private int IncDigit(int number, int pos)
{
int term = 1;
int i;
for (i = 0; i < pos; i++) term *= 10;
return number + term;
}
private int DecDigit(int number, int pos)
{
int term = 1;
int i;
for (i = 0; i < pos; i++) term *= 10;
return number - term;
}
private int IncTime(int time, int pos)
{
int newtime = time;
if ((pos == 5 && GetDigit(time, 5) < 2 && GetDigit(time, 4) < 4) ||
(pos == 5 && GetDigit(time, 5) < 1) ||
(pos == 4 && GetDigit(time, 4) < 3 && GetDigit(time, 5) <= 2) ||
(pos == 4 && GetDigit(time, 4) < 9 && GetDigit(time, 5) <= 1) ||
(pos == 3 && GetDigit(time, 3) < 5) ||
(pos == 1 && GetDigit(time, 1) < 5) ||
((pos == 0 || pos == 2) && GetDigit(time, pos) < 9)) {
newtime = IncDigit(time, pos);
}
return newtime;
}
/**
* Called to start the applet. You never need to call this directly; it
* is called when the applet's document is visited.
*/
public void start() {
if (hiding) {
// Control software already running, activate GUI updates:
hiding = false;
} else {
control.start();
}
}
/**
* Called to stop the applet. This is called when the applet's document is
* no longer on the screen. It is guaranteed to be called before destroy()
* is called. You never need to call this method directly
*/
public void stop() {
// For the clock to work even when it is not visible, all threads
// should keep on working. Now when we are not visible we should,
// however, not waste time on calling GUI methods. Set flag:
hiding = true;
}
public void setAlarmPulse(boolean onoff) {
alarmpulse = onoff;
clkcanvas.repaint();
}
public void itemStateChanged(ItemEvent evt) {
if (evt.getSource() == chb_alarmon) {
clkinput.alarmOn = evt.getStateChange() == ItemEvent.SELECTED;
clkinput.getSemaphoreInstance().give();
clkcanvas.repaint();
}
}
}
package done;
import se.lth.cs.realtime.semaphore.*;
public class ClockInput {
/**
* Return values for getChoice.
*/
public static final int SHOW_TIME = 0;
public static final int SET_ALARM = 1;
public static final int SET_TIME = 2;
/**
* Construct the interface with semaphore reset.
*/
public ClockInput() {
anyButtonChanged = new CountingSem();
}
/**
* Semaphore signaling when the user have changed any setting.
* Actually, according to normal rules for data abstraction,
* data (such as the flags and the semaphore in this class)
* should be accessed via methods, say awaitAnyButton() to
* take the semaphore. However, here we expose variables
* through a get-method for semaphore-teaching reasons.
*/
private Semaphore anyButtonChanged;
/**
* Get-method to access the semaphore instance directly.
*/
public Semaphore getSemaphoreInstance() {
return anyButtonChanged;
}
/* Package attributes, only used by the simulator/hardware. */
boolean alarmOn; // Alarm activation according to checkbox.
int choice; // The radio-button choice.
int lastValueSet; // Value from last clock or alarm set op.
/**
* Get check-box state.
*/
public boolean getAlarmFlag() {
return alarmOn;
}
/**
* Get radio-buttons choice.
*/
public int getChoice() {
return choice;
}
/**
* When getChoice returns a new choice, and the previous choice
* was either SET_ALARM or SET_TIME, the set-value of the display
* is returned in the format hhmmss where h, m, and s denotes
* hours, minutes, and seconds digits respectively. This means,
* for example, that the hour value is obtained by dividing the
* return value by 10000.
*/
public int getValue() {
return lastValueSet;
}
}
package done;
public class ClockOutput {
private ClockTimeDisplay theDisplay;
ClockOutput(ClockTimeDisplay display) {
theDisplay = display;
}
/**
* Wake-up clock user.
*/
public void doAlarm() {
System.out.println("Beep!");
theDisplay.setAlarmPulse(true);
try {Thread.sleep(300);} catch (InterruptedException e) {}
theDisplay.setAlarmPulse(false);
}
/**
* If the display is currently used to display the time, update it.