Example #1
0
    // Find events from the buffer and process them according to their type.
    public void processBufferEvents()
    {
        int timeout = 5000;

        print(hdr.nEvents);

        try
        {
            print("Waiting for events...");
            SamplesEventsCount sec = client.waitForEvents(nEvents - 1, timeout);
            if (sec.nEvents > nEvents)
            {
                BufferEvent[] evs = client.getEvents(nEvents, sec.nEvents - 1);
                nEvents = sec.nEvents;
                print("Got " + evs.Length + " events");

                //grab the newest event (This means it only take 1 event every update)
                BufferEvent evt     = evs[evs.Length - 1];
                string      evttype = evt.getType().toString();

                // Handle Exit event
                if (evttype.Equals("exit"))
                {
                    client.disconnect();
                }

                // Handle keyboard events
                else if (evttype.Equals("keyboard"))
                {
                    predictions      = evt.getValue().toString();
                    currentBCIAction = predictions;
                    print(predictions);
                }
                // handle predictions events
                else if (evttype.Equals("classifier.prediction"))
                {
                    // Get highest prediction
                    predictionArray = (double[])evt.getValue().array;

                    double maxValue = predictionArray.Max();
                    int    maxIndex = predictionArray.ToList().IndexOf(maxValue);
                    highestPredictionindex = maxIndex;
                    // set variable to index of highest prediction or some other boundary.
                    //highestPredictionindex = 1;
                }
            }
        }
        catch
        {
            print("catch Error");
        }
    }
Example #2
0
    void Update()
    {
        if (bufferClient.errorReturned != BufferClient.BUFFER_READ_ERROR && bufferIsConnected)
        {
            SamplesEventsCount count = bufferClient.wait(latestCapturedSample + 1, lastNumberOfEvents + 1, timeout);
            latestNumebrOfEventsInBuffer = count.nEvents;
            latestBufferSample           = count.nSamples;

            while (lastNumberOfEvents < latestNumebrOfEventsInBuffer)
            {
                bufferEvents.Add(bufferClient.getEvents(lastNumberOfEvents, lastNumberOfEvents)[0]);
                lastNumberOfEvents += 1;
                if (bufferEvents.Count > bufferEventsMaxCapacity)
                {
                    bufferEvents.RemoveAt(0);
                }
                OnNewEventsAdded(EventArgs.Empty);                //This notifies anyone who's listening that there had been an extra event added in the buffer
            }

            if (latestBufferSample > latestCapturedSample)
            {
                nSamples = latestBufferSample - latestCapturedSample;
                data     = bufferClient.getFloatData(latestCapturedSample, latestBufferSample - 1); //TO DO: The getFloat needs to change according to the buffers type of data
                bufferTimer.addSampleToRegression(latestBufferSample);                              //Updates the bufferTimer with the new samples.
                latestCapturedSample = latestBufferSample;
                OnNewDataCaptured(EventArgs.Empty);                                                 //That notifies anyone who's listening that data have been updated in the buffer
                if (newDataIn)
                {
                    dataPacketsLost += 1;
                }
                else
                {
                    newDataIn = true;
                }
            }
        }
    }
