/// <summary>
        /// Creates and initializes an instance.
        /// </summary>
        public NavioRCInputDevice()
        {
            // Initialize buffers
            _valueBuffer  = new ConcurrentQueue <PwmValue>();
            _valueTrigger = new AutoResetEvent(false);
            _frameBuffer  = new ConcurrentQueue <PwmFrame>();
            _frameTrigger = new AutoResetEvent(false);

            // Configure GPIO
            _inputPin = NavioHardwareProvider.ConnectGpio(0, GpioInputPinNumber, GpioPinDriveMode.Input, exclusive: true);
            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(_valueBuffer, _valueTrigger, _frameBuffer, _frameTrigger, _stop.Token); });

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

            // Hook events
            _inputPin.ValueChanged += OnInputPinValueChanged;
        }
Esempio n. 2
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;
            }
        }