Пример #1
0
        void NewDataCallback()
        {
            lock (this) // Prevent concurrent execution
            {
                bool newData = false;
                bool badData;

                while (BaseDevice.BufferedByteCountPipe(IN_PIPE) >= 5)
                {
                    badData = false;
                    byte[] data = BaseDevice.BufferedPeekPipe(IN_PIPE, 5);
                    if (data[0] != 0xFF)
                    {
                        badData = true;
                    }
                    for (int i = 0; i < 4; i++)
                    {
                        if (data[i + 1] >= 0x80)
                        {
                            // This is also a bad data (truncated) message, this can happen if the host falls behind.
                            badData = true;
                            break;
                        }
                    }
                    if (badData)
                    {
                        // Bad data. Try again next byte.
                        BaseDevice.BufferedSkipBytesPipe(IN_PIPE, 1);
                        continue;
                    }

                    // This looks like a button message
                    for (int i = 0; i < 4; i++)
                    {
                        ButtonValues[i]  = data[i + 1];
                        ButtonPressed[i] = ButtonValues[i] < ButtonThreshold;
                    }
                    newData = true;
                    DataCount++;
                    BaseDevice.BufferedSkipBytesPipe(IN_PIPE, 5);
                }

                if (newData)
                {
                    // Provide notification.
                }
            }
        }