Example #3
0
    // N.B. this function is called EVERY VIDEO FRAME!..... so should be as fast as possible...
    // TODO: the buffer communication should really move to be in a seperate thread!!!
    void updateBuffer()
    {
        if (bufferClient != null && bufferConnectionInitialized)
        {
            //int dataTrigger=-1;
            //if ( storeData ) {
            //	dataTrigger = latestCapturedSample+DATAUPDATEINTERVAL_SAMP;
            //}
            SamplesEventsCount count = null;
            bufferIsConnected = bufferClient.isConnected();
            if (!bufferIsConnected)                 // if we are not connected try to re-connect..
            {
                Debug.LogError("Buffer connection closed..... trying to reconnect!");
                bufferClient.reconnect();
                return;
            }
            try {
                count = bufferClient.poll();
            } catch {             // poll failed.... why?
                Debug.LogError("Poll failed.... waiting for valid response!");
                return;
            }
            latestNumberOfEventsInBuffer = count.nEvents;
            latestBufferSample           = count.nSamples;
            // reset if we have been re-awoken
            if (latestCapturedSample < 0)
            {
                latestCapturedSample = latestBufferSample;
            }
            if (lastNumberOfEvents < 0)
            {
                lastNumberOfEvents = latestNumberOfEventsInBuffer;
            }
            if (latestBufferSample < latestCapturedSample)
            {
                Debug.LogError("Buffer restart detected .. skipping everything before now...");
                bufferTimer.reset();
                latestCapturedSample = latestBufferSample;
                lastNumberOfEvents   = latestNumberOfEventsInBuffer;
            }
            bufferTimer.addSampleToRegression(latestBufferSample);            //Updates the bufferTimer with the new samples.

            // Loop ot push events to the event stream
            while (lastNumberOfEvents < latestNumberOfEventsInBuffer)
            {
                try {                 // Watch out can miss events if we are too slow...
                    bufferEvents.Add(bufferClient.getEvents(lastNumberOfEvents, lastNumberOfEvents)[0]);
                } catch (IOException ex) {
                }
                lastNumberOfEvents += 1;
                if (bufferEvents.Count > bufferEventsMaxCapacity)                // Implement a ring-buffer for the events we store...
                {
                    bufferEvents.RemoveAt(0);
                }
                OnNewEventsAdded(EventArgs.Empty);                //This notifies anyone who's listening that there had been an extra event added in the buffer
            }
            lastNumberOfEvents = latestNumberOfEventsInBuffer;

            // push new data into the data event stream
            if (latestBufferSample > latestCapturedSample)
            {
                if (storeData)                     // if we should track and store the data
                {
                    storedSamples = latestBufferSample - latestCapturedSample;
                    if (storedSamples * nChans > MAXDATASAMPLES)
                    {
                        storedSamples = MAXDATASAMPLES / nChans;
                    }
                    data = bufferClient.getFloatData(latestBufferSample - storedSamples, latestBufferSample - 1); //TO DO: The getFloat needs to change according to the buffers type of data
                    OnNewDataCaptured(EventArgs.Empty);                                                           //That notifies anyone who's listening that data have been updated in the buffer
                    if (newDataIn)
                    {
                        dataPacketsLost += 1;
                    }
                    else
                    {
                        newDataIn = true;
                    }
                }
            }
            latestCapturedSample = latestBufferSample;
        }
    }
Example #4
0
    // N.B. this function is called EVERY VIDEO FRAME!..... so should be as fast as possible...
    // TODO: the buffer communication should really move to be in a seperate thread!!!
    void Update()
    {
        if (bufferIsConnected && bufferClient != null && bufferClient.errorReturned != BufferClient.BUFFER_READ_ERROR && bufferClient.isConnected())
        {
            //int dataTrigger=-1;
            //if ( storeData ) {
            //	dataTrigger = latestCapturedSample+DATAUPDATEINTERVAL_SAMP;
            //}
            SamplesEventsCount count = bufferClient.poll();
            latestNumberOfEventsInBuffer = count.nEvents;
            latestBufferSample           = count.nSamples;
            // reset if we have been re-awoken
            if (latestCapturedSample < 0)
            {
                latestCapturedSample = latestBufferSample;
            }
            if (lastNumberOfEvents < 0)
            {
                lastNumberOfEvents = latestNumberOfEventsInBuffer;
            }

            while (lastNumberOfEvents < latestNumberOfEventsInBuffer)
            {
                try {                 // Watch out can miss events if we are too slow...
                    bufferEvents.Add(bufferClient.getEvents(lastNumberOfEvents, lastNumberOfEvents)[0]);
                } catch (IOException ex) {
                }
                lastNumberOfEvents += 1;
                if (bufferEvents.Count > bufferEventsMaxCapacity)                // Implement a ring-buffer for the events we store...
                {
                    bufferEvents.RemoveAt(0);
                }
                OnNewEventsAdded(EventArgs.Empty);                //This notifies anyone who's listening that there had been an extra event added in the buffer
            }
            lastNumberOfEvents = latestNumberOfEventsInBuffer;

            if (latestBufferSample > latestCapturedSample)
            {
                if (storeData)                     // if we should track and store the data
                {
                    storedSamples = latestBufferSample - latestCapturedSample;
                    if (storedSamples * nChans > MAXDATASAMPLES)
                    {
                        storedSamples = MAXDATASAMPLES / nChans;
                    }
                    data = bufferClient.getFloatData(latestBufferSample - storedSamples, latestBufferSample - 1); //TO DO: The getFloat needs to change according to the buffers type of data
                    OnNewDataCaptured(EventArgs.Empty);                                                           //That notifies anyone who's listening that data have been updated in the buffer
                    if (newDataIn)
                    {
                        dataPacketsLost += 1;
                    }
                    else
                    {
                        newDataIn = true;
                    }
                }
            }
            else if (latestBufferSample < latestCapturedSample)
            {
                Debug.LogError("Buffer restart detected");
                bufferTimer.reset();
            }
            bufferTimer.addSampleToRegression(latestBufferSample);            //Updates the bufferTimer with the new samples.
            latestCapturedSample = latestBufferSample;
        }
    }
