-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMETER.py
73 lines (66 loc) · 2.56 KB
/
METER.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
import contextlib
with contextlib.redirect_stdout(None):
import pygame
pygame.init()
MAXWIDTH=1000
MAXHEIGHT=100
MAXLINES=12
FONTSIZE=128
COLOR = (0,128,0)
units=['units']*MAXLINES
val=[0.0]*MAXLINES
desc=['']*MAXLINES
lines=1
size = width, height = MAXWIDTH, MAXHEIGHT
meterOpen=False
def openMETER(linesel=None):
global font, screen, size, height, width, lines, units, val, meterOpen
if linesel is None:
size = width, height = MAXWIDTH, MAXHEIGHT
lines=1
else:
assert ((linesel>=1) and (linesel<=MAXLINES)), "Line count must be between 1 and "+str(MAXLINES)
size = width, height = MAXWIDTH, MAXHEIGHT*linesel
lines=linesel
screen = pygame.display.set_mode(size)
pygame.display.set_caption('TINKERplate Meter')
font=pygame.font.SysFont(None,FONTSIZE)
refresh()
meterOpen=True
def closeMETER():
global meterOpen
if(meterOpen):
pygame.display.quit()
meterOpen=False
def setMETER(value,unit,descriptor,linesel=None):
global size, height, width, lines, units, val, desc
assert(meterOpen), "You must open a meter before you write to it."
if linesel is None:
line=1
else:
assert (linesel<=lines),"That line does not exist. Close your current meter and reopen with the correct number of lines."
assert ((linesel>=1) and (linesel<=MAXLINES)), "Line count must be between 1 and "+str(MAXLINES)
line=linesel
desc[line-1]=descriptor
val[line-1]=value
units[line-1]=unit
refresh()
def setTITLE(title):
pygame.display.set_caption(title)
def setCOLOR(newcolor):
global COLOR
assert (len(newcolor)==3), "Only three arguments accepted for color."
assert (newcolor[0]>=0 and newcolor[0]<=255), "Argument must be between 0 and 255"
assert (newcolor[1]>=0 and newcolor[1]<=255), "Argument must be between 0 and 255"
assert (newcolor[2]>=0 and newcolor[2]<=255), "Argument must be between 0 and 255"
COLOR=newcolor
def refresh():
global acreen, size, height, width, lines, units, val, desc
screen.fill((0,0,0))
for i in range(lines):
text_surface = font.render(desc[i]+str("%7.3f"%(val[i]))+' '+units[i], True, COLOR)
text_rect = text_surface.get_rect(center=(width/2, height*(2*i+1)/(2*lines)))
screen.blit(text_surface,text_rect)
for i in range(lines):
pygame.draw.line(screen,COLOR,[0,(i+1)*height/lines],[width,(i+1)*height/lines],3)
pygame.display.update()