Ejemplo n.º 1
0
        /// <summary>
        /// Reads device statuses.
        /// </summary>
        /// <returns>Device statuses</returns>
        private Status GetStatus()
        {
            _sensor.WriteByte((byte)Register.HMC_STATUS_REG_ADDR);
            byte status = _sensor.ReadByte();

            return((Status)status);
        }
Ejemplo n.º 2
0
Archivo: Bmp180.cs Proyecto: z-eh/iot
        /// <summary>
        ///  Reads an 8 bit value from a register
        /// </summary>
        /// <param name="register">
        ///  Register to read from
        /// </param>
        /// <returns>
        ///  Value from register
        /// </returns>
        internal byte Read8BitsFromRegister(byte register)
        {
            _i2cDevice.WriteByte(register);
            byte value = _i2cDevice.ReadByte();

            return(value);
        }
Ejemplo n.º 3
0
        private byte GetStatus()
        {
            _i2cDevice.WriteByte(0x71);
            // whithout this delay the reading the status fails often.
            Thread.Sleep(10);
            byte status = _i2cDevice.ReadByte();

            return(status);
        }
Ejemplo n.º 4
0
        private byte ReadRegister(Register register)
        {
            WakeUpDevice();
            _i2cDevice.WriteByte((byte)register);
            var ret = _i2cDevice.ReadByte();

            SleepDownDevice();
            return(ret);
        }
