-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain_test.py
68 lines (55 loc) · 1.82 KB
/
brain_test.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
"""
Some stuff for testing brain.py
This is some hacky crap. Redo later.
"""
import json
import serial
import time
def recordData(serialport="/dev/tty.usbserial-A7005EJD", serialspeed=9600
count=100):
""" Record data dumped by the NeuroSky headset into a set of nested lists.
A delay more than 100ms between bytes (presumably, the time between
packets) starts the next list.
"""
data = [[]]
s = serial.Serial(port=serialport, baudrate=serialspeed)
while len(data) < count:
t0 = time.time()
b = ord(s.read())
t1 = time.time()
if t1 - t0 > .1:
data.append([])
data[-1].append(b)
return data
def dumpData(data, filename="data-dump.json"):
"""
"""
with open(filename,'w') as f:
json.dump(f, data)
class FakeSerial(object):
""" A dummy serial connection that will stream data read from a file, one
byte at a time. Built-in delays try to simulate communicating with
the NeuroSky headset.
"""
def __init__(self, filename="data-100.json"):
with open(filename, 'r') as f:
self.data = json.load(f)
self.outerIter = iter(self.data)
self.innerIter = iter(self.outerIter.next())
self.closed = False
def read(self):
v = self.nextByte()
return chr(v)
def nextByte(self):
try:
time.sleep(0.0008)
return self.innerIter.next()
except StopIteration:
try:
self.innerIter = iter(self.outerIter.next())
return self.innerIter.next()
except StopIteration:
time.sleep(1)
self.outerIter = iter(self.data)
self.innerIter = iter(self.outerIter.next())
return self.innerIter.next()