Created
August 7, 2013 12:44
-
-
Save josephwilk/6173735 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import time | |
import json | |
import unicodedata | |
import gevent | |
from gevent import monkey | |
from pymindwave import headset | |
from pymindwave.pyeeg import bin_power | |
monkey.patch_all() | |
# connect to the headset | |
hs = None | |
hs = headset.Headset('/dev/tty.MindWave') | |
hs.disconnect() | |
time.sleep(1) | |
print 'connecting to headset...' | |
hs.connect() | |
time.sleep(1) | |
while hs.get('state') != 'connected': | |
print hs.get('state') | |
time.sleep(0.5) | |
if hs.get('state') == 'standby': | |
hs.connect() | |
print 'retrying connecting to headset' | |
def raw_to_spectrum(rawdata): | |
flen = 50 | |
spectrum, relative_spectrum = bin_power(rawdata, range(flen), 512) | |
return spectrum | |
while True: | |
t = time.time() | |
waves_vector = hs.get('waves_vector') | |
meditation = hs.get('meditation') | |
attention = hs.get('attention') | |
spectrum = raw_to_spectrum(hs.get('rawdata')).tolist() | |
with open("/tmp/brain-data","w") as fp: | |
s = {'timestamp': t, | |
'meditation':meditation, | |
'attention': attention, | |
'raw_spectrum': spectrum, | |
'delta_waves': waves_vector[0], | |
'theta_waves': waves_vector[1], | |
'alpha_waves': (waves_vector[2]+waves_vector[3])/2, | |
'low_alpha_waves': waves_vector[2], | |
'high_alpha_waves': waves_vector[3], | |
'beta_waves': (waves_vector[4]+waves_vector[5])/2, | |
'low_beta_waves': waves_vector[4], | |
'high_beta_waves': waves_vector[5], | |
'gamma_waves': (waves_vector[6]+waves_vector[7])/2, | |
'low_gamma_waves': waves_vector[6], | |
'mid_gamma_waves': waves_vector[7]} | |
s = json.dumps(s) | |
fp.write(s) | |
gevent.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment