forked from openyou/emokit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
168 lines (154 loc) · 5.15 KB
/
render.py
File metadata and controls
168 lines (154 loc) · 5.15 KB
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
#!/usr/bin/python
# Renders a window with graph values for each sensor and a box for gyro values.
try:
import psyco
psyco.full()
except ImportError:
print 'No psyco. Expect poor performance. Not really...'
import pygame
import platform
from pygame import FULLSCREEN
if platform.system() == "Windows":
import socket # Needed to prevent gevent crashing on Windows. (surfly / gevent issue #459)
import gevent
from emokit.emotiv import Emotiv
quality_color = {
"0": (0, 0, 0),
"1": (255, 0, 0),
"2": (255, 0, 0),
"3": (255, 255, 0),
"4": (255, 255, 0),
"5": (0, 255, 0),
"6": (0, 255, 0),
"7": (0, 255, 0),
}
old_quality_color = {
"0": (0, 0, 0),
"1": (255, 0, 0),
"2": (255, 255, 0),
"3": (0, 255, 0),
"4": (0, 255, 0),
}
class Grapher(object):
"""
Worker that draws a line for the sensor value.
"""
def __init__(self, screen, name, i):
"""
Initializes graph worker
"""
self.screen = screen
self.name = name
self.range = float(1 << 13)
self.x_offset = 40
self.y = i * gheight
self.buffer = []
font = pygame.font.Font(None, 24)
self.text = font.render(self.name, 1, (255, 0, 0))
self.text_pos = self.text.get_rect()
self.text_pos.centery = self.y + gheight
self.first_packet = True
self.y_offset = 0
def update(self, packet):
"""
Appends value and quality values to drawing buffer.
"""
if len(self.buffer) == 800 - self.x_offset:
self.buffer = self.buffer[1:]
self.buffer.append([packet.sensors[self.name]['value'], packet.sensors[self.name]['quality'], packet.old_model])
def calc_y(self, val):
"""
Calculates line height from value.
"""
return val - self.y_offset + gheight
def draw(self):
"""
Draws a line from values stored in buffer.
"""
if len(self.buffer) == 0:
return
if self.first_packet:
self.y_offset = self.buffer[0][0]
self.first_packet = False
pos = self.x_offset, self.calc_y(self.buffer[0][0]) + self.y
for i, (value, quality, old_model) in enumerate(self.buffer):
y = self.calc_y(value) + self.y
if old_model:
color = old_quality_color[str(quality)]
else:
color = quality_color[str(quality)]
pygame.draw.line(self.screen, color, pos, (self.x_offset + i, y))
pos = (self.x_offset + i, y)
self.screen.blit(self.text, self.text_pos)
def main():
"""
Creates pygame window and graph drawing workers for each sensor.
"""
global gheight
pygame.init()
screen = pygame.display.set_mode((800, 600))
graphers = []
recordings = []
recording = False
record_packets = []
updated = False
cursor_x, cursor_y = 400, 300
for name in 'AF3 F7 F3 FC5 T7 P7 O1 O2 P8 T8 FC6 F4 F8 AF4'.split(' '):
graphers.append(Grapher(screen, name, len(graphers)))
fullscreen = False
emotiv = Emotiv(display_output=True)
gevent.spawn(emotiv.setup)
gevent.sleep(0)
while emotiv.running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
emotiv.close()
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
emotiv.close()
return
elif event.key == pygame.K_f:
if fullscreen:
screen = pygame.display.set_mode((800, 600))
fullscreen = False
else:
screen = pygame.display.set_mode((800, 600), FULLSCREEN, 16)
fullscreen = True
elif event.key == pygame.K_r:
if not recording:
record_packets = []
recording = True
else:
recording = False
recordings.append(list(record_packets))
record_packets = None
packets_in_queue = 0
try:
while packets_in_queue < 8:
packet = emotiv.dequeue()
if abs(packet.gyro_x) > 1:
cursor_x = max(0, min(cursor_x, 800))
cursor_x -= packet.gyro_x
if abs(packet.gyro_y) > 1:
cursor_y += packet.gyro_y
cursor_y = max(0, min(cursor_y, 600))
map(lambda x: x.update(packet), graphers)
if recording:
record_packets.append(packet)
updated = True
packets_in_queue += 1
except Exception, ex:
print ex
if updated:
screen.fill((75, 75, 75))
map(lambda x: x.draw(), graphers)
pygame.draw.rect(screen, (255, 255, 255), (cursor_x - 5, cursor_y - 5, 10, 10), 0)
pygame.display.flip()
updated = False
gevent.sleep(0)
try:
gheight = 580 / 14
main()
except Exception, e:
print e