internal DirectionDefinitions ReadGesture()
        {
            byte fifoLevel = 0;
            int bytesRead = 0;
            byte[] fifoData = new byte[128];
            byte gstatus;
            DirectionDefinitions motion;
            int i;

            _gestureData = new GestureDataType();
            _gestureData.UData = new byte[32];
            _gestureData.DData = new byte[32];
            _gestureData.LData = new byte[32];
            _gestureData.RData = new byte[32];

            /* Make sure that power and gesture is on and data is valid */
            var mode = GetMode() & 65;
            if (!IsGestureAvailable() || mode == 0)
            {
                return DirectionDefinitions.DIR_NONE;
            }

            /* Keep looping as long as gesture data is valid */
            while (true)
            {
                /* Wait some time to collect next batch of FIFO data */
                Task.Delay(FIFO_PAUSE_TIME).Wait();

                /* Get the contents of the STATUS register. Is data still valid? */
                gstatus = ReadDataByte(APDS9960_GSTATUS);

                /* If we have valid data, read in FIFO */
                if ((gstatus & APDS9960_GVALID) == APDS9960_GVALID)
                {
                    /* Read the current FIFO level */
                    fifoLevel = ReadDataByte(APDS9960_GFLVL);
#if DEBUG
                    Debug.Write("FIFO Level: ");
                    Debug.WriteLine(fifoLevel);
#endif
                    /* If there's stuff in the FIFO, read it into our data block */
                    if (fifoLevel > 0)
                    {
                        fifoData = ReadDataBlock(APDS9960_GFIFO_U, fifoLevel * 4);
                        bytesRead = fifoData.Length;

                        if (bytesRead == 0)
                        {
                            return DirectionDefinitions.DIR_NONE;
                        }
                        /* If at least 1 set of data, sort the data into U/D/L/R */
                        if (bytesRead >= 4)
                        {
                            for (i = 0; i < bytesRead; i += 4)
                            {
                                _gestureData.UData[_gestureData.Index] = fifoData[i + 0];
                                _gestureData.DData[_gestureData.Index] = fifoData[i + 1];
                                _gestureData.LData[_gestureData.Index] = fifoData[i + 2];
                                _gestureData.RData[_gestureData.Index] = fifoData[i + 3];
                                _gestureData.Index++;
                                _gestureData.TotalGestures++;
                            }

                            /* Filter and process gesture data. Decode near/far state */
                            if (ProcessGestureData())
                            {
                                if (DecodeGesture())
                                {
#if DEBUG
                                    Debug.WriteLine(_gestureMotion);
#endif
                                }
                            }
                            
                            /* Reset data */
                            _gestureData.Index = 0;
                            _gestureData.TotalGestures = 0;
                        }
                    }
                }
                else
                {
                    /* Determine best guessed gesture and clean up */
                    Task.Delay(FIFO_PAUSE_TIME).Wait();
                    DecodeGesture();
                    motion = _gestureMotion;
                    ResetGestureParameters();
                    return motion;
                }
            }
        }
Beispiel #2
0
        internal DirectionDefinitions ReadGesture()
        {
            byte fifoLevel = 0;
            int  bytesRead = 0;

            byte[] fifoData = new byte[128];
            byte   gstatus;
            DirectionDefinitions motion;
            int i;

            _gestureData       = new GestureDataType();
            _gestureData.UData = new byte[32];
            _gestureData.DData = new byte[32];
            _gestureData.LData = new byte[32];
            _gestureData.RData = new byte[32];

            /* Make sure that power and gesture is on and data is valid */
            var mode = GetMode() & 65;

            if (!IsGestureAvailable() || mode == 0)
            {
                return(DirectionDefinitions.DIR_NONE);
            }

            /* Keep looping as long as gesture data is valid */
            while (true)
            {
                /* Wait some time to collect next batch of FIFO data */
                Task.Delay(FIFO_PAUSE_TIME).Wait();

                /* Get the contents of the STATUS register. Is data still valid? */
                gstatus = ReadDataByte(APDS9960_GSTATUS);

                /* If we have valid data, read in FIFO */
                if ((gstatus & APDS9960_GVALID) == APDS9960_GVALID)
                {
                    /* Read the current FIFO level */
                    fifoLevel = ReadDataByte(APDS9960_GFLVL);
#if DEBUG
                    Debug.Write("FIFO Level: ");
                    Debug.WriteLine(fifoLevel);
#endif
                    /* If there's stuff in the FIFO, read it into our data block */
                    if (fifoLevel > 0)
                    {
                        fifoData  = ReadDataBlock(APDS9960_GFIFO_U, fifoLevel * 4);
                        bytesRead = fifoData.Length;

                        if (bytesRead == 0)
                        {
                            return(DirectionDefinitions.DIR_NONE);
                        }
                        /* If at least 1 set of data, sort the data into U/D/L/R */
                        if (bytesRead >= 4)
                        {
                            for (i = 0; i < bytesRead; i += 4)
                            {
                                _gestureData.UData[_gestureData.Index] = fifoData[i + 0];
                                _gestureData.DData[_gestureData.Index] = fifoData[i + 1];
                                _gestureData.LData[_gestureData.Index] = fifoData[i + 2];
                                _gestureData.RData[_gestureData.Index] = fifoData[i + 3];
                                _gestureData.Index++;
                                _gestureData.TotalGestures++;
                            }

                            /* Filter and process gesture data. Decode near/far state */
                            if (ProcessGestureData())
                            {
                                if (DecodeGesture())
                                {
#if DEBUG
                                    Debug.WriteLine(_gestureMotion);
#endif
                                }
                            }

                            /* Reset data */
                            _gestureData.Index         = 0;
                            _gestureData.TotalGestures = 0;
                        }
                    }
                }
                else
                {
                    /* Determine best guessed gesture and clean up */
                    Task.Delay(FIFO_PAUSE_TIME).Wait();
                    DecodeGesture();
                    motion = _gestureMotion;
                    ResetGestureParameters();
                    return(motion);
                }
            }
        }