Example #1
0
        /// <summary>
        /// Writes the "on" and "off" values of a channel together, and updates it in <see cref="Channels"/>.
        /// </summary>
        /// <param name="index">Zero based channel number (0-15) or 16 for the "all call" channel.</param>
        /// <param name="value"><see cref="Pca9685ChannelValue"/> to write.</param>
        /// <returns>
        /// Updated channel value or null when all channels were updated.
        /// </returns>
        public Pca9685ChannelValue?WriteChannel(int index, Pca9685ChannelValue value)
        {
            // Validate
            if (index <0 | index> ChannelCount)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            // Calculate register address
            var register = GetChannelAddress(index);

            // Convert and write value
            var bytes = value.ToByteArray();

            I2cExtensions.WriteJoinBytes(_hardware, register, bytes);

            // Read and return result when single channel
            if (index < ChannelCount)
            {
                return(ReadChannel(index));
            }

            // Read all channels when "all call".
            ReadAllChannels();
            return(null);
        }
Example #2
0
        /// <summary>
        /// Clears all channels cleanly, then updates all <see cref="Channels"/>.
        /// </summary>
        /// <remarks>
        /// To "cleanly" clear the channels, it is necessary to first ensure they are not disabled,
        /// set them to zero, then disable them. Otherwise the ON value and the low OFF value
        /// remain because writes are ignored when the OFF channel bit 12 is already set.
        /// </remarks>
        public void Clear()
        {
            // Enable all channels
            I2cExtensions.WriteJoinByte(_hardware, (byte)Pca9685Register.AllChannelsOffHigh, 0x00);

            // Zero all channels
            I2cExtensions.WriteJoinBytes(_hardware, (byte)Pca9685Register.AllChannelsOnLow, new byte[] { 0x00, 0x00, 0x00, 0x00 });

            // Disable all channels
            I2cExtensions.WriteJoinByte(_hardware, (byte)Pca9685Register.AllChannelsOffHigh, 0x10);

            // Update channels
            ReadAllChannels();
        }
Example #3
0
        /// <summary>
        /// Writes the PWM "off" (falling) value of a channel.
        /// </summary>
        /// <param name="index">Zero based channel number (0-15) or 16 for the "all call" channel.</param>
        /// <param name="value">12-bit channel value in the range 0-<see cref="Pca9685ChannelValue.Maximum"/>.</param>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="value"/> is greater then <see cref="Pca9685ChannelValue.Maximum"/>.</exception>
        public void WriteChannelOff(int index, int value)
        {
            // Validate
            if (value > Pca9685ChannelValue.Maximum)
            {
                throw new ArgumentOutOfRangeException(nameof(value));
            }

            // Calculate register address of second word value
            var register = (byte)(GetChannelAddress(index) + sizeof(ushort));

            // Convert and write value
            var bytes = BitConverter.GetBytes(value);

            I2cExtensions.WriteJoinBytes(_hardware, register, bytes);
        }
Example #4
0
        /// <summary>
        /// Writes the PWM "on" (rising) value of a channel.
        /// </summary>
        /// <param name="index">Zero based channel number (0-15) or 16 for the "all call" channel.</param>
        /// <param name="value">12-bit channel value in the range 0-<see cref="Pca9685ChannelValue.Maximum"/>.</param>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="value"/> is greater than <see cref="Pca9685ChannelValue.Maximum"/>.</exception>
        public void WriteChannelOn(int index, int value)
        {
            // Validate
            if (value > Pca9685ChannelValue.Maximum)
            {
                throw new ArgumentOutOfRangeException(nameof(value));
            }

            // Calculate register address
            var register = GetChannelAddress(index);

            // Convert and write value
            var data = BitConverter.GetBytes(value);

            I2cExtensions.WriteJoinBytes(_hardware, register, data);
        }