|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Buffers.Binary; |
| 6 | +using System.Device.I2c; |
| 7 | +using System.Device.Model; |
| 8 | +using System.Numerics; |
| 9 | + |
| 10 | +namespace Iot.Device.Hmc5883l |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// 3-Axis Digital Compass HMC5883L |
| 14 | + /// </summary> |
| 15 | + [Interface("3-Axis Digital Compass HMC5883L")] |
| 16 | + public class Hmc5883l : IDisposable |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// HMC5883L Default I2C Address |
| 20 | + /// </summary> |
| 21 | + public const byte DefaultI2cAddress = 0x1E; |
| 22 | + |
| 23 | + private readonly byte _measuringMode; |
| 24 | + private readonly byte _outputRate; |
| 25 | + private readonly byte _gain; |
| 26 | + private readonly byte _samplesAmount; |
| 27 | + private readonly byte _measurementConfig; |
| 28 | + |
| 29 | + private I2cDevice _i2cDevice; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// HMC5883L Direction Vector |
| 33 | + /// </summary> |
| 34 | + [Telemetry] |
| 35 | + public Vector3 DirectionVector => ReadDirectionVector(); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// HMC5883L Heading (DEG) |
| 39 | + /// </summary> |
| 40 | + public double Heading => VectorToHeading(ReadDirectionVector()); |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// HMC5883L Status |
| 44 | + /// </summary> |
| 45 | + [Telemetry] |
| 46 | + public Status DeviceStatus => GetStatus(); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Initialize a new HMC5883L device connected through I2C |
| 50 | + /// </summary> |
| 51 | + /// <param name="i2cDevice">The I2C device used for communication.</param> |
| 52 | + /// <param name="gain">Gain Setting</param> |
| 53 | + /// <param name="measuringMode">The Mode of Measuring</param> |
| 54 | + /// <param name="outputRate">Typical Data Output Rate (Hz)</param> |
| 55 | + /// <param name="samplesAmount">Number of samples averaged per measurement output</param> |
| 56 | + /// <param name="measurementConfig">Measurement configuration</param> |
| 57 | + public Hmc5883l( |
| 58 | + I2cDevice i2cDevice, |
| 59 | + Gain gain = Gain.Gain1090, |
| 60 | + MeasuringMode measuringMode = MeasuringMode.Continuous, |
| 61 | + OutputRate outputRate = OutputRate.Rate15, |
| 62 | + SamplesAmount samplesAmount = SamplesAmount.One, |
| 63 | + MeasurementConfiguration measurementConfig = MeasurementConfiguration.Normal) |
| 64 | + { |
| 65 | + _i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice)); |
| 66 | + _gain = (byte)gain; |
| 67 | + _measuringMode = (byte)measuringMode; |
| 68 | + _outputRate = (byte)outputRate; |
| 69 | + _samplesAmount = (byte)samplesAmount; |
| 70 | + _measurementConfig = (byte)measurementConfig; |
| 71 | + |
| 72 | + Initialize(); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Initialize the sensor |
| 77 | + /// </summary> |
| 78 | + private void Initialize() |
| 79 | + { |
| 80 | + // Details in Datasheet P12 |
| 81 | + byte configA = (byte)(_samplesAmount | (_outputRate << 2) | _measurementConfig); |
| 82 | + byte configB = (byte)(_gain << 5); |
| 83 | + |
| 84 | + SpanByte commandA = new byte[] |
| 85 | + { |
| 86 | + (byte)Register.HMC_CONFIG_REG_A_ADDR, configA |
| 87 | + }; |
| 88 | + SpanByte commandB = new byte[] |
| 89 | + { |
| 90 | + (byte)Register.HMC_CONFIG_REG_B_ADDR, configB |
| 91 | + }; |
| 92 | + SpanByte commandMode = new byte[] |
| 93 | + { |
| 94 | + (byte)Register.HMC_MODE_REG_ADDR, _measuringMode |
| 95 | + }; |
| 96 | + |
| 97 | + _i2cDevice.Write(commandA); |
| 98 | + _i2cDevice.Write(commandB); |
| 99 | + _i2cDevice.Write(commandMode); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Read raw data from HMC5883L |
| 104 | + /// </summary> |
| 105 | + /// <returns>Raw Data</returns> |
| 106 | + private Vector3 ReadDirectionVector() |
| 107 | + { |
| 108 | + SpanByte xRead = new byte[2]; |
| 109 | + SpanByte yRead = new byte[2]; |
| 110 | + SpanByte zRead = new byte[2]; |
| 111 | + |
| 112 | + _i2cDevice.WriteByte((byte)Register.HMC_X_MSB_REG_ADDR); |
| 113 | + _i2cDevice.Read(xRead); |
| 114 | + _i2cDevice.WriteByte((byte)Register.HMC_Y_MSB_REG_ADDR); |
| 115 | + _i2cDevice.Read(yRead); |
| 116 | + _i2cDevice.WriteByte((byte)Register.HMC_Z_MSB_REG_ADDR); |
| 117 | + _i2cDevice.Read(zRead); |
| 118 | + |
| 119 | + short x = BinaryPrimitives.ReadInt16BigEndian(xRead); |
| 120 | + short y = BinaryPrimitives.ReadInt16BigEndian(yRead); |
| 121 | + short z = BinaryPrimitives.ReadInt16BigEndian(zRead); |
| 122 | + |
| 123 | + return new Vector3(x, y, z); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Calculate heading |
| 128 | + /// </summary> |
| 129 | + /// <param name="vector">HMC5883L Direction Vector</param> |
| 130 | + /// <returns>Heading (DEG)</returns> |
| 131 | + private double VectorToHeading(Vector3 vector) |
| 132 | + { |
| 133 | + double deg = Math.Atan2(vector.Y, vector.X) * 180 / Math.PI; |
| 134 | + |
| 135 | + if (deg < 0) |
| 136 | + { |
| 137 | + deg += 360; |
| 138 | + } |
| 139 | + |
| 140 | + return deg; |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Cleanup |
| 145 | + /// </summary> |
| 146 | + public void Dispose() |
| 147 | + { |
| 148 | + _i2cDevice?.Dispose(); |
| 149 | + _i2cDevice = null!; |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Reads device statuses. |
| 154 | + /// </summary> |
| 155 | + /// <returns>Device statuses</returns> |
| 156 | + private Status GetStatus() |
| 157 | + { |
| 158 | + _i2cDevice.WriteByte((byte)Register.HMC_STATUS_REG_ADDR); |
| 159 | + byte status = _i2cDevice.ReadByte(); |
| 160 | + |
| 161 | + return (Status)status; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments