Пример #1
0
 public byte[] DeviceIdentifier()
 {
     byte[] r1 = new byte[1];
     byte[] r2 = new byte[1];
     byte[] r3 = new byte[1];
     _i2cBus.ReadRegister(_i2cConfig, IdentificationRegister_1, r1, I2C_TIMEOUT);
     _i2cBus.ReadRegister(_i2cConfig, IdentificationRegister_2, r2, I2C_TIMEOUT);
     _i2cBus.ReadRegister(_i2cConfig, IdentificationRegister_3, r3, I2C_TIMEOUT);
     return(new byte[] { r1[0], r2[0], r3[0] });
 }
Пример #2
0
        /// <summary>
        ///     Turn the heater on or off.
        /// </summary>
        /// <param name="onOrOff">Heater status, true = turn heater on, false = turn heater off.</param>
        public void Heater(bool onOrOff)
        {
            var register = _si7021.ReadRegister(Registers.ReadUserRegister1);

            register &= 0xfd;
            if (onOrOff)
            {
                register |= 0x02;
            }
            _si7021.WriteRegister(Registers.WriteUserRegister1, register);
        }
Пример #3
0
        public override void Update()
        {
            _twiBus.ReadRegister(_i2cConfiguration, 0x1D, _buffer, 100);
            float pitch = (short)(_buffer[0] << 8) | _buffer[1];
            float roll  = (short)(_buffer[2] << 8) | _buffer[3];
            float yaw   = (short)(_buffer[4] << 8) | _buffer[5];

            Axes.Pitch = pitch / _factor - _zeroPitch;
            Axes.Roll  = roll / _factor - _zeroRoll;
            Axes.Yaw   = yaw / _factor - _zeroYaw;
        }
Пример #4
0
        /// <summary>
        ///     Force the sensor to make a reading and update the relevanyt properties.
        /// </summary>
        public void Read()
        {
            var controlRegister = _mag3110.ReadRegister((byte)Registers.Control1);

            controlRegister |= 0x02;
            _mag3110.WriteRegister((byte)Registers.Control1, controlRegister);
            var data = _mag3110.ReadRegisters((byte)Registers.XMSB, 6);

            X = (short)((data[0] << 8) | data[1]);
            Y = (short)((data[2] << 8) | data[3]);
            Z = (short)((data[4] << 8) | data[5]);
        }
Пример #5
0
        /// <summary>
        ///     Create a new MAG3110 object using the default parameters for the component.
        /// </summary>
        /// <param name="address">Address of the MAG3110 (default = 0x0e).</param>
        /// <param name="speed">Speed of the I2C bus (default = 400 KHz).</param>
        /// <param name="interruptPin">Interrupt pin used to detect end of conversions.</param>
        public MAG3110(byte address = 0x0e, ushort speed = 400, Cpu.Pin interruptPin = Cpu.Pin.GPIO_NONE)
        {
            _mag3110 = new I2CBus(address, speed);
            var deviceID = _mag3110.ReadRegister((byte)Registers.WhoAmI);

            if (deviceID != 0xc4)
            {
                throw new Exception("Unknown device ID, " + deviceID + " retruend, 0xc4 expected");
            }
            if (interruptPin != Cpu.Pin.GPIO_NONE)
            {
                _interruptPort = new InterruptPort(interruptPin, false,
                                                   Microsoft.SPOT.Hardware.Port.ResistorMode.Disabled,
                                                   Microsoft.SPOT.Hardware.Port.InterruptMode.InterruptEdgeHigh);
                _interruptPort.OnInterrupt += _interruptPort_OnInterrupt;
            }
            Reset();
        }
Пример #6
0
 private byte read8(byte addr)
 {
     return(i2c.ReadRegister(addr));
 }
Пример #7
0
        public MPU6050Device()
        {
            int exceptionCount = 0;

            while (true)
            {
                try
                {
                    Log.WriteLine("Initialising the MPU-6050 accelerometer and gyro package...");

                    // Check connectivity
                    byte[] data = new byte[] { new byte() };
                    _I2CBus.ReadRegister(_i2cConfig, MPU6050Registers.WHO_AM_I, data, _timeout);
                    Debug.Assert((data[0] & 0x68) == 0x68);

                    // PWR_MGMT_1 = Power Management 1
                    // 0xF9 = 11111001: Device Reset=true, Sleep=true, Cycle=true, Temp Sensor=On, Clock Select=PLL with X axis gyroscope reference
                    // TODO: Given the reset, half of these values are probably ignored, and the defaults are used instead.
                    _I2CBus.WriteRegister(_i2cConfig, MPU6050Registers.PWR_MGMT_1, 0xF9, _timeout);
                    Thread.Sleep(100); // allow to reset

                    // Exit sleep mode (CARE! Not sure why, but this resets the device too! - only set config after this line)
                    // TODO: Figure out what's going on here. Perhaps the reset at the line above was taking some time, hence why the Scale settings (below) were ignored
                    _I2CBus.WriteRegister(_i2cConfig, MPU6050Registers.PWR_MGMT_1, 0x01, _timeout);
                    Thread.Sleep(100); // allow to reset

                    // Gyro Scale
                    _gyroRange = GyroConfig.Range.plusMinus0500dps;
                    _I2CBus.WriteRegister(_i2cConfig, MPU6050Registers.GYRO_CONFIG, GyroConfig.Build(_gyroRange), _timeout);

                    // Accelerometer Scale
                    _accelRange = AccelConfig.Range.plusMinus08G;
                    _I2CBus.WriteRegister(_i2cConfig, MPU6050Registers.ACCEL_CONFIG, AccelConfig.Build(_accelRange), _timeout);

                    // Confirm I2C_MST_EN is disabled (required to connect to the HMC5883L)
                    data = new byte[] { new byte() };
                    _I2CBus.ReadRegister(_i2cConfig, 0x6A, data, 1000);
                    Debug.Assert((data[0] & 0x10) == 0);

                    // Turn on I2C Bypass (required to connect to the HMC5883L)
                    _I2CBus.WriteRegister(_i2cConfig, 0x37, 0x02, 1000);
                    data = new byte[] { new byte() };
                    _I2CBus.ReadRegister(_i2cConfig, 0x37, data, 1000);
                    Debug.Assert((data[0] & 0x02) == 0x02);

                    Log.WriteLine("MPU-6050 Ready\n");

                    break;
                }
                catch (System.Exception)
                {
                    exceptionCount++;
                    if (exceptionCount != 3)
                    {
                        Log.WriteLine("Exception during initialisation! Retrying...\n");
                        //I2CBus.DestroySingleton();
                        //_I2CBus = I2CBus.GetInstance();
                        continue;
                    }

                    Log.WriteLine("Exception during initialisation! Throwing...");
                    throw;
                }
            }
        }