public void Disconnect() { if (theStream != null) { theReader.Close(); mySocket.Close(); } bciState = BCIState.Disconnected; onBCIStateChanged.Invoke(Enum.GetName(typeof(BCIState), bciState), ""); }
void Start() { if (instance == null) { instance = this; } DontDestroyOnLoad(this); bciState = BCIState.Disconnected; onBCIStateChanged.Invoke(Enum.GetName(typeof(BCIState), bciState), ""); StartCoroutine("ConnectToBCI"); }
void Start() { if (instance == null) { instance = this; } DontDestroyOnLoad(this); consecThresholdBuffer = new int[consecutiveBufferSize]; consecThresholdBufferVal = new float[consecutiveBufferSize]; bciState = BCIState.Disconnected; loggingManager = GameObject.Find("LoggingManager").GetComponent <LoggingManager>(); LogMeta(); onBCIStateChanged.Invoke(Enum.GetName(typeof(BCIState), bciState), ""); StartCoroutine("ConnectToBCI"); inputNumber = 0; }
private IEnumerator ConnectToBCI() { while (mySocket == null || !mySocket.Connected) { bciState = BCIState.Connecting; LogStateEvent(); onBCIStateChanged.Invoke(Enum.GetName(typeof(BCIState), bciState), "Establishing connection to BCI Socket.."); yield return(new WaitForSeconds(0.5f)); try { mySocket = new TcpClient(Host, Port); theStream = mySocket.GetStream(); theWriter = new StreamWriter(theStream); theReader = new StreamReader(theStream); } catch (SocketException e) { UnityEngine.Debug.LogError(e.Message); bciState = BCIState.Disconnected; onBCIStateChanged.Invoke(Enum.GetName(typeof(BCIState), bciState), "Could not connect to the BCI Socket.."); } if (bciState == BCIState.Disconnected) { yield return(new WaitForSeconds(2f)); } } bciState = BCIState.ReceivingHeader; LogStateEvent(); onBCIStateChanged.Invoke(Enum.GetName(typeof(BCIState), bciState), "Waiting for data..Make sure that Acquisition is paired with PC."); StateObject state = new StateObject(); state.workSocket = mySocket; int variableSize = sizeof(UInt32); int variableCount = 8; int headerSize = variableCount * variableSize; byte[] headerBuffer = new byte[headerSize]; yield return(null); }
public double ReadSocket() { if (theStream == null) { return(-1); } if (theStream.DataAvailable) { // read header once if (bciState == BCIState.ReceivingHeader) { ReadHeader(); bciState = BCIState.ReceivingData; LogStateEvent(); onBCIStateChanged.Invoke(Enum.GetName(typeof(BCIState), bciState), ""); } if (bciState == BCIState.ReceivingData) { // raw signal data // [nSamples x nChannels] // all channels for one sample are sent in a sequence, then all channels of the next sample // create a signal object to send it to another RawOpenVibeSignal newSignal = new RawOpenVibeSignal(); newSignal.samples = testSampleCount; newSignal.channels = testChannelCount; double[,] newMatrix = new double[testSampleCount, testChannelCount]; //Debug.Log("SampleCount: " + testSampleCount); //Debug.Log("ChannelCount: " + testChannelCount); byte[] buffer = new byte[testSampleChannelSize]; theStream.Read(buffer, 0, testSampleChannelSize); int row = 0; int col = 0; for (int i = 0; i < testSampleCount * testChannelCount * (sizeof(double)); i = i + (sizeof(double) * testChannelCount)) { for (int j = 0; j < testChannelCount * sizeof(double); j = j + sizeof(double)) { byte[] temp = new byte[8]; for (int k = 0; k < 8; k++) { temp[k] = buffer[i + j + k]; } if (BitConverter.IsLittleEndian) { // Array.Reverse(temp); double test = BitConverter.ToDouble(temp, 0); // TODO TEST THIS //newMatrix[i / (8 * testChannelCount), j / 8] = test; newMatrix[row, col] = test; } col++; } row++; col = 0; } newSignal.signalMatrix = newMatrix; lastSignal = newSignal; lastMatrix = newMatrix; return(newMatrix[0, 0]); } } return(-1); }