예제 #1
0
        private void EnsureInitialized()
        {
            // If we're already initialized, ignore
            if (isInitialized)
            {
                return;
            }

            // Validate that the pin has been set
            if (pin == null)
            {
                throw new MissingIoException(nameof(Pin));
            }

            bool driveSet = false;

            // Use pull resistors?
            if (usePullResistors)
            {
                // Check if resistors are supported
                if (onValue == GpioPinValue.High)
                {
                    pin.SetDriveModeWithFallback(GpioPinDriveMode.InputPullDown);
                    driveSet = true;
                }
                else
                {
                    pin.SetDriveModeWithFallback(GpioPinDriveMode.InputPullUp);
                    driveSet = true;
                }
            }

            if (!driveSet)
            {
                pin.SetDriveMode(GpioPinDriveMode.Input);
            }

            // Set a debounce timeout to filter out switch bounce noise
            if (debounceTimeout > 0)
            {
                pin.DebounceTimeout = TimeSpan.FromMilliseconds(debounceTimeout);
            }

            // Determine state
            IsOn = (pin.Read() == onValue);

            // Subscribe to pin events
            pin.ValueChanged += Pin_ValueChanged;

            // Consider ourselves initialized now
            isInitialized = true;
        }
예제 #2
0
        /// <summary>
        /// Ensures that the helper has been initialized.
        /// </summary>
        public void EnsureInitialized()
        {
            if (isInitialized)
            {
                return;
            }

            // Validate that the pin has been set
            if (pin == null)
            {
                throw new MissingIoException(nameof(Pin));
            }

            // Set as input, use resistors if supported
            if (usePullResistors)
            {
                pin.SetDriveModeWithFallback(GpioPinDriveMode.InputPullUp);
            }
            else
            {
                pin.SetDriveMode(GpioPinDriveMode.Input);
            }

            // Set a debounce timeout to filter out switch bounce noise from a button press
            pin.DebounceTimeout = TimeSpan.FromMilliseconds(debounceTimeout);

            // Subscribe to pin events
            pin.ValueChanged += Pin_ValueChanged;

            // Consider ourselves initialized now
            isInitialized = true;
        }
예제 #3
0
        private void EnsureInitialized()
        {
            if (isInitialized)
            {
                return;
            }

            // Make sure helper is initialized
            buttonHelper.EnsureInitialized();

            // Validate that required pins have been set
            if (clockPin == null)
            {
                throw new MissingIoException(nameof(ClockPin));
            }
            if (directionPin == null)
            {
                throw new MissingIoException(nameof(DirectionPin));
            }

            // Use pull resistors?
            if (buttonHelper.UsePullResistors)
            {
                clockPin.SetDriveModeWithFallback(GpioPinDriveMode.InputPullUp);
                directionPin.SetDriveModeWithFallback(GpioPinDriveMode.InputPullUp);
            }
            else
            {
                clockPin.SetDriveMode(GpioPinDriveMode.Input);
                directionPin.SetDriveMode(GpioPinDriveMode.Input);
            }

            // Set a debounce timeout to filter out bounce noise
            //clockPin.DebounceTimeout = TimeSpan.FromMilliseconds(buttonHelper.DebounceTimeout);
            //directionPin.DebounceTimeout = TimeSpan.FromMilliseconds(buttonHelper.DebounceTimeout);
            clockPin.DebounceTimeout     = TimeSpan.Zero;
            directionPin.DebounceTimeout = TimeSpan.Zero;

            // Update last value
            lastDirValue = directionPin.Read();

            // Subscribe to pin events
            clockPin.ValueChanged += ClockPin_ValueChanged;

            // Consider ourselves initialized now
            isInitialized = true;
        }