コード例 #1
0
        /// <summary>
        /// Sets or clears one or more bits on the AK8963 magnetometer.
        /// </summary>
        /// <param name="register">Magnetometer register</param>
        /// <param name="mask"> Mask of the bit to set or clear according to value. Supports setting or clearing multiple bits.</param>
        /// <param name="value">Value of the bits, i.e. set or clear.</param>
        /// <returns>Value written to register</returns>
        /// <remarks>
        /// Commonly used to set register flags. Reads the current byte value, merges the positive or negative bit mask according to value,
        /// then writes the modified byte back.
        /// </remarks>
        private byte ReadWriteAK8963(Mpu9250MagRegister register, byte mask, bool value)
        {
            // Read existing byte
            var oldByte = ReadAK8963(register);

            // Merge bit (set or clear bit accordingly)
            var newByte = value ? (byte)(oldByte | mask) : (byte)(oldByte & ~mask);

            // Write new byte
            WriteAK8963(register, newByte);

            // Return the value written.
            return(newByte);
        }
コード例 #2
0
        /// <summary>
        /// Writes a single byte to the AK8963 magnetometer register.
        /// </summary>
        /// <param name="register">Magnetometer register.</param>
        /// <param name="data">Data to write.</param>
        private void WriteAK8963(Mpu9250MagRegister register, byte data)
        {
            // Set I2C slave address of the AK8963 and set for write.
            Hardware.WriteJoinByte((byte)Mpu9250Register.I2cSlave0Address, (byte)Mpu9250MagRegister.Ak8963I2cAddress);

            // Set I2C slave register address from where to begin data transfer.
            Hardware.WriteJoinByte((byte)Mpu9250Register.I2cSlave0Register, (byte)register);

            // Set I2C data written to I2C slave.
            Hardware.WriteJoinByte((byte)Mpu9250Register.I2cSlave0DO, data);

            // Set I2C enable and set 1 byte.
            Hardware.WriteJoinByte((byte)Mpu9250Register.I2cSlave0Control, 0x81);

            // Wait for write completion
            Task.Delay(WriteDelay).Wait();
        }
コード例 #3
0
        /// <summary>
        /// Reads one or more bytes from the AK8963 magnetometer register.
        /// </summary>
        /// <param name="register">External sensor data to read register.</param>
        /// <param name="magRegister">Magnetometer register.</param>
        /// <param name="control">Magnetometer register.</param>
        /// <param name="size">Amount of bytes to read.</param>
        /// <returns>Read register bytes.</returns>
        private byte[] ReadAK8963(Mpu9250Register register, Mpu9250MagRegister magRegister, byte control, int size)
        {
            // Validate size to make sure it's not out of the range for an sbyte type.
            if (size < -128 & size > 127)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }

            // Set I2C slave address of the AK8963 and set for read.
            Hardware.WriteJoinByte((byte)Mpu9250Register.I2cSlave0Address, (byte)Mpu9250MagRegister.Ak8963I2cAddress | SpiExtensions.ReadFlag);

            // Set I2C slave 0 register address from where to begin data transfer.
            Hardware.WriteJoinByte((byte)Mpu9250Register.I2cSlave0Register, (byte)magRegister);

            // Read bytes from the magnetometer
            Hardware.WriteJoinByte((byte)Mpu9250Register.I2cSlave0Control, control);

            // Wait for write completion
            Task.Delay(WriteDelay).Wait();

            // Read the data out registers
            return(Hardware.WriteReadBytes((byte)register, size));
        }
コード例 #4
0
 /// <summary>
 /// Reads one or more bytes from the AK8963 magnetometer register.
 /// </summary>
 /// <param name="register">Magnetometer register.</param>
 /// <param name="size">Amount of bytes to read.</param>
 /// <returns>Read register bytes.</returns>
 private byte[] ReadAK8963(Mpu9250MagRegister register, int size)
 {
     // Read the data out registers
     return(ReadAK8963(Mpu9250Register.ExternalSensorData00, register, (byte)((sbyte)size | 0x80), size));
 }
コード例 #5
0
 /// <summary>
 /// Reads a single byte result from the AK8963 magnetometer register.
 /// </summary>
 /// <param name="register">Magnetometer register.</param>
 /// <returns>Read register byte.</returns>
 private byte ReadAK8963(Mpu9250MagRegister register)
 {
     return(ReadAK8963(register, 1)[0]);
 }