Example #1
0
def _callback_save_settings(button):
    settings = button.parent.parent
    global headset
    try:
        headset = Headset(
            host=str(settings.host.value),  # "169.254.132.243"
            port=int(settings.port.value))  # 12003
    except Exception, e:
        headset = Headset()
        print e
    def __init__(self, dev=None, headset_id=None, rate=None):

        Headset.__init__(self, headset_id)

        self.device = dev
        self.bauderate = rate

        self.stream = Stream(device=self.device, bauderate=rate, version=Version.MINDWAVE)
        time.sleep(2)

        self.connect()
        self.run(self.stream)
Example #3
0
def setup(canvas):
    global headset
    global dimmer
    global images
    global samples
    global particles
    global attractor
    global ZOOM, ATTRACT, SPAWN, DIM, delay
    delay = 0
    global BLOB
    BLOB = Image(abspath("g", "blob.png"))  # See Attractor.draw_halo().
    global MUTE

    # ----------------------------------------------------
    #headset = Headset(host="169.254.132.243", port=12002)
    headset = Headset()
    # ----------------------------------------------------
    #dimmer = None
    dimmer = UDP("10.0.1.2", 7000)
    # ----------------------------------------------------

    # Blurred images:
    images = Images()
    for f in files(os.path.join("g", "cell", "*.png")):
        img = Image(abspath(f))
        images.cache(img.id, img, kernel=15)
        images[os.path.basename(f)] = images[img.id]

    # Audio samples:
    samples = {}
    samples["attract"] = sample(abspath("audio", "attract.wav"))
    samples["repulse"] = sample(abspath("audio", "repulse.wav"))
    samples["ambient_lo"] = loop(sample(abspath("audio", "ambient_lo.wav")))
    samples["ambient_hi"] = loop(sample(abspath("audio", "ambient_hi.wav")))

    # Particles:
    particles = []
    for i in range(40):
        particles.append(
            Particle(x=random(canvas.width),
                     y=random(canvas.height),
                     image=images["flower1.png"],
                     radius=15 + random(20),
                     bounds=(-65, -65, canvas.width + 65, canvas.height + 65),
                     type=SLEEPIE))

    # Attractor:
    attractor = Attractor(500, 250, radius=40, speed=1.0)
    attractor.bounds = (150, 100, canvas.width - 100, canvas.height - 100)

    # Canvas zoom (relative to attractor size):
    ZOOM = 1.25

    # Spacebar toggles between ignore/receive input.
    MUTE = False

    # Value that drops to zero when relaxed.
    DIM = 1.0
    def run(self,emotivHost,emotivPort,portBase,userNum):        
        self.senderLock=Lock()
        self.portBase = portBase
        self.channelCount=15
        self.outQueues = {}
        self.dataSenders={}
        self.newSenders={}
        self.headset=Headset(emotivHost,emotivPort,userNum)
        self.senderParent=SenderParent(portBase,self)
        self.senderParent.start()

        lastTime=time.clock()
        while(True):
            # arbitrary polling interval - 128hz
            curTime=time.clock()
            sleepTime=0.0078125-( curTime-lastTime)
            if sleepTime>0:
                time.sleep(0.0078125-( curTime-lastTime))
            
            lastTime=curTime

            self.headset.handleEvents()
# debug code to display all channel values            
#            values=[]
#            for c in range(0,15):
#                values.append(self.getHeadsetChannel(c))
#            print values
            # read the raw data from the headset
            self.headset.readRawData()
            # get the data from the channels we are listening to from the headset
            # and put the data into the out queues
            self.senderLock.acquire()
            for (channel,ds) in self.dataSenders.iteritems():
                if ds.isConnected():
                    if channel<100:
                        # emotion detection etc.
                        value=self.getHeadsetChannel(channel)
                        self.outQueues[channel].append(value)
                    elif channel<200:
                        # raw EEG/accelerometer etc. channels 100-125
                        if self.headset.hasState:
                            values=self.headset.getRawChannel(channel-100)
                            for c in values:
                                self.outQueues[channel].append("%f"%c)
                    elif channel<300:
                        # signal quality / connection quality
                        raise "need to implement signal quality channels"
            # trigger data senders for each out queue - trigger all at once so data comes nicely in sync
            for ds in self.dataSenders.itervalues():
                if ds.isConnected():
                    ds.dataReady()
            self.senderLock.release()
class EmotivDirectCollector():

    # userNum is the index of the headset to use
    def run(self,emotivHost,emotivPort,portBase,userNum):        
        self.senderLock=Lock()
        self.portBase = portBase
        self.channelCount=15
        self.outQueues = {}
        self.dataSenders={}
        self.newSenders={}
        self.headset=Headset(emotivHost,emotivPort,userNum)
        self.senderParent=SenderParent(portBase,self)
        self.senderParent.start()

        lastTime=time.clock()
        while(True):
            # arbitrary polling interval - 128hz
            curTime=time.clock()
            sleepTime=0.0078125-( curTime-lastTime)
            if sleepTime>0:
                time.sleep(0.0078125-( curTime-lastTime))
            
            lastTime=curTime

            self.headset.handleEvents()
# debug code to display all channel values            
#            values=[]
#            for c in range(0,15):
#                values.append(self.getHeadsetChannel(c))
#            print values
            # read the raw data from the headset
            self.headset.readRawData()
            # get the data from the channels we are listening to from the headset
            # and put the data into the out queues
            self.senderLock.acquire()
            for (channel,ds) in self.dataSenders.iteritems():
                if ds.isConnected():
                    if channel<100:
                        # emotion detection etc.
                        value=self.getHeadsetChannel(channel)
                        self.outQueues[channel].append(value)
                    elif channel<200:
                        # raw EEG/accelerometer etc. channels 100-125
                        if self.headset.hasState:
                            values=self.headset.getRawChannel(channel-100)
                            for c in values:
                                self.outQueues[channel].append("%f"%c)
                    elif channel<300:
                        # signal quality / connection quality
                        raise "need to implement signal quality channels"
            # trigger data senders for each out queue - trigger all at once so data comes nicely in sync
            for ds in self.dataSenders.itervalues():
                if ds.isConnected():
                    ds.dataReady()
            self.senderLock.release()

    # called by datasenders to get the data to be sent
    def getData(self,port):
        if self.outQueues.has_key(port) and len(self.outQueues[port])>0:
            return self.outQueues[port].popleft()
        else:
            return None
            
    def getMultiStream(self,queryString):
#        print "connect multistream %s"%queryString
        retVal=None
        self.senderLock.acquire()
        try:
            channelNum=int(queryString)
            if channelNum<self.channelCount and channelNum>=0:
                if not self.dataSenders.has_key( channelNum):
                    self.dataSenders[channelNum]=ChildDataSender(channelNum,self)
                    self.outQueues[channelNum]=deque([],1000)
                    self.dataSenders[channelNum].start()
                retVal= self.dataSenders[channelNum]
            elif channelNum>=100 and channelNum<125:
                # raw EEG / accelerometer data
                if not self.dataSenders.has_key( channelNum):
                    self.dataSenders[channelNum]=ChildDataSender(channelNum,self)
                    self.outQueues[channelNum]=deque([],1000)
                    self.dataSenders[channelNum].start()
                retVal= self.dataSenders[channelNum]
            elif channelNum>=200 and channelNum<216:
                # sensor signal quality
                raise "Implement sensor signal quality channels"
                retVal=None
        except ValueError,v:
            retVal=None # drop out
        if retVal==None:
            print "bad query string for Emotiv collector:",queryString
        self.senderLock.release()
        return retVal