コード例 #1
0
        /// <summary>
        /// New serial data is received and gets processed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void NewSerialDataRecieved(object sender, SerialDataEventArgs e)
        {
            //Reset Timer for disconnect
            dcTime = DateTime.Now;

            foreach (byte t in e.Data)
            {
                //Import Byte and convert if one package complete
                if (processData.ImportByte(t))
                {
                    processData.Convert2Byte();

                    //Reset all Values if its the very first package that arrives
                    if (firstPackage)
                    {
                        firstPackage = false;
                        processData.SetPackageNumber();
                        stopWatch.Reset();
                        processData.packageCount = 0;
                    }
                    //Check package number
                    else
                    {
                        if (!processData.CheckPackageNumber())
                        {
                            UpdateStatus(this, new UpdateStatusEvent("Paket verloren gegangen"));
                        }

                        if (!processData.CheckPackageNumber() && save)
                        {
                            saveData.FillMissingData(processData.lostPackages);
                            processData.packageCount += processData.lostPackages;
                        }
                    }

                    //Start Stop Watch the first time
                    if (!stopWatch.IsRunning)
                    {
                        stopWatch.Reset(); stopWatch.Start();
                    }

                    //Count till 2500 Packages have arrive (which should be 10 Seconds)
                    processData.packageCount++;
                    if (processData.packageCount >= 250 * 10)
                    {
                        processData.packageCount -= 250 * 10;
                        stopWatch.Stop();
                        //Add time deviation to ensure time synchronic
                        if (save)
                        {
                            saveData.AddLostTime(stopWatch, 10);
                        }
                        //Reset and Start Stop Watch again
                        stopWatch.Reset();
                        stopWatch.Start();
                    }

                    //IMU
                    processData.CalculateIMU();

                    //FFT
                    if (processData.Buffering())
                    {
                        // Send data to Form
                        if (NewDataAvailable != null)
                        {
                            //snore
                            //if (processData.snore) UpdateStatus(this, new UpdateStatusEvent("Schnarch"));

                            NewDataAvailable(this,
                                             new NewDataAvailableEvent(processData.audioArray, processData.fft, processData.activity,
                                                                       processData.sleepPosition, processData.snore));

                            //if (snorecounter > 20)
                            //{
                            //    UpdateStatus(this,
                            //        new UpdateStatusEvent(Convert.ToString(processData.volume) + ' ' +
                            //                              Convert.ToString(processData.length) + ' ' +
                            //                              Convert.ToString(processData.freq)));
                            //    snorecounter = 0;
                            //}
                            //snorecounter++;
                        }
                    }



                    //Save Data
                    if (save)
                    {
                        if (Statics.complexSave)
                        {
                            for (int i = 0; i < 3; i++)
                            {
                                saveData.sendData(i + 3, processData.gyro[i]);
                                saveData.sendData(i + 6, processData.accelerationRaw[i]);
                            }
                        }
                        saveData.sendData(1, (short)processData.activity);
                        saveData.sendData(2, (short)processData.sleepPosition);
                        saveData.sendData(0, processData.audio);
                    }
                }
            }
        }