예제 #1
0
파일: EegPlayer.py 프로젝트: chatbot/mybci
class EegPlayerThread(threading.Thread):
    def __init__(self, iterateFunc, cbIterateFunc):

        # function that returns data to send
        self.iterateFunc = iterateFunc
        # fucntion that will be called after sending each data block
        self.cbIterateFunc = cbIterateFunc
        # time between each data sending
        self.timeout = None

        self._spinevent = threading.Event()
        self._spinevent.clear()
        threading.Thread.__init__(self, name="EegPlayerThread")

        self.socket = EEGTransport("udp_m_cl", "224.0.0.1", 17001)

    def run(self):
        # endless cycle
        while True:
            self._spinevent.wait()

            # get and send data
            (size, data) = self.iterateFunc()
            if size:
                self.socket.sendChunked(data)
            self.cbIterateFunc(size)

            time.sleep(self.timeout)

    def pause(self):
        self._spinevent.clear()

    def wakeup(self):
        self._spinevent.set()
예제 #2
0
    def __init__(self):
        self.counter = 0;
        self.is_on = False
        self.is_loaded = False
        self.is_recording = False
        self.outfile = None
        self.channels = None
        self.freq = None

        self.transport = EEGTransport('udp_m_cl','224.0.0.1',21000)
        self.q = []
예제 #3
0
파일: EegPlayer.py 프로젝트: chatbot/mybci
    def __init__(self, iterateFunc, cbIterateFunc):

        # function that returns data to send
        self.iterateFunc = iterateFunc
        # fucntion that will be called after sending each data block
        self.cbIterateFunc = cbIterateFunc
        # time between each data sending
        self.timeout = None

        self._spinevent = threading.Event()
        self._spinevent.clear()
        threading.Thread.__init__(self, name="EegPlayerThread")

        self.socket = EEGTransport("udp_m_cl", "224.0.0.1", 17001)
예제 #4
0
파일: EegPlayer.py 프로젝트: chatbot/mybci
    def __init__(self, iterateFunc, cbIterateFunc):

        # function that returns data to send
        self.iterateFunc = iterateFunc
        # fucntion that will be called after sending each data block
        self.cbIterateFunc = cbIterateFunc
        # time between each data sending
        self.timeout = None
        self.min_timeout = None
        self.queue = []
        
        self._spinevent = threading.Event()
        self._spinevent.clear()
        threading.Thread.__init__(self, name="EegPlayerThread")

#        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#        self.address = ('234.0.0.1',21000)
        self.transport = EEGTransport('udp_m_cl','224.0.0.1',21000)
        self.transport.getTransportHeader().setEEGHeader(\
            'n-points: 250,frequency: 5000, type: uint32,names: a b c')
예제 #5
0
파일: EegPlayer.py 프로젝트: chatbot/mybci
class EegPlayerThread(threading.Thread):
    def __init__(self, iterateFunc, cbIterateFunc):

        # function that returns data to send
        self.iterateFunc = iterateFunc
        # fucntion that will be called after sending each data block
        self.cbIterateFunc = cbIterateFunc
        # time between each data sending
        self.timeout = None
        self.min_timeout = None
        self.queue = []
        
        self._spinevent = threading.Event()
        self._spinevent.clear()
        threading.Thread.__init__(self, name="EegPlayerThread")

#        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#        self.address = ('234.0.0.1',21000)
        self.transport = EEGTransport('udp_m_cl','224.0.0.1',21000)
        self.transport.getTransportHeader().setEEGHeader(\
            'n-points: 250,frequency: 5000, type: uint32,names: a b c')

    def run(self):
        # endless cycle
        while True:
            self._spinevent.wait()
            
            # get and send data
            for i in range(self.queue_maxlen):
                (size, data, props) = self.iterateFunc()
                #print size
                
            #    if (size):
                self.queue.append(data)
                 

            #if len(self.queue) == self.queue_maxlen:
            #    print len(self.queue)
            while (len(self.queue)>0):
                #self.socket.sendto(self.queue.pop(0), self.address)
                d = self.queue.pop()
                #print len(d),  self.transport.getTransportHeader()
                if props['type'] == int:
                    t = 'int'
                else:
                    t = 'float'    
                header = 'n-channels: ' + str(props['n-channels']) + ', ' + \
                         'n-points: ' + str(props['n-points']) + ', ' + \
                         'frequency: ' + str(props['frequency']) + ', ' + \
                         'type: ' + t
                #print header

                self.transport.getTransportHeader().setEEGHeader(header)
                self.transport.sendChunked(d)
