-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineTest.py
256 lines (213 loc) · 7.5 KB
/
LineTest.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
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk as NavigationToolbar2TkAgg
import multiprocessing
import random
from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Rectangle
from syntacts import *
from time import sleep, time
from math import sin
from math import pi
#Create a window
root=Tk()
sensor = Tk()
sensor.title("Sensors")
canvasTest = Canvas(sensor, width=450, height=230, borderwidth=0, highlightthickness=0)
canvasTest.grid()
def _create_circle(self, x, y, r, **kwargs):
return self.create_oval(x-r, y-r, x+r, y+r, **kwargs)
canvasTest.create_circle = _create_circle
root.wm_title("WHC 2021")
root.geometry("950x950")
image = plt.imread('1x/circle.png') #root.image
#print(image.shape)
clickOn=False
xMin=0.4773
yMin=0.4093
s = Session()
s.open()
#print(DataManagement.max_min_by_country_by_year('Colombia', 2020))
def rgbtohex(rgb):
r = int(rgb[0]*255.0)
g = int(rgb[1]*255.0)
b = int(rgb[2]*255.0)
return f'#{r:02x}{g:02x}{b:02x}'
def renderSensorScreen(Tou, radius):
#print(radius)
sensor1 = canvasTest.create_circle(canvasTest, 55, 55, radius[0]+1.5, fill=rgbtohex(Tou[0][0]), outline="#DDD", width=0)
sensor2 = canvasTest.create_circle(canvasTest, 165, 55, radius[1]+1.5, fill=rgbtohex(Tou[0][1]), outline="#DDD", width=0)
sensor3 = canvasTest.create_circle(canvasTest, 275, 55, radius[2]+1.5, fill=rgbtohex(Tou[0][2]), outline="#DDD", width=0)
sensor4 = canvasTest.create_circle(canvasTest, 385, 55, radius[3]+1.5, fill=rgbtohex(Tou[0][3]), outline="#DDD", width=0)
sensor5 = canvasTest.create_circle(canvasTest, 55, 165, radius[4]+1.5, fill=rgbtohex(Tou[1][0]), outline="#DDD", width=0)
sensor6 = canvasTest.create_circle(canvasTest, 165, 165, radius[5]+1.5, fill=rgbtohex(Tou[1][1]), outline="#DDD", width=0)
sensor7 = canvasTest.create_circle(canvasTest, 275, 165, radius[6]+1.5, fill=rgbtohex(Tou[1][2]), outline="#DDD", width=0)
sensor8 = canvasTest.create_circle(canvasTest, 385, 165, radius[7]+1.5, fill=rgbtohex(Tou[1][3]), outline="#DDD", width=0)
class RenderVibration:
def __init__(self):
self.listBefore = 'Not', 'Not', 'Not', 'Not', 'Not', 'Not', 'Not', 'Not'
self.countryBefore ='Not'
self.ValMaxSom=1.0
self.som = Sine(250) * Envelope(500)
self.t0=time()-self.som.length
self.TimeMont=2000
self.modeMonth=True
self.MontCount=0
self.listRadius=[0]*8
self.radiusMax=50.0
def Countrys(self,listCount):
#res = [x for x in listCount if x not in self.listBefore]
#print(listCount)
res = [count if count != self.listBefore[x] else 'Not' for x,count in enumerate(listCount)]
#print(res)
val=False
for i in res:
if i !='Not':
val=True
if val:
#print(listCount)
#print("diff")
self.ValCountry(res)
self.listBefore = listCount
if ((time() - self.t0) >= self.som.length):
self.start_all()
self.t0=time()
#print("startSom")
def ValCountry(self, listCount):
for i,country in enumerate(listCount):
if(country !='Not'):
MaxG=1
if country == 'Out':
countVal=0.0
else:
countVal=1-country
ValCountr = countVal * self.ValMaxSom
self.listRadius[i] = countVal * self.radiusMax
#print("Act {}: Country {}, Render: {}".format(i,countVal,ValCountr))
self.Actuators(i,ValCountr)
def Actuators(self,Indx,value):
if(Indx < 4):
ActNum = Indx * 2 + 1
else:
ActNum = (Indx - 4) * 2
self.render(ActNum, value)
def render(self,indx,value):
s.set_volume(indx,value)
#print("Vib {}: Render {}".format(indx,value))
def start_all(self):
s.play(0, self.som)
s.play(1, self.som)
s.play(2, self.som)
s.play(3, self.som)
s.play(4, self.som)
s.play(5, self.som)
s.play(6, self.som)
s.play(7, self.som)
def stop_all(self):
s.stop(0)
s.stop(1)
s.stop(2)
s.stop(3)
s.stop(4)
s.stop(5)
s.stop(6)
s.stop(7)
self.t0 = time()-self.som.length
self.listBefore = 'Not', 'Not', 'Not', 'Not', 'Not', 'Not', 'Not', 'Not'
self.MontCount = 0
def stop_sound(self):
s.stop(0)
s.stop(1)
s.stop(2)
s.stop(3)
s.stop(4)
s.stop(5)
s.stop(6)
s.stop(7)
def render_all(self,value):
s.set_volume(0,value)
s.set_volume(1,value)
s.set_volume(2,value)
s.set_volume(3,value)
s.set_volume(4,value)
s.set_volume(5,value)
s.set_volume(6,value)
s.set_volume(7,value)
def start_value(self, som):
s.play(0, som)
s.play(1, som)
s.play(2, som)
s.play(3, som)
s.play(4, som)
s.play(5, som)
s.play(6, som)
s.play(7, som)
renderVib = RenderVibration()
def ModeTotal(PixNow):
NearCoun,colors=NearestPixels(PixNow)
#print(colors)
renderVib.Countrys(NearCoun)
canvasTest.delete("all")
renderSensorScreen(colors,renderVib.listRadius)
def NearestPixels(PixNow):
t0=[0]*2
NearCoun=[]
colors = [[0,0,0,0],[0,0,0,0]]
for i in range(2):
for j in range(4):
t0[0]=PixNow[0]+i
t0[1]=PixNow[1]+j
if(t0[0]>=image.shape[0] or t0[1]>=image.shape[1]):
NearCoun.append("Out")
continue
Tou0=image[t0[0], t0[1],:]
colors[i][j] = Tou0
NearCoun.append(Tou0[0])
return NearCoun, colors
def reset_all():
renderVib.stop_all()
renderVib.MontCount=0
renderVib.countryBefore='Not'
def on_click(event):
#print('click')
global clickOn
if event.inaxes is not None:
#print(clickOn)
clickOn=True
t01=[int(event.ydata+yMin), int(event.xdata+xMin)]
ModeTotal(t01)
def off_click(event):
global clickOn
clickOn=False
canvasTest.delete("all")
colBlack=[0., 0., 0., 1.]
renderSensorScreen([[colBlack]*4]*2,[0.0]*8)
reset_all()
def on_move(event):
if event.inaxes is not None:
#print(clickOn)
if clickOn==True:
t01=[int(event.ydata+yMin), int(event.xdata+xMin)]
ModeTotal(t01)
def plot(): #Function to create the base plot, make sure to make global the lines, axes, canvas and any part that you would want to update later
#fig.add_subplot(1, 2, 2)
fig2 = plt.figure()
fig2.canvas.callbacks.connect('button_press_event', on_click)
fig2.canvas.callbacks.connect('motion_notify_event', on_move)
fig2.canvas.callbacks.connect('button_release_event', off_click)
im = plt.imshow(image) # later use a.set_data(new_data) image2
canvas2 = FigureCanvasTkAgg(fig2, master=root)
canvas2.draw()
#canvas2.get_tk_widget().grid(row=1, column=1)
canvas2.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
canvas2._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
def main():
#Create the base plot
plot()
print('Start...')
root.mainloop()
print ('Done')
if __name__ == '__main__':
main()