-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
122 lines (97 loc) · 4.2 KB
/
display.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
from collections import OrderedDict
import pygame
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args)
return cls._instances[cls]
class Display:
__metaclass__ = Singleton
def __init__(self):
(self.width, self.height) = (1280, 1024)
pygame.init()
pygame.display.set_caption('PID Controllers')
self.screen = pygame.display.set_mode((self.width, self.height))
self.screen.fill((0, 0, 0))
self.graphs = OrderedDict()
self.line_colors = [
(0, 255, 0),
(0, 100, 255),
(255, 0, 0),
(0, 0, 255),
(255, 255, 255),
(200, 200, 0),
(200, 0, 200)
]
def add_point(self, graph_name, text, point_data, min_bound, max_bound):
if len(point_data) < 1:
raise ValueError('point_data must include at least one line')
if graph_name not in self.graphs:
self.graphs[graph_name] = {
'name': graph_name,
'text': text,
'points': [],
'min_bound': min_bound,
'max_bound': max_bound
}
self.graphs[graph_name]['points'].append(point_data)
self.graphs[graph_name]['text'] = text
if len(self.graphs[graph_name]['points']) > self.width:
self.graphs[graph_name]['points'].pop(0)
@staticmethod
def clamp(n, min_bound, max_bound):
return min(max(n, min_bound), max_bound)
def rerender(self, state):
self.screen.fill((0, 0, 0))
if state['slam_telemetry']['telemetry_lost']:
font = pygame.font.Font(None, 36)
text_surface = font.render('***TELEMETRY LOST***', 1, (240, 100, 100))
text_pos = text_surface.get_rect()
text_pos.centerx = self.width / 2
text_pos.top = 36
self.screen.blit(text_surface, text_pos)
if len(self.graphs) > 0:
graph_height = self.height / len(self.graphs)
for graph_id, (graph_name, graph) in enumerate(self.graphs.iteritems()):
graph_top = graph_id * graph_height
# Verify sufficient points
if len(graph['points']) < 2:
continue
# Render text onto a separate surface, then blit to screen
font = pygame.font.Font(None, 14)
text = '{} {}'.format(graph['name'], graph['text'])
text_surface = font.render(text, 1, (230, 230, 230))
text_pos = text_surface.get_rect()
text_pos.centerx = self.width / 2
text_pos.top = graph_top
self.screen.blit(text_surface, text_pos)
for line_id in range(len(graph['points'][0])):
scaled_line_points = [(i,
graph_top + graph_height - (
self.clamp(p[line_id], graph['min_bound'], graph['max_bound']) - graph[
'min_bound']) * graph_height / (graph['max_bound'] - graph['min_bound']))
for
i, p in enumerate(graph['points'])]
pygame.draw.lines(self.screen,
self.line_colors[line_id],
False,
scaled_line_points,
1)
pygame.display.update()
def get_keyboard_events(self):
retval = []
for event in pygame.event.get():
if event.type==pygame.QUIT:
self.cleanup()
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
raise Exception('Shutdown')
if event.key == pygame.K_SPACE:
retval.append('spacebar')
if event.key == pygame.K_s:
retval.append('s')
return retval
def cleanup(self):
pygame.quit()
self.graphs = None