Exemple #1
0
 /// <summary>
 /// Convert the data if raw_data is false
 /// </summary>
 /// <param name="frame">Frame that will be convert</param>
 /// <returns>Convert frame</returns>
 private BITalinoFrame convert(BITalinoFrame frame)
 {
     int i = 0;
     foreach(ManagerBITalino.Channels channels in manager.AnalogChannels)
     {
         switch(channels)
         {
             case ManagerBITalino.Channels.EMG :
                 frame.SetAnalogValue(i, SensorDataConvertor.ScaleEMG_mV(frame.GetAnalogValue(i)));
                 break;
             case ManagerBITalino.Channels.EDA:
                 frame.SetAnalogValue(i, SensorDataConvertor.ScaleEDA(frame.GetAnalogValue(i)));
                 break;
             case ManagerBITalino.Channels.LUX:
                 frame.SetAnalogValue(i, SensorDataConvertor.ScaleLUX(frame.GetAnalogValue(i)));
                 break;
             case ManagerBITalino.Channels.ECG:
                 frame.SetAnalogValue(i, SensorDataConvertor.ScaleECG_mV(frame.GetAnalogValue(i)));
                 break;
             case ManagerBITalino.Channels.ACC:
                 frame.SetAnalogValue(i, SensorDataConvertor.ScaleACC(frame.GetAnalogValue(i)));
                 break;
             case ManagerBITalino.Channels.BATT:
                 frame.SetAnalogValue(i, SensorDataConvertor.ScaleBATT(frame.GetAnalogValue(i)));
                 break;
         }
         i++;
     }
     return frame;
 }
Exemple #2
0
 /// <summary>
 /// Save the read data in a file if data_file is true
 /// </summary>
 /// <param name="frame">data read</param>
 private void WriteData(BITalinoFrame frame)
 {
     try
     {
         if (dataFile)
         {
             if (sw == null)
             {
                 sw = File.AppendText(dataPath);
                 sw.WriteLine(getChannelsRead());
                 sw.Flush();
             }
             sw.WriteLine(CSV_Parser.ToCSV((stopWatch.Elapsed.TotalSeconds) + " " + frame.ToString(), manager.AnalogChannels.Length));
             sw.Flush();
         }
     }
     catch (Exception e)
     { UnityEngine.Debug.Log(e); }
 }
