forked from BouKiCHi/pyCLOCK
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyclock3.dge
873 lines (611 loc) · 19.4 KB
/
pyclock3.dge
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
#!/usr/bin/python
# PyCLOCK Copyright (C) 2012 - 2019 by BouKiCHi
# for Python3
#
import pygame
import random
import datetime
import calendar
import os
import math
pygame.init()
pygame.mouse.set_visible(False)
#
# global values
#
sw = 320
sh = 240
screen = pygame.display.set_mode([sw, sh])
half_sw = int(sw / 2)
half_sh = int(sh / 2)
font_s = pygame.font.SysFont("None", 16)
font_m = pygame.font.SysFont("None", 24)
color_white = (255, 255, 255)
color_red = (255, 0, 0)
color_lblue = (0, 255, 255)
color_purple = (255, 0, 255)
color_yellow = (255, 255, 0)
shadow = (0, 0, 0)
timer_sec = 180
setup_delta = datetime.timedelta()
alarm_min = 7 * 60
alarm_date = datetime.datetime.now()
def calc_alarm():
global alarm_date
day = datetime.datetime.now()
hour = int(alarm_min / 60)
minute = int(alarm_min % 60)
alarm_date = day.replace(hour=hour, minute=minute, second=0, microsecond=0)
delta = alarm_date - day
if delta < datetime.timedelta(0):
alarm_date += datetime.timedelta(1)
calc_alarm()
def sys_write(path, value):
sys_path = '/sys/' + str(path)
f = open(sys_path, 'w')
f.write(value)
f.close()
def sys_read(path):
sys_path = '/sys/' + str(path)
f = open(sys_path, 'r')
ret = f.read()
f.close()
return ret
def output_text(text):
print(text)
#
# READ RTC and SET ALARM
#
def do_sleep():
output_text("do_sleep!!")
day = datetime.datetime.now()
delta = alarm_date - day
output_text("After %dsec" % delta.seconds)
if delta.seconds >= 30:
output_text("Time to sleep!")
output_text("Wake before 20sec of the time")
rtc_time = int(sys_read("class/rtc/rtc0/since_epoch").rstrip())
rtc_time += (delta.seconds - 20)
sys_write("class/rtc/rtc0/wakealarm", "0")
sys_write("class/rtc/rtc0/wakealarm", "%d" % rtc_time)
sys_write("power/state", "mem")
#
# SET SYSTEM TIME and HWCLOCK
#
def do_setup():
output_text("do_setup!!")
day = datetime.datetime.now() - setup_delta
day_str = "%04d-%02d-%02d " % (day.year, day.month, day.day)
day_str += "%02d:%02d:%02d" % (day.hour, day.minute, day.second)
cmd_str = "sudo date --set \"%s\"" % day_str
output_text(cmd_str)
os.system(cmd_str)
os.system("sudo hwclock -uw")
def draw_string(msg, x, y, color, center=False):
surf_s = font_m.render(msg, True, shadow)
surf = font_m.render(msg, True, color)
if center:
x -= int(surf_s.get_width() / 2)
screen.blit(surf_s, (x+1, y+1))
screen.blit(surf, (x, y))
return surf_s
def draw_string_s(msg, x, y, color, center=False):
surf_s = font_s.render(msg, True, shadow)
surf = font_s.render(msg, True, color)
if center:
x -= int(surf_s.get_width() / 2)
screen.blit(surf_s, (x+1, y+1))
screen.blit(surf, (x, y))
return surf_s
def base_screen():
bg_color = [0, 0, 255]
screen.fill(bg_color)
screen.blit(font_s.render("pyCLOCK3", True, color_white), (0, 0))
#
# displays current time on bottom
#
def disp_current():
day = datetime.datetime.now()
str_current = "%04d-%02d-%02d %02d:%02d:%02d" % \
(day.year, day.month, day.day, day.hour, day.minute, day.second)
x = 30
y = 200
screen.blit(font_m.render(str_current, True, shadow), (x+1, y+1))
screen.blit(font_m.render(str_current, True, color_white), (x, y))
#
# displays bottom menu bar
#
def disp_menu_bar(pos):
menu = ["CAL", "CLOCK", "ALARM", "TIMER", "SETUP"]
for i in range(5):
y = 220
color = color_white
if i == pos:
color = color_purple
surf_s = font_s.render(menu[i], True, shadow)
x = (i * 64) + 32 - int(surf_s.get_width() / 2)
screen.blit(surf_s, (x+1, y+1))
screen.blit(font_s.render(menu[i], True, color), (x, y))
#
# displays calendar
#
def disp_cal(pos, diff):
base_screen()
mon = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
date = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
day = datetime.datetime.now()
present_year = day.year
present_mon = day.month
present_day = day.day
temp = day.year * 12 + (day.month - 1)
temp += diff
disp_year = int(temp / 12)
disp_mon = int(temp % 12) + 1
# draw current month
color = color_white
if pos == 0:
color = color_purple
draw_string("%s / %04d" %
(mon[disp_mon-1], disp_year), half_sw, 12, color, True)
# draw weekdays
day_w = int(sw / 7)
day_hw = int(day_w / 2)
y = 32
for i in range(7):
x = day_hw + (day_w * i)
color = color_white
if i == 0:
color = color_red
if i == 6:
color = color_lblue
draw_string_s(date[i], x, y, color, True)
# draw calendar
cal = calendar.monthrange(disp_year, disp_mon)
wday = int((cal[0] + 1) % 7)
y = 48
for i in range(cal[1]):
x = day_hw + (day_w * wday)
color = color_white
if wday == 0:
color = color_red
if wday == 6:
color = color_lblue
# today is colored
if disp_year == present_year \
and disp_mon == present_mon \
and (i + 1) == present_day:
color = color_yellow
draw_string("%2d" % (i+1), x, y, color, True)
wday = wday + 1
# next line
if wday > 6:
wday = 0
y = y + 20
disp_current()
if pos >= 0:
disp_menu_bar(-1)
#
# displays clock
#
def disp_clock():
base_screen()
draw_string("CLOCK", half_sw, 12, color_white, True)
day = datetime.datetime.now()
theta_fine = (2 * math.pi)/360
theta = (2 * math.pi)/24
ce_len = half_sh - 40 # endpoint
cn_len = half_sh - 41 # normal
cl_len = half_sh - 45 # long
h_len = 40
m_len = 65
s_len = 70
c_cx = half_sw
c_cy = half_sh - 10
s_step = (day.second * 6)
s_step -= 90
m_step = (day.minute * 6) + (day.second / 10)
m_step -= 90
h_step = ((day.hour * 3600) + (day.minute * 60) + day.second) / 120
h_step -= 90
for i in range(360):
if i % 6 == 0:
x_len = cn_len
if i % 30 == 0:
x_len = cl_len
x = c_cx + int((math.cos(theta_fine * i) * x_len))
y = c_cy + int((math.sin(theta_fine * i) * x_len))
dx = c_cx + int((math.cos(theta_fine * i) * ce_len))
dy = c_cy + int((math.sin(theta_fine * i) * ce_len))
pygame.draw.line(screen, shadow, (x+1, y+1), (dx+1, dy+1))
pygame.draw.line(screen, color_white, (x, y), (dx, dy))
x = c_cx + int((math.cos(theta_fine * h_step) * h_len))
y = c_cy + int((math.sin(theta_fine * h_step) * h_len))
pygame.draw.line(screen, shadow, (c_cx+1, c_cy+1), (x+1, y+1), 4)
pygame.draw.line(screen, color_white, (c_cx, c_cy), (x, y), 4)
x = c_cx + int((math.cos(theta_fine * m_step) * m_len))
y = c_cy + int((math.sin(theta_fine * m_step) * m_len))
pygame.draw.line(screen, shadow, (c_cx+1, c_cy+1), (x+1, y+1), 2)
pygame.draw.line(screen, color_white, (c_cx, c_cy), (x, y), 2)
x = c_cx + int((math.cos(theta_fine * s_step) * s_len))
y = c_cy + int((math.sin(theta_fine * s_step) * s_len))
pygame.draw.line(screen, shadow, (c_cx+1, c_cy+1), (x+1, y+1))
pygame.draw.line(screen, color_white, (c_cx, c_cy), (x, y))
disp_current()
#
# displays alarm
#
def disp_alarm(pos):
base_screen()
draw_string("ALARM", half_sw, 12, color_white, True)
x = 50
day_num = [
"%02d" % (alarm_min / 60),
"%02d" % (alarm_min % 60)]
day_after = [":", ""]
x = 140
y = 50
for i in range(2):
color = color_white
if pos == i:
color = color_purple
x = x + draw_string(day_num[i], x, y, color).get_width()
x = x + draw_string(day_after[i], x, y, color_white).get_width()
x = half_sw
y = 140
day = datetime.datetime.now()
alm = day.replace(hour=int(alarm_min / 60),
minute=int(alarm_min % 60), second=0, microsecond=0)
delta = alm - day
if delta < datetime.timedelta(0):
alm += datetime.timedelta(1)
delta += datetime.timedelta(1)
delta_min = delta.seconds / 60
msg = "ALARM AFTER %02d:%02d" % (delta_min / 60, delta_min % 60)
draw_string(msg, x, y, color_white, True)
x = int(half_sw/2)
y = 170
color = color_white
if pos == 2:
color = color_purple
draw_string("SLEEP", x, y, color, True)
x += half_sw
color = color_white
if pos == 3:
color = color_purple
draw_string("STOP", x, y, color, True)
disp_current()
if pos >= 0:
disp_menu_bar(-1)
#
# displays setup
#
def disp_setup(pos):
base_screen()
draw_string("SETUP", half_sw, 12, color_white, True)
x = 50
# YYYY-MM-DD HH:MM:SS
day = datetime.datetime.now() - setup_delta
day_num = [
"%04d" % day.year,
"%02d" % day.month,
"%02d" % day.day,
"%02d" % day.hour,
"%02d" % day.minute,
"%02d" % day.second]
day_after = ["-", "-", " ", ":", ":", ""]
x = 80
y = 100
for i in range(6):
color = color_white
if pos == i:
color = color_purple
x = x + draw_string(day_num[i], x, y, color).get_width()
x = x + draw_string(day_after[i], x, y, color_white).get_width()
x = int(half_sw/2)
y = 170
color = color_white
if pos == 6:
color = color_purple
draw_string("SET", x, y, color, True)
x += half_sw
color = color_white
if pos == 7:
color = color_purple
draw_string("RESET", x, y, color, True)
disp_current()
if pos >= 0:
disp_menu_bar(-1)
#
# displays timer
#
def disp_timer(pos):
base_screen()
draw_string("TIMER", half_sw, 12, color_white, True)
x = 140
y = 50
color = color_white
if pos == 0:
color = color_purple
x = x + draw_string("%02d" % (timer_sec / 60), x, y, color).get_width()
x = x + draw_string(":", x, y, color_white).get_width()
color = color_white
if pos == 1:
color = color_purple
x = x + draw_string("%02d" % (timer_sec % 60), x, y, color).get_width()
x = int(half_sw/2)
y = 170
color = color_white
if pos == 2:
color = color_purple
draw_string("START", x, y, color, True)
x = half_sw + int(half_sw / 2)
color = color_white
if pos == 3:
color = color_purple
draw_string("STOP", x, y, color, True)
disp_current()
if pos >= 0:
disp_menu_bar(-1)
#
# displays menu choose
#
def disp_menu(pos):
if pos == 0:
disp_cal(-1, 0)
elif pos == 1:
disp_clock()
elif pos == 2:
disp_alarm(-1)
elif pos == 3:
disp_timer(-1)
elif pos == 4:
disp_setup(-1)
else:
base_screen()
disp_menu_bar(pos)
#
#
#
def cal_loop():
global timer_sec
diff = 0
max_pos = 0
pos = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if pos > 0:
pos = pos - 1
else:
pos = max_pos
if event.key == pygame.K_RIGHT:
if pos < max_pos:
pos = pos + 1
else:
pos = 0
if event.key == pygame.K_UP:
diff -= 1
if event.key == pygame.K_DOWN:
diff += 1
if event.key == pygame.K_ESCAPE or event.key == pygame.K_LALT:
done = True
disp_cal(pos, diff)
pygame.display.update()
pygame.time.delay(100)
#
#
#
def timer_loop():
global timer_sec
max_pos = 3
pos = 0
done = False
timer_start = False
timer_next = 0
timer_sec = 180
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if pos > 0:
pos = pos - 1
else:
pos = max_pos
if event.key == pygame.K_RIGHT:
if pos < max_pos:
pos = pos + 1
else:
pos = 0
if event.key == pygame.K_UP:
if pos == 0 and timer_sec <= 5879:
timer_sec += 60
if pos == 1 and timer_sec <= 5999:
timer_sec += 1
if event.key == pygame.K_DOWN:
if pos == 0 and timer_sec >= 60:
timer_sec -= 60
if pos == 1 and timer_sec >= 1:
timer_sec -= 1
if event.key == pygame.K_RETURN or event.key == pygame.K_LCTRL:
if pos == 2:
timer_start = True
timer_next = pygame.time.get_ticks() + 1000
if pos == 3:
timer_start = False
pygame.mixer.music.stop()
if event.key == pygame.K_ESCAPE or event.key == pygame.K_LALT:
done = True
while timer_start and timer_next <= pygame.time.get_ticks():
timer_next += 1000
if timer_sec > 0:
timer_sec = timer_sec - 1
if timer_sec == 0:
timer_start = False
pygame.mixer.music.load("/usr/share/pyclock/tone.wav")
pygame.mixer.music.play(-1)
disp_timer(pos)
pygame.display.update()
pygame.time.delay(100)
#
#
#
def alarm_loop():
global alarm_min
global alarm_date
alarm_flag = False
max_pos = 3
pos = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if pos > 0:
pos = pos - 1
else:
pos = max_pos
if event.key == pygame.K_RIGHT:
if pos < max_pos:
pos = pos + 1
else:
pos = 0
old_alarm = alarm_min
if event.key == pygame.K_UP:
if pos == 0:
alarm_min += 60
if pos == 1:
alarm_min += 1
if event.key == pygame.K_DOWN:
if pos == 0:
alarm_min -= 60
if pos == 1:
alarm_min -= 1
while alarm_min < 0:
alarm_min += 1440
while alarm_min >= 1440:
alarm_min -= 1440
if old_alarm != alarm_min:
calc_alarm()
if event.key == pygame.K_RETURN or event.key == pygame.K_LCTRL:
if pos == 2:
do_sleep()
if pos == 3:
calc_alarm()
pygame.mixer.music.stop()
alarm_flag = False
if event.key == pygame.K_ESCAPE or event.key == pygame.K_LALT:
done = True
if alarm_date - datetime.datetime.now() < datetime.timedelta(0):
if not alarm_flag:
alarm_flag = True
pygame.mixer.music.load("/usr/share/pyclock/tone.wav")
pygame.mixer.music.play(-1)
disp_alarm(pos)
pygame.display.update()
pygame.time.delay(100)
#
#
#
def setup_loop():
global setup_delta
max_pos = 7
pos = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if pos > 0:
pos = pos - 1
else:
pos = max_pos
if event.key == pygame.K_RIGHT:
if pos < max_pos:
pos = pos + 1
else:
pos = 0
if event.key == pygame.K_UP:
if pos == 0:
setup_delta -= datetime.timedelta(365)
if pos == 1:
setup_delta -= datetime.timedelta(31)
if pos == 2:
setup_delta -= datetime.timedelta(1)
if pos == 3:
setup_delta -= datetime.timedelta(hours=1)
if pos == 4:
setup_delta -= datetime.timedelta(minutes=1)
if pos == 5:
setup_delta -= datetime.timedelta(seconds=1)
if event.key == pygame.K_DOWN:
if pos == 0:
setup_delta += datetime.timedelta(365)
if pos == 1:
setup_delta += datetime.timedelta(31)
if pos == 2:
setup_delta += datetime.timedelta(1)
if pos == 3:
setup_delta += datetime.timedelta(hours=1)
if pos == 4:
setup_delta += datetime.timedelta(minutes=1)
if pos == 5:
setup_delta += datetime.timedelta(seconds=1)
if event.key == pygame.K_RETURN or event.key == pygame.K_LCTRL:
if pos == 6:
do_setup()
setup_delta = datetime.timedelta(0)
if pos == 7:
setup_delta = datetime.timedelta(0)
if event.key == pygame.K_ESCAPE or event.key == pygame.K_LALT:
done = True
disp_setup(pos)
pygame.display.update()
pygame.time.delay(100)
def enter_sub(pos):
if pos == 3:
timer_loop()
if pos == 4:
setup_loop()
if pos == 2:
alarm_loop()
if pos == 0:
cal_loop()
def main_loop():
max_pos = 4
pos = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if pos > 0:
pos = pos - 1
else:
pos = max_pos
if event.key == pygame.K_RIGHT:
if pos < max_pos:
pos = pos + 1
else:
pos = 0
if event.key == pygame.K_RETURN or event.key == pygame.K_LCTRL:
enter_sub(pos)
if event.key == pygame.K_ESCAPE or event.key == pygame.K_LALT:
done = True
disp_menu(pos)
pygame.display.update()
pygame.time.delay(100) # Save your Battery!
main_loop()
pygame.quit()