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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#! /usr/bin/env python
# 'clock' -- A simple alarm clock
# The alarm can be set at 5 minute intervals on a 12 hour basis.
# It is controlled with the mouse:
# - Click and drag around the circle to set the alarm.
# - Click far outside the circle to clear the alarm.
# - Click near the center to set the alarm at the last time set.
# The alarm time is indicated by a small triangle just outside the circle,
# and also by a digital time at the bottom.
# The indicators disappear when the alarm is not set.
# When the alarm goes off, it beeps every minute for five minutes,
# and the clock turns into inverse video.
# Click or activate the window to turn the ringing off.
import stdwin
from stdwinevents import WE_MOUSE_DOWN, WE_MOUSE_MOVE, WE_MOUSE_UP, \
WE_TIMER, WE_DRAW, WE_SIZE, WE_CLOSE, WE_ACTIVATE
import mainloop
import time
from math import sin, cos, atan2, pi, sqrt
DEFWIDTH, DEFHEIGHT = 200, 200
MOUSE_EVENTS = (WE_MOUSE_DOWN, WE_MOUSE_MOVE, WE_MOUSE_UP)
ORIGIN = 0, 0
FARAWAY = 2000, 2000
EVERYWHERE = ORIGIN, FARAWAY
def main():
win = makewindow()
del win
mainloop.mainloop()
def makewindow():
stdwin.setdefwinsize(DEFWIDTH, DEFHEIGHT + stdwin.lineheight())
win = stdwin.open('clock')
setdimensions(win)
win.set = 1 # True when alarm is set
win.time = 11*60 + 40 # Time when alarm must go off
win.ring = 0 # True when alarm is ringing
win.dispatch = cdispatch
mainloop.register(win)
settimer(win)
return win
def cdispatch(event):
type, win, detail = event
if type == WE_DRAW:
drawproc(win, detail)
elif type == WE_TIMER:
settimer(win)
drawproc(win, EVERYWHERE)
elif type in MOUSE_EVENTS:
mouseclick(win, type, detail)
elif type == WE_ACTIVATE:
if win.ring:
# Turn the ringing off
win.ring = 0
win.begindrawing().invert(win.mainarea)
elif type == WE_SIZE:
win.change(EVERYWHERE)
setdimensions(win)
elif type == WE_CLOSE:
mainloop.unregister(win)
win.close()
def setdimensions(win):
width, height = win.getwinsize()
height = height - stdwin.lineheight()
if width < height: size = width
else: size = height
halfwidth = width/2
halfheight = height/2
win.center = halfwidth, halfheight
win.radius = size*45/100
win.width = width
win.height = height
win.corner = width, height
win.mainarea = ORIGIN, win.corner
win.lineheight = stdwin.lineheight()
win.farcorner = width, height + win.lineheight
win.statusarea = (0, height), win.farcorner
win.fullarea = ORIGIN, win.farcorner
def settimer(win):
now = time.time()
hours, minutes, seconds = win.times = calctime(now)
delay = 61 - seconds
win.settimer(10 * delay)
minutes = minutes + hours*60
if win.ring:
# Is it time to stop the alarm ringing?
since = (minutes - win.time + 720) % 720
if since >= 5:
# Stop it now
win.ring = 0
else:
# Ring again, once every minute
stdwin.fleep()
elif win.set and minutes == win.time:
# Start the alarm ringing
win.ring = 1
stdwin.fleep()
def drawproc(win, area):
hours, minutes, seconds = win.times
d = win.begindrawing()
d.cliprect(area)
d.erase(EVERYWHERE)
d.circle(win.center, win.radius)
d.line(win.center, calcpoint(win, hours*30 + minutes/2, 0.6))
d.line(win.center, calcpoint(win, minutes*6, 1.0))
str = "%02d:%02d" % (hours, minutes)
p = (win.width - d.textwidth(str))/2, win.height * 3 / 4
d.text(p, str)
if win.set:
drawalarm(win, d)
drawalarmtime(win, d)
if win.ring:
d.invert(win.mainarea)
def mouseclick(win, type, detail):
d = win.begindrawing()
if win.ring:
# First turn the ringing off
win.ring = 0
d.invert(win.mainarea)
h, v = detail[0]
ch, cv = win.center
x, y = h-ch, cv-v
dist = sqrt(x*x + y*y) / float(win.radius)
if dist > 1.2:
if win.set:
drawalarm(win, d)
erasealarmtime(win, d)
win.set = 0
elif dist < 0.8:
if not win.set:
win.set = 1
drawalarm(win, d)
drawalarmtime(win, d)
else:
# Convert to half-degrees (range 0..720)
alpha = atan2(y, x)
hdeg = alpha*360.0/pi
hdeg = 180.0 - hdeg
hdeg = (hdeg + 720.0) % 720.0
atime = 5*int(hdeg/5.0 + 0.5)
if atime <> win.time or not win.set:
if win.set:
drawalarm(win, d)
erasealarmtime(win, d)
win.set = 1
win.time = atime
drawalarm(win, d)
drawalarmtime(win, d)
def drawalarm(win, d):
p1 = calcpoint(win, float(win.time)/2.0, 1.02)
p2 = calcpoint(win, float(win.time)/2.0 - 4.0, 1.1)
p3 = calcpoint(win, float(win.time)/2.0 + 4.0, 1.1)
d.xorline(p1, p2)
d.xorline(p2, p3)
d.xorline(p3, p1)
def erasealarmtime(win, d):
d.erase(win.statusarea)
def drawalarmtime(win, d):
# win.time is in the range 0..720 with origin at 12 o'clock
# Convert to hours (0..12) and minutes (12*(0..60))
hh = win.time/60
mm = win.time%60
str = 'Alarm@%02d:%02d' % (hh, mm)
p1 = (win.width - d.textwidth(str))/2, win.height
d.text(p1, str)
def calcpoint(win, degrees, size):
alpha = pi/2.0 - float(degrees) * pi/180.0
x, y = cos(alpha), sin(alpha)
h, v = win.center
r = float(win.radius)
return h + int(x*size*r), v - int(y*size*r)
def calctime(now):
hours, minutes, seconds = time.localtime(now)[3:6]
hours = hours % 12
return hours, minutes, seconds
main()
|