Skip to content

Commit 3d44754

Browse files
authored
Adding Hmc5883l (#35)
1 parent 88442b6 commit 3d44754

26 files changed

+779
-0
lines changed

devices/Hmc5883l/Gain.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
namespace Iot.Device.Hmc5883l
5+
{
6+
/// <summary>
7+
/// HMC5883L Gain Setting
8+
/// </summary>
9+
public enum Gain
10+
{
11+
/// <summary>
12+
/// 1370, recommended sensor field range: ±0.88 Ga
13+
/// </summary>
14+
Gain1370 = 0x00,
15+
16+
/// <summary>
17+
/// 1090, recommended sensor field range: ±1.3 Ga
18+
/// </summary>
19+
Gain1090 = 0x01,
20+
21+
/// <summary>
22+
/// 820, recommended sensor field range: ±1.9 Ga
23+
/// </summary>
24+
Gain0820 = 0x02,
25+
26+
/// <summary>
27+
/// 660, recommended sensor field range: ±2.5 Ga
28+
/// </summary>
29+
Gain0660 = 0x03,
30+
31+
/// <summary>
32+
/// 440, recommended sensor field range: ±4.0 Ga
33+
/// </summary>
34+
Gain0440 = 0x04,
35+
36+
/// <summary>
37+
/// 390, recommended sensor field range: ±4.7 Ga
38+
/// </summary>
39+
Gain0390 = 0x05,
40+
41+
/// <summary>
42+
/// 330, recommended sensor field range: ±5.6 Ga
43+
/// </summary>
44+
Gain0330 = 0x06,
45+
46+
/// <summary>
47+
/// 230, recommended sensor field range: ±8.1 Ga
48+
/// </summary>
49+
Gain0230 = 0x07,
50+
}
51+
}

devices/Hmc5883l/Hmc5883l.cs

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}

devices/Hmc5883l/Hmc5883l.nfproj

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>{21A229AF-9CEF-44CD-B3B2-52F67A478F73}</ProjectGuid>
12+
<OutputType>Library</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>Iot.Device.Hmc5883l</RootNamespace>
16+
<AssemblyName>Iot.Device.Hmc5883l</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
<DocumentationFile>bin\$(Configuration)\Iot.Device.Hmc5883l.xml</DocumentationFile>
19+
<LangVersion>9.0</LangVersion>
20+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
21+
</PropertyGroup>
22+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
23+
<ItemGroup>
24+
<Reference Include="mscorlib">
25+
<HintPath>packages\nanoFramework.CoreLibrary.1.10.4-preview.11\lib\mscorlib.dll</HintPath>
26+
<Private>True</Private>
27+
</Reference>
28+
<Reference Include="System.Device.I2c">
29+
<HintPath>packages\nanoFramework.System.Device.I2c.1.0.1-preview.33\lib\System.Device.I2c.dll</HintPath>
30+
<Private>True</Private>
31+
</Reference>
32+
<Reference Include="System.Math">
33+
<HintPath>packages\nanoFramework.System.Math.1.4.0-preview.7\lib\System.Math.dll</HintPath>
34+
<Private>True</Private>
35+
</Reference>
36+
</ItemGroup>
37+
<ItemGroup>
38+
<None Include="packages.config" />
39+
<Compile Include="Gain.cs" />
40+
<Compile Include="MeasurementConfiguration.cs" />
41+
<Compile Include="MeasuringMode.cs" />
42+
<Compile Include="OutputRate.cs" />
43+
<Compile Include="Register.cs" />
44+
<Compile Include="Hmc5883l.cs" />
45+
<Compile Include="SamplesAmount.cs" />
46+
<Compile Include="Status.cs" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="Properties\AssemblyInfo.cs" />
50+
<None Include="*.md" />
51+
</ItemGroup>
52+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
53+
<Import Project="..\..\src\BinaryPrimitives\BinaryPrimitives.projitems" Label="Shared" />
54+
<Import Project="..\..\src\System.Device.Model\System.Device.Model.projitems" Label="Shared" />
55+
<Import Project="..\..\src\System.Numerics\System.Numerics.projitems" Label="Shared" />
56+
<ProjectExtensions>
57+
<ProjectCapabilities>
58+
<ProjectConfigurationsDeclaredAsItems />
59+
</ProjectCapabilities>
60+
</ProjectExtensions>
61+
<Import Project="packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets" Condition="Exists('packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" />
62+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
63+
<PropertyGroup>
64+
<ErrorText> This project references NuGet package(s) that are missing on this computer.Enable NuGet Package Restore to download them.For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
65+
</PropertyGroup>
66+
<Error Condition="!Exists('packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets'))" />
67+
</Target>
68+
</Project>

