/// <summary>
        /// Creates an initialized instance.
        /// </summary>
        /// <param name="frequency">
        /// <see cref="NxpPca9685Device.Frequency"/> for important information.
        /// Use <see cref="ServoFrequencyDefault"/> to support most analog servos.
        /// </param>
        /// <param name="clear">Clears all LED/PWM values.</param>
        /// <param name="restart">
        /// Set true to restart the device which also enables the osciallator.
        /// When false, you have to call <see cref="NxpPca9685Device.Wake"/> or <see cref="Restart"/> later to enable the oscillator.
        /// </param>
        /// <param name="enable">
        /// Set true to enable output, or false to continue with further initialization before enabling output.
        /// When false, you have to set it later before any output can occur.
        /// Defaults to false to prevent unexpected behaviour from damaging hardware.
        /// </param>
        public static NavioLedPwmDevice Initialize(float frequency,
                                                   bool clear = true, bool restart = true, bool enable = false)
        {
            // Connect to I2C device
            var device = NavioHardwareProvider.ConnectI2c(I2cControllerIndex, I2cAddress, true, true);

            if (device == null)
            {
                // Initialization error
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  Resources.Strings.I2cErrorDeviceNotFound, I2cAddress, I2cControllerIndex));
            }

            // Perform device initialization sequence...
            var navioDevice = new NavioLedPwmDevice(device);

            navioDevice.OutputEnabled = false;              // Disable output
            navioDevice.WriteFrequency(frequency);          // Set frequency
            if (clear)
            {
                navioDevice.Clear();                        // Clear LED/PWM values when specified
            }
            if (restart)
            {
                navioDevice.Restart();                      // Restart when specified
            }
            if (enable)
            {
                navioDevice.OutputEnabled = true;           // Enable output when specified
            }
            // Return initialized device
            return(navioDevice);
        }
        /// <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;
        }
Example #3
0
        /// <summary>
        /// Creates an instance of the correct type depending on the Navio model.
        /// </summary>
        public NavioFramDevice(NavioHardwareModel model)
        {
            // Connect to I2C device
            var device = NavioHardwareProvider.ConnectI2c(I2cControllerIndex, I2cAddress, true, true);

            if (device == null)
            {
                // Initialization error
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  Resources.Strings.I2cErrorDeviceNotFound, I2cAddress, I2cControllerIndex));
            }

            // Create model specific device
            switch (model)
            {
            case NavioHardwareModel.Navio:

                // Connect to upper I2C device
                var upperAddress = Mb85rc04vDevice.GetUpperI2cAddress(I2cAddress);
                var upperDevice  = NavioHardwareProvider.ConnectI2c(I2cControllerIndex, upperAddress, true, true);
                if (device == null)
                {
                    // Initialization error
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      Resources.Strings.I2cErrorDeviceNotFound, upperAddress, I2cControllerIndex));
                }

                // Create 512 byte device for Navio
                Hardware = new Mb85rc04vDevice(device, upperDevice);
                break;

            case NavioHardwareModel.NavioPlus:

                // Create 32KiB device for Navio+
                Hardware = new Mb85rc256vDevice(device);
                break;

            default:
                throw new NotImplementedException();
            }
        }
 public NavioMotionDevice()
     : base(NavioHardwareProvider.ConnectSpi(SpiControllerIndex, ChipSelect, Frequency, DataLength, SpiMode.Mode3))
 {
 }
Example #5
0
 public NavioBarometerDevice()
     : base(NavioHardwareProvider.ConnectI2c(I2cControllerIndex, I2cAddress, true, true), DefaultOsr)
 {
 }
 public NavioPositionDevice()
     : base(NavioHardwareProvider.ConnectSpi(SpiControllerIndex, ChipSelect, Frequency, DataLength, SpiMode.Mode0))
 {
     // Initialize message received handler
     Reader.MessageReceived += OnMessageReceived;
 }