-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstimulator.py
87 lines (74 loc) · 3.27 KB
/
stimulator.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
from tkinter import *
from utils import degree_to_radius as radius
from utils import get_screen_height, get_stimulator_geometry
from data import get_config_by_config_id
import time
class Stimulator(Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.master.overrideredirect(1)
self.width, self.height, self.x, self.y = get_stimulator_geometry()
self.master.geometry('{}x{}+{}+{}'.format(self.width, self.height, self.x, self.y))
self.canvas = Canvas(self.master, bg='white')
self.canvas.pack(fill = 'both', expand = True)
self.canvas.update()
self.origin_x = int(self.canvas.winfo_width() / 2)
self.origin_y = int(self.canvas.winfo_height() / 2)
self.init_canvas()
def init_canvas(self):
config = get_config_by_config_id()
self.min_degree = config.get("min_degree")
self.max_degree = config.get("max_degree")
self.chamber_height = config.get("chamber_height")
self.time_expand = config.get("time_expand")
self.time_hold = config.get("time_hold")
self.time_pause = config.get("time_pause")
self.repeat = config.get("repeat")
FLAME_PER_SECOND = 60
degree_step = ((self.max_degree - self.min_degree) / self.time_expand) / FLAME_PER_SECOND
screen_height = get_screen_height()
related = (self.chamber_height * 2 / screen_height) * self.origin_y
degree = self.min_degree
self.circle_list = []
self.canvas.delete('all')
while abs(degree - self.max_degree) >= 10**(-8) and degree < self.max_degree:
circle = self.canvas.create_oval(
self.origin_x - radius(degree, related),
self.origin_y - radius(degree, related),
self.origin_x + radius(degree, related),
self.origin_y + radius(degree, related),
fill='black'
)
self.canvas.itemconfigure(circle, state='hidden')
self.circle_list.append(circle)
degree += degree_step
self.canvas.update()
def stimulate(self):
FLAME_PER_SECOND = 60
self.canvas.itemconfigure(self.circle_list[0], state='normal')
for i in range(self.repeat):
expand_begin = time.time()
for circle in self.circle_list[1:]:
begin = time.time()
self.canvas.itemconfigure(circle, state='normal')
time.sleep(
(1 / FLAME_PER_SECOND - time.time() + begin)
if time.time() - begin <= 1 / FLAME_PER_SECOND
else 0.0
)
time.sleep(
(self.time_hold + self.time_expand - time.time() + expand_begin)
if time.time() - expand_begin <= self.time_hold + self.time_expand
else 0.0
)
pause_begin = time.time()
for circle in self.circle_list[1:]:
self.canvas.itemconfigure(circle, state='hidden')
time.sleep(
(self.time_pause - time.time() + pause_begin)
if time.time() - pause_begin <= self.time_pause
else 0.0
)
def close(self):
self.master.destroy()