#                print('send ok')
#                f=open('F:/out.eeg','w'); f.write(d); f.close()
#                sys.exit(0)                

            self.cbIterateFunc(size)
            time.sleep(self.timeout)        

    def pause(self):
        self._spinevent.clear()

    def wakeup(self):
        self._spinevent.set()        
예제 #6
0
class Eeg4mControl:
    """A class for controlling device via a Neurosoft DLL."""
    def __init__(self):
        self.counter = 0;
        self.is_on = False
        self.is_loaded = False
        self.is_recording = False
        self.outfile = None
        self.channels = None
        self.freq = None

        self.transport = EEGTransport('udp_m_cl','224.0.0.1',21000)
        self.q = []
       
    def load(self):
        # initialize library
   #     print 'Loading eeg4dll.dll...',
        self.hinst_lib = LoadLibrary("eeg4dll")
        self.nsdll = WinDLL("eeg4dll",handle=self.hinst_lib)
    #    print 'OK, ',
        
        # set error callback
    #    print 'Setting error callback...',
        ERRFUNC = CFUNCTYPE(None, c_int, c_int)
        self.nsdll.SetErrorFunction(ERRFUNC(self.errorCallback))
        self.is_loaded = True
    #    print 'OK'        
        
    def switchOn(self):
#        print 'Trying switch on device...',
        if (self.is_loaded == False):
            self.load()
        
        self.nsdll.SwitchOn.restype = POINTER(type_date_port)

        # If device is not connected there will be Eeg4mError
        try:        
            res = self.nsdll.SwitchOn(c_int(6))
        except WindowsError:
            raise
        else:
            self.is_on = True
#            print 'OK'


    def setEegFreq(self, freq):

        if (freq == '1000'):
            self.freq = freq
#             self.nsdll.SetFrecEEG(c_int(0))
        elif (freq == '5000'):
            self.freq = freq
#             self.nsdll.SetFrecEEG(c_int(1))

    def transmitEeg(self,chlist):

        self.counter = 0
        try:
            if (self.is_on == False):
                self.switchOn()
        except:
            return False


        # set frequency
        if (self.freq == '1000'):
            self.nsdll.SetFrecEEG(c_int(0))
        elif (self.freq == '5000'):
            self.nsdll.SetFrecEEG(c_int(1))
        else:
            raise Eeg4mError, 'Frequency not set'

            

        f = self.nsdll.value_param_amplifier_eeg4m
        channels_type = c_byte*26
        channels = channels_type()
        i=0
        while (i<26):
            channels[i] = chlist[i]
            i=i+1

        self.channels = chlist
        
        print 'transmit:',
        for ch in channels:
            print ch,
        print            

        f.argtypes = [c_uint,c_uint,c_uint,c_uint,c_uint,c_uint,c_uint,c_uint,channels_type]
        f(0,0,0,0,0,0,0,0,channels)

        # start transmit
        #q = type_buffer_eeg()
        #l=0
        EEGFUNC = CFUNCTYPE(None, type_buffer_eeg, c_long, c_int)
        self.nsdll.SetTransmitEEG(EEGFUNC(self.eegCallback))
        print 'Transmit started'
        return True

    def transmitEp(self):
        if (self.is_on == False):
            self.switchOn()        
        self.counter = 0

    def stop(self):
        self.nsdll.OnStopReceive()
        if (self.is_recording == True):
            self.outfile.flush()
            self.outfile.close()
        #print 'Transmit ended, eeg4m-counter is', self.counter        
        self.switchOff() # Yes, do it always!

    def switchOff(self):
        self.nsdll.SwitchOff()
        self.is_on = False

    def errorCallback(self, error, type_error):
        self.nsdll.return_text_error.restype = c_char_p
        text = self.nsdll.return_text_error(error | (1 << 24))
        tkMessageBox.showerror(_("Error"),_(text))
        if (type_error == 0):
            stderr.write('Error '+str(error)+' noncritical:'+text+'\n')
            stderr.flush()
            #raise Eeg4mError(text)
        else:
            stderr.write('Error '+str(error)+' critical:'+text+'\n')
            stderr.flush()            
            #raise Eeg4mCriticalError(text)

    def eegCallback(self, buf, length, time_on_bus):

        n_points = 200
        queue_maxlen = n_points/length
        # compute channels count
        n_channels = 0
        index=0
        #print 'channels', str(self.channels)
        #print 'len of channels:',len(self.channels)
        for i in range(26):
            if i==10:
                continue
            if (self.channels[i] == 0):
                continue
            n_channels = n_channels + 1

        #print 'n_channels: ',n_channels
        #print 'length:',length
        a = zeros(n_channels*length,dtype=int)    
        #print 'a:', a
        

        self.counter = self.counter + 1

        j=0
        for i in range(26):
            if i==10:
                continue
            if (self.channels[i]==0):
                continue
            

            jmin = j*length
            jmax = jmin+length

            bmin = i*2000
            bmax = bmin+length

            #print jmin,jmax,bmin,bmax
            #print a[jmin:jmax]
            #print buf[0][bmin:bmax]
            a[jmin:jmax]=buf[0][bmin:bmax]
            j=j+1

        self.q.append(a)
        #print a
        
        if (len(self.q)>=queue_maxlen):
            tosend = concatenate(self.q).tostring()

            header = 'n-channels: ' + str(n_channels) + ', ' + \
         'n-points: ' + str(n_points) + ', ' + \
         'frequency: ' + str(5000) + ', ' + \
         'type: int'
                
            self.transport.getTransportHeader().setEEGHeader(header)
            self.transport.sendChunked(tosend)
