Ejemplo n.º 1
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);
            }
        }