Example #5
0
    public static int Main(String[] args)
    {
        String hostname = "localhost";
        int    port     = 1972;
        int    timeout  = 5000;

        if (args.Length >= 1)
        {
            hostname = args[0];
        }
        if (args.Length >= 2)
        {
            try {
                port = Convert.ToInt32(args[1]);
            }
            catch {     //  (NumberFormatException e)
                port = 0;
            }
            if (port <= 0)
            {
                System.Console.WriteLine("Second parameter (" + args[1] + ") is not a valid port number.");
                return(1);
            }
        }
        if (args.Length >= 3)
        {
            try {
                port = Convert.ToInt32(args[2]);
            }
            catch {     //  (NumberFormatException e)
                timeout = 5000;
            }
        }

        BufferClientClock C = new BufferClientClock();

        Header hdr = null;

        while (hdr == null)
        {
            try {
                System.Console.Write("Connecting to " + hostname + ":" + port + "...");
                C.connect(hostname, port);
                System.Console.WriteLine("done");
                System.Console.Write("Getting Header...");
                if (C.isConnected())
                {
                    hdr = C.getHeader();
                }
                System.Console.WriteLine("done");
            } catch {     //(IOException e)
                hdr = null;
            }
            if (hdr == null)
            {
                System.Console.WriteLine("Invalid Header... waiting");
                System.Threading.Thread.Sleep(1000);
            }
        }
        System.Console.WriteLine("#channels....: " + hdr.nChans);
        System.Console.WriteLine("#samples.....: " + hdr.nSamples);
        System.Console.WriteLine("#events......: " + hdr.nEvents);
        System.Console.WriteLine("Sampling Freq: " + hdr.fSample);
        System.Console.WriteLine("data type....: " + hdr.dataType);
        for (int n = 0; n < hdr.nChans; n++)
        {
            if (hdr.labels[n] != null)
            {
                System.Console.WriteLine("Ch. " + n + ": " + hdr.labels[n]);
            }
        }

        // Now do the echo-server
        int nEvents = hdr.nEvents;

        endExpt = false;
        while (!endExpt)
        {
            SamplesEventsCount sec = C.waitForEvents(nEvents, timeout);    // Block until there are new events
            if (sec.nEvents > nEvents)
            {
                // get the new events
                BufferEvent[] evs = C.getEvents(nEvents, sec.nEvents - 1);
                //float[][] data = C.getFloatData(0,sec.nSamples-1); // Example of how to get data also
                nEvents = sec.nEvents;// update record of which events we've seen
                // filter for ones we want
                System.Console.WriteLine("Got " + evs.Length + " events");
                for (int ei = 0; ei < evs.Length; ei++)
                {
                    BufferEvent evt     = evs[ei];
                    String      evttype = evt.getType().toString(); // N.B. to*S*tring, not upper case!
                    // only process if it's an event of a type we care about
                    // In our case, don't echo our own echo events....
                    if (!evttype.Equals("echo"))          // N.B. use equals, not == to compare string contents!
                    {
                        if (evttype.Equals("exit"))       // check for a finish event
                        {
                            endExpt = true;
                        }
                        // Print the event to the console
                        System.Console.WriteLine(ei + ") t:" + evt.getType().toString() + " v:" + evt.getValue().toString() + " s:" + evt.sample);
                        // Now create the echo event, with auto-completed sample number
                        // N.B. -1 for sample means auto-compute based on the real-time-clock
                        C.putEvent(new BufferEvent("echo", evt.getValue().toString(), -1));
                    }
                }
            }
            else         // timed out without new events
            {
                System.Console.WriteLine("Timeout waiting for events");
            }
        }
        System.Console.WriteLine("Normal Exit");
        C.disconnect();
        return(0);
    }