Ejemplo n.º 5
0
 /// <summary>
 ///     Read a single byte without a register from an I2C device.
 /// </summary>
 /// <param name="bus">I2C device</param>
 /// <returns>Read value</returns>
 internal byte ReadI2CSingleByte(I2cDevice bus)
 {
     try
     {
         byte returnValue = bus.ReadByte();
         return(returnValue);
     }
     catch (Exception)
     {
         throw;
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Bmxx80Base"/> class.
        /// </summary>
        /// <param name="deviceId">The ID of the device.</param>
        /// <param name="i2cDevice">The <see cref="I2cDevice"/> to create with.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given <see cref="I2cDevice"/> is null.</exception>
        /// <exception cref="IOException">Thrown when the device cannot be found on the bus.</exception>
        protected Bmxx80Base(byte deviceId, I2cDevice i2cDevice)
        {
            _i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
            _i2cDevice.WriteByte((byte)Bmxx80Register.CHIPID);

            byte readSignature = _i2cDevice.ReadByte();

            if (readSignature != deviceId)
            {
                throw new IOException($"Unable to find a chip with id {deviceId}");
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 使用 prescale 来设置 PWM 频率
        /// </summary>
        public void SetPwmFrequency(byte prescale)
        {
            var oldmode = device.ReadByte();
            var newmode = (oldmode & 0x7F) | 0x10;             // sleep

            device.Write(new byte[] { MODE1, (byte)newmode }); // go to sleep
            device.Write(new byte[] { PRESCALE, prescale });
            device.Write(new byte[] { MODE1, oldmode });
            Thread.Sleep(5);

            device.Write(new byte[] { MODE1, (byte)(oldmode | 0x80) });
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Reads an 8 bit value from a register.
 /// </summary>
 /// <param name="register">Register to read from.</param>
 /// <returns>Value from register.</returns>
 protected internal byte Read8BitsFromRegister(byte register)
 {
     if (_communicationProtocol == CommunicationProtocol.I2c)
     {
         _i2cDevice.WriteByte(register);
         byte value = _i2cDevice.ReadByte();
         return(value);
     }
     else
     {
         throw new NotImplementedException();
     }
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Create a TCS4272x sensor
 /// </summary>
 /// <param name="i2cDevice">The I2C Device class</param>
 /// <param name="integrationTime">The time to wait for sensor to read the data, minimum is 0.024 seconds, maximum in the constructor is 0.7 seconds</param>
 /// <param name="gain">The gain when integrating the color measurement</param>
 /// <param name="autoDisposable">true to dispose the I2C Device class at dispose</param>
 public Tcs3472x(I2cDevice i2cDevice, double integrationTime = 0.0024, Gain gain = Gain.Gain16X, bool autoDisposable = true)
 {
     _i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
     // Maximum is 700 ms for the initialization. Value can be changed for a long one but not during this initialization phase
     _autoDisposable = autoDisposable;
     _i2cDevice.WriteByte((byte)(Registers.COMMAND_BIT | Registers.ID));
     ChipId          = (TCS3472Type)_i2cDevice.ReadByte();
     _isLongTime     = false;
     IntegrationTime = Math.Clamp(integrationTime, 0.0024, 0.7);
     SetIntegrationTime(integrationTime);
     Gain = gain;
     PowerOn();
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SensorHub"/> class.
 /// </summary>
 public SensorHub(I2cDevice i2cDevice)
 {
     _i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
     try
     {
         _i2cDevice.WriteByte((byte)_i2cDevice.ConnectionSettings.DeviceAddress);
         _i2cDevice.ReadByte();
     }
     catch (IOException ex)
     {
         throw new IOException($"No response from SensorHub with address {_i2cDevice.ConnectionSettings.DeviceAddress}", ex);
     }
 }
        public byte DigitalRead(Pin pin)
        {
            var wbuffer = new byte[4] {
                (byte)Command.DigitalRead, (byte)pin, 0x00, 0x00
            };
            var rBuffer = new byte[1];

            grovePiPlusDevice.Write(wbuffer);
//            grovePiPlusDevice.WriteRead(wbuffer, rBuffer);
            Thread.Sleep(2);
            var value = grovePiPlusDevice.ReadByte();

            return(value);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 初始化 PCA9685
        /// </summary>
        public PCA9685(int address = PCA9685_ADDRESS, int busId = 1)
        {
            Address = address;
            BusId   = busId;

            // Setup I2C interface for the device.
            device = I2cDeviceFactory.GetDevice(BusId, Address);

            SetPwm(0, 0);
            device.Write(new byte[] { MODE2, OUTDRV });
            device.Write(new byte[] { MODE1, ALLCALL });
            Thread.Sleep(5); // wait for oscillator

            int mode1 = device.ReadByte();

            mode1 = mode1 & ~SLEEP; // wake up (reset sleep)
            device.Write(new byte[] { MODE1, (byte)mode1 });
            Thread.Sleep(5);        // wait for oscillator
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Bmxx80Base"/> class.
        /// </summary>
        /// <param name="deviceId">The ID of the device.</param>
        /// <param name="i2cDevice">The <see cref="I2cDevice"/> to create with.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given <see cref="I2cDevice"/> is null.</exception>
        /// <exception cref="IOException">Thrown when the device cannot be found on the bus.</exception>
        protected Bmxx80Base(byte deviceId, I2cDevice i2cDevice)
        {
            _i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
            _i2cDevice.WriteByte((byte)Bmxx80Register.CHIPID);

            byte readSignature = _i2cDevice.ReadByte();

            if (readSignature != deviceId)
            {
                throw new IOException($"Unable to find a chip with id {deviceId}. Found one with id {readSignature}");
            }

            ReadCalibrationData();
            Reset();
#if NETCOREAPP2_1
            if (_calibrationData is null)
            {
                throw new Exception("BMxx80 device is not correctly configured.");
            }
#endif
        }
Ejemplo n.º 14
0
 private byte Read(Register register)
 {
     _i2c.WriteByte((byte)((byte)register | ReadMask));
     return(_i2c.ReadByte());
 }
Ejemplo n.º 15
0
 private JoystickState ReadState()
 {
     _i2c.WriteByte(StateRegister);
     return((JoystickState)_i2c.ReadByte());
 }
Ejemplo n.º 16
0
 private byte I2cRead8(Registers reg)
 {
     _i2cDevice.WriteByte((byte)(Registers.COMMAND_BIT | reg));
     return(_i2cDevice.ReadByte());
 }
Ejemplo n.º 17
0
 private byte ReadByte(Register reg)
 {
     _i2cDevice.WriteByte((byte)reg);
     return(_i2cDevice.ReadByte());
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Read 8 bits from a given <see cref="Register"/>.
        /// </summary>
        /// <param name="register">The <see cref="Register"/> to read from.</param>
        /// <returns>Value from register.</returns>
        /// <remarks>
        /// Cast to an <see cref="sbyte"/> if you want to read a signed value.
        /// </remarks>
        internal byte Read8Bits(Register register)
        {
            _i2cDevice.WriteByte((byte)register);

            return(_i2cDevice.ReadByte());
        }
Ejemplo n.º 19
0
 public byte ReadByte() => Device.ReadByte();
Ejemplo n.º 20
0
 /// <summary>
 /// Read a byte
 /// </summary>
 /// <param name="i2cDevice">An I2C device</param>
 /// <param name="reg">The register to read</param>
 /// <returns>The register value</returns>
 public override byte ReadByte(I2cDevice i2cDevice, byte reg)
 {
     i2cDevice.WriteByte(reg);
     return(i2cDevice.ReadByte());
 }