Пример #1
0
        /// <summary>
        /// Creates and initializes an instance.
        /// </summary>
        public Navio2RcioDevice()
        {
            try
            {
                // Create device
                _chip         = new Px4ioDevice(SpiBusNumber, SpiChipSelectLine, SpiOperationMode, SpiBitsPerWord, SpiFrequency, SpiSharingMode.Exclusive);
                _swdPort      = new GpioSwdPort(GpioBusNumber, GpioSwdClockPinNumber, GpioSwdIoPinNumber);
                _interruptPin = GpioExtensions.Connect(GpioBusNumber, GpioInterruptPinNumber, GpioPinDriveMode.Input, GpioSharingMode.Exclusive);
                _interruptPin.ValueChanged += OnInterruptPinValueChanged;

                // Initialize members
                _channels         = new int[_chip.Configuration.RCInputCount];
                _channelsReadOnly = new ReadOnlyCollection <int>(_channels);
            }
            catch
            {
                // Close devices in case partially initialized
                _interruptPin?.Dispose();
                _swdPort?.Dispose();
                _chip?.Dispose();

                // Continue error
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// Creates an instance and reads current values.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The required mode bits are set, but the device state and PWM values unchanged (supporting recovery).
        /// Initializing without restart allows the caller decide whether to recover or reset the device.
        /// Before starting any output be sure to check the <see cref="INavioPwmDevice.Frequency"/>.
        /// </para>
        /// <para>
        /// To start with new settings, call <see cref="Reset"/> then set <see cref="Enabled"/>.
        /// </para>
        /// </remarks>
        public Navio1LedPwmDevice()
        {
            try
            {
                // Initialize members
                _pwmChannels         = new PwmPulse[PwmChannelCount];
                _pwmChannelsReadOnly = new ReadOnlyCollection <PwmPulse>(_pwmChannels);

                // Connect to hardware
                _device    = new Pca9685Device(I2cControllerIndex, ChipNumber, ExternalClockSpeed);
                _enablePin = GpioExtensions.Connect(GpioControllerIndex, OutputEnableGpioPin,
                                                    GpioPinDriveMode.Output, GpioSharingMode.Exclusive);

                // Read properties
                Read();
            }
            catch
            {
                // Close devices in case partially initialized
                _device?.Dispose();
                _enablePin?.Dispose();

                // Continue error
                throw;
            }
        }
Пример #3
0
        /// <summary>
        /// Creates and initializes an instance.
        /// </summary>
        public Navio1RCInputDevice()
        {
            try
            {
                // Initialize buffers
                _pulseBuffer  = new ConcurrentQueue <PpmPulse>();
                _pulseTrigger = new AutoResetEvent(false);
                _frameBuffer  = new ConcurrentQueue <PpmFrame>();
                _frameTrigger = new AutoResetEvent(false);

                // Configure GPIO
                _inputPin = GpioExtensions.Connect(GpioControllerIndex, GpioInputPinNumber, GpioPinDriveMode.Input, GpioSharingMode.Exclusive);
                if (_inputPin == null)
                {
                    // Initialization error
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Strings.GpioErrorDeviceNotFound, GpioInputPinNumber, GpioControllerIndex));
                }
                if (_inputPin.DebounceTimeout != TimeSpan.Zero)
                {
                    _inputPin.DebounceTimeout = TimeSpan.Zero;
                }

                // Create decoder thread (CPPM only to start with, SBus desired)
                _decoder     = new CppmDecoder();
                _stop        = new CancellationTokenSource();
                _channels    = new int[_decoder.MaximumChannels];
                Channels     = new ReadOnlyCollection <int>(_channels);
                _decoderTask = Task.Factory.StartNew(() => { _decoder.DecodePulse(_pulseBuffer, _pulseTrigger, _frameBuffer, _frameTrigger, _stop.Token); });

                // Create receiver thread
                _receiverTask = Task.Factory.StartNew(() => { Receiver(); });

                // Hook events
                _inputPin.ValueChanged += OnInputPinValueChanged;

                // Start buffered event handling
                // TODO: Support buffered GPIO when possible
                //_inputPin.CreateInterruptBuffer();
                //_inputPin.StartInterruptBuffer();
                //_inputPin.StopInterruptCount();
            }
            catch
            {
                // Close device in case partially initialized
                _inputPin?.Dispose();

                // Continue error
                throw;
            }
        }
Пример #4
0
        /// <summary>
        /// Creates an instance and read current values.
        /// </summary>
        public Navio2LedDevice()
        {
            try
            {
                // Open pins
                _redPin   = GpioExtensions.Connect(GpioControllerIndex, 4, GpioPinDriveMode.Output, GpioSharingMode.Exclusive);
                _greenPin = GpioExtensions.Connect(GpioControllerIndex, 27, GpioPinDriveMode.Output, GpioSharingMode.Exclusive);
                _bluePin  = GpioExtensions.Connect(GpioControllerIndex, 6, GpioPinDriveMode.Output, GpioSharingMode.Exclusive);

                // Read current values
                Read();
            }
            catch
            {
                // Close devices in case partially intialized
                _redPin?.Dispose();
                _greenPin?.Dispose();
                _bluePin?.Dispose();

                // Continue error
                throw;
            }
        }