Пример #1
0
 public void initializeBuffer()
 {
     if (hostname == null)
     {
         //hostname = GetLocalIPAddress ();
         hostname = "127.0.0.1";
     }
     Debug.Log("Connecting to " + hostname + ":" + port);
     if (!bufferClient.isConnected())          // attempt to re-connect
     {
         bufferClient.connect(hostname, port);
     }
     if (bufferClient.isConnected())
     {
         Debug.Log("Connected to " + hostname + ":" + port + " succeeded");
         hdr = bufferClient.getHeader();
         if (bufferClient.errorReturned == BufferClient.BUFFER_READ_ERROR)
         {
             Debug.Log("No header on " + hostname + ":" + port + " failed");
             bufferConnectionInitialized = false;
             return;
         }
         latestBufferSample = hdr.nSamples;
         lastNumberOfEvents = hdr.nEvents;
         nChans             = hdr.nChans;
         fSample            = hdr.fSample;
         if (storeData)
         {
             initializeData();
         }
         bufferTimer = new BufferTimer(fSample);
         Debug.Log("Got valid header on " + hostname + ":" + port);
         // compute the min-update-interval in samples
         if (MINUPDATEINTERVAL_ms > 0)
         {
             DATAUPDATEINTERVAL_SAMP = (int)(MINUPDATEINTERVAL_ms * 1000.0 / hdr.fSample);
             if (DATAUPDATEINTERVAL_SAMP < 1)
             {
                 DATAUPDATEINTERVAL_SAMP = 1;
             }
         }
         bufferConnectionInitialized = true;
         if (bufferUpdateThread == null)
         {
             bufferUpdateThread = new Thread(run)
             {
                 Name = "BufferUpdateThread"
             };
             bufferUpdateThread.Start();                  // start the buffer-monitoring thread
         }
     }
     else
     {
         Debug.Log("Connection to " + hostname + ":" + port + " failed");
         bufferConnectionInitialized = false;
         return;
     }
 }
Пример #2
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;
        }
    }