예제 #1
0
        /// <summary>
        /// Writes word data to the register of the I2C slave device.
        /// </summary>
        /// <param name="register">The register address of the I2C slave device to write.</param>
        /// <param name="data">The word (2 bytes) data to write.</param>
        public void WriteRegisterWord(byte register, ushort data)
        {
            var ret = NativeI2c.WriteRegisterWord(_handle, register, data);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }
예제 #2
0
        /// <summary>
        /// Opens the connection to the I2C slave device.
        /// </summary>
        /// <param name="bus">The I2C bus number that the slave device is connected.</param>
        /// <param name="address">The address of the slave device.</param>
        public I2cDevice(int bus, int address)
        {
            var ret = NativeI2c.Open(bus, address, out _handle);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }
예제 #3
0
        /// <summary>
        /// Reads the bytes data from the I2C slave device.
        /// </summary>
        /// <param name="dataOut">The output byte array.</param>
        public void Read(byte[] dataOut)
        {
            var length = Convert.ToUInt32(dataOut.Length);
            var ret    = NativeI2c.Read(_handle, dataOut, length);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }
예제 #4
0
        /// <summary>
        /// Disposes the I2c.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            NativeI2c.Close(_handle);
            _disposed = true;
        }
예제 #5
0
        /// <summary>
        /// Reads word data from the register of the I2C slave device.
        /// </summary>
        /// <param name="register">The register address of the I2C slave device to read.</param>
        /// <returns>The word (2 bytes) data.</returns>
        public ushort ReadRegisterWord(byte register)
        {
            var ret = NativeI2c.ReadRegisterWord(_handle, register, out ushort data);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }

            return(data);
        }
예제 #6
0
        /// <summary>
        /// Writes the bytes data to the I2C slave device.
        /// </summary>
        /// <param name="data"></param>
        public void Write(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            var length = Convert.ToUInt32(data.Length);
            var ret    = NativeI2c.Write(_handle, data, length);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }