forked from jheidel/scrabble-opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameclock.py
More file actions
103 lines (83 loc) · 2.8 KB
/
Copy pathgameclock.py
File metadata and controls
103 lines (83 loc) · 2.8 KB
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
from mtTkinter import *
from threading import Thread, Lock
from datetime import datetime, timedelta
from time import sleep
import os
class GameClock(Thread):
def __init__(self, scorebox):
Thread.__init__(self)
self.daemon = True
self.active = True
self.l = Lock()
self.win = scorebox
#base time on clock
self.base = timedelta(0,0)
#Time from last start point
self.ref = datetime.now()
self.clock_running = False
self.warn_thresh = None
self.alarm_thresh = None
self.warn_fired = False
self.alarm_fired = False
self.alarm_callback = None
self._update_display()
def _reset_alarms(self):
self.warn_fired = False
self.alarm_fired = False
def _warn_alarm(self):
if not self.warn_fired:
self.warn_fired = True
self.win.set_clock_color("orange")
os.system("beep -f 1000 -l 50 &>/dev/null &")
def _alarm_alarm(self):
if not self.alarm_fired:
self.alarm_fired = True
self.win.set_clock_color("red")
os.system("beep -f 2000 -r 5 &>/dev/null &")
if self.alarm_callback is not None:
self.alarm_callback()
def _update_display(self):
cdt = self._get_delta()
tot_secs = int(cdt.total_seconds())
ms = cdt.microseconds / 1e3
mins = tot_secs / 60
secs = tot_secs - mins * 60
self.win.set_clock_text("%02d:%02d" % (mins, secs), "%03d" % ms)
if self.alarm_thresh is not None and tot_secs >= self.alarm_thresh:
self._alarm_alarm()
elif self.warn_thresh is not None and tot_secs >= self.warn_thresh:
self._warn_alarm()
def clock_start(self):
with self.l:
if self.clock_running:
return
self.clock_running = True
self.ref = datetime.now()
self.win.set_clock_color("black")
def clock_reset(self):
with self.l:
self.base = timedelta(0,0)
self.ref = datetime.now()
self._reset_alarms()
if self.clock_running:
self.win.set_clock_color("black")
def _get_delta(self):
time = self.base
if self.clock_running:
dt = datetime.now() - self.ref
time += dt
return time
def clock_stop(self):
with self.l:
if not self.clock_running:
return
self.base = self._get_delta()
self.clock_running = False
self.win.set_clock_color("blue")
def kill(self):
self.active=False
def run(self):
while self.active:
with self.l:
self._update_display()
sleep(0.073)