Пример #1
0
        // Called when data is received from a device

        static void OnDataReceived(object sender, EventArgs e)
        {
            //Device d = (Device)sender;

            Device.DataEventArgs de = (Device.DataEventArgs)e;
            DataRow[]            tempDataRowArray = de.DataRowArray;

            TGParser tgParser = new TGParser();

            tgParser.Read(de.DataRowArray);



            /* Loops through the newly parsed data of the connected headset*/
            // The comments below indicate and can be used to print out the different data outputs.

            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                if (tgParser.ParsedData[i].ContainsKey("Raw"))
                {
                    //Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
                }

                if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
                {
                    //The following line prints the Time associated with the parsed data
                    //A Poor Signal value of 0 indicates that your headset is fitting properly

                    poorSig = (byte)tgParser.ParsedData[i]["PoorSignal"];
                    try
                    {
                        _serialPort.Write("%" + tgParser.ParsedData[i]["PoorSignal"]);
                        //Console.WriteLine("Poor Signal:" + tgParser.ParsedData[i]["PoorSignal"]);
                        //Console.Write(tgParser.ParsedData[i]["PoorSignal"] + "%");
                    }
                    catch (System.InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }


                if (tgParser.ParsedData[i].ContainsKey("Attention"))
                {
                    try
                    {
                        // Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
                        _serialPort.Write("^" + tgParser.ParsedData[i]["Attention"]);
                        // Console.Write("^" + tgParser.ParsedData[i]["Attention"]);
                    }
                    catch (System.InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }


                if (tgParser.ParsedData[i].ContainsKey("Meditation"))
                {
                    try
                    {
                        // Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
                        _serialPort.Write("&" + tgParser.ParsedData[i]["Meditation"]);
                        // Console.Write("&" + tgParser.ParsedData[i]["Meditation"]);
                    }
                    catch (System.InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }


                if (tgParser.ParsedData[i].ContainsKey("EegPowerDelta"))
                {
                    try
                    {
                        // Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
                        _serialPort.Write("$" + tgParser.ParsedData[i]["EegPowerDelta"]);
                        //Console.WriteLine("$" + tgParser.ParsedData[i]["EegPowerDelta"]*");
                    }
                    catch (System.InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

                if (tgParser.ParsedData[i].ContainsKey("BlinkStrength"))
                {
                    //Console.WriteLine("Eyeblink " + tgParser.ParsedData[i]["BlinkStrength"]);
                }
            }
        }
        /**
         * Called when data is received from a device
         */



        static void OnDataReceived(object sender, EventArgs e)
        {
            //Device d = (Device)sender;
            Device.DataEventArgs de = (Device.DataEventArgs)e;
            DataRow[]            tempDataRowArray = de.DataRowArray;

            TGParser tgParser = new TGParser();

            tgParser.Read(de.DataRowArray);

            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                if (tgParser.ParsedData[i].ContainsKey("Raw"))
                {
                    OneBuffer.Add(tgParser.ParsedData[i]["Raw"]);
                    FiveBuffer.Insert(0, tgParser.ParsedData[i]["Raw"]);
                    if (FiveBuffer.Count > 512)
                    {
                        FiveBuffer.RemoveAt(512);
                    }
                    if (OneBuffer.Count >= 512)
                    {
                        double[] _FiveBuffer = new double[5 * 512];
                        _FiveBuffer = FiveBuffer.ToArray(typeof(double)) as double[];
                        double[] pf = FFT(_FiveBuffer, 1000);

                        for (int j = 0; j < 256; j++)
                        {
                            if ((double)(df * j) >= 1.0 && (double)(df * j) < 58.0)  //
                            {
                                total += pf[j];
                            }
                            if ((double)(df * j) >= 20.0 && (double)(df * j) <= 30.0)   // 미간 사이에 둬야 인식이 잘 됨
                            {
                                hbeta += pf[j];
                            }
                        }
                        Console.WriteLine("High-Beta : " + hbeta);
                        Console.WriteLine("total : " + total);

                        rp = hbeta / total;  // smr wave의 relative power
                        Console.WriteLine(rp);
                        //    Result.Add(new KeyValuePair<string, double>("name", rp));  // name에는 각각의 test 이름을 넣어줄 것. 아니면 순서는 고정 값으로 정해놓고 값만 넣든지
                        hbeta = 0;
                        total = 0;

                        /*
                         * string path = @"C:\Users\user\Desktop\BrainWave items\HelloEEG\HelloEEG\bin\Debug\text.txt";
                         * FileInfo fi = new FileInfo(path);
                         * if(fi.Exists==false)
                         * {
                         *  System.IO.File.WriteAllText(path, rp.ToString());
                         * }*/

                        // string source = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");


                        //string source = DateTime.Now.ToString("dd  HH-mm-ss");

                        //string makeFolder_path= @"C:\Users\김나혜\Desktop\";
                        // DirectoryInfo dir = new DirectoryInfo(@"C:\Users\김나혜\Desktop\");
                        // dir.Name = source;
                        // dir.Create();

                        /*  string makeFolder_path = @"C:\Users\김나혜\Desktop\";
                         * string source = DateTime.Now.ToString("dd  HH-mm-ss");
                         * string folderName = makeFolder_path + source;
                         * DirectoryInfo f = new DirectoryInfo(folderName);
                         * f.Create();*/

                        /*  DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\김나혜\Desktop\DB");
                         * FileInfo fileInfo = null;
                         * try
                         *    {
                         *        if (directoryInfo.Exists)
                         *        {
                         *            fileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, "DB"));
                         *            try
                         *            {
                         *                if (fileInfo.Exists)
                         *                {
                         *                //StreamWriter wr = new StreamWriter(fileInfo, FileMode.Append);
                         *                StreamWriter wr = new StreamWriter(@"C:\Users\김나혜\Desktop\DB" + count + ".txt");
                         *                count++;
                         *                }
                         *                else
                         *                {
                         *                    fileInfo.Create();
                         *                }
                         *            }
                         *        }
                         *        else
                         *        {
                         *            directoryInfo.Create();
                         *        }
                         *    }*/
                        //폴더 생성
                        string        makeFolder_path = @"C:\Users\정민지\Desktop\";
                        string        folderName      = makeFolder_path + "DB";
                        DirectoryInfo f = new DirectoryInfo(folderName);
                        f.Create();

                        StreamWriter wr = new StreamWriter(@"C:\Users\정민지\Desktop\DB\" + count + ".txt");
                        count++;

                        double val = rp;
                        wr.WriteLine(val);
                        wr.Close();

                        OneBuffer.Clear();
                        break;
                    }
                }

                if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
                {
                    //Console.WriteLine("Time:" + tgParser.ParsedData[i]["Time"]);
                    //Console.WriteLine("PQ Value:" + tgParser.ParsedData[i]["PoorSignal"]);
                    poorSig = (byte)tgParser.ParsedData[i]["PoorSignal"];

                    //this is required because heart rate value is not returned when you have poor signal 200
                    if (poorSig != 200)
                    {
                        int      recovery     = heartRateRecovery.getHeartRateRecovery(0, poorSig);
                        double[] acceleration = heartRateAcceleration.getAcceleration(0, poorSig);
                    }
                }

                if (tgParser.ParsedData[i].ContainsKey("Attention"))
                {
                    //Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
                }

                if (tgParser.ParsedData[i].ContainsKey("Meditation"))
                {
                    //Console.WriteLine("Med Value:" + tgParser.ParsedData[i]["Meditation"]);
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerDelta"))
                {
                    //Console.WriteLine("Delta: " + tgParser.ParsedData[i]["EegPowerDelta"]);
                }
                if (tgParser.ParsedData[i].ContainsKey("EnergyLevel"))
                {
                    //Console.WriteLine("Energy: " + tgParser.ParsedData[i]["EnergyLevel"]);
                }
                if (tgParser.ParsedData[i].ContainsKey("HeartRate"))
                {
                    //Console.WriteLine("HeartRate: " + tgParser.ParsedData[i]["HeartRate"]);

                    //int recovery = heartRateRecovery.getHeartRateRecovery((int)tgParser.ParsedData[i]["HeartRate"], poorSig);
                    //double[] acceleration = heartRateAcceleration.getAcceleration((int)tgParser.ParsedData[i]["HeartRate"], poorSig);

                    //if (recovery != -501)
                    //{
                    //    Console.WriteLine("heart rate recovery: " + recovery);

                    //    heartRateRecovery.enableHeartRateRecovery();
                    //}

                    //if (acceleration[0] > 0)
                    //{
                    //    Console.WriteLine("average heart rate = " + acceleration[0] + " and heart rate acceleration = " + acceleration[1] + " beats/min^2");
                    //}
                }

                if (tgParser.ParsedData[i].ContainsKey("HeartAge"))
                {
                    //Console.WriteLine("Heart Age: " + tgParser.ParsedData[i]["HeartAge"]);
                }
                if (tgParser.ParsedData[i].ContainsKey("Positivity"))
                {
                    //Console.WriteLine("Positivity: " + tgParser.ParsedData[i]["Positivity"]);
                }
            }
        }
Пример #3
0
        // Called when data is received from a device
        static void OnDataReceived(object sender, EventArgs e)
        {
            Device.DataEventArgs de = (Device.DataEventArgs)e;
            DataRow[]            tempDataRowArray = de.DataRowArray;

            TGParser tgParser = new TGParser();

            tgParser.Read(de.DataRowArray);

            string[] keys = { "PoorSignal", "Attention", "Meditation", "MentalEffort", "TaskFamiliarity", "EegPowerDelta", "EegPowerTheta", "EegPowerAlpha1", "EegPowerAlpha2", "EegPowerBeta1", "EegPowerBeta2", "EegPowerGamma1", "EegPowerGamma2" };
            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                string row  = tgParser.ParsedData[i]["Time"] + " ";
                bool   trip = false;
                foreach (string key in keys)
                {
                    if (tgParser.ParsedData[i].ContainsKey(key))
                    {
                        trip = true;
                        row += tgParser.ParsedData[i][key] + " ";

                        if (key == "PoorSignal" && tgParser.ParsedData[i][key] > 0)
                        {
                            Console.WriteLine("");
                            Console.WriteLine("There is a poor signal. Please adjust headset or check battery.");
                            Console.Write("Comment:");
                        }
                        if (key == "Attention")
                        {
                            AttentionTot += (uint)tgParser.ParsedData[i][key];
                            AttentionCount++;
                        }
                        if (key == "Meditation")
                        {
                            MeditationTot += (uint)tgParser.ParsedData[i][key];
                            MeditationCount++;
                        }
                        string subStr = key.Substring(0, 8);
                        if (subStr == "EegPower")
                        {
                            if (EegPowerMax < tgParser.ParsedData[i][key])
                            {
                                EegPowerMax = (uint)tgParser.ParsedData[i][key];
                            }
                            if (key == "EegPowerDelta")
                            {
                                EegPowerCount++;
                                DeltaTot  += (uint)tgParser.ParsedData[i][key];
                                ThetaTot  += (uint)tgParser.ParsedData[i]["EegPowerTheta"];
                                Alpha1Tot += (uint)tgParser.ParsedData[i]["EegPowerAlpha1"];
                                Alpha2Tot += (uint)tgParser.ParsedData[i]["EegPowerAlpha2"];
                                Beta1Tot  += (uint)tgParser.ParsedData[i]["EegPowerBeta1"];
                                Beta2Tot  += (uint)tgParser.ParsedData[i]["EegPowerBeta2"];
                                Gamma1Tot += (uint)tgParser.ParsedData[i]["EegPowerGamma1"];
                                Gamma2Tot += (uint)tgParser.ParsedData[i]["EegPowerGamma2"];
                            }
                        }
                    }
                    else
                    {
                        row += "- ";
                    }
                }
                if (trip == true)
                {
                    File.AppendAllText(path + "data.txt", row + "\n");
                }
            }
        }
Пример #4
0
        void OnDataReceived(object sender, EventArgs e)
        {
            /* Cast the event sender as a Device object, and e as the Device's DataEventArgs */
            Device d = (Device)sender;

            Device.DataEventArgs de = (Device.DataEventArgs)e;

            /* Create a TGParser to parse the Device's DataRowArray[] */
            TGParser tgParser = new TGParser();

            tgParser.Read(de.DataRowArray);

            /* Loop through parsed data TGParser for its parsed data... */
            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                // See the Data Types documentation for valid keys such
                // as "Raw", "PoorSignal", "Attention", etc.

                if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
                {
                    Console.WriteLine("PQ Value:" + tgParser.ParsedData[i]["PoorSignal"]);
                    Console.WriteLine("LAN" + timing.ToString());
                }

                if (tgParser.ParsedData[i].ContainsKey("Attention"))
                {
                    Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerBeta1"))
                {
                    low = tgParser.ParsedData[i]["EegPowerBeta1"].ToString();
                    if (startClick == true)
                    {
                        dtlow(low.ToString(), 1);
                    }

                    Console.WriteLine("Beta1:" + low);
                }
                if (tgParser.ParsedData[i].ContainsKey("Raw"))
                {
                    raw = tgParser.ParsedData[i]["Raw"].ToString();
                    if (startClick == true)
                    {
                        dtraw(raw.ToString(), 1);
                        //playsound random
                    }

                    // Console.WriteLine("Raw:" + raw);
                }
                if (tgParser.ParsedData[i].ContainsKey("EegPowerBeta2"))
                {
                    high = tgParser.ParsedData[i]["EegPowerBeta2"].ToString();
                    if (startClick == true)
                    {
                        dthigh(high.ToString(), 1);
                        //playsound random
                    }
                    Console.WriteLine("Beta2:" + high);
                }
            }
        }
Пример #5
0
        /**
         * Called when data is received from a device
         */
        static void OnDataReceived(object sender, EventArgs e)
        {
            //Device d = (Device)sender;
            Device.DataEventArgs de = (Device.DataEventArgs)e;
            DataRow[]            tempDataRowArray = de.DataRowArray;
            string   mybyte;
            TGParser tgParser = new TGParser();

            tgParser.Read(de.DataRowArray);

            //Console.WriteLine(buffer[0] + buffer[1]);
            //if (buffer=='1')
            //{
            //    Console.WriteLine("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww");
            //}
            /* Loop through new parsed data */
            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                if (tgParser.ParsedData[i].ContainsKey("MSG_MODEL_IDENTIFIED"))
                {
                    Console.WriteLine("Model Identified");
                    connector.setMentalEffortRunContinuous(true);
                    connector.setMentalEffortEnable(true);
                    connector.setTaskFamiliarityRunContinuous(true);
                    connector.setTaskFamiliarityEnable(true);
                    connector.setPositivityEnable(false);
                    //
                    // the following are included to demonstrate the overide messages
                    //
                    connector.setRespirationRateEnable(true); // not allowed with EEG
                    connector.setPositivityEnable(true);      // not allowed when famil/diff are enabled
                }
                if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
                {
                    // NOTE: this doesn't work well with BMD sensors Dual Headband or CardioChip

                    rcv_poorSignal = (byte)tgParser.ParsedData[i]["PoorSignal"];
                    if (rcv_poorSignal != rcv_poorSignal_last || rcv_poorSig_cnt >= 30)
                    {
                        // when there is a change of state OR every 30 reports
                        rcv_poorSig_cnt     = 0; // reset counter
                        rcv_poorSignal_last = rcv_poorSignal;
                        if (rcv_poorSignal == 0)
                        {
                            // signal is good, we are connected to a subject
                            Console.WriteLine("SIGNAL: we have good contact with the subject");
                            mybyte = "9";
                            tcp2.Send(Encoding.UTF8.GetBytes(mybyte));
                            //tcpClient1.Send(Encoding.UTF8.GetBytes(mybyte), mybyte.Length, ipEndPoint);
                        }
                        else
                        {
                            Console.WriteLine("SIGNAL: is POOR: " + rcv_poorSignal);
                            mybyte = "8";
                            tcp2.Send(Encoding.UTF8.GetBytes(mybyte));
                            //udpClient.Send(Encoding.UTF8.GetBytes(mybyte), mybyte.Length, ipEndPoint);
                        }
                    }
                    else
                    {
                        rcv_poorSig_cnt++;
                    }
                }


                if (tgParser.ParsedData[i].ContainsKey("Meditation"))
                {
                    if (tgParser.ParsedData[i]["Meditation"] != 0)
                    {
                        Console.WriteLine("Meditation: " + tgParser.ParsedData[i]["Meditation"]);

                        if (tgParser.ParsedData[i]["Meditation"] > 100)
                        {
                            tag = 4;
                        }
                        if (tgParser.ParsedData[i]["Meditation"] < 20)
                        {
                            tag = 6;
                        }
                        y = (int)tgParser.ParsedData[i]["Meditation"];
                    }
                }
                if (tgParser.ParsedData[i].ContainsKey("Attention"))
                {
                    if (tgParser.ParsedData[i]["Attention"] != 0)
                    {
                        Console.WriteLine("Attention : " + tgParser.ParsedData[i]["Attention"]);
                        flag  = 0;
                        flag1 = 0;
                        num++;
                        if (tgParser.ParsedData[i]["Attention"] > 90)
                        {
                            tag = 5;
                        }
                        x = (int)tgParser.ParsedData[i]["Attention"];
                    }
                }
                if (!golfZoneDemo) // turn this off for the Golf Zone Demo
                {
                    if (tgParser.ParsedData[i].ContainsKey("BlinkStrength"))
                    {
                        if (tgParser.ParsedData[i]["BlinkStrength"] > 55)
                        {
                            Console.WriteLine("\t\tBlinkStrength: " + tgParser.ParsedData[i]["BlinkStrength"]);
                            if (num >= 2)
                            {
                                flag++;
                                num = 0;
                            }
                            flag1++;
                            if (buffer[0] + buffer[1] == 97)
                            {
                                if (flag1 == 1)
                                {
                                    Console.WriteLine("KKKKKKKKKK");
                                    mybyte = "1";
                                    tcp2.Send(Encoding.UTF8.GetBytes(mybyte));
                                    //udpClient.Send(Encoding.UTF8.GetBytes(mybyte), mybyte.Length, ipEndPoint2);
                                }
                            }
                            else if (buffer[0] + buffer[1] == 98)
                            {
                                if (flag == 1 && flag1 == 1)
                                {
                                    Console.WriteLine("KKKKKKKKKK");
                                    mybyte = "1";
                                    tcp2.Send(Encoding.UTF8.GetBytes(mybyte));
                                    //udpClient.Send(Encoding.UTF8.GetBytes(mybyte), mybyte.Length, ipEndPoint2);
                                }
                            }
                        }
                    }
                }

                if ((x > 0 && y > 0) || tag > 0)
                {
                    mybyte = x * 10000 + y * 10 + tag + "";
                    tcp1.Send(Encoding.UTF8.GetBytes(mybyte));
                    //udpClient.Send(Encoding.UTF8.GetBytes(mybyte), mybyte.Length, ipEndPoint);
                    x   = 0;
                    y   = 0;
                    tag = 0;
                }
            }
        }
        private void OnDataReceived(object sender, EventArgs e)
        {
            Device d = (Device)sender;

            Device.DataEventArgs de = (Device.DataEventArgs)e;

            TGParser tgParser = new TGParser();

            tgParser.Read(de.DataRowArray);

            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                if (tgParser.ParsedData[i].ContainsKey("Raw"))
                {
                    //Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
                }

                if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
                {
                    //Console.WriteLine("PQ Value:" + tgParser.ParsedData[i]["PoorSignal"]);
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerDelta"))
                {
                    SetEegPowerDelta(tgParser.ParsedData[i]["EegPowerDelta"]);
                    Console.WriteLine("Power Delta: {0}", EegPowerDelta);
                    _event.EegPowerDelta = EegPowerDelta;
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerTheta"))
                {
                    SetEegPowerTheta(tgParser.ParsedData[i]["EegPowerTheta"]);
                    Console.WriteLine("Power Theta: {0}", EegPowerTheta);
                    _event.EegPowerTheta = EegPowerTheta;
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerAlpha1"))
                {
                    SetEegPowerAlpha1(tgParser.ParsedData[i]["EegPowerAlpha1"]);
                    Console.WriteLine("Power Alpha1: {0}", EegPowerAlpha1);
                    _event.EegPowerAlpha1 = EegPowerAlpha1;
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerAlpha2"))
                {
                    SetEegPowerAlpha2(tgParser.ParsedData[i]["EegPowerAlpha2"]);
                    Console.WriteLine("Power Alpha2: {0}", EegPowerAlpha2);

                    _event.EegPowerAlpha2 = EegPowerAlpha2;
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerBeta1"))
                {
                    SetEegPowerBeta1(tgParser.ParsedData[i]["EegPowerBeta1"]);
                    Console.WriteLine("Power Beta1: {0}", EegPowerBeta1);
                    _event.EegPowerBeta1 = EegPowerBeta1;
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerBeta2"))
                {
                    SetEegPowerBeta2(tgParser.ParsedData[i]["EegPowerBeta2"]);
                    Console.WriteLine("Power Beta2: {0}", EegPowerBeta2);
                    _event.EegPowerBeta2 = EegPowerBeta2;
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerGamma1"))
                {
                    SetEegPowerGamma1(tgParser.ParsedData[i]["EegPowerGamma1"]);
                    Console.WriteLine("Power Gamma1: {0}", EegPowerGamma1);
                    _event.EegPowerGamma1 = EegPowerGamma1;
                }

                if (tgParser.ParsedData[i].ContainsKey("EegPowerGamma2"))
                {
                    SetEegPowerGamma2(tgParser.ParsedData[i]["EegPowerGamma2"]);
                    Console.WriteLine("Power Gamma2: {0}", EegPowerGamma2);
                    _event.EegPowerGamma2 = EegPowerGamma2;
                }

                if (tgParser.ParsedData[i].ContainsKey("Attention"))
                {
                    SetAttention(tgParser.ParsedData[i]["Attention"]);
                    //Console.WriteLine("Att Value:" + Attention);
                    _event.AttValue = Attention;
                }

                if (tgParser.ParsedData[i].ContainsKey("Meditation"))
                {
                    SetMeditation(tgParser.ParsedData[i]["Meditation"]);
                    //Console.WriteLine("Med Value:" + Meditation);
                    _event.MedValue = Meditation;
                }
            }
        }
Пример #7
0
        /**
         * Called when data is received from a device
         */
        static void OnDataReceived(object sender, EventArgs e)
        {
            //Device d = (Device)sender;
            Device.DataEventArgs de = (Device.DataEventArgs)e;
            DataRow[]            tempDataRowArray = de.DataRowArray;

            TGParser tgParser = new TGParser();

            tgParser.Read(de.DataRowArray);

            /* Loop through new parsed data */
            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                if (tgParser.ParsedData[i].ContainsKey("MSG_MODEL_IDENTIFIED"))
                {
                    Console.WriteLine("Model Identified");

                    connector.setMentalEffortRunContinuous(false);
                    connector.setMentalEffortEnable(false);
                    connector.setTaskFamiliarityRunContinuous(false);
                    connector.setTaskFamiliarityEnable(false);
                    connector.setPositivityEnable(false);
                    connector.setBlinkDetectionEnabled(false);

                    //
                    // the following are included to demonstrate the overide messages
                    //
                    connector.setRespirationRateEnable(true); // not allowed with EEG
                }
                if (tgParser.ParsedData[i].ContainsKey("MSG_ERR_CFG_OVERRIDE"))
                {
                    Console.WriteLine("ErrorConfigurationOverride: " + tgParser.ParsedData[i]["MSG_ERR_CFG_OVERRIDE"]);
                }
                if (tgParser.ParsedData[i].ContainsKey("MSG_ERR_NOT_PROVISIONED"))
                {
                    Console.WriteLine("ErrorModuleNotProvisioned: " + tgParser.ParsedData[i]["MSG_ERR_NOT_PROVISIONED"]);
                }
                if (tgParser.ParsedData[i].ContainsKey("TimeStamp"))
                {
                    //Console.WriteLine("TimeStamp");
                }
                if (tgParser.ParsedData[i].ContainsKey("Raw"))
                {
                    //Console.WriteLine("Raw: " + tgParser.ParsedData[i]["Raw"]);
                }
                if (tgParser.ParsedData[i].ContainsKey("RawCh1"))
                {
                    //Console.WriteLine("RawCh1: " + tgParser.ParsedData[i]["RawCh1"]);
                }
                if (tgParser.ParsedData[i].ContainsKey("RawCh2"))
                {
                    //Console.Write(", Raw Ch2:" + tgParser.ParsedData[i]["RawCh2"]);
                }
                if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
                {
                    // NOTE: this doesn't work well with BMD sensors Dual Headband or CardioChip

                    rcv_poorSignal = (byte)tgParser.ParsedData[i]["PoorSignal"];
                    if (rcv_poorSignal != rcv_poorSignal_last || rcv_poorSig_cnt >= 30)
                    {
                        // when there is a change of state OR every 30 reports
                        rcv_poorSig_cnt     = 0; // reset counter
                        rcv_poorSignal_last = rcv_poorSignal;
                        if (rcv_poorSignal == 0)
                        {
                            // signal is good, we are connected to a subject
                            Console.WriteLine("SIGNAL: we have good contact with the subject");
                        }
                        else
                        {
                            Console.WriteLine("SIGNAL: is POOR: " + rcv_poorSignal);
                        }
                    }
                    else
                    {
                        rcv_poorSig_cnt++;
                    }
                }
                if (tgParser.ParsedData[i].ContainsKey("Attention"))
                {
                    if (tgParser.ParsedData[i]["Attention"] != 0)
                    {
                        Console.WriteLine("Attention : " + tgParser.ParsedData[i]["Attention"]);
                    }
                }
                if (tgParser.ParsedData[i].ContainsKey("Attention 1"))
                {
                    if (tgParser.ParsedData[i]["Attention 1"] != 0)
                    {
                        Console.WriteLine("Attention 1: " + tgParser.ParsedData[i]["Attention 1"]);
                    }
                }
                if (tgParser.ParsedData[i].ContainsKey("Attention 2"))
                {
                    if (tgParser.ParsedData[i]["Attention 2"] != 0)
                    {
                        Console.WriteLine("Attention 2: " + tgParser.ParsedData[i]["Attention 2"]);
                    }
                }
                if (tgParser.ParsedData[i].ContainsKey("Meditation"))
                {
                    if (tgParser.ParsedData[i]["Meditation"] != 0)
                    {
                        Console.WriteLine("Meditation: " + tgParser.ParsedData[i]["Meditation"]);
                    }
                }
                if (tgParser.ParsedData[i].ContainsKey("Meditation 1"))
                {
                    if (tgParser.ParsedData[i]["Meditation 1"] != 0)
                    {
                        Console.WriteLine("Meditation 1: " + tgParser.ParsedData[i]["Meditation 1"]);
                    }
                }
                if (tgParser.ParsedData[i].ContainsKey("Meditation 2"))
                {
                    if (tgParser.ParsedData[i]["Meditation 2"] != 0)
                    {
                        Console.WriteLine("Meditation 2: " + tgParser.ParsedData[i]["Meditation 2"]);
                    }
                }
                if (tgParser.ParsedData[i].ContainsKey("Positivity"))
                {
                    Console.WriteLine("\t\tPositivity: " + tgParser.ParsedData[i]["Positivity"]);
                }
                if (!golfZoneDemo) // turn this off for the Golf Zone Demo
                {
                    if (tgParser.ParsedData[i].ContainsKey("BlinkStrength"))
                    {
                        Console.WriteLine("\t\tBlinkStrength: " + tgParser.ParsedData[i]["BlinkStrength"]);
                    }
                    if (tgParser.ParsedData[i].ContainsKey("MentalEffort"))
                    {
                        Console.WriteLine("\t\tMental Effort: " + tgParser.ParsedData[i]["MentalEffort"]);
                    }

                    if (tgParser.ParsedData[i].ContainsKey("TaskFamiliarity"))
                    {
                        Console.WriteLine("\t\tTask Familiarity: " + tgParser.ParsedData[i]["TaskFamiliarity"]);
                    }
                }
                if (golfZoneDemo)
                {
                    if (tgParser.ParsedData[i].ContainsKey("ReadyZone"))
                    {
                        byte zone = (byte)tgParser.ParsedData[i]["ReadyZone"];
                        Console.Write("\t\tGolfZone: ");
                        switch (zone)
                        {
                        case 9:
                            Console.WriteLine("9: Elite: you are the best, putt when you are ready");
                            break;

                        case 8:
                            Console.WriteLine("8:");
                            break;

                        case 7:
                            Console.WriteLine("7:");
                            break;

                        case 6:
                            Console.WriteLine("6:");
                            break;

                        case 5:
                            Console.WriteLine("5: Intermediate: you are good, relax and putt smoothly");
                            break;

                        case 4:
                            Console.WriteLine("4:");
                            break;

                        case 3:
                            Console.WriteLine("3:");
                            break;

                        case 2:
                            Console.WriteLine("2:");
                            break;

                        case 1:
                            Console.WriteLine("1: Beginner: take a breath and don't rush");
                            break;

                        case 0:
                            Console.WriteLine("0: Try to relax, focus on your target");
                            break;

                        default:
                            Console.WriteLine("OOPS: zone value out of range: " + zone);
                            break;
                        }
                    }
                }
            }
        }
Пример #8
0
        static void OnDataReceived(object sender, EventArgs e)
        {
            //Device d = (Device)sender;

            Device.DataEventArgs         de = (Device.DataEventArgs)e;
            NeuroSky.ThinkGear.DataRow[] tempDataRowArray = de.DataRowArray;

            TGParser tgParser = new TGParser();

            tgParser.Read(de.DataRowArray);



            /* Loops through the newly parsed data of the connected headset*/
            // The comments below indicate and can be used to print out the different data outputs.
            int            SetThemAll = 0;
            List <PosTime> RAW        = new List <PosTime>();

            for (int i = 0; i < tgParser.ParsedData.Length; i++)
            {
                if (tgParser.ParsedData[i].ContainsKey("Raw"))
                {
                    //   Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
                    RAW.Add(new PosTime(tgParser.ParsedData[i]["Raw"], tgParser.ParsedData[i]["Time"]));
                }

                if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
                {
                    //The following line prints the Time associated with the parsed data
                    //Console.WriteLine("Time:" + tgParser.ParsedData[i]["Time"]);

                    //A Poor Signal value of 0 indicates that your headset is fitting properly
                    //              Console.WriteLine("Poor Signal:" + tgParser.ParsedData[i]["PoorSignal"]);

                    //  poorSig = (byte)tgParser.ParsedData[i]["PoorSignal"];
                }


                if (tgParser.ParsedData[i].ContainsKey("Attention"))
                {
                    SetThemAll++;
                    //                Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
                    BS.Attention = tgParser.ParsedData[i]["Attention"];
                }


                if (tgParser.ParsedData[i].ContainsKey("Meditation"))
                {
                    SetThemAll++;
                    //     Console.WriteLine("Med Value:" + tgParser.ParsedData[i]["Meditation"]);
                    BS.Meditation = tgParser.ParsedData[i]["Meditation"];
                }


                if (tgParser.ParsedData[i].ContainsKey("EegPowerDelta"))
                {
                    BS.Delta = tgParser.ParsedData[i]["EegPowerDelta"];
                    //         Console.WriteLine("Delta: " + tgParser.ParsedData[i]["EegPowerDelta"]);
                    SetThemAll++;
                }
                if (tgParser.ParsedData[i].ContainsKey("EegPowerTheta"))
                {
                    BS.Theta = tgParser.ParsedData[i]["EegPowerTheta"];
                    //        Console.WriteLine("Theta: " + tgParser.ParsedData[i]["EegPowerTheta"]);
                    SetThemAll++;
                }
                if (tgParser.ParsedData[i].ContainsKey("EegPowerAlpha1"))
                {
                    BS.A1 = tgParser.ParsedData[i]["EegPowerAlpha1"];
                    //        Console.WriteLine("Alpha1: " + tgParser.ParsedData[i]["EegPowerAlpha1"]);
                    SetThemAll++;
                }
                if (tgParser.ParsedData[i].ContainsKey("EegPowerAlpha2"))
                {
                    BS.A2 = tgParser.ParsedData[i]["EegPowerAlpha2"];
                    //       Console.WriteLine("Alpha2: " + tgParser.ParsedData[i]["EegPowerAlpha2"]);
                    SetThemAll++;
                }
                if (tgParser.ParsedData[i].ContainsKey("EegPowerBeta1"))
                {
                    BS.B1 = tgParser.ParsedData[i]["EegPowerBeta1"];
                    //       Console.WriteLine("Beta1: " + tgParser.ParsedData[i]["EegPowerBeta1"]);
                    SetThemAll++;
                }
                if (tgParser.ParsedData[i].ContainsKey("EegPowerBeta2"))
                {
                    BS.B2 = tgParser.ParsedData[i]["EegPowerBeta2"];
                    //      Console.WriteLine("Beta2: " + tgParser.ParsedData[i]["EegPowerBeta2"]);
                    SetThemAll++;
                }
                if (tgParser.ParsedData[i].ContainsKey("EegPowerGamma1"))
                {
                    BS.G1 = tgParser.ParsedData[i]["EegPowerGamma1"];
                    //     Console.WriteLine("Gamma1: " + tgParser.ParsedData[i]["EegPowerGamma1"]);
                    SetThemAll++;
                }
                if (tgParser.ParsedData[i].ContainsKey("EegPowerGamma2"))
                {
                    BS.G2 = tgParser.ParsedData[i]["EegPowerGamma2"];
                    //      Console.WriteLine("Gamma2: " + tgParser.ParsedData[i]["EegPowerGamma2"]);
                    SetThemAll++;
                }
                if (tgParser.ParsedData[i].ContainsKey("BlinkStrength"))
                {
                    //       Console.WriteLine("Eyeblink " + tgParser.ParsedData[i]["BlinkStrength"]);
                }
            }
            if (dt_last == null)
            {
                dt_last = DateTime.Now;
            }
            else
            {
                ts      = DateTime.Now - dt_last;
                dt_last = DateTime.Now;
            }
            incomelength = RAW.Count;
            lock (forlock)
            {
                if (RAW.Count > 0)
                {
                    for (int j = 0; j < RAW.Count; j++)
                    {
                        RAW_DATA.Add(RAW[j]);
                    }
                    if (RAW_DATA.Count > 1000)
                    {
                        int k = RAW_DATA.Count;
                        for (int j = 0; j < k - 1000; j++)
                        {
                            RAW_DATA.RemoveAt(0);
                        }
                    }
                    //Signal s = new Signal()
                    ComplexSignal CS = new ComplexSignal(1, RAW_DATA.Count, 1);
                    if (RAW_DATA.Count >= 999)
                    {
                        data = new double[RAW_DATA.Count];
                        for (int i = 0; i < RAW_DATA.Count; i++)
                        {
                            data[i] = RAW_DATA[i].Pos;//10 * Math.Sin(Math.PI * (i / 15.0));//// +
                        }
                        Accord.Math.CosineTransform.DCT(data);
                    }
                }
            }
            if (SetThemAll == 10)
            {
                M.AddToMemory(BS);
            }
        }