Пример #1
0
        private void Update()
        {
            // Read X and Y values
            var x = xChannel.ReadRatio();
            var y = yChannel.ReadRatio();

            // Scale to -1 to 1, and Y needs to be inverted
            x = (x * 2) - 1;
            y = (-y * 2) + 1;

            // Button
            bool pressed = false;

            if (buttonPin != null)
            {
                pressed = (buttonPin.Read() == GpioPinValue.Low);
            }

            // Update current value
            lock (currentReading)
            {
                currentReading = new ThumbstickReading(x, y, pressed);
            }

            // Notify
            readingChangedEvent.Raise(this, new ThumbstickReadingChangedEventArgs(currentReading));
        }
Пример #2
0
        private void EnsureInitialized()
        {
            if (isInitialized)
            {
                return;
            }

            // Validate
            if (xChannel == null)
            {
                throw new MissingIoException(nameof(XChannel));
            }
            if (yChannel == null)
            {
                throw new MissingIoException(nameof(YChannel));
            }

            // Start with fake reading
            currentReading = new ThumbstickReading(0, 0, false);

            // Initialize button?
            if (buttonPin != null)
            {
                if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                {
                    buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
                }
                else
                {
                    buttonPin.SetDriveMode(GpioPinDriveMode.Input);
                }
                buttonPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);
            }

            isInitialized = true;
        }