-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModuleMeter.py
220 lines (190 loc) · 8 KB
/
ModuleMeter.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
'''
Meter Gauge GUI
Downloaded from https://github.com/NickWaterton/iperf3-GUI
'''
import tkinter as tk
from tkinter import font as tkf
import math, time
# class to show a gauge or panel meter
class Meter(tk.Canvas):
def __init__(self,master,*args,**kwargs):
#super(Meter,self).__init__(master,*args,**kwargs)
tk.Canvas.__init__(self, master,*args,**kwargs)
self.ranges = [100] #default range
self.tick_values =[]
self.show_max = 0
self.max_val = 0
self.range = self.ranges[0]
self.current_value = 0
self.layoutparams()
self.graphics()
self.createhand()
self.setrange()
def layoutparams(self):
# set parameters that control the layout
height = int(self['height'])
width = int(self['width'])
# find a square that fits in the window
if(height*2 > width):
side = width
else:
side = height*2
# set axis for hand
self.centrex = side/2
self.centrey = side/2
# standard with of lines
self.linewidth = 2
# outer radius for dial
self.radius = int(0.40*float(side))
# set width of bezel
self.bezel = self.radius/15
self.bezelcolour1 = '#c0c0c0'
self.bezelcolour2 = '#808080'
# set lengths of ticks and hand
self.majortick = self.radius/8
self.minortick = self.majortick/2
self.handlen = self.radius - self.majortick - self.bezel - 1
self.blobrad = self.handlen/6
def graphics(self):
# create the static components
self.draw_bezel()
for deg in range(-60,241,6):
self.createtick(deg,self.minortick)
for deg in range(-60,241,30):
self.createtick(deg,self.majortick)
def draw_bezel(self):
self.create_oval(self.centrex-self.radius
,self.centrey-self.radius
,self.centrex+self.radius
,self.centrey+self.radius
,width = self.bezel
,outline = self.bezelcolour2)
self.create_oval(self.centrex-self.radius - self.bezel
,self.centrey-self.radius - self.bezel
,self.centrex+self.radius + self.bezel
,self.centrey+self.radius + self.bezel
,width = self.bezel
,outline = self.bezelcolour1)
for angle in [60, 0, 300]:
self.create_arc(self.centrex-self.radius
,self.centrey-self.radius
,self.centrex+self.radius
,self.centrey+self.radius
,width = self.bezel
,style = tk.ARC
,start = angle
,extent = 180 if angle == 60 else 60
,outline = 'green' if angle==60 else 'orange' if angle==0 else 'red')
def createhand(self):
# create text value display
self.textid = self.create_text(self.centrex
,self.centrey - 3*self.blobrad
,fill = 'red'
,font = tkf.Font(size = -int(2*self.majortick)))
# create units display
self.unitsid = self.create_text(self.centrex
,self.centrey + 3*self.blobrad
,fill = 'black'
,font = tkf.Font(size = -int(1.5*self.majortick)))
# create moving and changeable bits
self.handid = self.create_line(self.centrex,self.centrey
,self.centrex - self.handlen,self.centrey
,width = 2*self.linewidth
,fill = 'red')
self.blobid = self.create_oval(self.centrex - self.blobrad
,self.centrey - self.blobrad
,self.centrex + self.blobrad
,self.centrey + self.blobrad
,outline = 'black', fill = 'black')
def createtick(self,angle,length):
# helper function to create one tick
rad = math.radians(angle)
cos = math.cos(rad)
sin = math.sin(rad)
radius = self.radius - self.bezel
if length == self.majortick: #label major ticks
value = '{0:.2f}'.format((float(angle+60.0)/300.0*self.range))
self.tick_values.append(self.create_text(self.centrex - (radius - length - 10)*cos, self.centrey - (radius - length - 10)*sin, text=value))
self.create_line(self.centrex - radius*cos
,self.centrey - radius*sin
,self.centrex - (radius - length)*cos
,self.centrey - (radius - length)*sin
,width = self.linewidth
,fill = 'green' if angle< 120 else 'orange' if angle < 180 else 'red')
def setrange(self,start = 0, end=100):
self.start = start
self.range = end - start
#self.current_value = start
for val_index, angle in enumerate(range(-60,241,30)): #relabel major ticks.
value = '{0:.2f}'.format((float(angle+60)/300.0*self.range))
self.itemconfigure(self.tick_values[val_index],text = value)
def units(self,text):
self.itemconfigure(self.unitsid,text = str(text))
def smooth_set(self, value, arc=False):
'''
do a smooth update, rather than jump to value
y += alpha * (x-y) #low pass filter algorithm
alpha defines response time
'''
alpha = 0.1
result = self.current_value
while int(result) != int(value):
result += alpha * float(value-self.current_value)
self.set(int(result), arc)
time.sleep(0.01)
self.update_idletasks()
def set(self,value, arc=False):
# call this to set the hand (jump to value)
self.current_value = value
#update range if value is too high
if value > self.range+(self.range/20): #if more than 5% over-range
self.setrange(end=next((i for i in self.ranges if i>=value),self.ranges[-1])) #increase range if possible
# convert value to range 0,100
deg = 300*(value - self.start)/self.range - 240
self.itemconfigure(self.textid,text = str(value))
self.itemconfigure(self.textid,fill = 'green' if int(value) < self.range*6/10 else 'orange' if int(value) < self.range*8/10 else 'red')
rad = math.radians(deg)
# reposition hand
self.coords(self.handid,self.centrex,self.centrey
,self.centrex+self.handlen*math.cos(rad), self.centrey+self.handlen*math.sin(rad))
#draw arc if selected
if arc:
if self.show_max == 0: #off
return
elif self.show_max == 1: #live
pass
elif self.show_max == 2: #hold peak
if value < self.max_val:
return
else:
self.max_val = value
self.create_oval(self.centrex-self.radius - self.bezel
,self.centrey-self.radius - self.bezel
,self.centrex+self.radius + self.bezel
,self.centrey+self.radius + self.bezel
,width = self.bezel
,outline = self.bezelcolour1)
self.create_arc(self.centrex-self.radius - self.bezel
,self.centrey-self.radius - self.bezel
,self.centrex+self.radius + self.bezel
,self.centrey+self.radius + self.bezel
,width = self.bezel
,style = tk.ARC
,start = max(360-deg, 300)
,extent = min(240+deg, 300)
,outline = 'blue')#'green' if angle==60 else 'orange' if angle==0 else 'red')
'''
self.create_arc(self.centrex-self.radius - self.bezel
,self.centrey-self.radius - self.bezel
,self.centrex+self.radius + self.bezel
,self.centrey+self.radius + self.bezel
,width = self.bezel
,style = tk.ARC
,start = 300
,extent = max(60-deg,0)
,outline = self.bezelcolour1)
'''
#print("value: %s, deg: %s" % (value,deg))
def blob(self,colour):
# call this to change the colour of the blob
self.itemconfigure(self.blobid,fill = colour,outline = colour)