Beispiel #1
0
        /// <summary>
        /// Reads all channels and updates <see cref="Channels"/>.
        /// </summary>
        public void ReadAllChannels()
        {
            // Read all channels as one block of data
            var data = I2cExtensions.WriteReadBytes(_hardware, ChannelStartAddress, ChannelSize * ChannelCount);

            // Update properties
            for (var index = 0; index < ChannelCount; index++)
            {
                _channels[index] = Pca9685ChannelValue.FromByteArray(data, ChannelSize * index);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Reads a whole channel value (on and off), and updates it in <see cref="Channels"/>.
        /// </summary>
        /// <param name="index">Zero based channel number (0-15).</param>
        /// <returns>Channel value</returns>
        public Pca9685ChannelValue ReadChannel(int index)
        {
            // Validate
            if (index < 0 | index >= ChannelCount)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

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

            // Read value
            var bytes = I2cExtensions.WriteReadBytes(_hardware, register, sizeof(ushort) * 2);

            // Update channel property and return result
            var value = Pca9685ChannelValue.FromByteArray(bytes);

            return(_channels[index] = value);
        }
Beispiel #3
0
        /// <summary>
        /// Writes multiple channels together (both "on" and "off" values), 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="values">Collection of <see cref="Pca9685ChannelValue"/>s to write.</param>
        public void WriteChannels(int index, IList <Pca9685ChannelValue> values)
        {
            // Validate
            if (index <0 | index> ChannelCount)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (values == null || values.Count == 0)
            {
                throw new ArgumentNullException(nameof(values));
            }
            var count = values.Count;

            if (index + count > ChannelCount)
            {
                throw new ArgumentOutOfRangeException(nameof(values));
            }

            // Build I2C packet
            var data = new byte[1 + ChannelSize * count];

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

            data[0] = register;

            // Write channels and update properties
            for (int dataIndex = 0, dataOffset = 1; dataIndex < count; dataIndex++, dataOffset += ChannelSize)
            {
                // Get channel data
                var channelData = values[dataIndex].ToByteArray();

                // Copy to buffer
                Array.ConstrainedCopy(channelData, 0, data, dataOffset, ChannelSize);

                // Update property
                _channels[index + dataIndex] = Pca9685ChannelValue.FromByteArray(channelData);
            }

            // Send packet
            I2cExtensions.WriteBytes(_hardware, data);
        }
Beispiel #4
0
        /// <summary>
        /// Reads multiple channel values (both on and off for each), and updates it in <see cref="Channels"/>.
        /// </summary>
        /// <param name="index">Zero based channel number (0-15).</param>
        /// <param name="count">Number of channels to read.</param>
        /// <returns>Channel values</returns>
        public Collection <Pca9685ChannelValue> ReadChannels(int index, int count)
        {
            // Validate
            if (index < 0 | index >= ChannelCount)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (count < 1 || index + count > ChannelCount)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

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

            // Send I2C command to read channels in one operation
            var data = I2cExtensions.WriteReadBytes(_hardware, register, ChannelSize * count);

            // Update channel properties and add to results
            var results = new Collection <Pca9685ChannelValue>();

            for (int channelIndex = index, offset = 0; count > 0; count--, channelIndex++, offset += ChannelSize)
            {
                // Calculate value
                var value = Pca9685ChannelValue.FromByteArray(data, offset);

                // Update property
                _channels[channelIndex] = value;

                // Add to results
                results.Add(value);
            }

            // Return results
            return(results);
        }