Exemple #3
0
    /// <summary>
    /// Convert the data if raw_data is false
    /// </summary>
    /// <param name="frame">Frame that will be convert</param>
    /// <returns>Convert frame</returns>
    private BITalinoFrame convert(BITalinoFrame frame)
    {
        int i = 0;

        foreach (BitalinoManager.Channels channels in manager.AnalogChannels)
        {
            switch (channels)
            {
            case BitalinoManager.Channels.EMG:
                frame.SetAnalogValue(i, SensorDataConvertor.ScaleEMG_mV(frame.GetAnalogValue(i), i));
                break;

            case BitalinoManager.Channels.EDA:
                frame.SetAnalogValue(i, SensorDataConvertor.ScaleEDA(frame.GetAnalogValue(i), i));
                break;

            case BitalinoManager.Channels.LUX:
                frame.SetAnalogValue(i, SensorDataConvertor.ScaleLUX(frame.GetAnalogValue(i)));
                break;

            case BitalinoManager.Channels.ECG:
                frame.SetAnalogValue(i, SensorDataConvertor.ScaleECG_mV(frame.GetAnalogValue(i), i));
                break;

            case BitalinoManager.Channels.ACC:
                frame.SetAnalogValue(i, SensorDataConvertor.ScaleACC(frame.GetAnalogValue(i)));
                break;

            case BitalinoManager.Channels.BATT:
                frame.SetAnalogValue(i, SensorDataConvertor.ScaleBATT(frame.GetAnalogValue(i)));
                break;

            case BitalinoManager.Channels.EEG:
                frame.SetAnalogValue(i, SensorDataConvertor.ScaleEEG_mV(frame.GetAnalogValue(i), i));
                break;
            }
            i++;
        }
        return(frame);
    }
    public static BITalinoFrame Decode(byte [] buffer, int nbBytes, int nbAnalogChannels)
    {
        try
        {
            BITalinoFrame decodeFrame = new BITalinoFrame( );
            Console.WriteLine(buffer);

            int j = (nbBytes - 1), CRC = 0, x0 = 0, x1 = 0, x2 = 0, x3 = 0, outs = 0, inp = 0;
            CRC = (buffer [j - 0] & 0x0F) & 0xFF;

            // check CRC
            for (int bytes = 0; bytes < nbBytes; bytes++)
            {
                for (int bit = 7; bit > -1; bit--)
                {
                    inp = (buffer [bytes]) >> bit & 0x01;
                    if (bytes == (nbBytes - 1) && bit < 4)
                    {
                        inp = 0;
                    }
                    outs = x3;
                    x3   = x2;
                    x2   = x1;
                    x1   = outs ^ x0;
                    x0   = inp ^ outs;
                }
            }
            int val = ((x3 << 3) | (x2 << 2) | (x1 << 1) | x0);
            if (CRC == val)
            {
                /*parse frames*/
                decodeFrame.Sequence = ( short )((buffer [j - 0] & 0xF0) >> 4) & 0xf;
                decodeFrame.SetDigitalValue(0, ( short )((buffer [j - 1] >> 7) & 0x01));
                decodeFrame.SetDigitalValue(1, ( short )((buffer [j - 1] >> 6) & 0x01));
                decodeFrame.SetDigitalValue(2, ( short )((buffer [j - 1] >> 5) & 0x01));
                decodeFrame.SetDigitalValue(3, ( short )((buffer [j - 1] >> 4) & 0x01));

                switch (nbAnalogChannels)
                {
                case 6:
                    decodeFrame.SetAnalogValue(5, ( short )((buffer [j - 7] & 0x3F)) * (1023.0 / 63.0));
                    goto case 5;

                case 5:
                    decodeFrame.SetAnalogValue(4, ( short )((((buffer [j - 6] & 0x0F) << 2) | ((buffer [j - 7] & 0xC0) >> 6)) & 0x3F) * (1023.0 / 63.0));
                    goto case 4;

                case 4:
                    decodeFrame.SetAnalogValue(3, ( short )((((buffer [j - 5] & 0x3F) << 4) | ((buffer [j - 6] & 0xf0) >> 4)) & 0x3FF));
                    goto case 3;

                case 3:
                    decodeFrame.SetAnalogValue(2, ( short )((((buffer [j - 4] & 0xFF) << 2) | (((buffer [j - 5] & 0xC0) >> 6))) & 0x3FF));
                    goto case 2;

                case 2:
                    decodeFrame.SetAnalogValue(1, ( short )((((buffer [j - 2] & 0x03) << 8) | (buffer [j - 3]) & 0xFF) & 0x3FF));
                    goto case 1;

                case 1:
                    decodeFrame.SetAnalogValue(0, ( short )((((buffer [j - 1] & 0x0F) << 6) | ((buffer [j - 2] & 0XFC) >> 2)) & 0x3FF));
                    break;
                }
            }
            else
            {
                decodeFrame          = new BITalinoFrame( );
                decodeFrame.Sequence = -1;
            }

            return(decodeFrame);
        }
        catch
        {
            throw new BITalinoException(BITalinoErrorTypes.INCORRECT_DECODE);
        }
    }
