Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
EDAF55
workspace_student
Commits
d86e97d1
Commit
d86e97d1
authored
Aug 23, 2017
by
Mathias Haage
Browse files
Add ht16 alarmclock
parent
5f937c51
Changes
4
Hide whitespace changes
Inline
Side-by-side
alarmclock/src/done/ClockCanvas.java
View file @
d86e97d1
...
...
@@ -36,12 +36,12 @@ public class ClockCanvas extends Canvas {
public
void
paint
(
Graphics
g
)
{
backbuff
.
drawImage
(
clockimg
,
0
,
0
,
this
);
if
(
parent
.
b
1
)
{
if
(
parent
.
b
s
)
{
backbuff
.
drawImage
(
btnblue_p
,
90
,
120
,
this
);
}
else
{
backbuff
.
drawImage
(
btnblue
,
90
,
120
,
this
);
}
if
(
parent
.
b
2
)
{
if
(
parent
.
b
c
)
{
backbuff
.
drawImage
(
btnblue_p
,
90
,
150
,
this
);
}
else
{
backbuff
.
drawImage
(
btnblue
,
90
,
150
,
this
);
...
...
alarmclock/src/done/ClockGUI.java
View file @
d86e97d1
...
...
@@ -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
b
2
=
false
;
private
boolean
b
1p
=
false
;
private
boolean
b
2p
=
false
;
boolean
b
l
=
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
b
s
,
bsp
;
// shift
boolean
b
c
,
bcp
;
// control
boolean
b
l
,
blp
;
// left
boolean
b
r
,
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
(
b
1
!=
b
1
p
||
b
2
!=
b
2
p
)
{
if
(
b
1
&&
b
2
)
{
if
(
b
s
!=
b
s
p
||
b
c
!=
b
c
p
)
{
if
(
b
s
&&
b
c
)
{
clkinput
.
alarmOn
=
!
clkinput
.
alarmOn
;
chb_alarmon
.
setState
(
clkinput
.
alarmOn
);
chb_tshow
.
setState
(
true
);
clockmode
=
MODE_ATOGGLE
;
}
else
if
(
b
1
)
{
}
else
if
(
b
s
)
{
clkinput
.
choice
=
ClockInput
.
SET_TIME
;
chb_tset
.
setState
(
true
);
clockmode
=
MODE_TSET
;
}
else
if
(
b
2
)
{
}
else
if
(
b
c
)
{
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 chang
ed?
//
Editing to be complet
ed?
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
:
b
1
=
true
;
b
s
=
true
;
break
;
case
KeyEvent
.
VK_CONTROL
:
b
2
=
true
;
b
c
=
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
:
b
1
=
false
;
b
s
=
false
;
break
;
case
KeyEvent
.
VK_CONTROL
:
b
2
=
false
;
b
c
=
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
();
...
...
alarmclock/src/done/ClockOutput.java
View file @
d86e97d1
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
);
}
}
alarmclock/src/todo/AlarmClock.java
View file @
d86e97d1
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
;
s
em
=
input
.
getSemaphoreInstance
();
s
ignal
=
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 #"
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment