forked from parrot/parrot
/
clock.pir
158 lines (120 loc) · 2.75 KB
/
clock.pir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
=head1 NAME
clock.pir - LCD clock
=head1 SYNOPSIS
./parrot examples/sdl/lcd/clock.pir
=head1 DESCRIPTION
This example demonstrates the SDL::LCD object. It shows a simple clock.
=head1 FUNCTIONS
=over 4
=cut
.include "tm.pasm"
.include "timer.pasm"
.loadlib 'sys_ops'
.loadlib 'math_ops'
=item _main
The main function.
=cut
.sub _main :main
load_bytecode "SDL/App.pir"
load_bytecode "SDL/Event.pir"
load_bytecode "SDL/EventHandler.pir"
load_bytecode "SDL/LCD.pir"
# create the SDL application object
.local pmc app
app = new ['SDL'; 'App']
app.'init'( 'height' => 21, 'width' => 94, 'bpp' => 16, 'flags' => 5 )
.local pmc screen
screen = app.'surface'()
set_global 'screen', screen
# create the LCD
.local pmc lcd
lcd = new ['SDL'; 'LCD']
set_global 'LCD', lcd
# draw the watch
drawWatch()
# create the timer
$P1 = get_global "drawWatch"
$P0 = new 'Timer'
$P0[.PARROT_TIMER_NSEC] = 0.5
$P0[.PARROT_TIMER_HANDLER] = $P1
$P0[.PARROT_TIMER_REPEAT] = -1
$P0[.PARROT_TIMER_RUNNING] = 1
# store the timer somewhere, it will be
# collected and destroyed otherwise
set_global "timer", $P0
#
# event loop
#
.local pmc eh
.local pmc loop
eh = new ['SDL'; 'EventHandler']
loop = new ['SDL'; 'Event']
loop.'init'()
loop.'process_events'( 0.1, eh )
.end
=item drawWatch
Creates, sets and redraws the LCD display content.
=cut
.sub drawWatch
# decode the current time
$I0 = time
$P0 = decodelocaltime $I0
# use a dot or a space?
$N0 -= $I0
$S2 = ":"
if $N0 < 0.5 goto USE_DOTS
$S2 = " "
USE_DOTS:
# hours
$I0 = $P0[.TM_HOUR]
$I0 /= 10
if $I0 > 0 goto NO_SPACE
$S0 = ' '
branch HOUR
NO_SPACE:
$S0 = $I0
HOUR:
$I0 = $P0[.TM_HOUR]
cmod $I0, $I0, 10
$S1 = $I0
$S0 = concat $S0, $S1
# minutes
$S0 = concat $S0, $S2
$I0 = $P0[.TM_MIN]
$I0 /= 10
$S1 = $I0
$S0 = concat $S0, $S1
$I0 = $P0[.TM_MIN]
cmod $I0, $I0, 10
$S1 = $I0
$S0 = concat $S0, $S1
# seconds
$S0 = concat $S0, $S2
$I0 = $P0[.TM_SEC]
$I0 /= 10
$S1 = $I0
$S0 = concat $S0, $S1
$I0 = $P0[.TM_SEC]
cmod $I0, $I0, 10
$S1 = $I0
$S0 = concat $S0, $S1
# set the time
$P0 = get_global "LCD"
$P0 = $S0
# redraw the LCD
$P1 = get_global "screen"
$P0.'draw'( $P1 )
.end
=back
=head1 AUTHOR
Jens Rieks E<lt>parrot at jensbeimsurfen dot deE<gt> is the author
and maintainer.
Please send patches and suggestions to the Perl 6 Internals mailing list.
=head1 COPYRIGHT
Copyright (C) 2004-2010, Parrot Foundation.
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: