-
Notifications
You must be signed in to change notification settings - Fork 0
/
piClock.py
executable file
·264 lines (224 loc) · 8.25 KB
/
piClock.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# -*- coding: UTF-8 -*-
import os
import pygame
import time
import random
import math
class piClock :
screen = None;
maxx = 0;
maxy = 0;
origo = (0, 0);
def __init__(self):
"Ininitializes a new pygame screen using the framebuffer"
# Based on "Python GUI in Linux frame buffer"
# http://www.karoltomala.com/blog/?p=679
disp_no = os.getenv("DISPLAY")
if disp_no:
print "I'm running under X display = {0}".format(disp_no)
# Check which frame buffer drivers are available
# Start with fbcon since directfb hangs with composite output
drivers = ['fbcon', 'directfb', 'svgalib']
found = False
for driver in drivers:
# Make sure that SDL_VIDEODRIVER is set
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print 'Driver: {0} failed.'.format(driver)
continue
found = True
break
if not found:
raise Exception('No suitable video driver found!')
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
print "Framebuffer size: %d x %d" % (size[0], size[1])
self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
self.maxx = size[0]
self.maxy = size[1]
self.origo = (self.maxx/2, self.maxy/2)
# Clear the screen to start
print "Clearing screen..."
self.clear()
# Initialise font support
print "Init fonts..."
pygame.font.init()
# Render the screen
pygame.display.update()
print "Init complete."
def __del__(self):
"Destructor to make sure pygame shuts down, etc."
def clear(self):
"Clear screen"
self.screen.fill((0, 0, 0))
def clear_dial(self):
"Clear the part of the screen with the clock dial"
radius = min(self.maxx, self.maxy)/2
dialrect = pygame.Rect(0,0,0,0)
dialrect.width = 2*radius
dialrect.height = 2*radius
dialrect.center = self.origo
self.screen.fill((0, 0, 0), dialrect)
def test(self):
# Fill the screen with red (255, 0, 0)
red = (255, 0, 0)
self.screen.fill(red)
# Update the display
pygame.display.update()
def vect2coord(self, radius, angle):
# Convert from r,phi to x,y - clockwise
x = self.origo[0] + radius*math.sin(math.radians(90-angle))
y = self.origo[1] + radius*math.cos(math.radians(90-angle))
return (x, y)
def draw_dial(self):
# Draw a clock dial
radius = min(self.maxx, self.maxy)/2
color_min = (255, 0, 0)
color_hour = (0, 0, 255)
for ang in range(0, 360, 360/60):
p0 = self.vect2coord(0.9*radius, ang)
p1 = self.vect2coord(radius, ang)
pygame.draw.line(self.screen, color_min, p0, p1)
for ang in range(0, 360, 360/12):
p0 = self.vect2coord(0.8*radius, ang)
p1 = self.vect2coord(radius, ang)
if ang == 0:
color = color_hour
else:
color = color_min
pygame.draw.line(self.screen, color, p0, p1)
def draw_hands(self, tm):
# Draw the clock hands
radius = min(self.maxx, self.maxy)/2
color_hand = (255, 255, 255)
hour = tm.tm_hour
if hour > 12:
hour = hour -12
minute = tm.tm_min
second = tm.tm_sec
ang = 360 * (hour*60 + minute)/(12*60)
p = self.vect2coord(0.5*radius, ang)
pygame.draw.line(self.screen, color_hand, self.origo, p, 5)
ang = 360 * minute/60
p = self.vect2coord(0.8*radius, ang)
pygame.draw.line(self.screen, color_hand, self.origo, p, 3)
ang = 360 * second/60
p = self.vect2coord(1.0*radius, ang)
pygame.draw.line(self.screen, color_hand, self.origo, p, 1)
zone_start_h = 7
zone_start_m = 45
zone_end_h = 8
zone_end_m = 20
def draw_zone2(self, tm):
# Draw a zone (Go to school)
dminute = tm.tm_hour*60 + tm.tm_min
zone_start = self.zone_start_h*60 + self.zone_start_m
zone_end = self.zone_end_h*60 + self.zone_end_m
if dminute < zone_start or dminute > zone_end:
return
ratio = (dminute-zone_start)*100.0/(zone_end-zone_start)
if ratio < 30:
color_zone = (0, 200, 40)
elif ratio < 70:
color_zone = (200, 200, 0)
else:
color_zone = (200, 0, 0)
# color_passed_zone = color_zone/1.5
radius = min(self.maxx, self.maxy)/2
ang0 = 360 * self.zone_start_m/60
ang1 = 360 * self.zone_end_m/60
ang_now = 360 * tm.tm_min/60
if ang1 < ang0:
ang0 = ang0-360
if ang1 < ang_now:
ang_now = ang_now-360
for ang in range(ang0, ang1, 5):
p0 = self.vect2coord(0.5*radius, ang)
p1 = self.vect2coord(0.7*radius, ang)
# if ang < ang_now:
# pygame.draw.line(self.screen, color_passed_zone, p0, p1, 5)
# else:
pygame.draw.line(self.screen, color_zone, p0, p1, 5)
def draw_zone(self, tm):
# Draw a zone (Go to school)
dminute = tm.tm_hour*60 + tm.tm_min
zone_start = self.zone_start_h*60 + self.zone_start_m
zone_end = self.zone_end_h*60 + self.zone_end_m
if dminute < zone_start or dminute > zone_end:
return
ratio = (dminute-zone_start)*100.0/(zone_end-zone_start)
if ratio < 30:
color_zone = (0, 200, 40)
elif ratio < 70:
color_zone = (200, 200, 0)
else:
color_zone = (200, 0, 0)
radius = min(self.maxx, self.maxy)/2
arcrect = pygame.Rect(0,0,0,0)
arcrect.width = 2*0.7*radius
arcrect.height = 2*0.7*radius
arcrect.center = self.origo
ang0 = -360 * self.zone_start_m/60
ang1 = -360 * self.zone_end_m/60
if ang0 < -360:
ang0 = ang0+360
if ang0 < 0:
ang0 = ang0+360
if ang1 < -360:
ang1 = ang1+360
if ang1 < 0:
ang1 = ang1+360
print "Zone from {0} to {1}".format(ang0, ang1)
if ang1 < ang0:
ang0 = ang0-360
pygame.draw.arc(self.screen, color_zone, arcrect, math.radians(ang0), math.radians(ang1), 40)
def draw_timenum(self):
# Write time as digits
radius = min(self.maxx, self.maxy)/2
font = pygame.font.Font(None, 150)
timestr = time.strftime("%H:%M")
surface = font.render(timestr, 1, (0, 0, 255), (0, 0, 0))
surface = pygame.transform.rotate(surface, -90)
pos = (1100, 400)
size = surface.get_height()
textpos = (pos[0], pos[1]-size/2)
self.screen.blit(surface, textpos)
def get_timetext(self):
# Convert time to text
tm = time.localtime()
hour = tm.tm_hour
minute = tm.tm_min
second = tm.tm_sec
timestr = u"Klockan är "
parts = ['', "fem X ", "tio X ", "kvart X ", "tjugo X ", "fem X halv ", "halv "]
ix = int(round(minute/5.0, 0))
if ix > 6:
ixx = 12-ix
else:
ixx = ix
part = parts[ixx]
if ix < 5 or ix == 7:
part = part.replace("X", u"över")
else:
part = part.replace("X", "i")
if ix >= 5:
hour = hour+1
if hour > 12:
hour = hour-12
timestr = timestr + part
hours = ["tolv", "ett", u"två", "tre", "fyra", "fem", "sex", "sju", u"åtta", "nio", "tio", "elva", "tolv"]
hourstr = hours[hour]
timestr = timestr + hourstr
return timestr
def draw_timetext(self, str):
# Write a text
radius = min(self.maxx, self.maxy)/2
font = pygame.font.Font(None, 80)
surface = font.render(str, 1, (0, 0, 255), (0, 0, 0))
surface = pygame.transform.rotate(surface, -90)
pos = (100, 400)
size = surface.get_height()
textpos = (pos[0], pos[1]-size/2)
self.screen.blit(surface, textpos)