#            print 'send ok'
        
            while (len(self.q)>0):
                self.q.pop()

            print self.counter

        if (self.is_recording == False):
            return
        
        ss = ''
        for p in range(length):
            s=''
            for i in range(26):
                if (i == 10): # referrent
                    continue
                if (self.channels[i] == 0):
                    continue
                s = s + '\t' + str(buf[0][i*2000+p])
            ss = ss + s + '\r\n'
        self.outfile.write(ss)


    def epCallback(self):
        pass

    def record_on(self,filename):
        if (self.outfile != None):
            self.outfile.close()
        self.outfile = open(filename,"wb")
        self.is_recording = True

    def record_off(self):
        self.is_recording = False
        if (self.outfile != None):
            self.outfile.close()
            self.outfile = None
예제 #7
0
파일: eegfft.py 프로젝트: chatbot/mybci
#print 'tcpN_port = %i' % tcpN_port

# datablock queue
q = None
# maximum length of queue, computed from header.n_channels and n_fft_points
q_maxlen = None
# current length of queue
q_len = None

# current values of incoming data
n_channels = None
n_points = None

# Transports
xin = EEGTransport('udp_m_serv', '224.0.0.1', mcast_port)
xout = EEGTransport('udp_m_cl', '224.0.0.1', mcast_out_port)
#xout = EEGTransport('udp_m_cl', '224.0.0.1', 21001)

# FFT Library
libmyfft = CDLL("libmyfft.so")

#port = tcpN_port
addr = '0.0.0.0'

queue = Queue.Queue(0)
calc = FftCalculator(queue,xout)
calc.start()


예제 #8
0
cwt_n_points = 1024

# datablock queue
q = None
# maximum length of queue, computed from header.n_channels and cwt_n_points
q_maxlen = None
# current length of queue
q_len = None

# current values of incoming data
n_channels = None
n_points = None

# Transports
xin = EEGTransport('udp_m_serv', '224.0.0.1', 17000)
#xout = EEGTransport('udp_m_cl', '224.0.0.1', 21001)


port = 17000
addr = '0.0.0.0'

print 'Waiting for tcp connect on ' + addr + ':' + str(port)
tcpsock = socket(AF_INET, SOCK_STREAM)
tcpsock.bind((addr, port))
tcpsock.listen(10)
newsock, newaddr = tcpsock.accept()
print 'Connected: ', newsock.getsockname()


def reinit_arrays(new_n_channels, new_n_points):
예제 #9
0
파일: udp_server.py 프로젝트: chatbot/mybci
import sys 
sys.path.append('..')

from network.EEGTransport import *

x = EEGTransport('udp_m_serv', '224.0.0.1', 17000)

for i in xrange(4):
	data = x.recvChunked()
	header = x.getTransportHeader()
	#print 'packet: ' + str(header.getNum()) + ' chunks: ' + str(header.getChunks()) + '\n'
	#print 'header: ' + header.getEEGHeader().get_header()
	sys.stderr.write('Packet: ' + str(i))
	print data
	
x.close()
예제 #10
0
파일: udp_client.py 프로젝트: chatbot/mybci
import sys 
import time
sys.path.append('..')

from network.EEGTransport import *

x = EEGTransport('udp_m_cl', '224.0.0.1', 17000)

data = ''

len =100000
x.getTransportHeader().setEEGHeader('n-points: 250,frequency: 5000, type: uint32,names: a b c d e f')

for i in xrange(4):
	for c in xrange(len):
			data += str(c) +','
	x.sendChunked(data)
	print data
	time.sleep(1)
x.close()
예제 #11
0
파일: tcp_server.py 프로젝트: chatbot/mybci
import sys 
sys.path.append('..')

from network.EEGTransport import *

x = EEGTransport('tcp_serv', 'localhost', 999)

x = x.accept()


while 1:
        data = x.recv(10)
	if data:
		print data
	else:
		break
		
x.close()