Beispiel #1
0
        private async void GetInputThreadFunction()
        {
            int    count = 0;
            double accXVal, accYVal, accZVal;
            double velXVal = 0, velYVal = 0, velZVal = 0;
            Vector x_shift = new Vector(-1, 0);
            //int time = 0;
            Action onReadTimeoutAction = OnReadTimeout; // Create a delegate to execute on a timeout.
            var    cts = new CancellationTokenSource(); // The CancellationTokenSource specifies the timeout value and the action to take on a timeout.

            cts.Token.Register(onReadTimeoutAction);    // Specify the function to call on a timeout.
            PointCollection points   = new PointCollection();
            bool            zcDetect = true;
            int             oldCount = 0;
            int             packetCounterVal;
            int             lostPacketCount    = 0;
            int             prevPacketCounter  = 0;
            double          lossPercentage     = 0;
            double          prevLossPercentage = 0;;

            while (true)
            {
                try
                {
                    if (_deviceHandleObtained && (inputReportBuf != null))
                    {
                        cts.CancelAfter(60 * 1000);  // Cancel the read if it hasn't completed after a timeout.

                        // Stops waiting when data is available or on timeout:
                        Int32 bytesRead = await _myHid.GetInputReportViaInterruptTransfer(_deviceData, inputReportBuf, cts);

                        if (bytesRead > 0)
                        {
                            ErrorBox.Dispatcher.Invoke(() => { ErrorBox.Text = "bytes read (includes report ID) = " + Convert.ToString(bytesRead); });
                            //for (int i = 0; i < inputReportBuffer.Length; i++)
                            //   InfoBox.Text += inputReportBuffer[i].ToString() + " ";
                            packetCounterVal = inputReportBuf[1] + inputReportBuf[2] * 256;
                            if (0 != prevPacketCounter)
                            {
                                if (packetCounterVal != (prevPacketCounter + 1))
                                {
                                    lostPacketCount += (packetCounterVal - (prevPacketCounter + 1));
                                }
                            }
                            prevPacketCounter = packetCounterVal;


                            //ValueTextBlock.Dispatcher.Invoke(() => { ValueTextBlock.Text = ((sbyte)inputReportBuf[3]).ToString(); });
                            //AccValProgressBar.Dispatcher.Invoke(() => { AccValProgressBar.Value = (double)((sbyte)inputReportBuf[3]); });
                            totalInputReports++;
                            accXVal  = (double)((sbyte)inputReportBuf[3]);
                            accYVal  = (double)((sbyte)inputReportBuf[4]);
                            accZVal  = (double)((sbyte)(inputReportBuf[5] - 64));
                            velXVal += accXVal * 0.0327 * 15.31 / 2.0;
                            velYVal += accYVal * 0.0327 * 15.31 / 2.0;
                            velZVal += accZVal * 0.0327 * 15.31 / 2.0;
                            if (accXVal > accXMax)
                            {
                                accXMax = accXVal;
                            }
                            if (accXVal < accXMin)
                            {
                                accXMin = accXVal;
                            }
                            if (accXVal > 0)
                            {
                                zcDetect = true;
                            }
                            if ((accXVal < 0) && (zcDetect == true))
                            {
                                if ((oldCount > 0) && (inputReportFrequency > 0))
                                {
                                    accXCount++;
                                    accXFreq = (double)inputReportFrequency / (double)(totalInputReports - oldCount);
                                    AccXFreqText.Dispatcher.Invoke(() =>
                                    {
                                        AccXFreqText.Text  = string.Format("{0}", (int)(accXFreq * 60));
                                        AccXCountText.Text = string.Format("{0}", accXCount);
                                    });
                                }
                                oldCount = totalInputReports;
                                zcDetect = false;
                            }
                            count++;
                            AccXPolyline.Dispatcher.Invoke(() =>
                            {
                                if (count >= 200)
                                {
                                    accXPoints.RemoveAt(0);
                                    //velXPoints.RemoveAt(0);
                                    accYPoints.RemoveAt(0);
                                    //velYPoints.RemoveAt(0);
                                    accZPoints.RemoveAt(0);
                                    //velZPoints.RemoveAt(0);
                                    for (int i = 0; i < accXPoints.Count; i++)
                                    {
                                        accXPoints[i] += x_shift;
                                        // velXPoints[i] += x_shift;
                                        accYPoints[i] += x_shift;
                                        // velYPoints[i] += x_shift;
                                        accZPoints[i] += x_shift;
                                        // velZPoints[i] += x_shift;
                                    }
                                    accXPoints.Add(new Point(200, 150 + accXVal));
                                    //  velXPoints.Add(new Point(200, 150 + velXVal));
                                    accYPoints.Add(new Point(200, 150 + accYVal));
                                    //  velYPoints.Add(new Point(200, 150 + velYVal));
                                    accZPoints.Add(new Point(200, 150 + accZVal));
                                    //  velZPoints.Add(new Point(200, 150 + velZVal));
                                }
                                else
                                {
                                    accXPoints.Add(new Point(count, 150 + accXVal));
                                    // velXPoints.Add(new Point(count * 4, 150 + velXVal));
                                    accYPoints.Add(new Point(count, 150 + accYVal));
                                    // velYPoints.Add(new Point(count * 4, 150 + velYVal));
                                    accZPoints.Add(new Point(count, 150 + accZVal));
                                    //  velZPoints.Add(new Point(count * 4, 150 + velZVal));
                                }
                            });

                            CountTextBlock.Dispatcher.Invoke(() =>
                            {
                                if (totalInputReports > 0)
                                {
                                    lossPercentage = (double)(lostPacketCount) * 100 / totalInputReports;
                                }
                                CountTextBlock.Text      = totalInputReports.ToString();
                                LossTextBlock.Text       = string.Format("{0:0.0}%", lossPercentage);
                                LossTextBlock.Foreground = (lossPercentage > prevLossPercentage) ? Brushes.Red : Brushes.Green;
                                prevLossPercentage       = lossPercentage;
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    DisplayException(Name, ex);
                }
            }
        }