Пример #1
0
        /// <summary>
        /// Reads the PWM "off" (falling) value of a channel, and updates it in <see cref="Channels"/>.
        /// </summary>
        /// <param name="index">Zero based channel number (0-15).</param>
        /// <returns>Channel value.</returns>
        public int ReadChannelOff(int index)
        {
            // Validate
            if (index < 0 | index >= ChannelCount)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

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

            // Read and convert value
            var bytes = I2cExtensions.WriteReadBytes(_hardware, register, sizeof(ushort));
            var value = BitConverter.ToUInt16(bytes, 0);

            // Update channel when changed
            var oldValue = _channels[index];

            if (oldValue.Off != value)
            {
                _channels[index] = new Pca9685ChannelValue(oldValue.On, value);
            }

            // Return result
            return(value);
        }
Пример #2
0
        /// <summary>
        /// Reads a coefficient value from the PROM.
        /// </summary>
        /// <param name="index">Coefficient index (0-7).</param>
        /// <param name="buffer">Target buffer.</param>
        /// <param name="offset">Target offset.</param>
        /// <remarks>
        /// Reads <see cref="Ms5611PromData.CoefficientSize"/> bytes into the target buffer at the specified offset.
        /// </remarks>
        private void ReadPromCoefficient(int index, byte[] buffer, int offset)
        {
            // Calculate address
            var coefficientOffset = (byte)(index * Ms5611PromData.CoefficientSize);
            var address           = (byte)(Ms5611Command.PromRead + coefficientOffset);

            // Read from hardware return value
            var coefficient = I2cExtensions.WriteReadBytes(_hardware, address, Ms5611PromData.CoefficientSize);

            Array.ConstrainedCopy(coefficient, 0, buffer, offset, Ms5611PromData.CoefficientSize);
        }
Пример #3
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);
            }
        }
Пример #4
0
        /// <summary>
        /// Executes the <see cref="Ms5611Command.ConvertD2Temperature"/> command to measure
        /// pressure at the specified OSR, waits then returns the result.
        /// </summary>
        public int ConvertTemperature(Ms5611Osr rate)
        {
            // Send command to hardware
            I2cExtensions.WriteJoinByte(_hardware, (byte)(Ms5611Command.ConvertD2Temperature + (byte)rate), 0);

            // Wait for completion
            WaitForConversion(rate);

            // Return result
            var result = I2cExtensions.WriteReadBytes(_hardware, (byte)Ms5611Command.AdcRead, 3);

            return(result[0] << 16 | result[1] << 8 | result[2]);
        }
Пример #5
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);
        }
Пример #6
0
        /// <summary>
        /// Reads a "page" of bytes "sequentially" starting at the specified address.
        /// </summary>
        public byte[] ReadPage(int address, int length)
        {
            // Validate
            if (address < 0 || address > Size - 1)
            {
                throw new ArgumentOutOfRangeException(nameof(address));
            }
            if (length < 0 || length > Size - address)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            // Get correct I2C device and data for memory address and flags
            var device = GetI2cDeviceForMemoryAddress(address);

            // Read data with chunking when transfer size exceeds limit
            var resultBuffer = new byte[length];
            var remaining = length; var offset = 0;

            do
            {
                var addressBytes = GetMemoryAddressBytes(address);

                // Check transfer size and reduce when necessary
                var transferSize = remaining;
                if (transferSize > I2cExtensions.MaximumTransferSize)
                {
                    transferSize = I2cExtensions.MaximumTransferSize;
                }

                // Read data
                var buffer = I2cExtensions.WriteReadBytes(device, addressBytes, transferSize);
                Array.ConstrainedCopy(buffer, 0, resultBuffer, offset, transferSize);

                // Next transfer when necessary...
                remaining -= transferSize;
                offset    += transferSize;
                address   += transferSize;
            }while (remaining > 0);
            return(resultBuffer);
        }
Пример #7
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);
        }