devices/Hmc5883l/Hmc5883l.nuspec

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
3+
<metadata>
4+
<id>nanoFramework.Iot.Device.Hmc5883l</id>
5+
<version>$version$</version>
6+
<title>nanoFramework.Iot.Device.Hmc5883l</title>
7+
<authors>nanoFramework project contributors</authors>
8+
<owners>nanoFramework project contributors,dotnetfoundation</owners>
9+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10+
<license type="file">LICENSE.md</license>
11+
<releaseNotes>
12+
</releaseNotes>
13+
<developmentDependency>false</developmentDependency>
14+
<projectUrl>https://github.com/nanoframework/nanoFramework.IoT.Device</projectUrl>
15+
<iconUrl>https://secure.gravatar.com/avatar/97d0e092247f0716db6d4b47b7d1d1ad</iconUrl>
16+
<icon>images\nf-logo.png</icon>
17+
<repository type="git" url="https://github.com/nanoframework/nanoFramework.IoT.Device" commit="$commit$" />
18+
<copyright>Copyright (c) .NET Foundation and Contributors</copyright>
19+
<description>This package includes the .NET IoT Core binding Iot.Device.Hmc5883l for .NET nanoFramework C# projects.</description>
20+
<summary>Iot.Device.Hmc5883l assembly for .NET nanoFramework C# projects</summary>
21+
<tags>nanoFramework C# csharp netmf netnf Iot.Device.Hmc5883l</tags>
22+
<dependencies>
23+
<dependency id="nanoFramework.CoreLibrary" version="1.10.4-preview.11" />
24+
<dependency id="nanoFramework.System.Device.I2c" version="1.0.1-preview.33" />
25+
<dependency id="nanoFramework.System.Math" version="1.4.0-preview.71" />
26+
</dependencies>
27+
</metadata>
28+
<files>
29+
<file src="bin\Release\Iot.Device.Hmc5883l.dll" target="lib\Iot.Device.Hmc5883l.dll" />
30+
<file src="bin\Release\Iot.Device.Hmc5883l.pdb" target="lib\Iot.Device.Hmc5883l.pdb" />
31+
<file src="bin\Release\Iot.Device.Hmc5883l.pdbx" target="lib\Iot.Device.Hmc5883l.pdbx" />
32+
<file src="bin\Release\Iot.Device.Hmc5883l.pe" target="lib\Iot.Device.Hmc5883l.pe" />
33+
<file src="bin\Release\Iot.Device.Hmc5883l.xml" target="lib\Iot.Device.Hmc5883l.xml" />
34+
<file src="readme.md" target="" />
35+
<file src="..\..\assets\nf-logo.png" target="images" />
36+
<file src="..\..\LICENSE.md" target="" />
37+
</files>
38+
</package>

devices/Hmc5883l/Hmc5883l.sln

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 16
3+
VisualStudioVersion = 16.0.30413.136
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Hmc5883l", "Hmc5883l.nfproj", "{21A229AF-9CEF-44CD-B3B2-52F67A478F73}"
6+
EndProject
7+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Hmc5883l.Samples", "samples\Hmc5883l.Samples.nfproj", "{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}"
8+
EndProject
9+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared Projects", "Shared Projects", "{3B3E7B17-7E07-4C0B-8523-789E4E37C088}"
10+
EndProject
11+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "BinaryPrimitives", "..\..\src\BinaryPrimitives\BinaryPrimitives.shproj", "{3F28B003-6318-4E21-A9B6-6C0DBD0BDBFD}"
12+
EndProject
13+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "System.Device.Model", "..\..\src\System.Device.Model\System.Device.Model.shproj", "{23325B14-3651-4879-9697-9846FF123FEB}"
14+
EndProject
15+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "System.Numerics", "..\..\src\System.Numerics\System.Numerics.shproj", "{F5CCB96C-70B1-4F0A-9F0E-BB912B6A996C}"
16+
EndProject
17+
Global
18+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
19+
..\..\src\System.Device.Model\System.Device.Model.projitems*{23325b14-3651-4879-9697-9846ff123feb}*SharedItemsImports = 13
20+
..\..\src\BinaryPrimitives\BinaryPrimitives.projitems*{3f28b003-6318-4e21-a9b6-6c0dbd0bdbfd}*SharedItemsImports = 13
21+
..\..\src\System.Numerics\System.Numerics.projitems*{f5ccb96c-70b1-4f0a-9f0e-bb912b6a996c}*SharedItemsImports = 13
22+
EndGlobalSection
23+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
24+
Debug|Any CPU = Debug|Any CPU
25+
Release|Any CPU = Release|Any CPU
26+
EndGlobalSection
27+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
28+
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
31+
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Release|Any CPU.Deploy.0 = Release|Any CPU
34+
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
37+
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Release|Any CPU.Deploy.0 = Release|Any CPU
40+
EndGlobalSection
41+
GlobalSection(SolutionProperties) = preSolution
42+
HideSolutionNode = FALSE
43+
EndGlobalSection
44+
GlobalSection(NestedProjects) = preSolution
45+
{3F28B003-6318-4E21-A9B6-6C0DBD0BDBFD} = {3B3E7B17-7E07-4C0B-8523-789E4E37C088}
46+
{23325B14-3651-4879-9697-9846FF123FEB} = {3B3E7B17-7E07-4C0B-8523-789E4E37C088}
47+
{F5CCB96C-70B1-4F0A-9F0E-BB912B6A996C} = {3B3E7B17-7E07-4C0B-8523-789E4E37C088}
48+
EndGlobalSection
49+
GlobalSection(ExtensibilityGlobals) = postSolution
50+
SolutionGuid = {E738A85D-E77F-483B-A12E-0CD4F1543911}
51+
EndGlobalSection
52+
EndGlobal

0 commit comments

Comments
 (0)