示例#1
0
        public void OnSensorChanged(double x, double y, double z, DateTimeOffset now)
        {
            // calculate movement
            var totalMovement = Math.Abs(x + y + z - _lastX - _lastY - _lastZ);

            if (totalMovement > MinimumForce)
            {
                // store first movement time
                if (_firstDirectionChangeTime == DateTimeOffset.MinValue)
                {
                    _firstDirectionChangeTime = now;
                    _lastDirectionChangeTime  = now;
                }

                // check if the last movement was not long ago
                var lastChangeWasAgo = (now - _lastDirectionChangeTime).TotalMilliseconds;
                if (lastChangeWasAgo < MaxPauseBetweenDirectionChange)
                {
                    // store movement data
                    _lastDirectionChangeTime = now;
                    _directionChangeCount++;

                    // store last sensor data
                    _lastX = x;
                    _lastY = y;
                    _lastZ = z;

                    // check how many movements are so far
                    if (_directionChangeCount >= MinDirectionChange)
                    {
                        // check total duration
                        var totalDuration = (now - _firstDirectionChangeTime).TotalMilliseconds;
                        if (totalDuration < MaxDurationOfShake)
                        {
                            ResetShake();
                            _accelerometer.OnShaken(now);
                        }
                    }
                }
                else
                {
                    ResetShake();
                }
            }
        }