Exemple #5
0
 /// <summary>
 /// Save the read data in a file if data_file is true
 /// </summary>
 /// <param name="frame">data read</param>
 private void WriteData(BITalinoFrame frame)
 {
     try
     {
         if (dataFile)
         {
             if (sw == null)
             {
                 sw = File.AppendText(dataPath);
                 sw.WriteLine(getChannelsRead());
                 sw.Flush();
             }
             sw.WriteLine(CSV_Parser.ToCSV((stopWatch.Elapsed.TotalSeconds) + "," + frame.ToString(),manager.AnalogChannels.Length));
             sw.Flush();
         }
     }
     catch (Exception e)
     { UnityEngine.Debug.Log(e); }
 }
    public static BITalinoFrame Decode( byte [ ] buffer, int nbBytes, int nbAnalogChannels )
    {
        try
        {
            BITalinoFrame decodeFrame = new BITalinoFrame ( );

            int j = ( nbBytes - 1 ), CRC = 0, x0 = 0, x1 = 0, x2 = 0, x3 = 0, outs = 0, inp = 0;
            CRC = ( buffer [ j - 0 ] & 0x0F ) & 0xFF;

            // check CRC
            for ( int bytes = 0; bytes < nbBytes; bytes++ )
            {
                for ( int bit = 7; bit > -1; bit-- )
                {
                    inp = ( buffer [ bytes ] ) >> bit & 0x01;
                    if ( bytes == ( nbBytes - 1 ) && bit < 4 )
                    {
                        inp = 0;
                    }
                    outs = x3;
                    x3 = x2;
                    x2 = x1;
                    x1 = outs ^ x0;
                    x0 = inp ^ outs;
                }
            }
            int val = ( ( x3 << 3 ) | ( x2 << 2 ) | ( x1 << 1 ) | x0 );
            if ( CRC == val )
            {
                /*parse frames*/
                decodeFrame.Sequence = ( short ) ( ( buffer [ j - 0 ] & 0xF0 ) >> 4 ) & 0xf;
                decodeFrame.SetDigitalValue ( 0, ( short ) ( ( buffer [ j - 1 ] >> 7 ) & 0x01 ) );
                decodeFrame.SetDigitalValue ( 1, ( short ) ( ( buffer [ j - 1 ] >> 6 ) & 0x01 ) );
                decodeFrame.SetDigitalValue ( 2, ( short ) ( ( buffer [ j - 1 ] >> 5 ) & 0x01 ) );
                decodeFrame.SetDigitalValue ( 3, ( short ) ( ( buffer [ j - 1 ] >> 4 ) & 0x01 ) );

                switch ( nbAnalogChannels )
                {
                    case 6:
                        decodeFrame.SetAnalogValue ( 5, ( short ) ( ( buffer [ j - 7 ] & 0x3F ) ) * ( 1023.0 / 63.0 ) );
                        goto case 5;
                    case 5:
                        decodeFrame.SetAnalogValue ( 4, ( short ) ( ( ( ( buffer [ j - 6 ] & 0x0F ) << 2 ) | ( ( buffer [ j - 7 ] & 0xC0 ) >> 6 ) ) & 0x3F ) * ( 1023.0 / 63.0 ) );
                        goto case 4;
                    case 4:
                        decodeFrame.SetAnalogValue ( 3, ( short ) ( ( ( ( buffer [ j - 5 ] & 0x3F ) << 4 ) | ( ( buffer [ j - 6 ] & 0xf0 ) >> 4 ) ) & 0x3FF ) );
                        goto case 3;
                    case 3:
                        decodeFrame.SetAnalogValue ( 2, ( short ) ( ( ( ( buffer [ j - 4 ] & 0xFF ) << 2 ) | ( ( ( buffer [ j - 5 ] & 0xC0 ) >> 6 ) ) ) & 0x3FF ) );
                        goto case 2;
                    case 2:
                        decodeFrame.SetAnalogValue ( 1, ( short ) ( ( ( ( buffer [ j - 2 ] & 0x03 ) << 8 ) | ( buffer [ j - 3 ] ) & 0xFF ) & 0x3FF ) );
                        goto case 1;
                    case 1:
                        decodeFrame.SetAnalogValue ( 0, ( short ) ( ( ( ( buffer [ j - 1 ] & 0x0F ) << 6 ) | ( ( buffer [ j - 2 ] & 0XFC ) >> 2 ) ) & 0x3FF ) );
                        break;
                }
            }
            else
            {
                decodeFrame = new BITalinoFrame ( );
                decodeFrame.Sequence = -1;
            }

            return decodeFrame;
        }
        catch
        {
            throw new BITalinoException ( BITalinoErrorTypes.INCORRECT_DECODE );
        }
    }
    /// <summary>Read frames from the device. </summary>
    /// <param name="nbBytes">Size of a frame in byte. Calculate from <see>CalcNbBytes()</see>.</param>
    /// <param name="nbAnalogChannels">Number of channel read.</param>
    /// <param name="nbSamples">Number of frames read.</param>
    /// <returns>Return a table of frame that contain the frames read.</returns>
    /// <exception cref="BITalinoException"/>
    public BITalinoFrame [] ReadFrames(int nbBytes, int nbAnalogChannels, int nbFrames)
    {
        try
        {
            BITalinoFrame[] frames = new BITalinoFrame [nbFrames];

            byte[] buffer = new byte [nbBytes];

            byte[] bTemp = new byte [1];

            int sampleCounter = 0;

            BITalinoFrame decodedFrame;
            while (sampleCounter < nbFrames)
            {
                buffer = new byte [nbBytes];

                for (int i = 0; i < nbBytes; i++)
                {
                    SerialPort.Read(bTemp, 0, 1);

                    buffer [i] = bTemp [0];

                    bTemp = new byte [1];
                }

                decodedFrame = BITalinoFrameDecoder.Decode(buffer, nbBytes, nbAnalogChannels);

                if (decodedFrame.Sequence == -1)
                {
                    while (decodedFrame.Sequence == -1)
                    {
                        SerialPort.Read(bTemp, 0, 1);

                        for (int j = nbBytes - 2; j >= 0; j--)
                        {
                            buffer [j + 1] = buffer [j];
                        }

                        buffer [0] = bTemp [0];

                        decodedFrame = BITalinoFrameDecoder.Decode(buffer, nbBytes, nbAnalogChannels);
                    }

                    frames [sampleCounter] = decodedFrame;
                }
                else
                {
                    frames [sampleCounter] = decodedFrame;
                }

                sampleCounter++;
            }

            return(frames);
        }
        catch (Exception ex)
        {
            if (ex is InvalidOperationException)
            {
                throw new BITalinoException(BITalinoErrorTypes.COM_NOT_OPEN);
            }
            else if (ex is ArgumentOutOfRangeException || ex is ArgumentNullException || ex is ArgumentException)
            {
                throw new BITalinoException(BITalinoErrorTypes.INVALID_ARGUMENT);
            }
            else if (ex is TimeoutException)
            {
                throw new BITalinoException(BITalinoErrorTypes.TIME_OUT);
            }
            else
            {
                throw ex;
            }
        }
    }
    /// <summary>Read frames from the device. </summary>
    /// <param name="nbBytes">Size of a frame in byte. Calculate from <see>CalcNbBytes()</see>.</param>
    /// <param name="nbAnalogChannels">Number of channel read.</param>
    /// <param name="nbSamples">Number of frames read.</param>
    /// <returns>Return a table of frame that contain the frames read.</returns>
    /// <exception cref="BITalinoException"/>
    public BITalinoFrame[] ReadFrames( int nbBytes, int nbAnalogChannels, int nbFrames )
    {
        try
        {
            BITalinoFrame[] frames = new BITalinoFrame [ nbFrames ];

            byte[] buffer = new byte [ nbBytes ];

            byte[] bTemp = new byte [ 1 ];

            int sampleCounter = 0;

            BITalinoFrame decodedFrame;
            while ( sampleCounter < nbFrames )
            {
                buffer = new byte [ nbBytes ];

                for ( int i = 0; i < nbBytes; i++ )
                {
                    SerialPort.Read ( bTemp, 0, 1 );

                    buffer [ i ] = bTemp [ 0 ];

                    bTemp = new byte [ 1 ];
                }

                decodedFrame = BITalinoFrameDecoder.Decode ( buffer, nbBytes, nbAnalogChannels );

                if ( decodedFrame.Sequence == -1 )
                {
                    while ( decodedFrame.Sequence == -1 )
                    {
                        SerialPort.Read ( bTemp, 0, 1 );

                        for ( int j = nbBytes - 2; j >= 0; j-- )
                        {
                            buffer [ j + 1 ] = buffer [ j ];
                        }

                        buffer [ 0 ] = bTemp [ 0 ];

                        decodedFrame = BITalinoFrameDecoder.Decode ( buffer, nbBytes, nbAnalogChannels );
                    }

                    frames [ sampleCounter ] = decodedFrame;
                }
                else
                {
                    frames [ sampleCounter ] = decodedFrame;
                }

                sampleCounter++;
            }

            return frames;
        }
        catch ( Exception ex )
        {
            if ( ex is InvalidOperationException )
            {
                throw new BITalinoException ( BITalinoErrorTypes.COM_NOT_OPEN );
            }
            else if ( ex is ArgumentOutOfRangeException || ex is ArgumentNullException || ex is ArgumentException )
            {
                throw new BITalinoException ( BITalinoErrorTypes.INVALID_ARGUMENT );
            }
            else if ( ex is TimeoutException )
            {
                throw new BITalinoException ( BITalinoErrorTypes.TIME_OUT );
            }
            else
            {
                throw ex;
            }
        }
    }